public abstract class JSON extends Object implements JSONStreamAware, JSONAware
toJSONString(Object)
and parseObject(String, Class)
.
Here is an example of how fastjson is used for a simple Class:
Model model = new Model(); String json = JSON.toJSONString(model); // serializes model to Json Model model2 = JSON.parseObject(json, Model.class); // deserializes json into model2
If the object that your are serializing/deserializing is a ParameterizedType
(i.e. contains at least one type parameter and may be an array) then you must use the
toJSONString(Object)
or parseObject(String, Type, Feature[])
method. Here is an
example for serializing and deserialing a ParameterizedType
:
String json = "[{},...]"; Type listType = new TypeReference<List<Model>>() {}.getType(); List<Model> modelList = JSON.parseObject(json, listType);
TypeReference
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_GENERATE_FEATURE |
static int |
DEFAULT_PARSER_FEATURE |
static String |
DEFAULT_TYPE_KEY |
static Locale |
defaultLocale |
static TimeZone |
defaultTimeZone |
static String |
DEFFAULT_DATE_FORMAT |
static String |
VERSION |
Constructor and Description |
---|
JSON() |
Modifier and Type | Method and Description |
---|---|
static void |
addMixInAnnotations(Type target,
Type mixinSource) |
static void |
clearMixInAnnotations() |
static Type |
getMixInAnnotations(Type target) |
static <T> void |
handleResovleTask(DefaultJSONParser parser,
T value) |
static boolean |
isValid(String str)
Deprecated.
Please use
JSONValidator instead. |
static boolean |
isValidArray(String str)
Deprecated.
Please use
JSONValidator instead. |
static boolean |
isValidObject(String str)
Deprecated.
Please use
JSONValidator instead. |
static Object |
parse(byte[] input,
Feature... features) |
static Object |
parse(byte[] input,
int off,
int len,
CharsetDecoder charsetDecoder,
Feature... features) |
static Object |
parse(byte[] input,
int off,
int len,
CharsetDecoder charsetDecoder,
int features) |
static Object |
parse(String text) |
static Object |
parse(String text,
Feature... features) |
static Object |
parse(String text,
int features) |
static Object |
parse(String text,
ParserConfig config) |
static Object |
parse(String text,
ParserConfig config,
Feature... features) |
static Object |
parse(String text,
ParserConfig config,
int features) |
static JSONArray |
parseArray(String text) |
static <T> List<T> |
parseArray(String text,
Class<T> clazz) |
static <T> List<T> |
parseArray(String text,
Class<T> clazz,
ParserConfig config) |
static JSONArray |
parseArray(String text,
ParserConfig parserConfig) |
static List<Object> |
parseArray(String text,
Type[] types) |
static List<Object> |
parseArray(String text,
Type[] types,
ParserConfig config) |
static <T> T |
parseObject(byte[] bytes,
Charset charset,
Type clazz,
ParserConfig config,
ParseProcess processor,
int featureValues,
Feature... features) |
static <T> T |
parseObject(byte[] input,
int off,
int len,
CharsetDecoder charsetDecoder,
Type clazz,
Feature... features) |
static <T> T |
parseObject(byte[] bytes,
int offset,
int len,
Charset charset,
Type clazz,
Feature... features) |
static <T> T |
parseObject(byte[] bytes,
int offset,
int len,
Charset charset,
Type clazz,
ParserConfig config,
ParseProcess processor,
int featureValues,
Feature... features) |
static <T> T |
parseObject(byte[] bytes,
Type clazz,
Feature... features) |
static <T> T |
parseObject(char[] input,
int length,
Type clazz,
Feature... features) |
static <T> T |
parseObject(InputStream is,
Charset charset,
Type type,
Feature... features) |
static <T> T |
parseObject(InputStream is,
Charset charset,
Type type,
ParserConfig config,
Feature... features) |
static <T> T |
parseObject(InputStream is,
Charset charset,
Type type,
ParserConfig config,
ParseProcess processor,
int featureValues,
Feature... features) |
static <T> T |
parseObject(InputStream is,
Type type,
Feature... features) |
static JSONObject |
parseObject(String text) |
static <T> T |
parseObject(String text,
Class<T> clazz) |
static <T> T |
parseObject(String json,
Class<T> clazz,
Feature... features)
This method deserializes the specified Json into an object of the specified class.
|
static <T> T |
parseObject(String text,
Class<T> clazz,
ParseProcess processor,
Feature... features) |
static JSONObject |
parseObject(String text,
Feature... features) |
static <T> T |
parseObject(String json,
Type type,
Feature... features)
This method deserializes the specified Json into an object of the specified type.
|
static <T> T |
parseObject(String input,
Type clazz,
int featureValues,
Feature... features) |
static <T> T |
parseObject(String input,
Type clazz,
ParseProcess processor,
Feature... features) |
static <T> T |
parseObject(String input,
Type clazz,
ParserConfig config,
Feature... features) |
static <T> T |
parseObject(String input,
Type clazz,
ParserConfig config,
int featureValues,
Feature... features) |
static <T> T |
parseObject(String input,
Type clazz,
ParserConfig config,
ParseProcess processor,
int featureValues,
Feature... features) |
static <T> T |
parseObject(String text,
TypeReference<T> type,
Feature... features)
String jsonStr = "[{\"id\":1001,\"name\":\"Jobs\"}]";
List<Model> models = JSON.parseObject(jsonStr, new TypeReference<List<Model>>() {});
|
static void |
removeMixInAnnotations(Type target) |
static void |
setDefaultTypeKey(String typeKey)
config default type key
|
<T> T |
toJavaObject(Class<T> clazz) |
static <T> T |
toJavaObject(JSON json,
Class<T> clazz) |
<T> T |
toJavaObject(Type type) |
<T> T |
toJavaObject(TypeReference typeReference) |
static Object |
toJSON(Object javaObject)
This method serializes the specified object into its equivalent representation as a tree of
JSONObject s. |
static Object |
toJSON(Object javaObject,
ParserConfig parserConfig)
Deprecated.
|
static Object |
toJSON(Object javaObject,
SerializeConfig config) |
static byte[] |
toJSONBytes(Charset charset,
Object object,
SerializeConfig config,
SerializeFilter[] filters,
String dateFormat,
int defaultFeatures,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
int defaultFeatures,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializeConfig config,
int defaultFeatures,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializeConfig config,
SerializeFilter[] filters,
int defaultFeatures,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializeConfig config,
SerializeFilter[] filters,
String dateFormat,
int defaultFeatures,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializeConfig config,
SerializeFilter filter,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializeConfig config,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializeFilter[] filters,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializeFilter filter,
SerializerFeature... features) |
static byte[] |
toJSONBytes(Object object,
SerializerFeature... features) |
static byte[] |
toJSONBytesWithFastJsonConfig(Charset charset,
Object object,
SerializeConfig config,
SerializeFilter[] filters,
String dateFormat,
int defaultFeatures,
SerializerFeature... features)
Use the date format in FastJsonConfig to serialize JSON
|
String |
toJSONString() |
static String |
toJSONString(Object object)
This method serializes the specified object into its equivalent Json representation.
|
static String |
toJSONString(Object object,
boolean prettyFormat) |
static String |
toJSONString(Object object,
int defaultFeatures,
SerializerFeature... features) |
static String |
toJSONString(Object object,
SerializeConfig config,
SerializeFilter[] filters,
SerializerFeature... features) |
static String |
toJSONString(Object object,
SerializeConfig config,
SerializeFilter[] filters,
String dateFormat,
int defaultFeatures,
SerializerFeature... features) |
static String |
toJSONString(Object object,
SerializeConfig config,
SerializeFilter filter,
SerializerFeature... features) |
static String |
toJSONString(Object object,
SerializeConfig config,
SerializerFeature... features) |
static String |
toJSONString(Object object,
SerializeFilter[] filters,
SerializerFeature... features) |
static String |
toJSONString(Object object,
SerializeFilter filter,
SerializerFeature... features) |
static String |
toJSONString(Object object,
SerializerFeature... features) |
static String |
toJSONStringWithDateFormat(Object object,
String dateFormat,
SerializerFeature... features) |
static String |
toJSONStringZ(Object object,
SerializeConfig mapping,
SerializerFeature... features)
Deprecated.
|
String |
toString() |
String |
toString(SerializerFeature... features) |
void |
writeJSONString(Appendable appendable)
write JSON string to out.
|
static int |
writeJSONString(OutputStream os,
Charset charset,
Object object,
SerializeConfig config,
SerializeFilter[] filters,
String dateFormat,
int defaultFeatures,
SerializerFeature... features) |
static int |
writeJSONString(OutputStream os,
Charset charset,
Object object,
SerializerFeature... features) |
static int |
writeJSONString(OutputStream os,
Object object,
int defaultFeatures,
SerializerFeature... features) |
static int |
writeJSONString(OutputStream os,
Object object,
SerializerFeature... features)
write object as json to OutputStream
|
static void |
writeJSONString(Writer writer,
Object object,
int defaultFeatures,
SerializerFeature... features) |
static void |
writeJSONString(Writer writer,
Object object,
SerializerFeature... features)
This method serializes the specified object into its equivalent json representation.
|
static void |
writeJSONStringTo(Object object,
Writer writer,
SerializerFeature... features)
Deprecated.
use writeJSONString
|
static int |
writeJSONStringWithFastJsonConfig(OutputStream os,
Charset charset,
Object object,
SerializeConfig config,
SerializeFilter[] filters,
String dateFormat,
int defaultFeatures,
SerializerFeature... features) |
public static TimeZone defaultTimeZone
public static Locale defaultLocale
public static String DEFAULT_TYPE_KEY
public static String DEFFAULT_DATE_FORMAT
public static int DEFAULT_PARSER_FEATURE
public static int DEFAULT_GENERATE_FEATURE
public static final String VERSION
public static void setDefaultTypeKey(String typeKey)
public static Object parse(String text, ParserConfig config)
public static Object parse(String text, ParserConfig config, Feature... features)
public static Object parse(String text, ParserConfig config, int features)
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Feature... features)
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features)
public static JSONObject parseObject(String text, Feature... features)
public static JSONObject parseObject(String text)
public static <T> T parseObject(String text, TypeReference<T> type, Feature... features)
String jsonStr = "[{\"id\":1001,\"name\":\"Jobs\"}]"; List<Model> models = JSON.parseObject(jsonStr, new TypeReference<List<Model>>() {});
text
- json stringtype
- type referncefeatures
- parser featurespublic static <T> T parseObject(String json, Class<T> clazz, Feature... features)
parseObject(String, Type, Feature[])
. If you have the Json in a InputStream
instead of
a String, use parseObject(InputStream, Type, Feature[])
instead.json
- the string from which the object is to be deserializedclazz
- the class of Tfeatures
- parser featurespublic static <T> T parseObject(String text, Class<T> clazz, ParseProcess processor, Feature... features)
public static <T> T parseObject(String json, Type type, Feature... features)
parseObject(String, Class, Feature[])
instead. If you have the Json in a InputStream
instead of
a String, use parseObject(InputStream, Type, Feature[])
instead.T
- the type of the desired objectjson
- the string from which the object is to be deserializedtype
- The specific genericized type of src. You can obtain this type by using the
TypeReference
class. For example, to get the type for
Collection<Foo>
, you should use:
Type type = new TypeReference<Collection<Foo>>(){}.getType();
public static <T> T parseObject(String input, Type clazz, ParseProcess processor, Feature... features)
public static <T> T parseObject(String input, Type clazz, int featureValues, Feature... features)
public static <T> T parseObject(String input, Type clazz, ParserConfig config, Feature... features)
public static <T> T parseObject(String input, Type clazz, ParserConfig config, int featureValues, Feature... features)
public static <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)
public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, Feature... features)
public static <T> T parseObject(byte[] bytes, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)
public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)
public static <T> T parseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features)
public static <T> T parseObject(char[] input, int length, Type clazz, Feature... features)
public static <T> T parseObject(InputStream is, Type type, Feature... features) throws IOException
IOException
public static <T> T parseObject(InputStream is, Charset charset, Type type, Feature... features) throws IOException
IOException
public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, Feature... features) throws IOException
IOException
public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) throws IOException
IOException
public static JSONArray parseArray(String text, ParserConfig parserConfig)
public static <T> List<T> parseArray(String text, Class<T> clazz, ParserConfig config)
public static List<Object> parseArray(String text, Type[] types, ParserConfig config)
public static String toJSONString(Object object)
Writer
, use writeJSONString(Writer, Object, SerializerFeature[])
instead.object
- the object for which json representation is to be created setting for fastjsonobject
.public static String toJSONString(Object object, SerializerFeature... features)
public static String toJSONString(Object object, int defaultFeatures, SerializerFeature... features)
public static String toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features)
public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features)
public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializeFilter filter, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, int defaultFeatures, SerializerFeature... features)
public static String toJSONString(Object object, SerializeConfig config, SerializerFeature... features)
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features)
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, SerializerFeature... features)
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
public static String toJSONStringZ(Object object, SerializeConfig mapping, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializeConfig config, int defaultFeatures, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializeFilter[] filters, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, int defaultFeatures, SerializerFeature... features)
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
public static byte[] toJSONBytes(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
public static byte[] toJSONBytesWithFastJsonConfig(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
dateFormat
- the date format in FastJsonConfigspublic static void writeJSONStringTo(Object object, Writer writer, SerializerFeature... features)
public static void writeJSONString(Writer writer, Object object, SerializerFeature... features)
writer
- Writer to which the json representation needs to be writtenobject
- the object for which json representation is to be created setting for fastjsonfeatures
- serializer featurespublic static void writeJSONString(Writer writer, Object object, int defaultFeatures, SerializerFeature... features)
public static final int writeJSONString(OutputStream os, Object object, SerializerFeature... features) throws IOException
os
- output streamobject
- features
- serializer featuresIOException
public static final int writeJSONString(OutputStream os, Object object, int defaultFeatures, SerializerFeature... features) throws IOException
IOException
public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializerFeature... features) throws IOException
IOException
public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) throws IOException
IOException
public static final int writeJSONStringWithFastJsonConfig(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) throws IOException
IOException
public String toJSONString()
toJSONString
in interface JSONAware
public String toString(SerializerFeature... features)
public void writeJSONString(Appendable appendable)
JSONStreamAware
writeJSONString
in interface JSONStreamAware
public static Object toJSON(Object javaObject)
JSONObject
s.public static Object toJSON(Object javaObject, ParserConfig parserConfig)
public static Object toJSON(Object javaObject, SerializeConfig config)
public <T> T toJavaObject(Class<T> clazz)
public <T> T toJavaObject(Type type)
public <T> T toJavaObject(TypeReference typeReference)
public static boolean isValid(String str)
JSONValidator
instead.public static boolean isValidObject(String str)
JSONValidator
instead.public static boolean isValidArray(String str)
JSONValidator
instead.public static <T> void handleResovleTask(DefaultJSONParser parser, T value)
public static void removeMixInAnnotations(Type target)
public static void clearMixInAnnotations()
Copyright © 2012–2022 Alibaba Group. All rights reserved.