Class JSONBase

All Implemented Interfaces:
Validatable

public class JSONBase extends ValidationSupport
This class is used access data from JSON immediately after parsing. It expects a JSON Object to be the top structure in a JSON document. For instance, given this document:
     {
         "university": {
            "students" : ["mary", "joe"]
         }
     }
     
we can parse and access data such as:
     JSONBase jsonBase = new JSONBase(jsonString);
     

Once we have the instance, we can reach to a deep object inside the JSON document:

     JSONList list = jsonBase.getList("university.students");
     

As you can see, we are expecting the type at the path "university.students" to be a java.util.List (formerly a JSON array).

Both JSONMap and JSONBase have this capability we call Deep Path that allows a developer to reach directly to a deep object without having to pick apart one layer at the time.
  • Constructor Details

    • JSONBase

      public JSONBase(String json)
      Parses a JSON document from a string and creates an internal structure.
      Parameters:
      json - JSON string.
    • JSONBase

      public JSONBase(Map jsonMap)
  • Method Details

    • getList

      public JSONList getList(String attribute)
      Returns a list deep from the structure of a JSON document. Example doc:
           {
               "university": {
                   "students" : ["mary", "joe"]
      
               }
           }
       
      Parameters:
      attribute - accepts a deep path format: university.students, where every intermediate entry must be a map.
      Returns:
      list from the depths of the JSON structure.
    • getMap

      public JSONMap getMap(String attribute)
      Returns a map deep from the structure of JSON document.
           {
               "university": {
                   "students" : {
                       "mary" : {
                                 "first_name": "Mary",
                                 "last_name": "Smith"
                        },
                       "joe" : {
                           "first_name": "Joe",
                           "last_name": "Shmoe"
                       }
                   }
               }
           }
       
      Parameters:
      attribute - accepts a deep path format: university.students.joe, where every entry must be a map
      Returns:
      map from the depths of the JSON structure.
    • get

      public Object get(String attributePath)
      Returns a value of a deep attribute. Given this structure, you can get an attribute value by providing a path to the attribute such as: university.students.mary.first_name. It is presumed that the last part of a path is key inside a map of the JSON document.

      Example:
           {
               "university": {
                   students : {
                       "mary" : {
                                 "first_name": "Mary",
                                 "last_name": "Smith",
                        },
                       "joe" : {
                           "first_name": "Joe",
                           "last_name": "Shmoe",
                       }
                   }
               }
           }
       
      Specified by:
      get in interface Validatable
      Overrides:
      get in class ValidationSupport
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students.joe.first_name" where every entry must be a map except the last one.
      Returns:
      map from the depths of the JSON structure.
    • getBoolean

      public boolean getBoolean(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getBigDecimal

      public BigDecimal getBigDecimal(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getDate

      public Date getDate(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getDouble

      public Double getDouble(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getFloat

      public Float getFloat(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getInteger

      public Integer getInteger(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getLong

      public Long getLong(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getShort

      public Short getShort(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • getString

      public String getString(String attributePath)
      Parameters:
      attributePath - accepts a dot-delimited format: "university.students" where every entry must be a map.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • validateList

      public JSONBase validateList(String pathToList)
    • validateMap

      public JSONBase validateMap(String pathToMap)
    • validateBoolean

      public JSONBase validateBoolean(String pathToBoolean)
    • validateBoolean

      public JSONBase validateBoolean(String pathToBoolean, boolean expected)