views:

1258

answers:

6

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?

A: 

LinqToJson

tuinstoel
+4  A: 

Json.Net is a JSON library for .NET. It is available in CodePlex.

kgiannakakis
+7  A: 

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.

Marc Gravell
According to MSDN, JavaScriptSerializer is in System.Web.Extensions.dll which is available in .NET 3.5, not necessarily .NET 3.5 SP1.
Morgan Cheng
IIRC, it has been marked obsolete and just recently been "un-deprecated" with 3.5 SP1.
Christian.K
A: 

just use your own api.. its easy to create json.. but you can also use JSON libraries like JSON.NET..

happy coding

Aristotle Ucab
A: 

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.

McKAMEY
A: 

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); 
mythz