JSON类的JavaDoc
This is the main class for using Fastjson. You usually call these two methods 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);
ParameterizedType
ParameterizedType
是Type
的子接口,表示一个有参数的类型,例如Collection<T>
,Map<K,V>
等。但实现上 ParameterizedType
并不直接表示Collection<T>
和Map<K,V>
等,而是表示 Collection<String>
和Map<String,String>
等这种具体的类型。是不是看着眼熟,其实这就是我们常说的泛型。而ParameterizedType
代表的是一个泛型的实例,我们就称ParameterizedType
为“泛型实例”吧。
当创建泛型P
(如:Collection<String>
)时,将解析P
实例化的泛型类型声明(如:Collection<T>
),并且递归地创建P
的所有泛型参数(如:String
)。
实现这个接口的“类”必须实现一个equals()
方法,该方法将任何“泛型类型”(如:Collection<T>
)声明相同且“类型参数”(如:String
)也相同的两个“类”等同起来。
JSON属性
java"> public static TimeZone defaultTimeZone = TimeZone.getDefault();public static Locale defaultLocale = Locale.getDefault();public static String DEFAULT_TYPE_KEY = "@type";static final SerializeFilter[] emptyFilters = new SerializeFilter[0];public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";public static int DEFAULT_PARSER_FEATURE;public static int DEFAULT_GENERATE_FEATURE;private static final ConcurrentHashMap<Type, Type> mixInsMapper = new ConcurrentHashMap<Type, Type>(16);static {int features = 0;features |= Feature.AutoCloseSource.getMask();features |= Feature.InternFieldNames.getMask();features |= Feature.UseBigDecimal.getMask();features |= Feature.AllowUnQuotedFieldNames.getMask();features |= Feature.AllowSingleQuotes.getMask();features |= Feature.AllowArbitraryCommas.getMask();features |= Feature.SortFeidFastMatch.getMask();features |= Feature.IgnoreNotMatch.getMask();DEFAULT_PARSER_FEATURE = features;}static {int features = 0;features |= SerializerFeature.QuoteFieldNames.getMask();features |= SerializerFeature.SkipTransientField.getMask();features |= SerializerFeature.WriteEnumUsingName.getMask();features |= SerializerFeature.SortField.getMask();DEFAULT_GENERATE_FEATURE = features;config(IOUtils.DEFAULT_PROPERTIES);}
配置方法
private static void config(Properties properties)
public static void setDefaultTypeKey(String typeKey)
核心方法
parse()
public static Object parse(String text){}
public static Object parse(String text, int features){}
public static Object parse(String text, Feature... features){}
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, Feature... features){}
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) {}
parseObject()
public static JSONObject parseObject(String text)
public static <T> T parseObject(String text, Class<T> clazz)
public static JSONObject parseObject(String text, Feature... features)public static <T> T parseObject(String text, TypeReference<T> type, Feature... features) public static <T> T parseObject(String json, Class<T> clazz, Feature... features)public static <T> T parseObject(String text, Class<T> clazz, ParseProcess processor, Feature... features)public static <T> T parseObject(String json, Type type, Feature... features)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, Type clazz, 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)
public static <T> T parseObject(InputStream is, Charset charset, Type type, ParserConfig config, ParseProcess processor, int featureValues, Feature... features)
这parse()
和parseObject()
方法大同小异,最终都是通过实例化一个DefaultJSONParser
来进行解析处理,只是可能会对DefaultJSONParser
设置不同的属性参数,然后通过DefaultJSONParser.parse()
和DefaultJSONParser.parseObject()
方法来进行处理和操作。
DefaultJSONParser
构造函数,最终均使用该构造函数,仅在参数上有所区分
public DefaultJSONParser(final Object input, final JSONLexer lexer, final ParserConfig config){this.lexer = lexer;this.input = input;this.config = config;this.symbolTable = config.symbolTable;int ch = lexer.getCurrent();if (ch == '{') {lexer.next();((JSONLexerBase) lexer).token = JSONToken.LBRACE;} else if (ch == '[') {lexer.next();((JSONLexerBase) lexer).token = JSONToken.LBRACKET;} else {lexer.nextToken(); // prime the pump}}
除此外,parseObject()
方法还会设置其它参数
if (processor != null) {if (processor instanceof ExtraTypeProvider) {parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);}if (processor instanceof ExtraProcessor) {parser.getExtraProcessors().add((ExtraProcessor) processor);}if (processor instanceof FieldTypeResolver) {parser.setFieldTypeResolver((FieldTypeResolver) processor);}
}
DefaultJSONParser.parse()
1、找到对应的ObjectDeserializer
2、序列化操作
2.1、找出Object对应的field及fieldClass(属性类型)
public static JavaBeanInfo build(Class<?> clazz //, Type type //, PropertyNamingStrategy propertyNamingStrategy //, boolean fieldBased //, boolean compatibleWithJavaBean, boolean jacksonCompatible)public static Field getField(Class<?> clazz, String fieldName, Field[] declaredFields)