Class JsonUtilities
java.lang.Object
com.inductiveautomation.ignition.common.JsonUtilities
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEncodes byte[] objects as Base64 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T>arrayMap(com.inductiveautomation.ignition.common.gson.JsonArray array, Function<com.inductiveautomation.ignition.common.gson.JsonElement, T> mapper) Converts a json array into a java list of objects, mapping each element to the type desired.static com.inductiveautomation.ignition.common.gson.JsonObjectasJsonObject(Object... pairs) Convenience function for making simple name-value pair objects.static com.inductiveautomation.ignition.common.gson.JsonElementcreateDeterministicCopy(com.inductiveautomation.ignition.common.gson.JsonElement json) Like a JsonElement.deepCopy, except sorts the keys of each JsonObject first.static com.inductiveautomation.ignition.common.gson.JsonObjectcreateFilteredCopy(com.inductiveautomation.ignition.common.gson.JsonObject root, Predicate<JsonPath> pass) Creates a deep copy of the json object omitting any portions that don't pass the predicate.static com.inductiveautomation.ignition.common.gson.JsonElementjavaToJson(Object value) Attempts to parse a java object into a JsonElement.static Objectjson2Java(com.inductiveautomation.ignition.common.gson.JsonElement element) Converts from a json tree to the canonical java object representation.static com.inductiveautomation.ignition.common.gson.JsonObjectmerge(com.inductiveautomation.ignition.common.gson.JsonObject... jsonObjects) Merge any number ofJsonObjects.static com.inductiveautomation.ignition.common.gson.JsonPrimitivestatic Optional<com.inductiveautomation.ignition.common.gson.JsonElement>static Optional<com.inductiveautomation.ignition.common.gson.JsonElement>static Optional<com.inductiveautomation.ignition.common.gson.JsonArray>static booleanreadBoolean(com.inductiveautomation.ignition.common.gson.JsonElement json, String path, boolean defaultValue) static intreadInteger(com.inductiveautomation.ignition.common.gson.JsonElement json, String path, int defaultValue) Attempts to read an integer value from a json structure.static longreadLong(com.inductiveautomation.ignition.common.gson.JsonElement json, String path, long defaultValue) static doublereadNumber(com.inductiveautomation.ignition.common.gson.JsonElement json, String path, double defaultValue) Attempts to read a double value from a json structure.static Optional<com.inductiveautomation.ignition.common.gson.JsonObject>readObject(com.inductiveautomation.ignition.common.gson.JsonElement json, String path) static shortreadShort(com.inductiveautomation.ignition.common.gson.JsonElement json, String path, short defaultValue) static StringreadString(com.inductiveautomation.ignition.common.gson.JsonElement json, String path, String defaultValue) Attempts to read a string value from a json structure.static voidwrite(DocumentElement element, JsonPath jsonPath, Object value, boolean allowNewMembers) Writes the given value to the location in the document described by the jsonPath.static voidwrite(com.inductiveautomation.ignition.common.gson.JsonElement element, JsonPath jsonPath, Object value, boolean allowNewMembers) Writes the given value to the location in the document described by the jsonPath.
-
Constructor Details
-
JsonUtilities
public JsonUtilities()
-
-
Method Details
-
asJsonObject
Convenience function for making simple name-value pair objects. -
readString
public static String readString(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull String path, @Nullable String defaultValue) Attempts to read a string value from a json structure. Extremely lenient. If anything isn't there along the path (including if the entire json structure is null), then the provided default value will be used instead.- Parameters:
json- The json structure to search through.path- A path to what you're trying to read. Will be parsed withJsonPath.parse(String), and will throw anIllegalArgumentExceptionif the path is malformeddefaultValue- The value to use if anything isn't where it should be.
-
readNumber
public static double readNumber(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull String path, double defaultValue) Attempts to read a double value from a json structure. Extremely lenient. If anything isn't there along the path (including if the entire json structure is null), then the provided default value will be used instead.- Parameters:
json- The json structure to search through.path- A path to what you're trying to read. Will be parsed withJsonPath.parse(String), and will throw anIllegalArgumentExceptionif the path is malformeddefaultValue- The value to use if anything isn't where it should be.
-
readInteger
public static int readInteger(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull String path, int defaultValue) Attempts to read an integer value from a json structure. Extremely lenient. If anything isn't there along the path (including if the entire json structure is null), then the provided default value will be used instead.- Parameters:
json- The json structure to search through.path- A path to what you're trying to read. Will be parsed withJsonPath.parse(String), and will throw anIllegalArgumentExceptionif the path is malformeddefaultValue- The value to use if anything isn't where it should be.
-
readShort
public static short readShort(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull String path, short defaultValue) -
readLong
public static long readLong(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull String path, long defaultValue) -
readBoolean
public static boolean readBoolean(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull String path, boolean defaultValue) -
readObject
-
readArray
-
read
-
read
-
merge
public static com.inductiveautomation.ignition.common.gson.JsonObject merge(com.inductiveautomation.ignition.common.gson.JsonObject... jsonObjects) Merge any number ofJsonObjects. Similar to Object.assign in the web world: the properties of object n + 1 will overwrite the properties of object n where n refers to the index of the nth var arg.- Parameters:
jsonObjects- The JsonObjects to merge. If it is null, this method will return null. If any one of the arguments is null, it will be skipped in the merging process.- Returns:
- The merged JsonObject or null if the input was null.
-
createDeterministicCopy
public static com.inductiveautomation.ignition.common.gson.JsonElement createDeterministicCopy(com.inductiveautomation.ignition.common.gson.JsonElement json) Like a JsonElement.deepCopy, except sorts the keys of each JsonObject first. -
createFilteredCopy
public static com.inductiveautomation.ignition.common.gson.JsonObject createFilteredCopy(com.inductiveautomation.ignition.common.gson.JsonObject root, Predicate<JsonPath> pass) Creates a deep copy of the json object omitting any portions that don't pass the predicate. Any array elements that don't pass the predicate will be replaced with nulls so that arrays stay the same size. -
write
public static void write(com.inductiveautomation.ignition.common.gson.JsonElement element, JsonPath jsonPath, Object value, boolean allowNewMembers) throws IllegalArgumentException Writes the given value to the location in the document described by the jsonPath.- Parameters:
element- the document (or array, etc) to write to.jsonPath- the path to the target element.value- the value to write.allowNewMembers- if false, will require that the entire path be located for write. If true and the last element of the jsonPath doesn't exist, and the penultimate element is an array or an object, a new member will be created.- Throws:
IllegalArgumentException- thrown if the path cannot be found, or if it is not a valid write target.
-
write
public static void write(DocumentElement element, JsonPath jsonPath, Object value, boolean allowNewMembers) throws IllegalArgumentException Writes the given value to the location in the document described by the jsonPath.- Parameters:
element- the document (or array, etc) to write to.jsonPath- the path to the target element.value- the value to write.allowNewMembers- if false, will require that the entire path be located for write. If true and the last element of the jsonPath doesn't exist, and the penultimate element is an array or an object, a new member will be created.- Throws:
IllegalArgumentException- thrown if the path cannot be found, or if it is not a valid write target.
-
json2Java
Converts from a json tree to the canonical java object representation. For example, a JsonPrimitive representing a string, becomes a java String. An array becomes a List, an object becomes a HashMap, etc. -
javaToJson
Attempts to parse a java object into a JsonElement. -
primitive
-
arrayMap
@Nonnull public static <T> List<T> arrayMap(@Nonnull com.inductiveautomation.ignition.common.gson.JsonArray array, @Nonnull Function<com.inductiveautomation.ignition.common.gson.JsonElement, T> mapper) Converts a json array into a java list of objects, mapping each element to the type desired.
-