JSON & XML
C# De/Serializer

Contents:


What does it do?

OwlConverter can export exemplars of complex typed objects to JSON and XML strings,
and build equal typed exemplars from this strings.
For example you have two classes like:

public class Parent { public Child selected; public List<Child> children = new List<Child>(); } public class Child { public Parent parent; public bool selected; public string name; public Vector2 position; }

You may convert exemplar of Parent with all its children in one line:

string jsonString = Converter.ToJson(parent, true);

and after get equal object in one line, too

Parent parent = Converter.FromJson<Parent>(jsonString);

What types does it support?

OwlConverter supports types:
  • simple c# types like: string, int, float, bool
  • Unity3d types as Vecto2,3,4, Quaternion, etc
  • List<T> of above
  • objects which fields represented by above and similar objects
Note, that all fields have to be public.

What JSON libraries does Converter support?

Converter may works with any library you prefer,
using custom adapter that implements
IJsonUtils interface
(containing only two methods: Serialize and Deserialize).
And this package includs MiniJSON library, and realisation for it, with a source code,
when you will see it, you will be able to write your custom adapter in a minute.

Example

This example contains in package demonstration scene.

Have you ever play any MMORPG?
Let's imagine type, object of it, describes the world, so
class World, will contains list of fractions,
and each fraction contains list of races,
and each race contains list of character classes.

On c# it looks like
DataTypes.cs

using System; using System.Collections; using System.Collections.Generic; public class World { public List<Fraction> fractions = new List<Fraction>(); } public class Fraction { public string name; public List<Race> races = new List<Race>(); public Fraction() { } public Fraction(string name) { this.name = name; } } public class Race { public string name; public List<CharacterClass> classes = new List<CharacterClass>(); public Race() { } public Race(string name) { this.name = name; } } public class CharacterClass { public string name; public int maxHP; public CharacterClass() { } public CharacterClass(string name, int maxHP) { this.name = name; this.maxHP = maxHP; } }

And static function that fills world exemplar some test date

public static void FillData(World world) { Fraction horde = new Fraction("horde"); Fraction aliance = new Fraction("aliance"); Race orcs = new Race("orcs"); Race undeads = new Race("undeads"); Race humans = new Race("humans"); Race elves = new Race("elves"); CharacterClass warrior = new CharacterClass("warrior",20000); CharacterClass warlock = new CharacterClass("warlock",10000); CharacterClass shaman = new CharacterClass("shaman",15000); CharacterClass druid = new CharacterClass("druid",12000); world.fractions.Add(horde); world.fractions.Add(aliance); horde.races.Add(orcs); horde.races.Add(undeads); aliance.races.Add(humans); aliance.races.Add(elves); humans.classes.Add(warrior); humans.classes.Add(warlock); elves.classes.Add(warrior); elves.classes.Add(druid); orcs.classes.Add(warrior); orcs.classes.Add(shaman); undeads.classes.Add(warrior); undeads.classes.Add(warlock); }

Now we may create emptyGame object and component with code
Example.cs

using UnityEngine; using Owl.Converter; public class Example : MonoBehaviour { void Start () { //Before you start to use Converter, //you have to set utils, in this example I use miniJSON Converter.SetUtils(JsonMiniConverter.instance); // now let's create object World world = new World(); // fill it, FillData(world); // and sevialize to string string jsonString = Converter.ToJson(world); print(jsonString); } }

And console outputs text like this:

{"fractions": [ { "name":"horde", "races": [ { "name":"orcs", "classes": [ { "name":"warrior", "maxHP":20000 }, { "name":"shaman", "maxHP":15000 } ] }, { "name":"undeads", "classes": [ { "name":"warrior", "maxHP":20000 }, { "name":"warlock", "maxHP":10000 } ] } ] }, { "name":"aliance", "races": [ { "name":"humans", "classes": [ { "name":"warrior", "maxHP":20000 }, { "name":"warlock", "maxHP":10000 } ] }, { "name":"elves", "classes": [ { "name":"warrior", "maxHP":20000 }, { "name":"druid", "maxHP":12000 } ] } ] } ] }

With converter you may rebuild new object equal to converted:

using UnityEngine; using Owl.Converter; public class Example : MonoBehaviour { void Start () { Converter.SetUtils(JsonMiniConverter.instance); World world = new World(); FillData(world); string jsonString = Converter.ToJson(world); // creates clone World clone = Converter.FromJson<World>(jsonString); } }

But in this case, objects that was same became a different
they are equal but not same

CharacterClass orcWarrior; CharacterClass undeadWarrior; orcWarrior = world.fractions[0].races[0].classes[0]; undeadWarrior = world.fractions[0].races[1].classes[0]; //same in original print(orcWarrior == undeadWarrior); //true orcWarrior = clone.fractions[0].races[0].classes[0]; undeadWarrior = clone.fractions[0].races[1].classes[0]; //different in clone print(orcWarrior == undeadWarrior); //false

to prevent this, set skipSame argument to true

jsonString = Converter.ToJson(world, true); World clone = Converter.FromJson<World>(jsonString); orcWarrior = clone.fractions[0].races[0].classes[0]; undeadWarrior = clone.fractions[0].races[1].classes[0]; // now they are same print(orcWarrior == undeadWarrior); //true

With true skipSame, generated json strins looks like

{ "_id":1, "fractions": [ { "_id":2, "name":"horde", "races": [ { "_id":3, "name":"orcs", "classes": [ { "_id":4, "name":"warrior", "maxHP":20000 }, { "_id":5, "name":"shaman", "maxHP":15000 } ] }, { "_id":6, "name":"undeads", "classes": [ { "_id":4 }, { "_id":7, "name":"warlock", "maxHP":10000 } ] } ] }, { "_id":8, "name":"aliance", "races": [ { "_id":9 "name":"humans", "classes": [ { "_id":4 }, { "_id":7 } ] }, { "_id":10, "name":"elves", "classes": [ { "_id":4 }, { "_id":11, "name":"druid", "maxHP":12000 } ] } ] } ] }

As you can see, character classes of humanas have only ids, and will refer to object with theirs ids created before.
skipSame is also useful if you have cycle reference in your date.

if you have types from different assemby, or define fields with interfaces or abstract classes,
use addTypes param in ToJson method

World clone = Converter.FromJson<World>(jsonString,false,true);

The most safe way to export data correct, is to set both additional params to true.

Note:
- all fields to export have to be public
- all types may have some constructors, but one must have no arguments.

This example contains in package demonstration scene.

Reference

Converter package contains main class of converter, one interface, one realization, and MiniJson.cs

IJsonUtils.cs - interface for json lib adapter.
You may wirte your realization of this interface, to use other json library.
Below you'll find realisation for MiniJson.

namespace Owl.Converter { public interface IJsonUtils { object Deserialize(string jsonString); string Serialize(object obj); } }


MiniJsonUtils.cs - IJsonUtils realization for MiniJSON lib, included in package

using MiniJSON; using Owl.Converter; public class MiniJsonUtils : IJsonUtils { public static MiniJsonUtils instance = new MiniJsonUtils(); public string Serialize(object obj) { return Json.Serialize(obj); } public object Deserialize(string jsonString) { return Json.Deserialize(jsonString); } }


Converter.cs - main class api

See example for more information.

Download:

Unity Asset Store link:
https://www.assetstore.unity3d.com/#/content/9420

Contacts:

To ask any question mail me:
pyro@pyromancers.com