Projects‎ > ‎

Parser JSON

This project defines how JSON syntax will be parsed.  The parser org.openptk.structure.JsonConverter is bi-directional. It can "decode" a JSON String into a hierarchy of OpenPTK Structures. It can also "encode" a hierarchy of OpenPTK Structures into a JSON String.
  • Nested JSON objects are added as Children to a Parent OpenPTK Structure
  • JSON arrays are identified as being multi-value will have their nested objects added as Values to a Parent OpenPTK Structure.  Multi-valued JSON objects are declared in the opentpk.xml configuration file.
This project uses the Jackson JsonParser

Release 2.2
Singular Attribute

The value is a primitive type:
  • String
  • Number
  • Boolean
  • NULL
{
   "name":"value"
}

JSON Structure
string : string

OpenPTK Structure
The structure will contain a single value which is a String:

StructureIF struct = new BasicStructure("name","value");


Complex Attribute ... a.k.a. sub-attributes

The value is a collection of objects (key / value pairs)
{
   "name" : {
      "name1" : "value1",
      "name2" : "value2"
   }
}


JSON Structure
string : object (containing key/value pairs)

OpenPTK Structure
The structure will contain two sub-structures ... added as Children

StructureIF struct = new BasicStructure("name");
struct.addChild(new BasicStructure("name1","value1");
struct.addChild(new BasicStructure("name2","value2");


Multi-valued Attribute (primitive types)


Each value is a JSON primitive type (not an object):
  • String
  • Number
  • Boolean
  • NULL
{
   "name" : [ "value1", "value2" ]
}

JSON Structure
string : array (containing strings)

OpenPTK Structure
The structure will contain multiple children, there will be a child for each array object:

StructureIF struct = new BasicStructure("name");
struct.addValue("value1");
struct.addValue("value2");


Multi-valued Attribute (structures)

Each "value" is a collection, of key-value pairs
{
   "name" : [
      {
         "name1" : "value1",
         "name2" : "value2",
         "name3" : "value3"
      },
      {
         "name1" : "value1",
         "name2" : "value2"
      }
   ]
}

JSON Structure
string : array (containing objects (level1), each level1 object contains key/value pairs)

OpenPTK Structure

StructureIF struct = new BasicStructure("name");

StructureIF item1 = new BasicStructure("item1");
item1.addChild(new BasicStructure("name1","value1"));
item1.addChild(new BasicStructure("name2","value2"));
item1.addChild(new BasicStructure("name3","value3"));

StructureIF item2 = new BasicStructure("item2");
item2.addChild(new BasicStructure("name1","value1"));
item2.addChild(new BasicStructure("name2","value2"));

struct.addChild(item1);
struct.addChild(item2);

Implementation

The attribute "categories" (identified above) need to implemented using the OpenPTK StructureIF interface.

  • If one or more "child" structures are added to a "parent" structure then it will be treated as Multi-valued (array of values).
  • If the ChildrenAsArray "flag" is set to TRUE, then child structures will be treated an an ordered array

 Category Value JSON Syntax Pseudo Code
 Singular
 primitive type"name" : "..."struct = new Structure("name","value")
 Complex object"name" : {
   "..." : "..." ,
   "..." : "..."
}
struct = new Structure("name")
struct.addChild(new Structure("",""))
struct.addChild(new Structure("",""))
 Multi-valued
 primitive type"name" : [
   "..." ,
   "..." ,
   "..."
]
struct = new Structure("name")
struct.addValue("...")
struct.addValue("...")
 Multi-valued
 object"name" : [
   {
      "..." : "..." ,
      "..." : "..." 
   } ,
   {
      "..." : "...",
      "..." : "..." 
   }
]
struct = new Structure("name")

val1 = new Structure()
val1.addChild(new Structure("","")
val1.addChild(new Structure("","")

val2 = new Structure()
val2.addChild(new Structure("","")
val2.addChild(new Structure("","")

struct.addChild(val1)
struct.addChild(val2)

Configuration

The following Converter section of the configuration file shows how multi-valued objects are defined.

      <Converter type="json" classname="org.openptk.structure.JsonConverter">
         <Structures>
            <Structure id="subjects">
               <Properties>
                  <Property name="multivalue" value="subject"/>
               </Properties>
            </Structure>
            <Structure id="forgottenPasswordAnswers">
               <Properties>
                  <Property name="multivalue" value="answer"/>
               </Properties>
            </Structure>
            <Structure id="forgottenPasswordQuestions">
               <Properties>
                  <Property name="multivalue" value="question"/>
               </Properties>
            </Structure>
            <Structure id="roles">
               <Properties>
                  <Property name="multivalue" value="role"/>
               </Properties>
            </Structure>
            <Structure id="emails">
               <Properties>
                  <Property name="multivalue" value="email"/>
               </Properties>
            </Structure>
            <Structure id="phoneNumbers">
               <Properties>
                  <Property name="multivalue" value="phoneNumber"/>
               </Properties>
            </Structure>
         </Structures>
      </Converter>

Examples

This is a String representation, toString(), of a Structure hierarchy

{
   subject={
      uniqueid="jbauer";
      lastname="Bauer";
      forgottenPasswordAnswers=["bauer","Los Angles","1234"];
      title="Agent";
      forgottenPasswordQuestions=[
         "Mothers Maiden Name",
         "City you were born",
         "Last 4 digits of Frequent Flyer"
      ];
      firstname="Jack";
      telephone="secret";
      email="jbauer@ctu.gov";
      manager="pres";
      roles=["agent"];
      fullname="Jack Bauer";
      organization="ctu"
   }
}

Here is the encode JSON output

{
   "subject":{
      "uniqueid":"jbauer",
      "lastname":"Bauer",
      "forgottenPasswordAnswers":["bauer","Los Angles","1234"],
      "title":"Agent",
      "forgottenPasswordQuestions":["Mothers Maiden Name","City you were born","Last 4 digits of Frequent Flyer"],
      "firstname":"Jack",
      "telephone":"secret",
      "email":"jbauer@ctu.gov",
      "manager":"pres",
      "roles":["agent"],
      "fullname":"Jack Bauer",
      "organization":"ctu"
   }
}