I am currently trying out Json.NET, and it seems to work well.
Any other good JSON libraries for .NET?
Text. Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (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).
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;
}
The class JavaScriptSerializer from the System.Web.Script.Serialization namespace provides good JSON serialization/deserialization support.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With