public class JsonUtils
extends java.lang.Object
Many of the parse/write methods will take a Type object instead of a Class object.
This is to allow deserializing objects which have direct generic parameters through
the use of TypeToken. You can use TypeToken as follows:
private static final Type STRING_LIST_TYPE = new com.google.gson.reflect.TypeToken<List<String>>(){ }.getType();.
Created by covers1624 on 11/11/21.
| Constructor and Description |
|---|
JsonUtils() |
| Modifier and Type | Method and Description |
|---|---|
static JsonPrimitive |
getAsPrimitive(JsonObject obj,
java.lang.String key)
Try and get a
JsonPrimitive child from the given JsonObject. |
static JsonPrimitive |
getAsPrimitiveOrNull(JsonObject obj,
java.lang.String key)
Try and get a
JsonPrimitive child from the given JsonObject. |
static int |
getInt(JsonObject obj,
java.lang.String key)
Try and get an
int child from the given JsonObject. |
static int |
getInt(JsonObject obj,
java.lang.String key,
int default_)
Try and get an
int child from the given JsonObject. |
static java.lang.String |
getString(JsonObject obj,
java.lang.String key)
Try and get a
String child from the given JsonObject. |
static java.lang.String |
getString(JsonObject obj,
java.lang.String key,
java.lang.String default_)
Try and get an
String child from the given JsonObject. |
static <T> T |
parse(Gson gson,
java.io.InputStream is,
java.lang.reflect.Type t)
Deserialize Json from the given
InputStream as the given Type. |
static <T> T |
parse(Gson gson,
java.nio.file.Path path,
java.lang.reflect.Type t)
Deserialize Json from the given
Path as the given Type. |
static <T> T |
parse(Gson gson,
java.io.Reader reader,
java.lang.reflect.Type t)
Deserialize Json from the given
Reader as the given Type. |
static void |
write(Gson gson,
java.io.OutputStream os,
java.lang.Object instance)
Serialize the provided Object to Json and write to the given
OutputStream. |
static void |
write(Gson gson,
java.io.OutputStream os,
java.lang.Object instance,
java.lang.reflect.Type t)
Serialize the provided Object to Json and write to the given OutputStream.
|
static void |
write(Gson gson,
java.nio.file.Path path,
java.lang.Object instance)
Serialize the provided Object to Json and write to the given
Path. |
static void |
write(Gson gson,
java.nio.file.Path path,
java.lang.Object instance,
java.lang.reflect.Type t)
Serialize the provided Object to json and write it to the given
Path. |
public static <T> T parse(Gson gson,
java.nio.file.Path path,
java.lang.reflect.Type t)
throws java.io.IOException,
JsonParseException
Path as the given Type.gson - The Gson instance to use.path - The Path to read from.t - The Type to deserialize from.JsonParseException - Propagated from Gson#fromJson(Reader, Type),
thrown when Gson encounters an error deserializing the object.java.io.IOException - Thrown when an IO error occurs.public static <T> T parse(Gson gson,
java.io.InputStream is,
java.lang.reflect.Type t)
throws java.io.IOException,
JsonParseException
InputStream as the given Type.gson - The Gson instance to use.is - The InputStream to read from.t - The Type to deserialize from.JsonParseException - Propagated from Gson#fromJson(Reader, Type),
thrown when Gson encounters an error deserializing the object.java.io.IOException - Thrown when an IO error occurs.public static <T> T parse(Gson gson,
java.io.Reader reader,
java.lang.reflect.Type t)
throws java.io.IOException,
JsonParseException
Reader as the given Type.gson - The Gson instance to use.reader - The Reader to read from.t - The Type to deserialize from.JsonParseException - Propagated from Gson#fromJson(Reader, Type),
thrown when Gson encounters an error deserializing the object.java.io.IOException - Thrown when an IO error occurs.public static void write(Gson gson,
java.nio.file.Path path,
java.lang.Object instance)
throws java.io.IOException,
JsonIOException
Path.
Use this method when your instance doesn't have any direct generic parameters.
gson - The Gson instance to use.path - The Path to write to.instance - The Object instance to serialize.JsonIOException - Propagated from Gson#toJson(Object, Type, Appendable),
thrown when Gson encounters an error serializing the instance.java.io.IOException - Thrown when an IO error occurs.public static void write(Gson gson,
java.nio.file.Path path,
java.lang.Object instance,
java.lang.reflect.Type t)
throws java.io.IOException,
JsonIOException
Path.
Use this method directly if your instance has direct generic parameters.
gson - The Gson instance to use.path - The Path to write to.instance - The Object instance to serialize.t - The Type of the Object instance.JsonIOException - Propagated from Gson#toJson(Object, Type, Appendable),
thrown when Gson encounters an error serializing the instance.java.io.IOException - Thrown when an IO error occurs.public static void write(Gson gson,
java.io.OutputStream os,
java.lang.Object instance)
throws java.io.IOException,
JsonIOException
OutputStream.
Use this method when your instance doesn't have any direct generic parameters.
gson - The Gson instance to use.os - The OutputStream to write to.instance - The Object instance to serialize.JsonIOException - Propagated from Gson#toJson(Object, Type, Appendable),
thrown when Gson encounters an error serializing the instance.java.io.IOException - Thrown when an IO error occurs.public static void write(Gson gson,
java.io.OutputStream os,
java.lang.Object instance,
java.lang.reflect.Type t)
throws java.io.IOException,
JsonIOException
Use this method directly if your instance has direct generic parameters.
gson - The Gson instance to use.os - The OutputStream to write to.instance - The Object instance to write.t - The Type of the Object instance.JsonIOException - Propagated from Gson#toJson(Object, Type, Appendable),
thrown when Gson encounters an error serializing the instance.java.io.IOException - Thrown when an IO error occurs.public static JsonPrimitive getAsPrimitive(JsonObject obj,
java.lang.String key)
throws JsonParseException
JsonPrimitive child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.JsonPrimitive.JsonParseException - If the given child did not exist, or was not a JsonPrimitive.public static JsonPrimitive getAsPrimitiveOrNull(JsonObject obj,
java.lang.String key)
JsonPrimitive child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.JsonPrimitive, or null if the child
does not exist, or is not a JsonPrimitive.public static int getInt(JsonObject obj,
java.lang.String key)
throws JsonParseException
int child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.JsonParseException - If the given child did not exist, was not a JsonPrimitive, or was not a Number.public static int getInt(JsonObject obj,
java.lang.String key,
int default_)
int child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.default_ - The default value to return, in the event that the child does not exist,
is not a JsonPrimitive, or is not a Number.default_.public static java.lang.String getString(JsonObject obj,
java.lang.String key)
throws JsonParseException
String child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.String.JsonParseException - If the given child did not exist, was not a JsonPrimitive, or was not a String.public static java.lang.String getString(JsonObject obj,
java.lang.String key,
java.lang.String default_)
String child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.default_ - The default value to return, in the event that the child does not exist,
is not a JsonPrimitive, or is not a Number.String. Otherwise, default_.