public final class Json
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
fromJson(java.io.Reader json,
java.lang.Class<T> classOfT)
This method deserializes the Json read from the specified reader into an object of the
specified class.
|
static <T> T |
fromJson(java.io.Reader json,
java.lang.reflect.Type typeOfT)
This method deserializes the Json read from the specified reader into an object of the
specified type.
|
static <T> T |
fromJson(java.lang.String json,
java.lang.Class<T> classOfT)
This method deserializes the specified Json into an object of the specified class.
|
static <T> T |
fromJson(java.lang.String json,
java.lang.reflect.Type typeOfT)
This method deserializes the specified Json into an object of the specified type.
|
static java.lang.reflect.Type |
genericTypeOf(java.lang.Class<?> rawType,
java.lang.reflect.Type... typeArguments)
Returns a new parameterized type, applying
typeArguments to rawType. |
static java.lang.String |
toJson(java.lang.Object src)
This method serializes the specified object into its equivalent Json representation.
|
static void |
toJson(java.lang.Object src,
java.lang.Appendable writer)
This method serializes the specified object into its equivalent Json representation.
|
static java.lang.String |
toJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc)
This method serializes the specified object, including those of generic types, into its
equivalent Json representation.
|
static void |
toJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc,
java.lang.Appendable writer)
This method serializes the specified object, including those of generic types, into its
equivalent Json representation.
|
static java.lang.String |
toPrettyJson(java.lang.Object src)
This method serializes the specified object into its equivalent Json representation.
|
static void |
toPrettyJson(java.lang.Object src,
java.lang.Appendable writer)
This method serializes the specified object into its equivalent Json representation.
|
static java.lang.String |
toPrettyJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc)
This method serializes the specified object, including those of generic types, into its
equivalent Json representation.
|
static void |
toPrettyJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc,
java.lang.Appendable writer)
This method serializes the specified object, including those of generic types, into its
equivalent Json representation.
|
public static java.lang.String toJson(java.lang.Object src)
Object.getClass() to get the type for the specified object, but the
getClass() loses the generic type information because of the Type Erasure feature
of Java. Note that this method works fine if the any of the object fields are of generic type,
just the object itself should not be of a generic type. If the object is of generic type, use
toJson(Object, Type) instead. If you want to write out the object to a
Writer, use toJson(Object, Appendable) instead.src - the object for which Json representation is to be createdsrc.public static java.lang.String toJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc)
toJson(Object) instead. If you want to write out
the object to a Appendable, use toJson(Object, Type, Appendable) instead.src - the object for which JSON representation is to be createdtypeOfSrc - The specific genericized type of src. You can obtain
this type by using the genericTypeOf(Class, Type...) method. For example,
to get the type for Collection<Foo>, you should use:
Type typeOfSrc = genericTypeOf(Collection.class, Foo.class);
srcpublic static void toJson(java.lang.Object src,
java.lang.Appendable writer)
throws java.io.IOException
Object.getClass() to get the type for the specified object, but the
getClass() loses the generic type information because of the Type Erasure feature
of Java. Note that this method works fine if the any of the object fields are of generic type,
just the object itself should not be of a generic type. If the object is of generic type, use
toJson(Object, Type, Appendable) instead.src - the object for which Json representation is to be createdwriter - Writer to which the Json representation needs to be writtenjava.io.IOException - if there was a problem writing to the writerpublic static void toJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc,
java.lang.Appendable writer)
throws java.io.IOException
toJson(Object, Appendable) instead.src - the object for which JSON representation is to be createdtypeOfSrc - The specific genericized type of src. You can obtain
this type by using the genericTypeOf(Class, Type...) method. For example,
to get the type for Collection<Foo>, you should use:
Type typeOfSrc = genericTypeOf(Collection.class, Foo.class);
writer - Writer to which the Json representation of src needs to be written.java.io.IOException - if there was a problem writing to the writerpublic static java.lang.String toPrettyJson(java.lang.Object src)
Object.getClass() to get the type for the specified object, but the
getClass() loses the generic type information because of the Type Erasure feature
of Java. Note that this method works fine if the any of the object fields are of generic type,
just the object itself should not be of a generic type. If the object is of generic type, use
toJson(Object, Type) instead. If you want to write out the object to a
Writer, use toPrettyJson(Object, Appendable) instead.src - the object for which Json representation is to be createdsrc.public static java.lang.String toPrettyJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc)
toPrettyJson(Object) instead. If you want to write out
the object to a Appendable, use toPrettyJson(Object, Type, Appendable) instead.src - the object for which JSON representation is to be createdtypeOfSrc - The specific genericized type of src. You can obtain
this type by using the genericTypeOf(Class, Type...) method. For example,
to get the type for Collection<Foo>, you should use:
Type typeOfSrc = genericTypeOf(Collection.class, Foo.class);
srcpublic static void toPrettyJson(java.lang.Object src,
java.lang.Appendable writer)
throws java.io.IOException
Object.getClass() to get the type for the specified object, but the
getClass() loses the generic type information because of the Type Erasure feature
of Java. Note that this method works fine if the any of the object fields are of generic type,
just the object itself should not be of a generic type. If the object is of generic type, use
toPrettyJson(Object, Type, Appendable) instead.src - the object for which Json representation is to be createdwriter - Writer to which the Json representation needs to be writtenjava.io.IOException - if there was a problem writing to the writerpublic static void toPrettyJson(java.lang.Object src,
java.lang.reflect.Type typeOfSrc,
java.lang.Appendable writer)
throws java.io.IOException
toPrettyJson(Object, Appendable) instead.src - the object for which JSON representation is to be createdtypeOfSrc - The specific genericized type of src. You can obtain
this type by using the genericTypeOf(Class, Type...) method. For example,
to get the type for Collection<Foo>, you should use:
Type typeOfSrc = genericTypeOf(Collection.class, Foo.class);
writer - Writer to which the Json representation of src needs to be written.java.io.IOException - if there was a problem writing to the writerpublic static <T> T fromJson(java.lang.String json,
java.lang.Class<T> classOfT)
throws JsonSyntaxException
fromJson(String, Type). If you have the Json in a Reader instead of
a String, use fromJson(Reader, Class) instead.T - the type of the desired objectjson - the string from which the object is to be deserializedclassOfT - the class of TJsonSyntaxException - if json is not a valid representation for an object of type
classOfTpublic static <T> T fromJson(java.lang.String json,
java.lang.reflect.Type typeOfT)
throws JsonSyntaxException
fromJson(String, Class) instead. If you have the Json in a Reader instead of
a String, use fromJson(Reader, Type) instead.T - the type of the desired objectjson - the string from which the object is to be deserializedtypeOfT - The specific genericized type of src. You can obtain this type by using the
genericTypeOf(Class, Type...) method. For example, to get the type for
Collection<Foo>, you should use:
Type typeOfT = genericTypeOf(Collection.class, Foo.class);
JsonSyntaxException - if json is not a valid representation for an object of typepublic static <T> T fromJson(java.io.Reader json,
java.lang.Class<T> classOfT)
throws java.io.IOException,
JsonSyntaxException
fromJson(Reader, Type). If you have the Json in a String form instead of a
Reader, use fromJson(String, Class) instead.T - the type of the desired objectjson - the reader producing the Json from which the object is to be deserialized.classOfT - the class of Tjava.io.IOException - if there was a problem reading from the ReaderJsonSyntaxException - if json is not a valid representation for an object of typepublic static <T> T fromJson(java.io.Reader json,
java.lang.reflect.Type typeOfT)
throws java.io.IOException,
JsonSyntaxException
fromJson(Reader, Class) instead. If you have the Json in a
String form instead of a Reader, use fromJson(String, Type) instead.T - the type of the desired objectjson - the reader producing Json from which the object is to be deserializedtypeOfT - The specific genericized type of src. You can obtain this type by using the
genericTypeOf(Class, Type...) method. For example, to get the type for
Collection<Foo>, you should use:
Type typeOfT = genericTypeOf(Collection.class, Foo.class);
java.io.IOException - if there was a problem reading from the ReaderJsonSyntaxException - if json is not a valid representation for an object of typepublic static java.lang.reflect.Type genericTypeOf(java.lang.Class<?> rawType,
java.lang.reflect.Type... typeArguments)
typeArguments to rawType.rawType - the raw typetypeArguments - type arguments