Skip to content

2 classes that allow to cast a json to any provided generic type.

Notifications You must be signed in to change notification settings

vLabayen/NestedJsonParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 

Repository files navigation

NestedJsonParser

3 classes that allow to cast a json to any provided generic type and to obtain the json of a class The parser, reader and writter are not Unity dependent. The classes are implemented using reflection and recusive approaches.

To import them, just download JsonParser, JsonReader & JsonWriter to your proyect folder
Usage example in Test and TestObjects class.

One usage example (extracted from the tests) :

Given the following structure of classes:

public class TestObject7{
    public TestObject6[] test_arr { get; set; }
}

public class TestObject6{
    public NestedObject2 nested { get; set; }
}

public class NestedObject2 {
    public NestedObject[] nested_arr { get; set; }
    public TestObject5 nested_test { get; set; }
}

public class TestObject5{
    public NestedObject[] nesteds { get; set; }
}

public class NestedObject {
    public string name { get; set; }
    public int id { get; set; }
}

And given a json string like :

{"test_arr":[{
    "nested":{
        "nested_arr":[
            {"name":"vic","id":0},
            {"name":"yuan","id":1000}
        ],
        "nested_test":{
            "nesteds":[
                {"name":"vic","id":0},
                {"name":"yuan","id":1000},
                {"name":"pepe","id":-30}
            ]
        }
    }
},{
    "nested":{
        "nested_arr":[
            {"name":"vic","id":0},
            {"name":"yuan","id":1000}
        ],
        "nested_test":{
            "nesteds":[
                {"name":"vic","id":0},
                {"name":"yuan","id":1000},
                {"name":"pepe","id":-30}
            ]
        }
    }
}]}"

We can parse it to a TestObject7 instance like :
TestObject7 obj = JsonParser.FromJson<TestObject7>(jsonString);

From the object we can obtain its json with :
string json = JsonParser.ToJson<TestObject7>(obj);

More examples can be found in the test class. They are Unity dependent to run, but they can also be easily parsed to a console application.

About

2 classes that allow to cast a json to any provided generic type.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages