Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why System.Version in JSON string does not deserialize correctly?

Tags:

Context: I need to pass an object containing a large number of properties/fields (to UI Layer from Middle Tier Layer). Among this list of properties, one is of type Version which is not getting deserialized correctly from JSON string format. I have a chosen JSON format over XML as JSON serialization to string will return short string result.

Problem: System.Version does not get deserialized correctly. I have tried two different .NET Libraries. Following are the code snippets for each:

Code Snippet 1 using ServiceStack .NET library:

        var version = new Version(1, 2, 3, 0);         string reportJSON = JsonSerializer.SerializeToString<Version>(version);         //{"Major":1,"Minor":2,"Build":3,"Revision":0,"MajorRevision":0,"MinorRevision":0}           Version report2 = JsonSerializer.DeserializeFromString<Version>(reportJSON);         string reportJSON2 = JsonSerializer.SerializeToString<Version>(report2);         //{"Major":0,"Minor":0,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1} 

Code Snippet 2 using Newtonsoft .NET library but with same result:

        var version = new Version(1, 2, 3, 0);         string reportJSON = JsonConvert.SerializeObject(version);         //{"Major":1,"Minor":2,"Build":3,"Revision":0,"MajorRevision":0,"MinorRevision":0}           Version report2 = JsonConvert.DeserializeObject<Version>(reportJSON);         string reportJSON2 = JsonConvert.SerializeObject(report2);         //{"Major":0,"Minor":0,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1} 

How to fix this? Or Which other JSON.NET library can work correctly?

like image 555
monish001 Avatar asked Nov 01 '12 03:11

monish001


People also ask

What is System JSON deserialize?

Deserializes the specified JSON string into an Apex object of the specified type. deserializeUntyped(jsonString) Deserializes the specified JSON string into collections of primitive data types. serialize(objectToSerialize) Serializes Apex objects into JSON content.

Which is better Newtonsoft JSON or System text JSON?

Json does case-insensitive property name matching by default. The System. Text. Json default is case-sensitive, which gives better performance since it's doing an exact match.

Is System text JSON faster than Newtonsoft?

Text. Json is much faster than the Newtonsoft. Json.

Is polymorphic deserialization possible in System text JSON?

Polymorphic Deserialization With System. Text. Json. In contrast to the serialization case, there is no simple way to perform deserialization (simple or polymorphic) on a JSON string.


1 Answers

The Newtonsoft.Json library provides a set of common converters in the Newtonsoft.Json.Convertersnamespace, including a VersionConverter you can use to serialize and deserialize System.Version.

Note that you have to use the VersionConverterboth for serialization and deserialization, though.
That's because standard serialization would generate eg.:{"Major":1,"Minor":2,"Build":3,"Revision":0,"MajorRevision":0,"MinorRevision":0} while VersionConverterdeserialization expects a simple string as in "1.2.3".

So usage would be:

using Newtonsoft.Json; using Newtonsoft.Json.Converters;    string s = JsonConvert.SerializeObject(version, new VersionConverter()); Version v = JsonConvert.DeserializeObject<Version>(s, new VersionConverter()); 

I'm not sure what's the first version of Newtonsoft.Jsonthat includes that converter. Mine has it and it's 5.0.6.

like image 132
Manuel Avatar answered Sep 23 '22 21:09

Manuel