This project is read-only.
The JSON deserializer is a tool that will parse a JSON string and populate a given object with the properties identified in that JSON string.

Assuming that we have a class Person:
 class Person
        public int id;
        public string name;
        public long money;

and the JSON string:
        id: "1",

if we parse and deserialize the JSON string into the Person class we will end up with an object that has the id and name properties set to 1 and "test". The money property will not be set (it will have the default value).

There are two stages of the program:
  • The first one is the parsing stage. The JSON parser follows the JSON specs All the JSON constructs described there are supported by this parser. What the parser does is create a tree that describes the JSON string passed in. That tree uses types in the Syntax namespace to describe the text. That tree is then used to populate the object.
  • The second stage of the program is the deserialization part. This means that, after the JSON string is parsed, we can use the information gathered while parsing to populate an object. The deserializer takes a parse tree and an object type and will instantiate an object of the type passed in and populate it with the properties found in the parse tree.

How to use it:
  • In order to parse a JSON text you first need to lex it. Lexing it means that you remove all the whitespace from the text and each JSON element ( {, [, :, etc) is recognized and transformed into a token.
  • Then, the list of tokens is passed to the Parser who will interpret the stream of tokens. The return type of a parse method is a CompilationUnit which will contain a parse tree that describes the JSON text. Calling ToString on the CompilationUnit will return a string that is a representation of the JSON object that was parsed. This text is NOT a valid JSON string. You can obtain that parse tree by calling the static method ParseText on the Parser class and pass in the text to be parsed.
  • Once you have a CompilationUnit object you can deserialize that into a .NET object by calling the static method Deserialize on the Deserializer class and pass in the type of the object you want to deserialize to and the CompilationUnit object to use when deserializing. The Deserialize method will return an object of the type you specified with all the properties set based on the values in the CompilationUnit object.

class MyValue
        public string Value;
        public List<MyValue> Elem;
        public double Val;
        public bool b;

string textTest = "{\"Val\":3.5, \"Value\":\"sdf\", \"b\":true, \"b\":false, \"Elem\":null, \"Elem\":[ {\"Value\":\"aaa\"},{\"Value\": \"bbb\"}]}";
var elem = Parser.ParseText(textTest);
MyValue val = Deserializer.Deserialize<MyValue>(elem);

The val variable will contain an object of type MyValue with the properties set according to the JSON object.

To use this you just need to add a reference to the library that contains the JSON parser.

Last edited Mar 1, 2011 at 1:04 AM by mariagh, version 1


No comments yet.