Skip to content

JSON Processing

Rajab Davudov edited this page Jun 20, 2019 · 2 revisions

JSON Processing

Dealing with JSON format of Bio Object is pretty straightforward. In order to convert to JSON you have to use following:

Engine e = new Engine() ;
e.set(Engine.CAPACITY, 4000);

Car c = new Car() ;
c.set(Car.ENGINE, e) ;
c.set(Car.PRODUCER, "Toyota") ;
c.set("undefined tag", "Hello World") ;
System.out.println(c.toJson()) ;

We will get

{
    "undefined tag": "Hello World",
    "engine": {
        "horse_power": 250,
        "engine_type": "vee",
        "number_of_cylinders": 6,
        "capacity": 4000
    },
    "producer": "Toyota",
    "year_of_production": 2007,
    "is_new_car": false
}

Note that we may have more values than we provided because Bio Object can define initial values.

In order to parse from JSON you have to use following code:

BioObject c = BioObject.fromJson(json) ;
System.out.println(c.getClass()) ;
class com.linkedlogics.bio.BioObject

Other than XML processing in JSON we don't store Bio Object type/code information. Therefore, parsing from JSON will return a BioObject instance not a Car instance. But we can use it in factory method to create actual Car instance.

BioObject cJson = BioObject.fromJson(json) ;
Car c = BioDictionary.getDictionary().getFactory().newBioObject(Car.class, cJson) ;
System.out.println(c.getClass()) ;
class com.linkedlogics.bio.test.car.Car