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 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
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 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.
-
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.
-
-