What does it do?
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, tooParent parent = Converter.FromJson<Parent>(jsonString);
What types does it support?
What JSON libraries does Converter support?
Example
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 ()
{
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);
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];
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];
{ "_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
namespace Owl.Converter { public interface IJsonUtils { object Deserialize(string jsonString); string Serialize(object obj); } }
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); } }
method | public static void SetUtils(IJsonUtils utils) | usage | Converter.SetUtils(MiniJsonUtils.instance); |
description | Sets adapter object that exercise serialization and deserialization. You need to set it first, before start using converter. |
method |
public static string ToJson(object obj, bool skipSameObjects=False, bool addTypes=False)
additional arguments are comportable with MonoDevelop |
usage | string jsonString = Converter.ToJson(myCustomObject); |
description |
Converts your custom object to json string.
|
method |
public static string ToXML(object obj, bool skipSameObjects=False, bool addTypes=False)
additional arguments are comportable with MonoDevelop |
usage | string xmlString = Converter.ToXML(myCustomObject); |
description | Converts your custom object to xml string, works like ToJson, see description above |
method | public static <T> FromJson<T>(string jsonString) | usage | MyCustomType myObj = Converter.FromJson<MyCustomType>(string jsonStr); |
description | Build typed object equal to exported one, from json string |
method | public static <T> FromXML<T>(string xmlString) | usage | MyCustomType myObj = Converter.FromXML<MyCustomType>(string jsonStr); |
description | Build typed object equal to exported one, from xml string |
Download:
Contacts: