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 com.google.gson.JsonPrimitive |
getAsPrimitive(com.google.gson.JsonObject obj,
java.lang.String key)
Try and get a
JsonPrimitive child from the given JsonObject. |
static @Nullable com.google.gson.JsonPrimitive |
getAsPrimitiveOrNull(com.google.gson.JsonObject obj,
java.lang.String key)
Try and get a
JsonPrimitive child from the given JsonObject. |
static int |
getInt(com.google.gson.JsonObject obj,
java.lang.String key)
Try and get an
int child from the given JsonObject. |
static int |
getInt(com.google.gson.JsonObject obj,
java.lang.String key,
int default_)
Try and get an
int child from the given JsonObject. |
static java.lang.String |
getString(com.google.gson.JsonObject obj,
java.lang.String key)
Try and get a
String child from the given JsonObject. |
static @Nullable java.lang.String |
getString(com.google.gson.JsonObject obj,
java.lang.String key,
@Nullable java.lang.String default_)
Try and get an
String child from the given JsonObject. |
static <T> T |
parse(com.google.gson.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(com.google.gson.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(com.google.gson.Gson gson,
java.io.Reader reader,
java.lang.reflect.Type t)
Deserialize Json from the given
Reader as the given Type. |
static <T> T |
parse(com.google.gson.Gson gson,
java.lang.String str,
java.lang.reflect.Type t)
Deserialize Json from the given
String as the given Type. |
static com.google.gson.JsonElement |
parseRaw(java.io.InputStream is)
Deserialize Json from the given
InputStream as a raw JsonElement. |
static com.google.gson.JsonElement |
parseRaw(java.nio.file.Path path)
Deserialize Json from the given
Path as a raw JsonElement. |
static com.google.gson.JsonElement |
parseRaw(java.io.Reader reader)
Deserialize Json from the given
Reader as a raw JsonElement. |
static com.google.gson.JsonElement |
parseRaw(java.lang.String str)
Deserialize Json from the given
String as a raw JsonElement. |
static void |
write(com.google.gson.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(com.google.gson.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(com.google.gson.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(com.google.gson.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(com.google.gson.Gson gson,
java.nio.file.Path path,
java.lang.reflect.Type t)
throws java.io.IOException,
com.google.gson.JsonParseException
Path as the given Type.gson - The Gson instance to use.path - The Path to read from.t - The Type to deserialize from.com.google.gson.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(com.google.gson.Gson gson,
java.lang.String str,
java.lang.reflect.Type t)
throws java.io.IOException,
com.google.gson.JsonParseException
String as the given Type.gson - The Gson instance to use.str - The String representing the json to parse.t - The Type to deserialize from.com.google.gson.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(com.google.gson.Gson gson,
java.io.InputStream is,
java.lang.reflect.Type t)
throws java.io.IOException,
com.google.gson.JsonParseException
InputStream as the given Type.gson - The Gson instance to use.is - The InputStream to read from.t - The Type to deserialize from.com.google.gson.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(com.google.gson.Gson gson,
java.io.Reader reader,
java.lang.reflect.Type t)
throws java.io.IOException,
com.google.gson.JsonParseException
Reader as the given Type.gson - The Gson instance to use.reader - The Reader to read from.t - The Type to deserialize from.com.google.gson.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 com.google.gson.JsonElement parseRaw(java.nio.file.Path path)
throws java.io.IOException,
com.google.gson.JsonParseException
Path as a raw JsonElement.path - The Path to read from.JsonElement.com.google.gson.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 com.google.gson.JsonElement parseRaw(java.lang.String str)
throws java.io.IOException,
com.google.gson.JsonParseException
String as a raw JsonElement.str - The String representing the json to parse.JsonElement.com.google.gson.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 com.google.gson.JsonElement parseRaw(java.io.InputStream is)
throws java.io.IOException,
com.google.gson.JsonParseException
InputStream as a raw JsonElement.is - The InputStream to read from.JsonElement.com.google.gson.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 com.google.gson.JsonElement parseRaw(java.io.Reader reader)
throws java.io.IOException,
com.google.gson.JsonParseException
Reader as a raw JsonElement.reader - The Reader to read from.JsonElement.com.google.gson.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(com.google.gson.Gson gson,
java.nio.file.Path path,
java.lang.Object instance)
throws java.io.IOException,
com.google.gson.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.com.google.gson.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(com.google.gson.Gson gson,
java.nio.file.Path path,
java.lang.Object instance,
java.lang.reflect.Type t)
throws java.io.IOException,
com.google.gson.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.com.google.gson.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(com.google.gson.Gson gson,
java.io.OutputStream os,
java.lang.Object instance)
throws java.io.IOException,
com.google.gson.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.com.google.gson.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(com.google.gson.Gson gson,
java.io.OutputStream os,
java.lang.Object instance,
java.lang.reflect.Type t)
throws java.io.IOException,
com.google.gson.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.com.google.gson.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 com.google.gson.JsonPrimitive getAsPrimitive(com.google.gson.JsonObject obj,
java.lang.String key)
throws com.google.gson.JsonParseException
JsonPrimitive child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.JsonPrimitive.com.google.gson.JsonParseException - If the given child did not exist, or was not a JsonPrimitive.@Nullable
public static @Nullable com.google.gson.JsonPrimitive getAsPrimitiveOrNull(com.google.gson.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(com.google.gson.JsonObject obj,
java.lang.String key)
throws com.google.gson.JsonParseException
int child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.com.google.gson.JsonParseException - If the given child did not exist, was not a JsonPrimitive, or was not a Number.public static int getInt(com.google.gson.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(com.google.gson.JsonObject obj,
java.lang.String key)
throws com.google.gson.JsonParseException
String child from the given JsonObject.obj - The JsonObject to get the child from.key - The name of the child.String.com.google.gson.JsonParseException - If the given child did not exist, was not a JsonPrimitive, or was not a String.@Nullable
@Contract(value="_,_,!null->!null")
public static @Nullable java.lang.String getString(com.google.gson.JsonObject obj,
java.lang.String key,
@Nullable
@Nullable 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_.