Package com.alibaba.fastjson
Class JSON
- java.lang.Object
-
- com.alibaba.fastjson.JSON
-
- All Implemented Interfaces:
JSONAware,JSONStreamAware
- Direct Known Subclasses:
JSONArray,JSONObject
public abstract class JSON extends Object implements JSONStreamAware, JSONAware
This is the main class for using Fastjson. You usually call these two methodstoJSONString(Object)andparseObject(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 thetoJSONString(Object)orparseObject(String, Type, Feature[])method. Here is an example for serializing and deserialing aParameterizedType:String json = "[{},...]"; Type listType = new TypeReference<List<Model>>() {}.getType(); List<Model> modelList = JSON.parseObject(json, listType);- Author:
- wenshao[szujobs@hotmail.com]
- See Also:
TypeReference
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_GENERATE_FEATUREstatic intDEFAULT_PARSER_FEATUREstatic StringDEFAULT_TYPE_KEYstatic LocaledefaultLocalestatic TimeZonedefaultTimeZonestatic StringDEFFAULT_DATE_FORMATstatic StringVERSION
-
Constructor Summary
Constructors Constructor Description JSON()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidaddMixInAnnotations(Type target, Type mixinSource)static voidclearMixInAnnotations()static TypegetMixInAnnotations(Type target)static <T> voidhandleResovleTask(DefaultJSONParser parser, T value)static booleanisValid(String str)Deprecated.Please useJSONValidatorinstead.static booleanisValidArray(String str)Deprecated.Please useJSONValidatorinstead.static booleanisValidObject(String str)Deprecated.Please useJSONValidatorinstead.static Objectparse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features)static Objectparse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Feature... features)static Objectparse(byte[] input, Feature... features)static Objectparse(String text)static Objectparse(String text, int features)static Objectparse(String text, Feature... features)static Objectparse(String text, ParserConfig config)static Objectparse(String text, ParserConfig config, int features)static Objectparse(String text, ParserConfig config, Feature... features)static JSONArrayparseArray(String text)static JSONArrayparseArray(String text, ParserConfig parserConfig)static <T> List<T>parseArray(String text, Class<T> clazz)static <T> List<T>parseArray(String text, Class<T> clazz, ParserConfig config)static List<Object>parseArray(String text, Type[] types)static List<Object>parseArray(String text, Type[] types, ParserConfig config)static <T> TparseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features)static <T> TparseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, Feature... features)static <T> TparseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)static <T> TparseObject(byte[] bytes, Type clazz, Feature... features)static <T> TparseObject(byte[] bytes, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)static <T> TparseObject(char[] input, int length, Type clazz, Feature... features)static <T> TparseObject(InputStream is, Type type, Feature... features)static <T> TparseObject(InputStream is, Charset charset, Type type, Feature... features)static <T> TparseObject(InputStream is, Charset charset, Type type, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)static <T> TparseObject(InputStream is, Charset charset, Type type, ParserConfig config, Feature... features)static JSONObjectparseObject(String text)static JSONObjectparseObject(String text, Feature... features)static <T> TparseObject(String text, TypeReference<T> type, Feature... features)String jsonStr = "[{\"id\":1001,\"name\":\"Jobs\"}]"; List<Model> models = JSON.parseObject(jsonStr, new TypeReference<List<Model>>() {});static <T> TparseObject(String text, Class<T> clazz)static <T> TparseObject(String text, Class<T> clazz, ParseProcess processor, Feature... features)static <T> TparseObject(String json, Class<T> clazz, Feature... features)This method deserializes the specified Json into an object of the specified class.static <T> TparseObject(String input, Type clazz, int featureValues, Feature... features)static <T> TparseObject(String input, Type clazz, ParseProcess processor, Feature... features)static <T> TparseObject(String json, Type type, Feature... features)This method deserializes the specified Json into an object of the specified type.static <T> TparseObject(String input, Type clazz, ParserConfig config, int featureValues, Feature... features)static <T> TparseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)static <T> TparseObject(String input, Type clazz, ParserConfig config, Feature... features)static voidremoveMixInAnnotations(Type target)static voidsetDefaultTypeKey(String typeKey)config default type keystatic <T> TtoJavaObject(JSON json, Class<T> clazz)<T> TtoJavaObject(TypeReference typeReference)<T> TtoJavaObject(Class<T> clazz)<T> TtoJavaObject(Type type)static ObjecttoJSON(Object javaObject)This method serializes the specified object into its equivalent representation as a tree ofJSONObjects.static ObjecttoJSON(Object javaObject, ParserConfig parserConfig)Deprecated.static ObjecttoJSON(Object javaObject, SerializeConfig config)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[]toJSONBytes(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, 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 JSONStringtoJSONString()static StringtoJSONString(Object object)This method serializes the specified object into its equivalent Json representation.static StringtoJSONString(Object object, boolean prettyFormat)static StringtoJSONString(Object object, int defaultFeatures, SerializerFeature... features)static StringtoJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, SerializerFeature... features)static StringtoJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)static StringtoJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features)static StringtoJSONString(Object object, SerializeConfig config, SerializerFeature... features)static StringtoJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features)static StringtoJSONString(Object object, SerializeFilter filter, SerializerFeature... features)static StringtoJSONString(Object object, SerializerFeature... features)static StringtoJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features)static StringtoJSONStringZ(Object object, SerializeConfig mapping, SerializerFeature... features)Deprecated.StringtoString()StringtoString(SerializerFeature... features)static intwriteJSONString(OutputStream os, Object object, int defaultFeatures, SerializerFeature... features)static intwriteJSONString(OutputStream os, Object object, SerializerFeature... features)write object as json to OutputStreamstatic intwriteJSONString(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)static intwriteJSONString(OutputStream os, Charset charset, Object object, SerializerFeature... features)static voidwriteJSONString(Writer writer, Object object, int defaultFeatures, SerializerFeature... features)static voidwriteJSONString(Writer writer, Object object, SerializerFeature... features)This method serializes the specified object into its equivalent json representation.voidwriteJSONString(Appendable appendable)write JSON string to out.static voidwriteJSONStringTo(Object object, Writer writer, SerializerFeature... features)Deprecated.use writeJSONStringstatic intwriteJSONStringWithFastJsonConfig(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
-
-
-
Field Detail
-
defaultTimeZone
public static TimeZone defaultTimeZone
-
defaultLocale
public static Locale defaultLocale
-
DEFAULT_TYPE_KEY
public static String DEFAULT_TYPE_KEY
-
DEFFAULT_DATE_FORMAT
public static String DEFFAULT_DATE_FORMAT
-
DEFAULT_PARSER_FEATURE
public static int DEFAULT_PARSER_FEATURE
-
DEFAULT_GENERATE_FEATURE
public static int DEFAULT_GENERATE_FEATURE
-
VERSION
public static final String VERSION
- See Also:
- Constant Field Values
-
-
Method Detail
-
setDefaultTypeKey
public static void setDefaultTypeKey(String typeKey)
config default type key- Since:
- 1.2.14
-
parse
public static Object parse(String text, ParserConfig config)
- Since:
- 1.2.38
-
parse
public static Object parse(String text, ParserConfig config, Feature... features)
- Since:
- 1.2.68
-
parse
public static Object parse(String text, ParserConfig config, int features)
- Since:
- 1.2.38
-
parse
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Feature... features)
-
parse
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features)
-
parseObject
public static JSONObject parseObject(String text, Feature... features)
-
parseObject
public static JSONObject parseObject(String text)
-
parseObject
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>>() {});- Parameters:
text- json stringtype- type referncefeatures- parser features- Returns:
- an object of type T from the string
-
parseObject
public static <T> T parseObject(String json, Class<T> clazz, Feature... features)
This method deserializes the specified Json into an object of the specified class. It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java. Therefore, this method should not be used if the desired type is a generic type. Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type. For the cases when the object is of generic type, invokeparseObject(String, Type, Feature[]). If you have the Json in aInputStreaminstead of a String, useparseObject(InputStream, Type, Feature[])instead.- Parameters:
json- the string from which the object is to be deserializedclazz- the class of Tfeatures- parser features- Returns:
- an object of type T from the string classOfT
-
parseObject
public static <T> T parseObject(String text, Class<T> clazz, ParseProcess processor, Feature... features)
-
parseObject
public static <T> T parseObject(String json, Type type, Feature... features)
This method deserializes the specified Json into an object of the specified type. This method is useful if the specified object is a generic type. For non-generic objects, useparseObject(String, Class, Feature[])instead. If you have the Json in aInputStreaminstead of a String, useparseObject(InputStream, Type, Feature[])instead.- Type Parameters:
T- the type of the desired object- Parameters:
json- the string from which the object is to be deserializedtype- The specific genericized type of src. You can obtain this type by using theTypeReferenceclass. For example, to get the type forCollection<Foo>, you should use:Type type = new TypeReference<Collection<Foo>>(){}.getType();- Returns:
- an object of type T from the string
-
parseObject
public static <T> T parseObject(String input, Type clazz, ParseProcess processor, Feature... features)
-
parseObject
public static <T> T parseObject(String input, Type clazz, int featureValues, Feature... features)
-
parseObject
public static <T> T parseObject(String input, Type clazz, ParserConfig config, Feature... features)
- Since:
- 1.2.11
-
parseObject
public static <T> T parseObject(String input, Type clazz, ParserConfig config, int featureValues, Feature... features)
-
parseObject
public static <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)
-
parseObject
public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, Feature... features)- Since:
- 1.2.11
-
parseObject
public static <T> T parseObject(byte[] bytes, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)- Since:
- 1.2.55
-
parseObject
public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)- Since:
- 1.2.55
-
parseObject
public static <T> T parseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features)
-
parseObject
public static <T> T parseObject(char[] input, int length, Type clazz, Feature... features)
-
parseObject
public static <T> T parseObject(InputStream is, Type type, Feature... features) throws IOException
- Throws:
IOException- Since:
- 1.2.11
-
parseObject
public static <T> T parseObject(InputStream is, Charset charset, Type type, Feature... features) throws IOException
- Throws:
IOException- Since:
- 1.2.11
-
parseObject
public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, Feature... features) throws IOException
- Throws:
IOException- Since:
- 1.2.55
-
parseObject
public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) throws IOException
- Throws:
IOException- Since:
- 1.2.55
-
parseArray
public static JSONArray parseArray(String text, ParserConfig parserConfig)
-
parseArray
public static <T> List<T> parseArray(String text, Class<T> clazz, ParserConfig config)
-
parseArray
public static List<Object> parseArray(String text, Type[] types, ParserConfig config)
-
toJSONString
public static String toJSONString(Object object)
This method serializes the specified object into its equivalent Json representation. 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 you want to write out the object to aWriter, usewriteJSONString(Writer, Object, SerializerFeature[])instead.- Parameters:
object- the object for which json representation is to be created setting for fastjson- Returns:
- Json representation of
object.
-
toJSONString
public static String toJSONString(Object object, SerializerFeature... features)
-
toJSONString
public static String toJSONString(Object object, int defaultFeatures, SerializerFeature... features)
- Since:
- 1.2.11
-
toJSONStringWithDateFormat
public static String toJSONStringWithDateFormat(Object object, String dateFormat, SerializerFeature... features)
- Since:
- 1.1.14
-
toJSONString
public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features)
-
toJSONString
public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features)
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializerFeature... features)
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializeFilter filter, SerializerFeature... features)
-
toJSONBytes
public static byte[] toJSONBytes(Object object, int defaultFeatures, SerializerFeature... features)
- Since:
- 1.2.11
-
toJSONString
public static String toJSONString(Object object, SerializeConfig config, SerializerFeature... features)
-
toJSONString
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features)
-
toJSONString
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, SerializerFeature... features)
-
toJSONString
public static String toJSONString(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
- Returns:
- Since:
- 1.2.9
-
toJSONStringZ
public static String toJSONStringZ(Object object, SerializeConfig mapping, SerializerFeature... features)
Deprecated.
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializerFeature... features)
- Since:
- 1.2.42
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializeConfig config, int defaultFeatures, SerializerFeature... features)
- Since:
- 1.2.11
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializeFilter[] filters, SerializerFeature... features)
- Since:
- 1.2.42
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features)
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, int defaultFeatures, SerializerFeature... features)
- Since:
- 1.2.42
-
toJSONBytes
public static byte[] toJSONBytes(Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
- Since:
- 1.2.55
-
toJSONBytes
public static byte[] toJSONBytes(Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features)
- Since:
- 1.2.55
-
toJSONBytesWithFastJsonConfig
public 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- Parameters:
dateFormat- the date format in FastJsonConfigs- Since:
- 1.2.55
-
writeJSONStringTo
public static void writeJSONStringTo(Object object, Writer writer, SerializerFeature... features)
Deprecated.use writeJSONString
-
writeJSONString
public static void writeJSONString(Writer writer, Object object, SerializerFeature... features)
This method serializes the specified object into its equivalent json representation.- Parameters:
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 features- Since:
- 1.2.11
-
writeJSONString
public static void writeJSONString(Writer writer, Object object, int defaultFeatures, SerializerFeature... features)
- Since:
- 1.2.11
-
writeJSONString
public static final int writeJSONString(OutputStream os, Object object, SerializerFeature... features) throws IOException
write object as json to OutputStream- Parameters:
os- output streamobject-features- serializer features- Throws:
IOException- Since:
- 1.2.11
-
writeJSONString
public static final int writeJSONString(OutputStream os, Object object, int defaultFeatures, SerializerFeature... features) throws IOException
- Throws:
IOException- Since:
- 1.2.11
-
writeJSONString
public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializerFeature... features) throws IOException
- Throws:
IOException
-
writeJSONString
public static final int writeJSONString(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) throws IOException
- Throws:
IOException
-
writeJSONStringWithFastJsonConfig
public static final int writeJSONStringWithFastJsonConfig(OutputStream os, Charset charset, Object object, SerializeConfig config, SerializeFilter[] filters, String dateFormat, int defaultFeatures, SerializerFeature... features) throws IOException
- Throws:
IOException
-
toJSONString
public String toJSONString()
- Specified by:
toJSONStringin interfaceJSONAware- Returns:
- JSON text
-
toString
public String toString(SerializerFeature... features)
- Since:
- 1.2.57
-
writeJSONString
public void writeJSONString(Appendable appendable)
Description copied from interface:JSONStreamAwarewrite JSON string to out.- Specified by:
writeJSONStringin interfaceJSONStreamAware
-
toJSON
public static Object toJSON(Object javaObject)
This method serializes the specified object into its equivalent representation as a tree ofJSONObjects.
-
toJSON
public static Object toJSON(Object javaObject, ParserConfig parserConfig)
Deprecated.
-
toJSON
public static Object toJSON(Object javaObject, SerializeConfig config)
-
toJavaObject
public <T> T toJavaObject(Class<T> clazz)
- Since:
- 1.2.9
-
toJavaObject
public <T> T toJavaObject(Type type)
- Since:
- 1.2.33
-
toJavaObject
public <T> T toJavaObject(TypeReference typeReference)
- Since:
- 1.2.33
-
isValid
public static boolean isValid(String str)
Deprecated.Please useJSONValidatorinstead.
-
isValidObject
public static boolean isValidObject(String str)
Deprecated.Please useJSONValidatorinstead.
-
isValidArray
public static boolean isValidArray(String str)
Deprecated.Please useJSONValidatorinstead.
-
handleResovleTask
public static <T> void handleResovleTask(DefaultJSONParser parser, T value)
-
removeMixInAnnotations
public static void removeMixInAnnotations(Type target)
-
clearMixInAnnotations
public static void clearMixInAnnotations()
-
-