Tuesday, March 15, 2011

Handling null strings and ints in Json

JSON ignores any parameters with null values. So, when I create a string using JsonConverter.ExportToString these properties are missing. Also any integers with null values are replaced with -2147483648

This becomes an issue when I try to deserialize this string (I am writing my own deserializer and not using Json.Import)

What's the best way of handling this?

From stackoverflow
  • Could you use an empty string i.e. "" instead of null, and use a placeholder number e.g. -2147483648 to indicate a null integer value?

  • Can you explain how I can use a placeholder number to be replaced? Unfortunately, I cannot have "" instead of null.

  • I am not sure that I understand the question. JSON is just a subset of javascript and properties with null values can be represented like so:

    {"property1": 1, "property2": null}
    

    In this case, property1 is a numeric and had value 1, while property2 has value null. I'm not sure from which library the Json.Import and JsonConverter.ExportToString calls are coming. Anyway, assigning a null value to an integer is typical "strong-typed speek". In javascript, assigning null to a numeric (no such thing as integer in js) would just make that variable stop being numeric.

    So maybe you should give us more context: libraries used, language you are using the data from (apparently not javascript).

    annakata : +1 this is absolutely not a problem bound by JSON, but by whatever is producing *this* JSON

0 comments:

Post a Comment