I am currently building JSON by using data stored in datatables. However what is occuring is where the data does not exist in a column for a data table it is returning an empty ("") string to my property.
This is then coming out on my JSON and then it fails to ingest due to the web service in question not validating it.
However if I set the value to "Nothing" it doesn't serialize and thus doesn't appear in the JSON. How can I get Nothing to return to all my string values where the string = "".
I could do this by writing a Function that tests for the string and returns Nothing howevever I feel there must be something wrong for me to have to do it this way.
Here is an example
Public Class Policy
<JsonProperty("policy_id", NullValueHandling:=NullValueHandling.Ignore)>
Public Property policy_id As String = Nothing
<JsonProperty("insurer_name", NullValueHandling:=NullValueHandling.Ignore)>
Public Property insurer_name As String = Nothing
<JsonProperty("policy_name", NullValueHandling:=NullValueHandling.Ignore)>
Public Property policy_name As String = Nothing
<JsonProperty("product_name", NullValueHandling:=NullValueHandling.Ignore)>
Public Property product_name As String = Nothing
<JsonProperty("sale_date", NullValueHandling:=NullValueHandling.Ignore)>
Public Property sale_date As DateTime = Nothing
<JsonProperty("start_date", NullValueHandling:=NullValueHandling.Ignore)>
Public Property start_date As DateTime = Nothing
<JsonProperty("end_date", NullValueHandling:=NullValueHandling.Ignore)>
Public Property end_date As DateTime = Nothing
<JsonProperty("status", NullValueHandling:=NullValueHandling.Ignore)>
Public Property status As String = Nothing
<JsonProperty("vehicles", NullValueHandling:=NullValueHandling.Ignore)>
Public Property vehicles As New List(Of Vehicle)
<JsonProperty("people", NullValueHandling:=NullValueHandling.Ignore)>
Public Property persons As New List(Of Person)
End Class
Private Function get_JSON(ByVal branch As String, ByVal policyref As String) As String
For Each p As DataRow In dt_policies.Rows
Dim oPolicy As New Policy() With {
.policy_id = p("B@") & p("PolRef@"),
.insurer_name = p("insurer_name"),
.policy_name = p("policy_name"),
.product_name = p("product_name"),
.sale_date = p("sale_date"),
.start_date = p("start_date"),
.end_date = p("end_date"),
.status = p("status"),
.vehicles = get_vehicles(p("B@"), p("PolRef@")),
.persons = get_persons(p("B@"), p("PolRef@"))
}
Dim json As String = JsonConvert.SerializeObject(oPolicy, NullValueHandling.Ignore)
Return json
Next
End Function
Private Function ReturnNothing(ByVal rstring As String) As String
If rstring = "" Then
Return Nothing
Else
Return rstring
End If
End Function