XML JSON Serialization

XML-JSON Serialization provides pure C# serializers and deserializers for XML and JSON. These serialization classes are tiny, compatible with Flash, and don't depend on System.dll. The available classes are:

  1. XMLInStream: deserialize XML strings
  2. XMLOutStream: serialize to XML
  3. JSONInStream: deserialize JSON strings
  4. JSONOutStream: serialize to JSON
  5. JSONSerializer: serialize classes from/to JSON using C# Reflection (not compatible with Flash)
  6. XMLSerializer: serialize classes from/to XML using C# Reflection (not compatible with Flash)
  7. EscapeString: escape/unescape strings for web browser communication

XML Deserialization API

To deserialize this XML string
<root>
  <foo>3</foo>
  <bar>baz</bar>
</root>
int foo;
string bar;
XMLInStream stream = new XMLInStream(xmlString);
stream.Content("foo", out foo);
stream.Content("bar", out bar);
You can also chain the calls
int foo;
string bar;
XMLInStream stream = new XMLInStream(xmlString);
stream.Content("foo", out foo)
      .Content("bar", out bar);
To enter a child element use Start and End
<root>
  <baz>3</baz>
  <foo>
    </bar>2</bar>
  </foo>
</root>
int bar, baz;
    XMLInStream stream = new XMLInStream(xmlString);
    stream.Content("baz", out baz)
          .Start("foo")
              .Content("bar", out bar)
          .End();
You can read an attribute
<root>
  <foo bar="2">
</root>
int foo;
    int bar;
    XMLInStream stream = new XMLInStream(xmlString);
    stream.Start("foo")
             .Attribute("bar", out bar)
         .End();
You can use AttributeOptional and ContentOptional if elements are not mandatory
<root>
  <foo>
    </baz>2</baz>
  </foo>
</root>
int bar = -1;
    int baz = -1;
    XMLInStream stream = new XMLInStream("2");
    stream.Start("foo")
              .AttributeOptional("bar", out bar)
              .ContentOptional("baz", out baz)
          .End();
You can use these types in the Content, Attribute, ContentOptional and AttributeOptional functions:
  1. string
  2. int
  3. float
  4. bool
  5. Vector2
  6. Vector3
  7. Quaternion
  8. Color
  9. Rect
Complex data structures are serialized with attributes: They can also be deserialized from arrays like:[1,2,3] You can test that a child exists with the Has function
<root>
  <foo>
    </bar>2</bar>
  </foo>
</root>
XMLInStream stream = new XMLInStream(xmlString);
    if(stream.Has("foo")) {
        int bar;
        stream.Start("foo").Content("bar", out bar);
    }
You can deserialize list of children with the same tag with the List function:
<root>
  <foo>1</foo>
  <foo>2</foo>
  <foo>3</foo>
</root>
XMLInStream stream = new XMLInStream(xmlString);
stream.List("foo", delegate(XMLInStream fooStream){
    int value;
    fooStream.Content(out value);
});

XML Serialization API

To write XML, use the same methods on the OutStream class:
XMLOutStream stream = new OutStream();
stream.Start("root")
           .Content("foo", 2)
           .Start("bar")
              .Attribute("baz", 1)
              .Content("hello")
           .End()
       .End();

string xml = stream.Serialize();

JSON API

The JSON API is very similar to the XML API. The main differences are:
  1. There are no attributes
  2. The List method has a different semantics
A simple example:
json = {a: 2, b: {c: 1}}
int a, c;
JSONInStream stream = new JSONInStream(json);
stream.Content("a", out a)
      .Start("b")
          .Content("c", out c)
      .End();
To deserialize a list use the List method:
json = {a:["hello", "world"]}
JSONInStream stream = new JSONInStream(json);
stream.List("a", delegate(int index, JSONInStream elementStream){
     string value;
     elementStream.Content(out value);
});
To serialize a list:
JSONOutStream stream = new JSONOutStream(json);
stream.List("a")
          .Content("hello")
          .Content("world")
      .End();
This will serialize the list to a field named "a" in an object. You can just serialize the list by omitting the argument to the List function.

JSONSerializer and XMLSerializer API

JSONSerializer and XMLSerializer provide direct serializers to C# classes using Reflection. These classes don't work with Flash. The methods are:
string JSONSerializer.Serialize(T obj)
    T JSONSerializer.Deserialize(string s)
    string XMLSerializer.Serialize(T obj)
    T XMLSerializer.Deserialize(string s)
How to use it:
private class SampleClassA
{
  public string s;
  public float  f;
  public Vector3 v;
  public List c;
  public SampleClassB sb;
}

private class SampleClassB
{
  public Quaternion q;
  public Rect r;
  public Vector2 v;
}

public static void TestSimpleSerialization()
{
  SampleClassB sb = new SampleClassB();
  sb.q = new Quaternion(1, 2, 3, 4);
  sb.r = new Rect(0,10,20,30);
  sb.v = new Vector2(1.1f,2.2f);
  SampleClassA sa = new SampleClassA();
  sa.s = "hello";
  sa.f = 3.2f;
  sa.v = Vector3.up;
  sa.c = new List();
  sa.sb = sb;
  sa.c.Add(Color.red);
  sa.c.Add(Color.green);

  string s = JSONSerializer.Serialize(sa);

  SampleClassA sa2 = JSONSerializer.Deserialize(s);
}

EscapeString API

To escape or unescape use the following static functions: These functions encode strings like encodeURI does in javascript, with apostrophe characters ' encoded into %27. The javascript equivalent would be:
function EscapeStringEscape(str){ encodeURI(str).replace(/\'/g, "%27"); }
    function EscapeStringUnescape(str){ decodeURI(str).replace(/%27/g, "\'"); }

Performance

Some simple tests in the Unity Editor using a dual core i5 2.3Ghz Macbook Pro:

Limits

These classes will parse standard XML files, but there can be rough edges on very unusually written XML files. If you happen to find files you can't parse, please send them to us.

Contact

To contact us: info@aworldforus.com