I am retrieving a JSON response from a web server and have created strongly typed classes for all properties without any issue, except for one property that can be a single string value or a nested array.
Example of the JSON:
"meta_data": [
{
"id": 12772,
"key": "_shipping_phone",
"value": ""
},
{
"id": 12786,
"key": "status",
"value": "completed"
},
{
"id": 12788,
"key": "Payment type",
"value": "instant"
},
{
"id": 12796,
"key": "_transaction_fee",
"value": "0.39"
},
{
"id": 12806,
"key": "connect_destination_normalized",
"value": "1"
},
{
"id": 12807,
"key": "wc_connect_labels",
"value": [
{
"label_id": 1633947,
"tracking": "9400***************",
"refundable_amount": 3.930000000000000159872115546022541821002960205078125,
"created": 1589499950667,
"carrier_id": "usps",
"service_name": "USPS - First Class Mail",
"status": "PURCHASED",
"package_name": "Bubble Mailer",
"product_names": [
"Peace Love And Wine Sub Tshirt"
],
"receipt_item_id": 60476008,
"created_date": 1589499955000,
"main_receipt_id": 46201718,
"rate": 3.930000000000000159872115546022541821002960205078125,
"currency": "USD",
"expiry_date": 1605051955000,
"label_cached": 1589499962000
},
{
"label_id": 1633942,
"tracking": null,
"refundable_amount": 0,
"created": 1589499912741,
"carrier_id": null,
"service_name": "USPS - First Class Mail",
"status": "PURCHASE_ERROR",
"package_name": "Bubble Mailer",
"product_names": [
"Peace Love And Wine Sub Tshirt"
],
"receipt_item_id": -1,
"created_date": 1589499912000,
"error": "The transaction was declined."
},
{
"label_id": 1633913,
"tracking": null,
"refundable_amount": 0,
"created": 1589499712367,
"carrier_id": null,
"service_name": "USPS - First Class Mail",
"status": "PURCHASE_ERROR",
"package_name": "Bubble Mailer",
"product_names": [
"Peace Love And Wine Sub Tshirt"
],
"receipt_item_id": -1,
"created_date": 1589499712000,
"error": "The transaction was declined."
}
]
}
As you can see, the value key is a normal string value (or blank string). However, it can be much more as the final entry shows.
Here are my strongly typed classes:
Public Class Meta_Data
Public Property id As Integer
Public Property key As String
'<JsonConverter(GetType(SingleOrArrayConverter(Of Values())))>
Public Property value As Values()
End Class
Public Class Values
Public Property label_id As Integer
Public Property tracking As String
Public Property refundable_amount As String
Public Property created As String
Public Property carrier_id As String
Public Property service_name As String
Public Property status As String
Public Property package_name As String
Public Property product_names As String()
Public Property receipt_item_id As String
Public Property created_date As String
Public Property [error] As String
Public Property main_receipt_id As String
Public Property rate As String
Public Property currency As String
Public Property expiry_date As String
Public Property label_cached As String
End Class
I've scoured the internet for a solution and was not able to come up with one. I've tried the different converter examples I've found, but none work as they all still try to pass the single strings through the Values class.
Here is my deserialize call:
Dim info As Order = JsonConvert.DeserializeObject(Of Order)(responseFromServer)
I have multiple other classes that I wasn't going to take up space showing, but the root class is called Order.
Basically, is there a way to check value to see if it's a string while processing the deserialization and prevent it from processing through the Values class?