2

Is there a way to deserialize some JSON to a DTO where the JSON contains unquoted hexadecimal values? I'm using JsonConvert.DeserializeObject.

This is the JSON that I need to deserialize. It is being produced by a device, and I don't have any control over it:

{"ID":7,"T":1511819861,"E":777, "ET":2,"DEVID":f525873c,"DEVTS":1511801849}

The problem is with the field "DEVID".

I know that this JSON is considered invalid, but I need to find a way to work with it. Is there any way to configure or cast that field?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
gemr1423
  • 739
  • 2
  • 11
  • 33
  • 1
    You probably need to make the json valid before deserializing it. – Magnus Nov 30 '17 at 15:37
  • You don't cast the field, you fix the json. Use your preferred way to put that value into `""` (`Regex`, `string.Split`, machine learning, etc.) , deserialize as string (e.g. using another field in DTO), set value to `ulong` property. – Sinatr Nov 30 '17 at 15:38
  • What is creating this JSON? – Brian Rogers Nov 30 '17 at 15:46
  • 1
    The JSON is produced by a device. Currently I don't have access to modify that response. – gemr1423 Nov 30 '17 at 15:51
  • 1
    Technically, that isn't valid json - see jsonlint.com. I use quite a bit of json with byte arrays as values (encoded like your devid value), but they are always quoted. – snow_FFFFFF Nov 30 '17 at 15:59
  • You could also write a custom JsonConverter like this fellow explained to me a while back. (https://stackoverflow.com/questions/33088462/can-i-specify-a-path-in-an-attribute-to-map-a-property-in-my-class-to-a-child-pr) Don't have time to write it for you, but I think you do something there to handle the lack of quotes on a hexadecimal value. – David P Nov 30 '17 at 16:57
  • Actually @BrianRogers wrote mine, and I see he is commenting here. Brian? – David P Nov 30 '17 at 16:58
  • @DavidP Unfortunately, a custom `JsonConverter` will not help here. Since the value is unquoted and not a decimal number, it does not conform to the JSON spec, and so the parser will throw an exception well before any converter would be called. You'd have to modify code for the `JsonTextReader` class to be able to detect and handle the unquoted hex value, but I think that would be way more trouble than it is worth. I think the best solution here is just to fix the JSON with a simple string replace prior to deserializing, as was suggested in the accepted answer. – Brian Rogers Nov 30 '17 at 20:58

1 Answers1

1

As you don't have access to fix the response, I'd suggest you to fix the json before deserializing it. I did this function that might give you a hint how to fix your issue:

public static string FixJson(string json, string property) {
        var index = json.IndexOf(property);     
        var indexColon = json.IndexOf(':', index + 1);      
        var indexComma = json.IndexOf(',', index + 1);      
        var val = json.Substring(indexColon + 1, indexComma - indexColon - 1);      
        var replace = string.Format("'{0}': {1}", property, val.Trim());        
        var forR = string.Format("'{0}': '{1}'", property, val.Trim());     
        return json.Replace(replace, forR);
}

You cant test it on

https://dotnetfiddle.net/kidAIO

raven
  • 2,381
  • 2
  • 20
  • 44