Class JsonUtilities
- java.lang.Object
-
- com.inductiveautomation.ignition.common.JsonUtilities
-
public class JsonUtilities extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JsonUtilities.ByteArrayAdapter
Encodes byte[] objects as Base64
-
Constructor Summary
Constructors Constructor Description JsonUtilities()
-
Method Summary
All 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.JsonObject
asJsonObject(java.lang.Object... pairs)
Convenience function for making simple name-value pair objects.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.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.static java.lang.Object
json2Java(com.inductiveautomation.ignition.common.gson.JsonElement element)
Converts from a json tree to the canonical java object representation.static com.inductiveautomation.ignition.common.gson.JsonObject
merge(com.inductiveautomation.ignition.common.gson.JsonObject... jsonObjects)
Merge any number ofJsonObjects
.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 boolean
readBoolean(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, boolean defaultValue)
static int
readInteger(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, int defaultValue)
Attempts to read an integer value from a json structure.static double
readNumber(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 short
readShort(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path, short defaultValue)
static java.lang.String
readString(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.
-
-
-
Method Detail
-
asJsonObject
public static com.inductiveautomation.ignition.common.gson.JsonObject asJsonObject(java.lang.Object... pairs)
Convenience function for making simple name-value pair objects.
-
readString
public 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 withJsonPath.parse(String)
, and will throw anIllegalArgumentException
if 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 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 withJsonPath.parse(String)
, and will throw anIllegalArgumentException
if 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 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 withJsonPath.parse(String)
, and will throw anIllegalArgumentException
if 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 java.lang.String path, short defaultValue)
-
readBoolean
public static boolean readBoolean(@Nullable com.inductiveautomation.ignition.common.gson.JsonElement json, @Nonnull java.lang.String path, boolean defaultValue)
-
readObject
public static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonObject> readObject(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)
-
readArray
public static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonArray> readArray(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)
-
read
public static java.util.Optional<com.inductiveautomation.ignition.common.gson.JsonElement> read(com.inductiveautomation.ignition.common.gson.JsonElement json, java.lang.String path)
-
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, 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.
-
json2Java
public 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.
-
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.
-
-