FastJson JSON源码学习

server/2024/11/14 11:52:08/

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

ParameterizedTypeType的子接口,表示一个有参数的类型,例如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)
2.2、解析input,添加按规则添加至ResolveTask
3. 执行handleResovleTask()
4. 关闭parser,返回结果

DefaultJSONParser.parseObject(Type type, Object fieldName)

1、找到对应的ObjectDeserializer
2、反序列化操作
2.1、找出Object对应的field及fieldClass(属性类型)
2.2、解析input,添加按规则添加至ResolveTask
3. 执行handleResovleTask()
4. 关闭parser,返回结果

http://www.ppmy.cn/server/102149.html

相关文章

nginx安全控制

nginx的安全控制 参考文档&#xff1a;https://docs.nginx.com/nginx/admin-guide/security-controls/controlling-access-proxied-http/ 1. nginx作为web服务器 # nginx作为web服务器&#xff0c;配置虚拟主机 server {listen 80; server_name www.sxl1.com; # 域名…

区块链开源的项目有哪些?

区块链领域有许多开源项目&#xff0c;它们覆盖了从基础设施到应用层的不同方面。以下是一些著名的区块链开源项目&#xff1a; 1. Bitcoin (比特币)&#xff1a;第一个去中心化的加密货币&#xff0c;源代码在 GitHub 上开源。它实现了区块链技术的基本概念。 2. Ethereum (…

微前端架构中子应用版本控制的实践与策略

在微前端架构中&#xff0c;子应用的独立性是其核心特征之一。每个子应用可以由不同的团队独立开发、部署和维护。这种独立性要求每个子应用能够自主控制其版本&#xff0c;以确保功能的迭代、修复和兼容性。本文将探讨在微前端架构中如何有效进行子应用的版本控制&#xff0c;…

PHP写API接口教程与实例

PHP API接口编写步骤 1、创建接口文件 首先&#xff0c;我们需要创建一个PHP文件作为API接口&#xff0c;例如api.php。 2、处理请求 在api.php中&#xff0c;我们需要获取客户端发送的请求&#xff0c;并根据请求类型&#xff08;GET或POST&#xff09;进行处理。 3、返回数据…

图卷积(GCN)

一、基本概述 图卷积看起来好像是利用周围的特征&#xff0c;但是在图中每个点的邻居是不确定的 图中常见任务&#xff1a; 节点分类&#xff0c;对每个节点进行预测&#xff0c;不同点是否有连接预测整个图分类&#xff0c;部分图分类等&#xff0c;不同子图是否相似&#…

wordpress网站“ERR_CONNECTION_REFUSED”错误

wordpress网站“ERR_CONNECTION_REFUSED”错误&#xff0c;是一个常见的wordpress错误。当WordPress网站出现“ERR_CONNECTION_REFUSED”错误时&#xff0c;可能的原因和解决方法如下&#xff1a; 检查互联网连接&#xff1a;首先确保你的设备已连接到互联网。如果连接没有问题…

【信息学奥赛一本通】1007:计算(a+b)×c的值

1007&#xff1a;计算(ab)c的值 时间限制: 1000 ms 内存限制: 65536 KB 提交数:184662 通过数: 150473 【题目描述】 给定3个整数a、b、c&#xff0c;计算表达式(ab)c的值。 【输入】 输入仅一行&#xff0c;包括三个整数a、b、c, 数与数之间以一个空格分开。(&#xff0d;10,…

【Day04】0基础微信小程序入门-学习笔记

文章目录 基础加强学习目标自定义组件1. 创建与引用2. 样式3. 数据、方法和属性4. 数据监听器5. 纯数据字段6. 组件生命周期6.1 created6.2 attached&#xff08;使用最多&#xff09;6.3 detached6.4 定义生命周期函数 7. 组件所在页面的生命周期7.1 定义使用7.2 生成随机的RG…