Package com.epimorphics.json
Class JsonUtil
- java.lang.Object
-
- com.epimorphics.json.JsonUtil
-
public class JsonUtil extends Object
Utilities to make it easier to access JSON data and convert between JSON encodings and plain java encodings.- Author:
- Dave Reynolds
-
-
Constructor Summary
Constructors Constructor Description JsonUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.apache.jena.atlas.json.JsonValueasJson(Object obj)Convert java objects to JSON objects.static org.apache.jena.atlas.json.JsonObjectemptyObject()Return a new singleton empty objectstatic ObjectfromJson(org.apache.jena.atlas.json.JsonValue jv)Convert a JSON value to a corresponding plain Java value.static booleangetBooleanValue(org.apache.jena.atlas.json.JsonObject jo, String field, boolean dflt)static intgetIntValue(org.apache.jena.atlas.json.JsonObject jo, String field, int dflt)static longgetLongValue(org.apache.jena.atlas.json.JsonObject jo, String field, long dflt)static <T> TgetPath(org.apache.jena.atlas.json.JsonValue root, Class<T> target, Object... selectors)Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf.static ObjectgetPath(org.apache.jena.atlas.json.JsonValue root, Object... selectors)Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf.static StringgetStringValue(org.apache.jena.atlas.json.JsonObject jo, String field)static StringgetStringValue(org.apache.jena.atlas.json.JsonObject jo, String field, String dflt)static org.apache.jena.atlas.json.JsonValueloadFromFile(String filename)static org.apache.jena.atlas.json.JsonObjectmakeJson(Object... args)Create a simple Json Object.static org.apache.jena.atlas.json.JsonObjectmakeJson(org.apache.jena.atlas.json.JsonObject base, Object... args)Create a JSON object as a copy of an existing object, with new/replacement key values taken from the argsstatic org.apache.jena.atlas.json.JsonObjectmerge(org.apache.jena.atlas.json.JsonObject base, org.apache.jena.atlas.json.JsonObject extend)Create a new json object by cloning one object and then asserting all values from the second.static org.apache.jena.atlas.json.JsonObjectmergeInto(org.apache.jena.atlas.json.JsonObject base, org.apache.jena.atlas.json.JsonObject extend)Merge all the extend values into the base object returning the base object, no copy.static org.apache.jena.atlas.json.JsonArrayreadArray(String filename)Parse a JSON or YAML file to an array of json objects, supports multi-document yaml filesstatic org.apache.jena.atlas.json.JsonArrayreadArray(String filename, InputStream is)Parse a JSON or YAML file to an array of json objects, supports multi-document yaml filesstatic org.apache.jena.atlas.json.JsonObjectreadObject(String filename)Parse a JSON or YAML file to a json object.static org.apache.jena.atlas.json.JsonObjectreadObject(String filename, InputStream is)Parse a JSON or YAML file to a json object.
-
-
-
Method Detail
-
loadFromFile
public static org.apache.jena.atlas.json.JsonValue loadFromFile(String filename)
-
getStringValue
public static String getStringValue(org.apache.jena.atlas.json.JsonObject jo, String field)
-
getStringValue
public static String getStringValue(org.apache.jena.atlas.json.JsonObject jo, String field, String dflt)
-
getIntValue
public static int getIntValue(org.apache.jena.atlas.json.JsonObject jo, String field, int dflt)
-
getLongValue
public static long getLongValue(org.apache.jena.atlas.json.JsonObject jo, String field, long dflt)
-
getBooleanValue
public static boolean getBooleanValue(org.apache.jena.atlas.json.JsonObject jo, String field, boolean dflt)
-
asJson
public static org.apache.jena.atlas.json.JsonValue asJson(Object obj)
Convert java objects to JSON objects. Handles strings, numbers, booleans, maps and lists and simple nests thereof. Throws runtime exception if this is not possible.
-
makeJson
public static org.apache.jena.atlas.json.JsonObject makeJson(Object... args)
Create a simple Json Object.- Parameters:
args- alternating parameter names and parameter values
-
makeJson
public static org.apache.jena.atlas.json.JsonObject makeJson(org.apache.jena.atlas.json.JsonObject base, Object... args)Create a JSON object as a copy of an existing object, with new/replacement key values taken from the args- Parameters:
base- object to copy, can be nullargs- alternating parameter names and parameter values
-
fromJson
public static Object fromJson(org.apache.jena.atlas.json.JsonValue jv)
Convert a JSON value to a corresponding plain Java value. A near inverse of asJson except that number types might be changed.
-
getPath
public static Object getPath(org.apache.jena.atlas.json.JsonValue root, Object... selectors)
Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf. Throws runtime exception if the structure does not match.
-
getPath
public static <T> T getPath(org.apache.jena.atlas.json.JsonValue root, Class<T> target, Object... selectors)Return an element of a JSON structure by following a sequence of object field selectors (strings) or array indexes (integers) and return the java-ized version of the leaf. Returns the value as the requested target class if possible. Throws runtime exception if the structure does not match.
-
emptyObject
public static org.apache.jena.atlas.json.JsonObject emptyObject()
Return a new singleton empty object
-
merge
public static org.apache.jena.atlas.json.JsonObject merge(org.apache.jena.atlas.json.JsonObject base, org.apache.jena.atlas.json.JsonObject extend)Create a new json object by cloning one object and then asserting all values from the second. Shallow copy only.
-
mergeInto
public static org.apache.jena.atlas.json.JsonObject mergeInto(org.apache.jena.atlas.json.JsonObject base, org.apache.jena.atlas.json.JsonObject extend)Merge all the extend values into the base object returning the base object, no copy.
-
readArray
public static org.apache.jena.atlas.json.JsonArray readArray(String filename, InputStream is)
Parse a JSON or YAML file to an array of json objects, supports multi-document yaml files
-
readArray
public static org.apache.jena.atlas.json.JsonArray readArray(String filename) throws FileNotFoundException
Parse a JSON or YAML file to an array of json objects, supports multi-document yaml files- Throws:
FileNotFoundException
-
readObject
public static org.apache.jena.atlas.json.JsonObject readObject(String filename, InputStream is)
Parse a JSON or YAML file to a json object.
-
readObject
public static org.apache.jena.atlas.json.JsonObject readObject(String filename) throws FileNotFoundException
Parse a JSON or YAML file to a json object.- Throws:
FileNotFoundException
-
-