Reputation: 76008
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?
Upvotes: 18
Views: 7444
Reputation: 143319
I have a very fast open source JsonSerializer available that can serialize any POCO or DataContract type, including Interfaces anonymous and late bound types, etc.
Basic Example
var customer = new Customer { Name="Joe Bloggs", Age=31 };
var json = JsonSerializer.SerializeToString(customer);
var fromJson = JsonSerializer.DeserializeFromString<Customer>(json);
Upvotes: 4
Reputation: 17539
JsonFx.NET has an open-source serializer which allows serialization to/from strongly typed classes which might be what you're looking for. You can control how dates are serialized and override many aspects of the serialization. It even interacts well with Linq by supporting serialization of anonymous objects. The API works just like .NET XML serialization.
Upvotes: 0
Reputation: 74
just use your own api.. its easy to create json.. but you can also use JSON libraries like JSON.NET..
happy coding
Upvotes: 0
Reputation: 1062865
There are two;
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.
Upvotes: 26
Reputation: 104178
Json.Net is a JSON library for .NET. It is available in CodePlex.
Upvotes: 10