Package com.storedobject.common
Class JSON
java.lang.Object
com.storedobject.common.JSON
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey
(String key) Check if this contains the given key or not.static JSON
create()
Create a blank JSON.static JSON
Construct a JSON instance from an Object that could hopefully parse into a JSON compatible String.static String
Encode a String to replace escape double-quotes so that it can be stuffed into as a part of JSON string.static JSON
Create JSON from JSS (JavaScript Structure).get
(int n) Get the value at position 'n' assuming that this is an array.Get the value for the given key.Retrieve the value for the given key and get the value from the given position from that value, assuming that value is an array type.int
Get the array size.Get this as a Boolean value.getBoolean
(String key) Get the value for the given key as a Boolean.getBoolean
(String key, int n) Retrieve the value for the given key and get the value from the given position as a Boolean from that value, assuming that value is an array type.Get this as aBigDecimal
value.getDecimal
(String key) Get the value for the given key as aBigDecimal
.getDecimal
(String key, int n) Retrieve the value for the given key and get the value from the given position as aBigDecimal
from that value, assuming that value is an array type.Get this as aNumber
.Get the value for the given key as aNumber
.Retrieve the value for the given key and get the value from the given position as aNumber
from that value, assuming that value is an array type.Get this as a String.Get the value for the given key as a String.Retrieve the value for the given key and get the value from the given position as a String from that value, assuming that value is an array type.getType()
Get theJSON.Type
of the JSON.boolean
isNull()
Check whether the value is null or not.keys()
Get the top-level keys.static String
Parse a JSS (JavaScript Structure) string to a JSON compatible string.static void
prettyWrite
(Map<String, Object> map, Writer writer) Convert a map to formatted JSON representation and write to a given Writer.void
Set JSON from an Object that could hopefully parse into a JSON compatible String.toMap()
Return the JSON as aMap
of key/value pairs.Return the JSON as a formatted (more human-readable) String.static String
toPrettyString
(Map<String, Object> map) Convert a map to formatted JSON representation.toString()
Return the JSON as a String.static String
Convert a map to JSON representation.static void
Convert a map to JSON representation and write to a given Writer.
-
Constructor Details
-
JSON
public JSON()Construct an empty JSON. -
JSON
Construct a JSON instance from an Object that could hopefully parse into a JSON compatible String. Typically, it could be aMap
or some sort of array or collection. It could also be a standalone object that can be converted to a valid JSON string. Otherwise, the following types are handled as special cases: (1) AJsonNode
instance - directly set internally, (2) AnotherJSON
instance - directly set internally, (3) AnInputStream
instance - expects a string value from the stream, (4) An instance of aReader
- expects a string value from it, (5) AURL
instance - content of theURL
is read and processed.- Parameters:
object
- JSON to construct from this Object.
-
-
Method Details
-
create
-
create
Construct a JSON instance from an Object that could hopefully parse into a JSON compatible String. Typically, it could be aMap
or some sort of array or collection. It could also be a standalone object that can be converted to a valid JSON string. Otherwise, the following types are handled as special cases: (1) AJsonNode
instance - directly set internally, (2) AnotherJSON
instance - directly set internally, (3) AnInputStream
instance - expects a string value from the stream, (4) An instance of aReader
- expects a string value from it, (5) AURL
instance - content of theURL
is read and processed.- Parameters:
object
- JSON to construct from this Object.
-
set
Set JSON from an Object that could hopefully parse into a JSON compatible String. Typically, it could be aMap
or some sort of array or collection. It could also be a standalone object that can be converted to a valid JSON string. Otherwise, the following types are handled as special cases: (1) AJsonNode
instance - directly set internally, (2) AnotherJSON
instance - directly set internally, (3) AnInputStream
instance - expects a string value from the stream, (4) An instance of aReader
- expects a string value from it, (5) AURL
instance - content of theURL
is read and processed.- Parameters:
object
- JSON to construct from this Object.
-
keys
-
getType
-
getString
-
getNumber
-
getDecimal
Get this as aBigDecimal
value. If value isnull
or if theJSON.Type
is different,null
will be returned.- Returns:
- The value.
-
getBoolean
-
getArraySize
public int getArraySize()Get the array size. (If theJSON.Type
is not an array or if it is null, 0 is returned).- Returns:
- Size of the array.
-
get
Get the value at position 'n' assuming that this is an array.Null
will be returned if this isnull
, not an array or position is outside the range.- Parameters:
n
- Position.- Returns:
- Value at the position.
-
get
-
containsKey
Check if this contains the given key or not.- Parameters:
key
- Key.- Returns:
- True or false.
-
getString
-
getNumber
-
getDecimal
Get the value for the given key as aBigDecimal
.Null
will be returned if this isnull
, key isnull
, no value exists for the given key or the value type is not matching.- Parameters:
key
- Key.- Returns:
- Value for the key.
-
getBoolean
-
get
Retrieve the value for the given key and get the value from the given position from that value, assuming that value is an array type.Null
will be returned if this isnull
, key isnull
, no value exists for the given key, the value type is not an array or nothing found at the given position.- Parameters:
key
- Key.n
- Position.- Returns:
- Value for the key.
-
getString
Retrieve the value for the given key and get the value from the given position as a String from that value, assuming that value is an array type.Null
will be returned if this isnull
, key isnull
, no value exists for the given key, the value type is not an array, nothing found at the given position or the value type at the position is not matching.- Parameters:
key
- Key.n
- Position.- Returns:
- Value for the key.
-
getNumber
Retrieve the value for the given key and get the value from the given position as aNumber
from that value, assuming that value is an array type.Null
will be returned if this isnull
, key isnull
, no value exists for the given key, the value type is not an array, nothing found at the given position or the value type at the position is not matching.- Parameters:
key
- Key.n
- Position.- Returns:
- Value for the key.
-
getDecimal
Retrieve the value for the given key and get the value from the given position as aBigDecimal
from that value, assuming that value is an array type.Null
will be returned if this isnull
, key isnull
, no value exists for the given key, the value type is not an array, nothing found at the given position or the value type at the position is not matching.- Parameters:
key
- Key.n
- Position.- Returns:
- Value for the key.
-
getBoolean
Retrieve the value for the given key and get the value from the given position as a Boolean from that value, assuming that value is an array type.Null
will be returned if this isnull
, key isnull
, no value exists for the given key, the value type is not an array, nothing found at the given position or the value type at the position is not matching.- Parameters:
key
- Key.n
- Position.- Returns:
- Value for the key.
-
isNull
public boolean isNull()Check whether the value is null or not.- Returns:
- True/false.
-
toMap
-
toString
-
toPrettyString
Return the JSON as a formatted (more human-readable) String.- Returns:
- String representation.
-
encode
-
toString
-
toPrettyString
-
write
Convert a map to JSON representation and write to a given Writer.- Parameters:
map
- Map to convert.writer
- Writer to write to.- Throws:
IOException
- If writing fails.
-
prettyWrite
Convert a map to formatted JSON representation and write to a given Writer.- Parameters:
map
- Map to convert.writer
- Writer to write to.- Throws:
IOException
- If writing fails.
-
fromJSS
Create JSON from JSS (JavaScript Structure). A JavaScript structure looks similar to JSON string with some differences: (a) Keys are not in double-quotes, (b) String values may use single-quotes or double-quotes, (c) Extraneous commas may be there after the last items. This method (a) inserts double-quotes around the keys, (b) replaces single-quotes with double-quotes and (c) removes extraneous commas. For escaping literal single-quotes or double-quotes, you should use a '\' character (backslash) just before it. In fact, any character following the backslash character will be copied to the output without any special processing. Example of a JSS is: { person: { name: 'Syam Pillai', age: 25 } }- Parameters:
jss
- JSS string.- Returns:
- JSON created from the parsed JSS.
-
parseJSS
Parse a JSS (JavaScript Structure) string to a JSON compatible string. A JavaScript structure looks similar to JSON string with some differences: (a) Keys are not in double-quotes, (b) String values may use single-quotes or double-quotes, (c) Extraneous commas may be there after the last items. This method (a) inserts double-quotes around the keys, (b) replaces single-quotes with double-quotes and (c) removes all extraneous commas. For escaping literal single-quotes or double-quotes, you should use a '\' character (backslash) just before it. In fact, any character following the backslash character will be copied to the output without any special processing. Example of a JSS is: { person: { name: 'Syam Pillai', age: 25 } }- Parameters:
jss
- JSS string.- Returns:
- A JSON compatible string.
-