JSON Tools for .NET

JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON is an object notation in text format that Javacript engine fully understands and there is no need to parse for it.

The library contains some basic types that Javascript requires. IJSONObject, JSONElement, JSONNumber, JSONString, JSONBoolean, JSONCollection, JSONArray and JSONObject.

  • JSONObject : Represents JSON objects beginning and ending with curly braces ({}). It is a collection of JSONElement objects. Object members consist if string and values separated by colon in JSON notation.
  • JSONArray : Represents a javascript array that begin and end with braces and contain values. In the notation values are separated by commas, but the library does all the syntactic issues.
  • JSONCollection : A base type for JSONObject and JSONArray.
  • JSONElement : Base class for all JSON types. It has the base properties and can be used with arrays or objects or individually.
  • JSONString : Represents JavaScript string type and created by double quotes. It is inherited from a JSONElement
  • JSONBoolean : Represents JavaScript boolean type
  • JSONNumber : Represents a javascript number type, it can be integer, or floating point numbers.

JSON Tools for .NET is the library that helps to build JSON objects from .NET Framework objects. I just wanted to share the code that I was working previously. It is not a complete library. There is a need for some helper classes for working with dynamic objects and collections. JSONReflector might be useful as a new feature.

You can download the JSON Tools for .NET and play with it. Also, you are very welcome to join and contribute to the project.

A simple example of JSON output looks like this:

{ 
"Total" : 45500, 
"Offset" : 0, 
"Result_List" : [ 
   { "Description" : "JSON (JavaScript Object Notation) (Pronounced like Jason, IPA /d?e?s?n/ ) is a lightweight computer data interchange format. It is a text-based, human-readable format for ... ", 
     "Title" : "JSON - Wikipedia, the free encyclopedia", 
     "URL" : "http://en.wikipedia.org/wiki/Json" } , 

   { "Description" : "object {} { members } members pair pair , members pair string : value array [] [ elements ] elements value value , elements value string number object array true false null", 
     "Title" : "JSON", 
     "URL" : "http://www.json.org/" } , 

] } 

This example is the one of the outputs from the sample directory. It is actually a live search query displayed in JSON string.

In C#, each object is contracted with an IJSONObject interface. That interface has a single WriteObject Method that does the JavScript representation of the actual object.

Here is a sample implementation for a result type object.

public JSONObject WriteObject()
{   
    JSONNumber<int> jTotal = new JSONNumber<int>("Total", m_Total);
    JSONNumber<int> jOffset = new JSONNumber<int>("Offset", m_Offset); 

    List<jsonobject> ResultObjects = new List<jsonobject>(m_Results.Count);
    foreach (SearchResult r in m_Results)
    {
        ResultObjects.Add(r.WriteObject());
    } 

    JSONArray jResults = new JSONArray("Result_List", ResultObjects); 

    JSONObject js = new JSONObject("LiveResults", jTotal, jOffset, jResults);
    return js;
} 

Another representation of a result by using different JSON types.

public JSONObject WriteObject()
       {    
           JSONString jDescription = new JSONString("Description", m_Description);
           JSONString jTitle = new JSONString("Title", m_Title);
           JSONString jURL= new JSONString("URL", m_URL); 

           JSONObject js = new JSONObject("Result", jDescription, jTitle, jURL);
           return js;
       } 

I liked the way it creates JSON object out of an object. No reflection, no dynamic code emit, which means it should have no problems for busy heavy servers. On the other hand a JSONReflector and a JSONParser could be implemented using the same class library.