Package org.javalite.json
Class JSONBase
java.lang.Object
org.javalite.validation.ValidationSupport
org.javalite.json.JSONBase
- All Implemented Interfaces:
Validatable
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns a value of a deep attribute.getBigDecimal(String attributePath)
boolean
getBoolean(String attributePath)
getInteger(String attributePath)
Returns a list deep from the structure of a JSON document.Returns a map deep from the structure of JSON document.toString()
validateBoolean(String pathToBoolean)
validateBoolean(String pathToBoolean, boolean expected)
validateList(String pathToList)
validateMap(String pathToMap)
Methods inherited from class org.javalite.validation.ValidationSupport
addFailedValidator, converterForClass, converterForValue, convertWith, convertWith, dateFormat, dateFormat, errors, errors, isValid, removeValidator, timestampFormat, timestampFormat, validate, validate, validateNumericalityOf, validatePresenceOf, validateWith, validateWith, validateWith, validators
-
Constructor Details
-
JSONBase
Parses a JSON document from a string and creates an internal structure.- Parameters:
json
- JSON string.
-
JSONBase
-
-
Method Details
-
getList
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
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
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 interfaceValidatable
- Overrides:
get
in classValidationSupport
- 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
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getBigDecimal
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getDate
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getDouble
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getFloat
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getInteger
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getLong
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getShort
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
getString
- Parameters:
attributePath
- accepts a dot-delimited format: "university.students" where every entry must be a map.
-
toString
-
validateList
-
validateMap
-
validateBoolean
-
validateBoolean
-