Search code examples
vb.netjson.net

How to configure class definition to consume Unicode marked JSON


If I use the regex function to remove the u', the JSON is consumed correctly. Is there a way in Newtonsoft to process it without the regex preprocessing?

I am using VB.net if it makes a difference. Here is a snippet for some context:

{
  "memo": [
    "{u'braums': {u'printing_data': {u'customerFirstName': u'xxxxxx', u'orderNumber': 4495, u'customerLastName': u'xxxxxx', u'num_refrigerated_labels': 1, u'num_dry_labels': 1, u'packedBy': u'xxxxxxx xxxxxxx', u'pickupTime': u'11/18/2020 15:00', u'num_frozen_labels': 0}}}",
    "{u'braums': {u'cancellation_reasons': {u'who': u'xxxxxxx', u'when': u'11/18/2020 8:02 PM', u'why': u'called to cancel'}}}"
  ]
}

As per @dbc suggestion, here is an example that shows the issue. https://dotnetfiddle.net/pM1qs2

I expanded his TestClass with a sub that removes the Unicode markers using regex (TestClass.ShowMemoWork()) and then a sub that leaves the Unicode markers (TestClass.ShowMemoIssue())

The fact that JObject worked, means there is something in the syntax that I am missing.


Solution

  • To my knowledge, Json.Net does not support having a u prefix in front of strings in JSON. This is definitely not part of the JSON standard, as you can see here. \u can be used inside a string to prefix a string of 4 hex digits to represent a unicode character, but that is entirely different than what is in your sample.

    I think you are being tricked into thinking it "works" with JObject, but you aren't really doing the same test in that case. In your ShowMemoIssue example, you first deserialize the full JSON (which works fine), and then you attempt to deserialize one of the strings from the memo array. The second step fails because that string isn't valid JSON due to the u prefixes.

    But you never try the second step with the JObject. You only deserialize the outer JSON and then print it out, calling it a success. If you try to take one of the array items and deserialize that to a JObject, you will see that it fails in the same way. Here is a demo: https://dotnetfiddle.net/W7XQpd

    So in short, I think you will have to continue to use the regex preprocessing to handle your JSON. Better yet would be to change the emitter of the JSON not to include the invalid prefixes in the first place, but I understand that may not be in your control.