I am currently trying out Json.NET, and it seems to work well.
Any other good JSON libraries for .NET?
I am currently trying out Json.NET, and it seems to work well.
Any other good JSON libraries for .NET?
The class JavaScriptSerializer from the System.Web.Script.Serialization namespace provides good JSON serialization/deserialization support.
There is the JavascriptSerialiser which is used by asp.net mvc and asp.net ajax. Also there is the DataContractJsonSerialiser used by WCF. The only problem I have encountered with the JavascriptSerialiser is that it uses funny way to serialise dates, which I do not think will parse into a javascript date. But this is easyly solved by this snippet
public double MilliTimeStamp()
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = DateTime.UtcNow;
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
Jayrock works well and transparently turns your objects to and from JSON objects providing they have a public constructor. It also creates the script for you so you can just call your web service like a Javascript class.
public class Person
{
public string Name { get;set;}
public int Age { get;set; }
public Person() { }
}
public class MyService : JsonRpcHandler
{
[JsonRpcMethod("getBob")]
public Person GetBob()
{
return new Person() { Name="Bob",Age=20};
}
}
And the Javascript:
var service = new MyService();
var result = service.getBob();
alert(result.name); // JSON objects are camel-cased.