1 Fastjson2 简介
Fastjson2
是 Fastjson
的升级版,特色:
- 协定反对:反对
JSON
/JSONB
两种协定 - 局部解析:能够应用
JSONPath
进行局部解析获取须要的值 - 语言反对:
Java
/Kotlin
- 场景反对:
Android8+
/ 服务端 - 其余个性反对:
Graal Native-Image
、JSON Schema
2 根底应用
2.1 测试环境
环境:
JDK
版本:1.8.0_341
Fastjson2
版本:2.0.19
测试类:
@Builder
@Data
@ToString
public class Entity {
private String field1;
private Integer field2;
}
2.2 JSON
序列化
System.out.println(JSON.toJSONString(entity));
2.3 JSON
反序列化
System.out.println(JSON.parseObject(str,Entity.class));
2.4 JSONB
序列化
byte[] bytes = JSONB.toBytes(entity);
2.5 JSONB
反序列化
System.out.println(JSONB.parseObject(bytes,Entity.class));
2.6 JSON Schema
JSON Schema
可用于反序列化时对 JSON
字段进行验证应用,配置 Schema
能够通过 @JSONField
/@JSONType
,这里以@JSONField
为例:
public class Entity {
private String field1;
@JSONField(schema = "{'minimum':0,'maximum':100}")
private Integer field2;
}
测试代码:
Entity entity1 = Entity.builder().field2(-1).build();
Entity entity2 = Entity.builder().field2(50).build();
Entity entity3 = Entity.builder().field2(101).build();
String str1 = JSON.toJSONString(entity1);
String str2 = JSON.toJSONString(entity2);
String str3 = JSON.toJSONString(entity3);
try {JSON.parseObject(str1, Entity.class);
} catch (Exception e) {e.printStackTrace();
}
JSON.parseObject(str2, Entity.class);
try {JSON.parseObject(str3, Entity.class);
} catch (Exception e) {e.printStackTrace();
}
输入:
2.7 JSONPath
JSONPath
可用于局部解析 JSON
字符串,示例:
Entity entity = Entity.builder().field1("a").field2(2).build();
// $ 符号示意根对象
// $.field1 示意根对象的 field1 属性
System.out.println(JSONPath.eval(entity,"$.field1"));
System.out.println(JSONPath.eval(entity,"$.field2"));
List<Entity> list = new ArrayList<>();
list.add(Entity.builder().field1("entity1").field2(1).build());
list.add(Entity.builder().field1("entity2").field2(2).build());
// 如果传的是 List,反对通过下标解析
// 此处是返回 [0,0] 下标范畴内的值
List<Entity> names = (List<Entity>)JSONPath.eval(list,"[0,0]");
System.out.println(names.get(0));
具体的解析语法以及更多例子请参考官网文档。
2.8 AutoType
AutoType
是在序列化的时候带上类型的一种机制,这样在反序列化时就不须要传入类型,实现类型自动识别,例子:
Entity entity = Entity.builder().field1("a").field2(2).build();
String str = JSON.toJSONString(entity, JSONWriter.Feature.WriteClassName);
System.out.println(str);
System.out.println(JSON.parseObject(str, Object.class, JSONReader.Feature.SupportAutoType));
输入:
因为在 Fastjson1
中呈现过 AutoType
破绽,因而官网提供了一个 JVM
参数齐全禁止(safeMode
性能):
-Dfastjson2.parser.safeMode=true
3 底层实现探索
3.1 序列化
3.1.1 概览
序列化的实现能够参考官网的一张类图:
大略流程如下:
- 获取
ObjectWriter
- 如果从
ObjectWriterProvider
缓存有ObjectWriter
,间接提取 - 如果
ObjectWriterProvider
缓存没有ObjectWriter
,结构对应的ObjectWriter
,并缓存 - 获取到
ObjectWriter
后,将JavaBean
对象写入JSONWriter
JSONWriter
对根底类型进行写入- 返回后果
3.1.2 入口
这里的序列化探索以 JSON.toJSONString(Object object)
作为入口:
static String toJSONString(Object object) {
// 初始化 ObjectWriterProvider
JSONWriter.Context writeContext = new JSONWriter.Context(JSONFactory.defaultObjectWriterProvider);
// 格式化管制
boolean pretty = (writeContext.features & com.alibaba.fastjson2.JSONWriter.Feature.PrettyFormat.mask) != 0L;
Object jsonWriter;
// 默认有三个 JSONWriter,JDK8 一个,针对 JDK9 之后的 byte[]实现的字符串优化也有一个,还有一个基于 char[]实现的 UTF16
if (JDKUtils.JVM_VERSION == 8) {jsonWriter = new JSONWriterUTF16JDK8(writeContext);
} else if ((writeContext.features & com.alibaba.fastjson2.JSONWriter.Feature.OptimizedForAscii.mask) != 0L) {jsonWriter = new JSONWriterUTF8JDK9(writeContext);
} else {jsonWriter = new JSONWriterUTF16(writeContext);
}
try {
// 格式化管制
JSONWriter writer = pretty ? new JSONWriterPretty((JSONWriter)jsonWriter) : jsonWriter;
String var12;
try {if (object == null) {
// null 的话间接写入 "null" 字符串
((JSONWriter)writer).writeNull();} else {
// 设置根对象
((JSONWriter)writer).setRootObject(object);
Class<?> valueClass = object.getClass();
if (valueClass == JSONObject.class) {
// 如果指标类是 JSNOObject,间接调用 writer 的 write 办法
((JSONWriter)writer).write((JSONObject)object);
} else {
// 如果不是
JSONWriter.Context context = ((JSONWriter)writer).context;
boolean fieldBased = (context.features & com.alibaba.fastjson2.JSONWriter.Feature.FieldBased.mask) != 0L;
// 通过 Provider 获取 ObjectWriter
ObjectWriter<?> objectWriter = context.provider.getObjectWriter(valueClass, valueClass, fieldBased);
// ObjectWriter 将数据写入 JSONWriter
objectWriter.write((JSONWriter)writer, object, (Object)null, (Type)null, 0L);
}
}
// 后果
var12 = writer.toString();
// 上面的代码与序列化关系不大,能够不看
} catch (Throwable var10) {if (writer != null) {
try {((JSONWriter)writer).close();} catch (Throwable var9) {var10.addSuppressed(var9);
}
}
throw var10;
}
if (writer != null) {((JSONWriter)writer).close();}
return var12;
} catch (NumberFormatException | NullPointerException var11) {throw new JSONException("JSON#toJSONString cannot serialize'" + object + "'", var11);
}
}
3.1.3 获取ObjectWriterProvider
JSON.toJSONString()
入口:
JSONWriter.Context writeContext = new JSONWriter.Context(JSONFactory.defaultObjectWriterProvider);
其中会调用默认的构造方法初始化ObjectWriterProvider
:
public ObjectWriterProvider() {this.init();
ObjectWriterCreator creator = null;
switch (JSONFactory.CREATOR) {
case "reflect":
creator = ObjectWriterCreator.INSTANCE;
break;
case "lambda":
creator = ObjectWriterCreatorLambda.INSTANCE;
break;
case "asm":
default:
try {creator = ObjectWriterCreatorASM.INSTANCE;} catch (Throwable var5) { }
if (creator == null) {creator = ObjectWriterCreatorLambda.INSTANCE;}
}
this.creator = (ObjectWriterCreator)creator;
}
ObjectWriterCreator
采取的是单例模式,默认采纳 ASM
动静字节码实现。
3.1.4 获取ObjectWriter
有了 ObjectWriterProvider
后,下一步就是获取 ObjectWriter
,也就是JSON.toJSONString()
中的:
JSONWriter.Context context = ((JSONWriter)writer).context;
boolean fieldBased = (context.features & com.alibaba.fastjson2.JSONWriter.Feature.FieldBased.mask) != 0L;
ObjectWriter<?> objectWriter = context.provider.getObjectWriter(valueClass, valueClass, fieldBased);
getObjectWriter()
如下:
public ObjectWriter getObjectWriter(Type objectType, Class objectClass, boolean fieldBased) {
// fieldBased 是基于字段序列化的意思
// false 的话示意基于 getter 序列化
// 依据不同的类型从不同的缓存 map 中获取
ObjectWriter objectWriter = fieldBased ? (ObjectWriter)this.cacheFieldBased.get(objectType) : (ObjectWriter)this.cache.get(objectType);
// 首次获取应该为 null
if (objectWriter != null) {return (ObjectWriter)objectWriter;
} else {
// 这个 useModules 布尔变量笔者不太理解
// 这里的逻辑是 基于字段反序列化 并且 指标 class 不为空 并且 指标 class 能够赋值给 Iterable 并且 指标 class 不能赋值给 class
boolean useModules = true;
if (fieldBased && objectClass != null && Iterable.class.isAssignableFrom(objectClass) && !Collection.class.isAssignableFrom(objectClass)) {useModules = false;}
// 这里的例子是 true
if (useModules) {for(int i = 0; i < this.modules.size(); ++i) {
// 获取 ObjectWriterModule
ObjectWriterModule module = (ObjectWriterModule)this.modules.get(i);
objectWriter = module.getObjectWriter(objectType, objectClass);
// 为 null
if (objectWriter != null) {ObjectWriter previous = fieldBased ? (ObjectWriter)this.cacheFieldBased.putIfAbsent(objectType, objectWriter) : (ObjectWriter)this.cache.putIfAbsent(objectType, objectWriter);
if (previous != null) {objectWriter = previous;}
return (ObjectWriter)objectWriter;
}
}
}
// 第一次执行临时还拿不到 ObjectWriter,这里的条件全副合乎
if (objectWriter == null && objectClass != null && !fieldBased) {switch (objectClass.getName()) {
// 针对 Guava 库的 map 内置了 ObjectWriter
case "com.google.common.collect.HashMultimap":
case "com.google.common.collect.LinkedListMultimap":
case "com.google.common.collect.LinkedHashMultimap":
case "com.google.common.collect.ArrayListMultimap":
case "com.google.common.collect.TreeMultimap":
objectWriter = GuavaSupport.createAsMapWriter(objectClass);
break;
// 不是 JSONObject 类
case "com.alibaba.fastjson.JSONObject":
objectWriter = ObjectWriterImplMap.of(objectClass);
}
}
// ObjectWriter 还没拿到
if (objectWriter == null) {// 第一次拿须要通过 ObjectWriterCreator()去创立
ObjectWriterCreator creator = this.getCreator();
if (objectClass == null) {objectClass = TypeUtils.getMapping(objectType);
}
// 创立 ObjectWriter
// ObjectWriter 的外面会创立 FieldWriter,这外面的逻辑很长,通过一些列简单逻辑的判断,再针对字段获取
objectWriter = creator.createObjectWriter(objectClass, fieldBased ? Feature.FieldBased.mask : 0L, this);
// 放入缓存
ObjectWriter previous = fieldBased ? (ObjectWriter)this.cacheFieldBased.putIfAbsent(objectType, objectWriter) : (ObjectWriter)this.cache.putIfAbsent(objectType, objectWriter);
if (previous != null) {objectWriter = previous;}
}
return (ObjectWriter)objectWriter;
}
}
其中 FiledWriter
获取逻辑如下:
// ObjectWriterCreatorASM.java 236 行左右的地位
BeanUtils.fields(objectClass, (field) -> {if (fieldBased || Modifier.isPublic(field.getModifiers())) {fieldInfo.init();
// 创立 FieldWriter
FieldWriter fieldWriter = this.creteFieldWriter(objectClass, writerFeatures, provider.modules, beanInfo, fieldInfo, field);
if (fieldWriter != null) {
// 放入缓存
fieldWriterMap.putIfAbsent(fieldWriter.fieldName, fieldWriter);
}
}
});
3.1.5 write()
操作
获取到 ObjectWriter
之后,就能够进行 write()
操作了,JSON.toJSONString()
入口:
objectWriter.write((JSONWriter)writer, object, (Object)null, (Type)null, 0L);
因为自定义类的 ObjectWriter
是运行时拿到的,无奈通过调试获取到,然而能够通过内置的 ObjectWriter
来判断大略的 write()
流程:
比方 LocalDateWriter
的write()
如下:
public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
try {int year = (Integer)this.getYear.invoke(object);
int monthOfYear = (Integer)this.getMonthOfYear.invoke(object);
int dayOfMonth = (Integer)this.getDayOfMonth.invoke(object);
Object chronology = this.getChronology.invoke(object);
if (chronology != this.utc && chronology != null) {jsonWriter.startObject();
// 写入 key
jsonWriter.writeName("year");
// 写入 value
jsonWriter.writeInt32(year);
jsonWriter.writeName("month");
jsonWriter.writeInt32(monthOfYear);
jsonWriter.writeName("day");
jsonWriter.writeInt32(dayOfMonth);
jsonWriter.writeName("chronology");
jsonWriter.writeAny(chronology);
jsonWriter.endObject();} else {LocalDate localDate = LocalDate.of(year, monthOfYear, dayOfMonth);
DateTimeFormatter formatter = this.getDateFormatter();
if (formatter == null) {formatter = jsonWriter.getContext().getDateFormatter();}
if (formatter == null) {jsonWriter.writeLocalDate(localDate);
} else {String str = formatter.format(localDate);
jsonWriter.writeString(str);
}
}
} catch (InvocationTargetException | IllegalAccessException var14) {throw new JSONException("write LocalDateWriter error", var14);
}
}
依据 getter
获取字段值并调用对应的 Writer
去写JSON
。
3.2 反序列化
3.2.1 概览
反序列化也能够参考官网的一张图:
大略流程与序列化相似:
- 获取
ObjectReader
- 如果
ObjectReaderProvider
有缓存,从缓存提取 - 如果
ObjectReaderProvider
没有缓存,创立ObjectReader
并且缓存到ObjectReaderProvider
中 - 通过
JSONReader
失去Object
- 返回后果
3.2.2 入口
Entity entity = Entity.builder().field1("a").field2(2).build();
String str = JSON.toJSONString(entity);
System.out.println(JSON.parseObject(str,Entity.class));
其中 parseObject()
如下:
static <T> T parseObject(String text, Class<T> clazz) {if (text != null && !text.isEmpty()) {
// 获取 JSONReader
JSONReader reader = JSONReader.of(text);
Object var7;
try {
JSONReader.Context context = reader.context;
// 判断是否基于字段反序列化
boolean fieldBased = (context.features & Feature.FieldBased.mask) != 0L;
// 通过 ObjectReaderProvider 获取 ObjectReader
// 这个 context 看起来和 ObjectReaderProvider 无关,实际上外部实现 Context 蕴含了 ObjectReaderProvider
ObjectReader<T> objectReader = context.provider.getObjectReader(clazz, fieldBased);
// 反序列化外围办法
T object = objectReader.readObject(reader, (Type)null, (Object)null, 0L);
if (reader.resolveTasks != null) {reader.handleResolveTasks(object);
}
if (reader.ch != 26 && (reader.context.features & Feature.IgnoreCheckClose.mask) == 0L) {throw new JSONException(reader.info("input not end"));
}
var7 = object;
} catch (Throwable var9) {if (reader != null) {
try {reader.close();
} catch (Throwable var8) {var9.addSuppressed(var8);
}
}
throw var9;
}
if (reader != null) {reader.close();
}
return var7;
} else {return null;}
}
3.2.3 获取JSONReader
JSON.parseObject()
入口:
JSONReader reader = JSONReader.of(text);
其中 JSONReader.of()
实现如下:
public static JSONReader of(String str) {if (str == null) {throw new NullPointerException();
} else {
// 创立 Context
// Context 外部蕴含了 ObjectReaderProvider
Context context = JSONFactory.createReadContext();
int length;
// 测试环境 JDK8,此处 if 不成立
if (JDKUtils.JVM_VERSION > 8 && JDKUtils.UNSAFE_SUPPORT) {
try {length = JDKUtils.STRING_CODER != null ? JDKUtils.STRING_CODER.applyAsInt(str) : UnsafeUtils.getStringCoder(str);
if (length == 0) {byte[] bytes = JDKUtils.STRING_VALUE != null ? (byte[])JDKUtils.STRING_VALUE.apply(str) : UnsafeUtils.getStringValue(str);
return new JSONReaderASCII(context, str, bytes, 0, bytes.length);
}
} catch (Exception var4) {throw new JSONException("unsafe get String.coder error");
}
}
length = str.length();
char[] chars;
// 测试环境 JDK8
if (JDKUtils.JVM_VERSION == 8) {
// 获取 char array
chars = JDKUtils.getCharArray(str);
} else {chars = str.toCharArray();
}
// 因为 JDK8 的 String 还是应用 char[]实现的,所以返回 UTF16 的 JSONReader
return new JSONReaderUTF16(context, str, chars, 0, length);
}
}
3.2.4 获取ObjectReader
JSON.parseObject()
入口:
ObjectReader<T> objectReader = context.provider.getObjectReader(clazz, fieldBased);
getObjectReader()
如下:
public ObjectReader getObjectReader(Type objectType, boolean fieldBased) {if (objectType == null) {objectType = Object.class;}
// 有缓存间接从缓存提取
ObjectReader objectReader = fieldBased ? (ObjectReader)this.cacheFieldBased.get(objectType) : (ObjectReader)this.cache.get(objectType);
// 第一次获取 ObjectReader 为 null
if (objectReader != null) {return objectReader;} else {Iterator var4 = this.modules.iterator();
ObjectReader previous;
while(var4.hasNext()) {ObjectReaderModule module = (ObjectReaderModule)var4.next();
// 获取到的 ObjectReader 为 null
objectReader = module.getObjectReader(this, (Type)objectType);
if (objectReader != null) {previous = fieldBased ? (ObjectReader)this.cacheFieldBased.putIfAbsent(objectType, objectReader) : (ObjectReader)this.cache.putIfAbsent(objectType, objectReader);
if (previous != null) {objectReader = previous;}
return objectReader;
}
}
Type rawType;
// 条件不合乎
if (objectType instanceof TypeVariable) {Type[] bounds = ((TypeVariable)objectType).getBounds();
if (bounds.length > 0) {rawType = bounds[0];
if (rawType instanceof Class) {previous = this.getObjectReader(rawType, fieldBased);
if (previous != null) {ObjectReader previous = this.getPreviousObjectReader(fieldBased, (Type)objectType, previous);
if (previous != null) {previous = previous;}
return previous;
}
}
}
}
// 条件不合乎
if (objectType instanceof ParameterizedType) {ParameterizedType parameterizedType = (ParameterizedType)objectType;
rawType = parameterizedType.getRawType();
Type[] typeArguments = parameterizedType.getActualTypeArguments();
if (rawType instanceof Class) {Class rawClass = (Class)rawType;
boolean generic = false;
for(Class clazz = rawClass; clazz != Object.class; clazz = clazz.getSuperclass()) {if (clazz.getTypeParameters().length > 0) {
generic = true;
break;
}
}
if (typeArguments.length == 0 || !generic) {ObjectReader rawClassReader = this.getObjectReader(rawClass, fieldBased);
if (rawClassReader != null) {ObjectReader previous = this.getPreviousObjectReader(fieldBased, (Type)objectType, rawClassReader);
if (previous != null) {rawClassReader = previous;}
return rawClassReader;
}
}
}
}
Class<?> objectClass = TypeUtils.getMapping((Type)objectType);
String className = objectClass.getName();
if (objectReader == null && !fieldBased) {switch (className) {
// 针对 Guava 的 MultiMap,这里的条件不合乎
case "com.google.common.collect.ArrayListMultimap":
objectReader = ObjectReaderImplMap.of((Type)null, objectClass, 0L);
}
}
// ObjectReader 为 null
if (objectReader == null) {
// 获取 ObjectReaderCreator
ObjectReaderCreator creator = this.getCreator();
// 创立 ObjectReader
objectReader = creator.createObjectReader(objectClass, (Type)objectType, fieldBased, this);
}
previous = this.getPreviousObjectReader(fieldBased, (Type)objectType, objectReader);
if (previous != null) {objectReader = previous;}
return objectReader;
}
}
在创立 ObjectReader
的同时,会在其中创立FieldReader
:
// ObjectReaderCreator.java 453
FieldReader[] fieldReaderArray = this.createFieldReaders(objectClass, objectType, beanInfo, fieldBased, provider);
3.2.5 read()
操作
有了 ObjectReader
之后就能够进行具体的 read()
操作了,JSON.parseObject()
入口:
T object = objectReader.readObject(reader, (Type)null, (Object)null, 0L);
此处的 readObject()
如下:
// ObjectReaderNoneDefaultConstructor.java 171 行
public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
// 判断是否带有 JSONReader.Feature.ErrorOnNoneSerializable
if (!this.serializable) {jsonReader.errorOnNoneSerializable(this.objectClass);
}
// 是否 JSONB,不合乎
if (jsonReader.isJSONB()) {return this.readJSONBObject(jsonReader, fieldType, fieldName, 0L);
} else if (jsonReader.isArray() && jsonReader.isSupportBeanArray(features | this.features)) {
// 是数组而且反对 JSONReader.Feature.SupportArrayToBean,不合乎
jsonReader.next();
LinkedHashMap<Long, Object> valueMap = null;
for(int i = 0; i < this.fieldReaders.length; ++i) {Object fieldValue = this.fieldReaders[i].readFieldValue(jsonReader);
if (valueMap == null) {valueMap = new LinkedHashMap();
}
long hash = this.fieldReaders[i].fieldNameHash;
valueMap.put(hash, fieldValue);
}
if (!jsonReader.nextIfMatch(']')) {throw new JSONException(jsonReader.info("array not end," + jsonReader.current()));
} else {jsonReader.nextIfMatch(',');
return this.createInstanceNoneDefaultConstructor((Map)(valueMap == null ? Collections.emptyMap() : valueMap));
}
} else {
// 读取字符
boolean objectStart = jsonReader.nextIfObjectStart();
// 条件为 false,进入 else
if (!objectStart && !jsonReader.isTypeRedirect() && jsonReader.nextIfEmptyString()) {return null;} else {
// 获取 Context,读取其中的 Features
JSONReader.Context context = jsonReader.getContext();
long featuresAll = this.features | features | context.getFeatures();
// 此处存储对象的值
// key 是字段的 long 哈希值,哈希采取的是 fnv1a 64 算法生成的
// value 是字段的具体值
LinkedHashMap<Long, Object> valueMap = null;
// 读取
for(int i = 0; !jsonReader.nextIfMatch('}'); ++i) {
// 读取字段名对应的 hashCode
long hashCode = jsonReader.readFieldNameHashCode();
if (hashCode != 0L) {if (hashCode == this.typeKeyHashCode && i == 0) {long typeHash = jsonReader.readTypeHashCode();
// 此处 if 条件不合乎
if (typeHash != this.typeNameHash) {boolean supportAutoType = (featuresAll & Feature.SupportAutoType.mask) != 0L;
ObjectReader autoTypeObjectReader;
String typeName;
if (supportAutoType) {autoTypeObjectReader = context.getObjectReaderAutoType(typeHash);
if (autoTypeObjectReader == null) {typeName = jsonReader.getString();
autoTypeObjectReader = context.getObjectReaderAutoType(typeName, this.objectClass, this.features);
}
} else {typeName = jsonReader.getString();
autoTypeObjectReader = context.getObjectReaderAutoType(typeName, this.objectClass);
}
if (autoTypeObjectReader == null) {typeName = jsonReader.getString();
autoTypeObjectReader = context.getObjectReaderAutoType(typeName, this.objectClass, this.features);
}
if (autoTypeObjectReader != null) {Object object = autoTypeObjectReader.readObject(jsonReader, fieldType, fieldName, 0L);
jsonReader.nextIfMatch(',');
return object;
}
}
} else {
// 获取字段名哈希对应的 FieldReader
FieldReader fieldReader = this.getFieldReader(hashCode);
// 条件不合乎
if (fieldReader == null && (featuresAll & Feature.SupportSmartMatch.mask) != 0L) {long hashCodeLCase = jsonReader.getNameHashCodeLCase();
if (hashCodeLCase != hashCode) {fieldReader = this.getFieldReaderLCase(hashCodeLCase);
}
}
// fieldReader 不为 null
if (fieldReader == null) {this.processExtra(jsonReader, (Object)null);
} else {
// 读取字段值
Object fieldValue = fieldReader.readFieldValue(jsonReader);
if (valueMap == null) {valueMap = new LinkedHashMap();
}
long hash;
if (fieldReader instanceof FieldReaderObjectParam) {hash = ((FieldReaderObjectParam)fieldReader).paramNameHash;
} else {hash = fieldReader.fieldNameHash;}
// 写入
valueMap.put(hash, fieldValue);
}
}
}
}
// 结构对象
T object = this.createInstanceNoneDefaultConstructor((Map)(valueMap == null ? Collections.emptyMap() : valueMap));
if (this.setterFieldReaders != null && valueMap != null) {for(int i = 0; i < this.setterFieldReaders.length; ++i) {FieldReader fieldReader = this.setterFieldReaders[i];
// 读取字段值
Object fieldValue = valueMap.get(fieldReader.fieldNameHash);
if (fieldValue != null) {
// 通过 setter 注入
fieldReader.accept(object, fieldValue);
}
}
}
jsonReader.nextIfMatch(',');
return object;
}
}
}
4 结尾
其实文章中很多细节的中央因为篇幅的限度无奈过于具体的解释,比方内置的各类型的 Reader
/Writer
具体是如何获取值进行序列化 / 反序列操作的,想要深刻探索这部分就只能本人去挖源码了。另外须要留神的是,文章的环境是在 JDK8
下的,因为 Fastjson2
在不同的 JDK
下会有不同的序列化实现,因而仅供参考。
最初,对于性能的比拟,能够参考官网的比拟基准。
5 参考
- Fastjson2 Github
- Fastjson2 Wiki