I started up a new MVC 5 Web API project, and I want to manually encode an object as JSON to save to a database. However, it seems that there are at least four different JSON-like serializer classes already available in my project:
System.Runtime.Serialization.Json.DataContractJsonSerializer
System.Web.Helpers.Json
Newtonsoft.Json.JsonConvert
Newtonsoft.Json.JsonSerializer
I sort-of understand why these four are conceptually different: one's from WCF, two are from Newtonsoft; two are quick-and-dirty converts and two are configurable serializers, etc.
What I can't figure out is, does it really matter which one I should use. Are there any functional differences between these 4 options? Will there be interoperability problems if I use one class to serialize and a different one to deserialize in another application?
The Newtonsoft serializer is faster than the legacy DataContractJsonSerializer
which is why it's generally included with recent versions of MVC. The two Newtonsoft types you refer to aren't both serializers though - I believe that JsonConvert
is just a utility type that uses the JsonSerializer
internally.
So in answer to your question the simplest (and one of the) fastest ways to serialize/deserialize json is like this:
// Serialize
YourType instance = new YourType();
string json = JsonConvert.SerializeObject(instance);
// Deserialize
string json = "json_string";
YourType instance = JsonConvert.DeserializeObject<YourType>(json);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With