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:

[
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
      "id":"demo.U1",
      "prop":{
         "name":"StudyRight",
         "students":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Student",
               "id":"demo.S2"
            }
         ],
         "rooms":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R3"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R4"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R5"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R6"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R7"
            }
         ]
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Student",
      "id":"demo.S2",
      "prop":{
         "name":"Karli",
         "id":"4242",
         "university":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
            "id":"demo.U1"
         },
         "in":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
            "id":"demo.R3"
         }
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
      "id":"demo.R3",
      "prop":{
         "name":"senate",
         "topic":"math",
         "credits":17,
         "university":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
            "id":"demo.U1"
         },
         "students":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Student",
               "id":"demo.S2"
            }
         ],
         "doors":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R4"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R5"
            }
         ],
         "assignments":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Assignment",
               "id":"demo.A8"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Assignment",
               "id":"demo.A9"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Assignment",
               "id":"demo.A10"
            }
         ]
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
      "id":"demo.R4",
      "prop":{
         "name":"7522",
         "topic":"arts",
         "credits":16,
         "university":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
            "id":"demo.U1"
         },
         "doors":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R3"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R5"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R6"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R7"
            }
         ]
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
      "id":"demo.R5",
      "prop":{
         "name":"gymnasium",
         "topic":"sports",
         "credits":25,
         "university":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
            "id":"demo.U1"
         },
         "doors":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R3"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R4"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R6"
            }
         ]
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
      "id":"demo.R6",
      "prop":{
         "name":"The End",
         "topic":"exam",
         "university":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
            "id":"demo.U1"
         },
         "doors":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R5"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R4"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R7"
            }
         ]
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
      "id":"demo.R7",
      "prop":{
         "name":"7422",
         "topic":"Software Engineering",
         "credits":42,
         "university":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.University",
            "id":"demo.U1"
         },
         "doors":[
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R4"
            },
            {
               "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
               "id":"demo.R6"
            }
         ]
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Assignment",
      "id":"demo.A8",
      "prop":{
         "content":"Matrix Multiplication",
         "points":5,
         "room":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
            "id":"demo.R3"
         }
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Assignment",
      "id":"demo.A9",
      "prop":{
         "content":"Series",
         "points":6,
         "room":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
            "id":"demo.R3"
         }
      }
   },
   {
      "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Assignment",
      "id":"demo.A10",
      "prop":{
         "content":"Integrals",
         "points":8,
         "room":{
            "class":"org.sdmlib.test.examples.studyrightWithAssignments.model.Room",
            "id":"demo.R3"
         }
      }
   }
]

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;