Storyboard JsonPersistency

How to serialize an object model to json and how to read json into an object model

Start: Example object structure:

Step 1: Serialize to json:

      IdMap idMap = UniversityCreator.createIdMap("demo");

      JsonArray jsonArray = idMap.toJsonArray(university);

      String jsonText = jsonArray.toString(3);

      // you might write jsonText into a file

Results in:

[
   {
      "session":"demo",
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
      "id":"U1023105832581772",
      "timestamp":"1023105832581772",
      "prop":{
         "name":"StudyRight",
         "students":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Student",
               "id":"4242"
            }
         ]
      }
   },
   {
      "session":"demo",
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Student",
      "id":"4242",
      "timestamp":"242",
      "prop":{
         "name":"Karli",
         "id":"4242",
         "university":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
            "id":"U1023105832581772"
         }
      }
   }
]

Step 2: Now read it back again

      // read jsonText from file
      IdMap readerMap = UniversityCreator.createIdMap("demo");

      Object rootObject = readerMap.decode(jsonText);

      University readUniversity = (University) rootObject;