Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there Any Off-The-Shelf Json Serialization helper class in .NET BCL?

I need to serialize/de-serialize some objects into/from string and transfer them as just opaque data. I can use XmlSerializer to do it, but generated string looks clumsy and long. So, is there any concise Serializer available in .NET?

The first thing coming to my mind is that perhaps .NET should have JSON Serializer, but I cannot find it. Is there any off-the-shelf approach in .NET to convert object to/from concise string?

like image 978
Morgan Cheng Avatar asked Dec 24 '08 10:12

Morgan Cheng


People also ask

How to deserialize JSON data in c#?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

How to deserialize string to JSON object in c#?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data.

What is serialization and deserialization in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).


1 Answers

There are two;

  • DataContractJsonSerializer in 3.5
  • JavaScriptSerializer in 3.5 SP1

In the traditional way, they aren't 100% compatible with each-other, especially re DateTime; DCJS uses (IIRC) a literal string; JSS uses new - and neither can read t'other correctly.

Of course, if the text if opaque, you could also use any concise binary serializer, and simply base-64 encode it; for example, protobuf-net is pretty terse. But using JSON minimizes the external code needed at each end, so may well be your best bet.

like image 162
Marc Gravell Avatar answered Sep 28 '22 10:09

Marc Gravell