This project is read-only.



Json class is the constant repository. This class is use as singleton to store all the information’s regarding Json syntax and default option such Culture.


public class Json
   public const char NameValueSeparator = ':';
   public const char PairSeparator = ',';
   public const char ValueSeparator = ',';
   public const char StartObject = '{';
   public const char EndObject = '}';
   public const char StartArray = '[';
   public const char EndArray = ']';
   public const char StringDelimiter = '"';
   public const string NullKeyword = "null";
   public const string TrueKeyword = "true";
   public const string FalseKeyword = "false";

public static readonly CultureInfo DefaultCultureInfo = CultureInfo.GetCultureInfo("En-us");


This is the class user should use to serialize an object to a JSON string. However you can simply cut and paste some of the code into your own. The class syntax is the same as the one provided by the .Net framework but limited to some methods.

public int MaxJsonLength
public int RecursionLimit

public String Serialize(object source)
public String Serialize(object source, IFormatProvider culture)
public void Serialize(object source, StringBuilder output)
public void Serialize(object source, StringBuilder output, IFormatProvider culture)



                JsonPair is an internal use class. This class is intended to provide a customized way to write the data to the output JSON string. Derived class SHOULD override the Write method to provide they own behavior. The most representative case is the DateTimePair which format the DateTime object as specific JSON Date Time Format.

In order to work with the Serializer, thus pair has to be bind with a Type. This is the Goal of the _values private field and static Bind, Unbind methods.

internal static Dictionary<Type, Type> _values;
static JsonPair()
     _values = new Dictionary<Type, Type>();
     _values.Add(typeof(string), typeof(JsonString));
     _values.Add(typeof(Guid), typeof(JsonString));
     _values.Add(typeof(DateTime), typeof(JsonDateTime));
     _values.Add(typeof(Byte), typeof(JsonByte));
     _values.Add(typeof(Char), typeof(JsonChar));
     _values.Add(typeof(Int16), typeof(JsonInt16));
     _values.Add(typeof(UInt16), typeof(JsonUInt16));
     _values.Add(typeof(Int32), typeof(JsonInt32));
     _values.Add(typeof(UInt32), typeof(JsonUInt32));
     _values.Add(typeof(Int64), typeof(JsonInt64));
     _values.Add(typeof(UInt64), typeof(JsonUInt64));
     _values.Add(typeof(Single), typeof(JsonSingle));
     _values.Add(typeof(Double), typeof(JsonDouble));
     _values.Add(typeof(Boolean), typeof(JsonBoolean));


This is a good place to extend the “standard” way of the Serialization, adding your own type to make the serialization Faster (I especially recommend this in device oriented development where the set of object type is well identified).

Ex :

 _values.Add(typeof(MyClass), typeof(JsonMyClass));
internal class JsonMyClass: JsonPair
   public override void WriteValue(object p_src, JSonContext p_ctx)
       // my code here

Default Serialization class is JsonString which is Serialize the data using the ToString() method, embracing the value between quote (as defined into JSON specification)All primitive type are provided, but you can still find some lack, so this is THE place to add standard type.


The JsonContext is responsible to keep the context information during the serialization process. Context informations are :

  • Current Serializer
  • Current Culture
  • Current StringBuilder
  • Recursive check information when using with _CHECK_RECURSION option
  • Size check information when using with _CHECK_SIZE option

Internally, this class is used by the JsonPair’s to Append the generated string to the buffer, using the Append(string) and Append(char) methods.

Advanced user could customize these two method to perform they own check before/after serialization. We made the choice to NOT provide this facility, while the demand is not clear and the goal is clearly on performance. However, the need to concentrate all the buffer access in a couple of methods, instead of simply using the StringBuilder methods, is a good development pattern and also provide us the way to check the output size.


JsonObjectTemplate is the internal class holding the object serialization graph. This is where the object graph is created using introspection at the first use of a Type, when invoking the GetJsonObjectTemplate method.

internal static JsonObjectTemplate GetJsonObjectTemplate(Type p_type)

 A static cache is implemented, then the graph is build once and stay in memory for future use.

You can use the Clear method to clear this cache when need memory. You can also build specific garbage collecting behavior around this cache, such clearing graph based on timeout of use for each type (this is one of next feature targeting device development).

NOTE : If using DataContract Description such the one you use in WCF, you can use the _SERVICE_MODEL Compilation directive to include DataContract compatible serialization, using DataContractAttribute and DataMemberAttribute.


This interface give us the way to access the Value into Field and Property. The goal is to avoid testing between Field and Property Type at runtime. The following sub classes are provided

  • NullAccessor
  •  FieldAccessor
  • PropertyAccessor


When Serialize the data, you MAY not want to serialize certain fields or properties, while the basic idea is to serialize all the PUBLIC fields and properties. The ScriptIgnoreAttribute is designed for this use. You may simply tag the desired field or property to avoid serialization for it.

interface MyInterface
   public int data1;
   public int cache;


Last edited Mar 3, 2011 at 11:16 AM by Gaume, version 4


No comments yet.