Class JsonUtilities
- java.lang.Object
- 
- com.inductiveautomation.ignition.common.JsonUtilities
 
- 
 public class JsonUtilities extends java.lang.Object
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classJsonUtilities.ByteArrayAdapterEncodes byte[] objects as Base64
 - 
Constructor SummaryConstructors Constructor Description JsonUtilities()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.List<T>arrayMap(com.inductiveautomation.ignition.common.gson.JsonArray array, java.util.function.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(java.lang.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, java.util.function.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(java.lang.Object value)Attempts to parse a java object into a JsonElement.static java.lang.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.JsonPrimitiveprimitive(java.lang.Object value)static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonElement>read(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonArray>readArray(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)static booleanreadBoolean(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, boolean defaultValue)static intreadInteger(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, int defaultValue)Attempts to read an integer value from a json structure.static longreadLong(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, long defaultValue)static doublereadNumber(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, double defaultValue)Attempts to read a double value from a json structure.static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonObject>readObject(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)static shortreadShort(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, short defaultValue)static java.lang.StringreadString(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, java.lang.String defaultValue)Attempts to read a string value from a json structure.static voidwrite(DocumentElement element, JsonPath jsonPath, java.lang.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, java.lang.Object value, boolean allowNewMembers)Writes the given value to the location in the document described by the jsonPath.
 
- 
- 
- 
Method Detail- 
asJsonObjectpublic static com.inductiveautomation.ignition.common.gson.JsonObject asJsonObject(java.lang.Object... pairs) Convenience function for making simple name-value pair objects.
 - 
readStringpublic static java.lang.String readString(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull java.lang.String path, @Nullable java.lang.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 with- JsonPath.parse(String), and will throw an- IllegalArgumentExceptionif the path is malformed
- defaultValue- The value to use if anything isn't where it should be.
 
 - 
readNumberpublic static double readNumber(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull java.lang.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 with- JsonPath.parse(String), and will throw an- IllegalArgumentExceptionif the path is malformed
- defaultValue- The value to use if anything isn't where it should be.
 
 - 
readIntegerpublic static int readInteger(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull java.lang.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 with- JsonPath.parse(String), and will throw an- IllegalArgumentExceptionif the path is malformed
- defaultValue- The value to use if anything isn't where it should be.
 
 - 
readShortpublic static short readShort(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull java.lang.String path, short defaultValue)
 - 
readLongpublic static long readLong(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull java.lang.String path, long defaultValue)
 - 
readBooleanpublic static boolean readBoolean(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull java.lang.String path, boolean defaultValue)
 - 
readObjectpublic static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonObject> readObject(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)
 - 
readArraypublic static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonArray> readArray(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)
 - 
readpublic static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonElement> read(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)
 - 
mergepublic 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.
 
 - 
createDeterministicCopypublic 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.
 - 
createFilteredCopypublic static com.inductiveautomation.ignition.common.gson.JsonObject createFilteredCopy(com.inductiveautomation.ignition.common.gson.JsonObject root, java.util.function.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.
 - 
writepublic static void write(com.inductiveautomation.ignition.common.gson.JsonElement element, JsonPath jsonPath, java.lang.Object value, boolean allowNewMembers) throws java.lang.IllegalArgumentExceptionWrites 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:
- java.lang.IllegalArgumentException- thrown if the path cannot be found, or if it is not a valid write target.
 
 - 
writepublic static void write(DocumentElement element, JsonPath jsonPath, java.lang.Object value, boolean allowNewMembers) throws java.lang.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:
- java.lang.IllegalArgumentException- thrown if the path cannot be found, or if it is not a valid write target.
 
 - 
json2Javapublic static java.lang.Object json2Java(com.inductiveautomation.ignition.common.gson.JsonElement element) 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.
 - 
javaToJsonpublic static com.inductiveautomation.ignition.common.gson.JsonElement javaToJson(java.lang.Object value) Attempts to parse a java object into a JsonElement.
 - 
primitivepublic static com.inductiveautomation.ignition.common.gson.JsonPrimitive primitive(java.lang.Object value) 
 - 
arrayMap@Nonnull public static <T> java.util.List<T> arrayMap(@Nonnull com.inductiveautomation.ignition.common.gson.JsonArray array, @Nonnull java.util.function.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.
 
- 
 
-