Package com.api.json
Class JSONObject
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<String,Object>
-
- com.api.json.JSONObject
-
- All Implemented Interfaces:
JSONArtifact,Serializable,Cloneable,Map<String,Object>
public class JSONObject extends HashMap<String,Object> implements JSONArtifact
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description JSONObject()Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static booleanisValidObject(Object object)Determines whether the supplied object is a valid JSON valuestatic booleanisValidType(Class<?> clazz)Determines whether the supplied class is a valid JSON typeObjectmerge(String key, Object value, BiFunction<Object,Object,Object> remappingFunction)Provides merger of the supplied key and value according to logic ni the supplied remappingFunctionstatic JSONObjectparse(InputStream is)Parse the supplied input stream to generate a JSONObjectstatic JSONObjectparse(Reader reader)Parse the supplied reader to generate a JSONObjectstatic JSONObjectparse(String input)Parse the supplied input to generate a JSONObjectObjectput(String key, Object value)voidputAll(Map m)ObjectputIfAbsent(String key, Object value)Objectreplace(String key, Object value)booleanreplace(String key, Object oldValue, Object value)Stringserialize()Convert this object into a String of JSON text.Stringserialize(boolean verbose)voidserialize(OutputStream os)Convert this object into a stream of JSON text.voidserialize(OutputStream os, boolean verbose)Convert this object into a stream of JSON text.voidserialize(Writer writer)Convert this object into a stream of JSON text.voidserialize(Writer writer, boolean verbose)Convert this object into a stream of JSON text, specifying verbosity.StringtoString()StringtoString(StringBuilder sb, int indent, int incr)Abstract method to help with recursive formatting of JSON into a String-
Methods inherited from class java.util.HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, remove, remove, replaceAll, size, values
-
Methods inherited from class java.util.AbstractMap
equals, hashCode
-
-
-
-
Method Detail
-
isValidObject
public static boolean isValidObject(Object object)
Determines whether the supplied object is a valid JSON value- Parameters:
object- The object to be tested for validity- Returns:
- True if the supplied object is a valid JSON value
-
isValidType
public static boolean isValidType(Class<?> clazz)
Determines whether the supplied class is a valid JSON type- Parameters:
clazz- The class to be tested for validity- Returns:
- True if the supplied class is a valid JSON type
-
parse
public static JSONObject parse(InputStream is) throws IOException
Parse the supplied input stream to generate a JSONObject- Parameters:
is- InputStream to be parsed- Returns:
- JSONObject parsed from the supplied input stream
- Throws:
IOException- if an error occurs reading or parsing the input stream
-
parse
public static JSONObject parse(Reader reader) throws IOException
Parse the supplied reader to generate a JSONObject- Parameters:
reader- Reader to be parsed- Returns:
- JSONObject parsed from the supplied reader
- Throws:
IOException- if an error occurs reading or parsing the reader
-
parse
public static JSONObject parse(String input) throws IOException
Parse the supplied input to generate a JSONObject- Parameters:
input- InputStream to be parsed- Returns:
- JSONObject parsed from the supplied input
- Throws:
IOException- if an error occurs reading or parsing the input
-
merge
public Object merge(String key, Object value, BiFunction<Object,Object,Object> remappingFunction)
Provides merger of the supplied key and value according to logic ni the supplied remappingFunction- Parameters:
key- The key of the object to be mergedvalue- The value of the object to be mergedremappingFunction- The function to control how merger is accomplished- Returns:
- The resulting object after merger
-
putAll
public void putAll(Map m)
-
putIfAbsent
public Object putIfAbsent(String key, Object value)
- Specified by:
putIfAbsentin interfaceMap<String,Object>- Overrides:
putIfAbsentin classHashMap<String,Object>- See Also:
HashMap.putIfAbsent(java.lang.Object, java.lang.Object)
-
serialize
public String serialize() throws IOException
Description copied from interface:JSONArtifactConvert this object into a String of JSON text.- Specified by:
serializein interfaceJSONArtifact- Returns:
- String containing the JSON formatted according to the verbose flag.
- Throws:
IOException- Thrown on IO errors during serialization.- See Also:
JSONArtifact.serialize()
-
serialize
public String serialize(boolean verbose) throws IOException
- Specified by:
serializein interfaceJSONArtifact- Parameters:
verbose- Whether or not to write the JSON text in a verbose format.- Returns:
- String containing the JSON formatted according to the verbose flag.
- Throws:
IOException- Thrown on IO errors during serialization.- See Also:
JSONArtifact.serialize(boolean)
-
serialize
public void serialize(OutputStream os) throws IOException
Description copied from interface:JSONArtifactConvert this object into a stream of JSON text. Same as calling serialize(os,false); Note that encoding is always written as UTF-8, as per JSON spec.- Specified by:
serializein interfaceJSONArtifact- Parameters:
os- The output stream to serialize data to.- Throws:
IOException- Thrown on IO errors during serialization.- See Also:
JSONArtifact.serialize(java.io.OutputStream)
-
serialize
public void serialize(OutputStream os, boolean verbose) throws IOException
Description copied from interface:JSONArtifactConvert this object into a stream of JSON text. Same as calling serialize(writer,false); Note that encoding is always written as UTF-8, as per JSON spec.- Specified by:
serializein interfaceJSONArtifact- Parameters:
os- The output stream to serialize data to.verbose- Whether or not to write the JSON text in a verbose format.- Throws:
IOException- Thrown on IO errors during serialization.- See Also:
JSONArtifact.serialize(java.io.OutputStream, boolean)
-
serialize
public void serialize(Writer writer) throws IOException
Description copied from interface:JSONArtifactConvert this object into a stream of JSON text. Same as calling serialize(writer,false);- Specified by:
serializein interfaceJSONArtifact- Parameters:
writer- The writer which to serialize the JSON text to.- Throws:
IOException- Thrown on IO errors during serialization.- See Also:
JSONArtifact.serialize(java.io.Writer)
-
serialize
public void serialize(Writer writer, boolean verbose) throws IOException
Description copied from interface:JSONArtifactConvert this object into a stream of JSON text, specifying verbosity.- Specified by:
serializein interfaceJSONArtifact- Parameters:
writer- The writer which to serialize the JSON text to.verbose- Whether or not to write the JSON text in a verbose format.- Throws:
IOException- Thrown on IO errors during serialization.- See Also:
JSONArtifact.serialize(java.io.Writer, boolean)
-
toString
public String toString()
- Overrides:
toStringin classAbstractMap<String,Object>- Returns:
- An unformatted rendering of this
JSONObject
-
toString
public String toString(StringBuilder sb, int indent, int incr)
Description copied from interface:JSONArtifactAbstract method to help with recursive formatting of JSON into a String- Specified by:
toStringin interfaceJSONArtifact- Parameters:
sb- Buffer to receive formatted JSON contentindent- The amount of indentation expected in the formatted output.incr- the amount of incremental spacing used for formatted output.- Returns:
- Formatted JSON output.
- See Also:
JSONArtifact.toString(java.lang.StringBuilder, int, int)
-
-