tags:

views:

1155

answers:

5

I'd be interested in hearing what JSON library folks in the community have been using inside of .NET? I have a need to parse/serialize some JSON object graphs from inside .NET (C#) to actual .NET types. I could roll my own, but if there are some solid libraries folks have used, I'd like to hear your comments. I saw the list of libraries on the json.org site, but it's a fairly big list and the community is usually good at vetting out the contenders from the pretenders

Any details (pros/cons) of your experience with the library would be incredibly helpful. -- thanks in advance.

+10  A: 

I've used Json.NET with success in the past.

Example from the site:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": new Date(1230422400000),
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
brianng
+8  A: 

I wrote my own JSON serializer using DataContractJsonSerializer in the System.ServiceModel.Web.dll assembly [which is a component of WCF included in .NET 3.5 as a standard assembly, and in the .NET 3.5 SP1 Client Profile] (in .NET 4.0 and Silverlight 4, it's been moved to System.Runtime.Serialization.dll).

using System.IO;
using System.Runtime.Serialization.Json;

public class JsonObjectSerializer 
{
    public string Serialize<T>(T instance) where T : class
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var memoryStream = new MemoryStream())
        {
            serializer.WriteObject(memoryStream, instance);

            memoryStream.Flush();
            memoryStream.Position = 0;

            using (var reader = new StreamReader(memoryStream))
            {
                return reader.ReadToEnd();
            }
        }
    }

    public T Deserialize<T>(string serialized) where T : class
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var memoryStream = new MemoryStream())
        {
            using (var writer = new StreamWriter(memoryStream))
            {
                writer.Write(serialized);
                writer.Flush();

                memoryStream.Position = 0;

                return serializer.ReadObject(memoryStream) as T;
            }
        }
    }
}
bendewey
Tweaked it to an extension method for myself, thanks :)
Chad Grant
+1  A: 

Check out the System.Runtime.Serialization.Json Namespace included with .NET 3.5.

Richard
For those who are interested, it lives in System.ServiceModel.Web
borisCallens
-1 Until I figure out what this adds over http://stackoverflow.com/questions/571168/what-json-library-works-well-for-you-in-net/571200#571200 which was posted before this
Ruben Bartelink
+3  A: 

There are at least two built into the framework.

The newer : System.Runtime.Serialization.Json

and the older : System.Web.Serialization

I prefer to not have dependencies on 3rd party libraries. I work with JSON every day and have never needed anything more than what already exists in the framework.

Chad Grant
A: 

You should also try my ServiceStack JsonSerializer - it's the fastest .NET JSON serializer at the moment based on the benchmarks of the leading JSON serializers and supports serializing any POCO Type, DataContracts, Lists/Dictionaries, Interfaces, Inheritance, Late-bound objects including anonymous 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