I created a very simple and really fast JSON parser for .NET. You can download it
here.
Introduction
json-sharp contains 4 methods:
string JSON.Serialize(object o);
string object.ToJSON();
T JSON.Deserialize<T>(string json);
object JSON.Deserialize(string json);
It is of note that only public properties are serialized and
deserialized.
Setup
Minimal setup is required to get json-sharp running.
- Download either the DLL or the source file.
- Include either the DLL or the source file in your project.
- Include a using reference to System.Web in the .cs file you intend
to use json-sharp if there isn't one already.
using System.Web;
Examples
Serializing
There are two ways in which you can serialize an object. First
of all, you can use the straight-forward Serialize method.
string myJsonString = JSON.Serialize(MyRandomObject);
or you can use the even easier object.ToJSON method.
string myJsonString = MyRandomObject.ToJSON();
Deserializing
There are also two ways in which you can deserialize a JSON
string. First of all, you can use the more generic Deserialize method without specifying
a type.
ArrayList numberListList = JSON.Deserialize("[[20.4,-30,-42.6,-0.5e10], [-0.5e-3,0.5E2,0.323E-2,50,60]]");
or you can specify a type and get a useful object.
string x = "[[10,20.4,-30,-42.6,-0.5e10], [-0.5e-10,0.5E10,0.323E-10,50,60]]";
double[][] y = JSON.Deserialize(x);
Comparison to other Libraries
I ran some tests against two other popular libraries
to test speed and ease of use. The other libraries I tested json-sharp against are
Newtonsoft's Json.NET and Microsoft's built in JSON deserializer. The test is a
fairly simple, but large (~253 KB), JSON file. The JSON is just a large array of
a simple object. This object contains 10 Properties (4 ints, 3 decimals, 2 strings,
1 datetime). I store the json in a string variable, then call each library's deserialization
method 100 times and report the overall time it took.
Microsoft
- Lines of Code: 18
- Execution time: 3.875 seconds
- Using statements: 2
- Libraries imported: 0
- Size of libraries: 0
Newtonsoft
- Lines of code: 1
- Execution time (typecasted): 12.234 seconds
- Execution time (generic): 8.609 seconds
- Using statements: 1
- Libraries imported: 1
- Size of libraries: 192 KB
json-sharp
- Lines of code: 1
- Execution time (typecasted): 5.859 seconds
- Execution time (generic): 1.688 seconds
- Using statements: 1
- Libraries imported 1
- Size of libraries: 13 KB
Tags: JSON