Skip to content

Commit

Permalink
feat(Java): fastjson2和JsonPath介绍
Browse files Browse the repository at this point in the history
  • Loading branch information
youngzil committed Jun 17, 2022
1 parent aa74ace commit 630f97a
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 12 deletions.
94 changes: 94 additions & 0 deletions quickstart-json/docs/Fastjson2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# FASTJSON2介绍

## FASTJSON2文档

[FASTJSON2 Github](https://github.com/alibaba/fastjson2)

[fastjson2 文档](https://github.com/alibaba/fastjson2/wiki)



🚄 FASTJSON2是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库



`FASTJSON v2``FASTJSON`项目的重要升级,目标是为下一个十年提供一个高性能的`JSON`库。通过同一套`API`

- 支持`JSON/JSONB`两种协议,[`JSONPath`](https://alibaba.github.io/fastjson2/jsonpath_cn) 是一等公民。
- 支持全量解析和部分解析。
- 支持`Java`服务端、客户端`Android`、大数据场景。
- 支持`Kotlin` https://alibaba.github.io/fastjson2/kotlin_cn
- 支持`JSON Schema` https://alibaba.github.io/fastjson2/json_schema_cn
- 支持`Android 8+` [(2.0.7.android)](https://repo1.maven.org/maven2/com/alibaba/fastjson2/fastjson2/2.0.7.android/)
- 支持`Graal Native-Image` [(2.0.7.graal)](https://repo1.maven.org/maven2/com/alibaba/fastjson2/fastjson2/2.0.7.graal/)





相关文档:

- `JSONB`格式文档: https://alibaba.github.io/fastjson2/jsonb_format_cn
- `FASTJSON v2`性能有了很大提升,具体性能数据看这里:
https://alibaba.github.io/fastjson2/benchmark_cn

- [kimmking:JSON Best Practice(最佳实践)](http://kimmking.github.io/2017/06/06/json-best-practice/)
- [qq_35873847:解决FastJson中“$ref 循环引用”的问题<三种方式对应不同需求>](http://blog.csdn.net/qq_35873847/article/details/78850528)



## 1.2 其他模块

### `Fastjson v1`兼容模块

如果原来使用`fastjson 1.2.x`版本,可以使用兼容包,兼容包不能保证100%兼容,请仔细测试验证,发现问题请及时反馈。



### `Fastjson Kotlin`集成模块

如果项目使用`Kotlin`,可以使用`fastjson-kotlin`模块,使用方式上采用`kotlin`的特性。



### `Fastjson Extension`扩展模块

如果项目使用`SpringFramework`等框架,可以使用`fastjson-extension`模块,使用方式参考 [SpringFramework Support](https://github.com/alibaba/fastjson2/blob/main/docs/spring_support_cn.md)





`fastjson v2`中,`package``1.x`不一样,是`com.alibaba.fastjson2`。如果你之前用的是`fastjson1`,大多数情况直接更包名就即可。



## FASTJSON 2 Autotype机制介绍

### 1. AutoType功能介绍

FASTJSON支持AutoType功能,这个功能在序列化的JSON字符串中带上类型信息,在反序列化时,不需要传入类型,实现自动类型识别。

### 2. AutoType安全机制介绍

- 必须显式打开才能使用。和fastjson 1.x不一样,fastjson 1.x为了兼容有一个白名单,在fastjson 2中,没有任何白名单,也不包括任何Exception类的白名单,必须显式打开才能使用。这可以保证缺省配置下是安全的。
- 支持配置safeMode,在safeMode打开后,显式传入AutoType参数也不起作用
- 显式打开后,会经过内置黑名单过滤。该黑名单能拦截大部分常见风险,这个机制不能保证绝对安全,打开AutoType不应该在暴露在公网的场景下使用。










## 使用参考

[官方说明文档](https://github.com/alibaba/fastjson2)

[FASTJSON 2.0 发布,FASTJSON 项目的重要升级](https://www.oschina.net/news/191783/fastjson2-released)


[Jackson与Fastjson的恩怨情仇(完结篇)](https://blog.51cto.com/u_3631118/3153843)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ fast字段基本只能是基础数据类型



[fastjson Github](https://github.com/alibaba/fastjson)

[fastjson 文档](https://github.com/alibaba/fastjson/wiki/Quick-Start-CN)

https://github.com/alibaba/fastjson

https://github.com/alibaba/fastjson/wiki
https://github.com/Alibaba/fastjson/wiki/%E9%A6%96%E9%A1%B5

https://my.oschina.net/jsonavaj/blog/1573833

Expand All @@ -20,6 +18,15 @@ http://www.oschina.net/p/fastjson
https://github.com/eishay/jvm-serializers



- [kimmking:JSON Best Practice(最佳实践)](http://kimmking.github.io/2017/06/06/json-best-practice/)
- [qq_35873847:解决FastJson中“$ref 循环引用”的问题<三种方式对应不同需求>](http://blog.csdn.net/qq_35873847/article/details/78850528)






fastjson 详细介绍
fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。

Expand All @@ -42,13 +49,13 @@ public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JS
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。


3、有关类库的一些说明
SerializeWriter:相当于StringBuffer
JSONArray:相当于List<Object>
JSONObject:相当于Map<String, Object>

JSON反序列化没有真正数组,本质类型都是List<Object>

fastjson还有很多很高级的特性,比如支持注解、支持全类型序列化,这些都是很好的特性,功能强大,不在本次测试范围。
Expand All @@ -57,4 +64,3 @@ fastjson还有很多很高级的特性,比如支持注解、支持全类型序




2 changes: 2 additions & 0 deletions quickstart-json/docs/HJSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ HOCON
[HOCON官网](https://github.com/lightbend/config)
[HOCON文档](https://github.com/lightbend/config/blob/master/HOCON.md)

HOCON (Human-Optimized Config Object Notation)


configuration library for JVM languages using HOCON files

Expand Down
60 changes: 60 additions & 0 deletions quickstart-json/docs/JsonPath.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# JsonPath


## JsonPath的来源

JSONPath is a query language for JSON with features similar to XPath for XML. JSONPath is used for selecting and extracting a sub-section from the JSON document.

JSONPath 是一种 JSON 查询语言,其功能类似于 XML 的 XPath。 JSONPath 用于从 JSON 文档中选择和提取子部分。



看它的名字你就能知道,这家伙和JSON文档有关系,正如XPath之于XML文档一样,JsonPath为Json文档提供了解析能力,通过使用JsonPath,你可以方便的查找节点、获取想要的数据,JsonPath是Json版的XPath。


你可以在http://jsonpath.com/站点进行验证JsonPath的执行效果。


[JsonPath 语法手册](https://www.jianshu.com/p/9808ab64fc0c)


在线查询工具:[JsonPATH](http://jsonpath.com/)

- 使用JSON实现path查询操作(类似XML-PATH):[JsonPATH](https://github.com/json-path/JsonPath)




## JsonPath框架


### Jayway

[Jayway JsonPath](https://github.com/json-path/JsonPath)

[使用示例](https://github.com/json-path/JsonPath)

[Jayway JsonPath介绍](https://blog.csdn.net/liuxiao723846/article/details/108760342)

[JsonPath介绍](https://restfulapi.net/json-jsonpath/)

[jsonpath - 使用 JSONPath 解析 JSON 完整内容详解](https://www.cnblogs.com/wynjauu/articles/9556396.html)

[JsonPath 教程](https://newdocs.zhangkong365.com/JsonPath/index.html)



### Fastjson

fastjson 1.2.0之后的版本支持JSONPath。这是一个很强大的功能,可以在java框架中当作对象查询语言(OQL)来使用。

参考

[fastjson JSONPath 使用](https://github.com/alibaba/fastjson/wiki/JSONPath)

[fastjson2 jsonpath 性能测试](https://github.com/alibaba/fastjson2/wiki/jsonpath_benchmark)

[使用fastjson中的JSONPath处理json数据](https://segmentfault.com/a/1190000022504301)



20 changes: 20 additions & 0 deletions quickstart-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,32 @@

<dependencies>


<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.7.0</version>
</dependency>


<!-- 包路径:com.alibaba.fastjson. -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>

<!-- 包路径:com.alibaba.fastjson2. -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

<!-- 包路径:com.google.gson. -->
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package org.quickstart.json.fastjson2;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

public class Fastjson2Test {

@Test
public void testJSONObject() {

// 将JSON解析为JSONObject
String text = "{\"id\": 2,\"name\": \"fastjson2\"}";
JSONObject obj = JSON.parseObject(text);

byte[] bytes = text.getBytes();
JSONObject data2 = JSON.parseObject(bytes);

// 获取简单属性
int id = obj.getIntValue("id");
String name = obj.getString("name");

}

@Test
public void testJSONArray() {

// 2.2 将JSON解析为JSONArray
String text = "[2, \"fastjson2\"]";
JSONArray array = JSON.parseArray(text);

// 获取简单属性
int id = array.getIntValue(0);
String name = array.getString(1);

}

@Test
public void testJavaObject() {
// 2.3 将JSON解析为Java对象
String text = "...";
User data = JSON.parseObject(text, User.class);

// 读取JavaBean
/*JSONArray array = ...
JSONObject obj = ...
User user = array.getObject(0, User.class);
User user = obj.getObject("key", User.class);*/

// 转为JavaBean
/*JSONArray array = ...
JSONObject obj = ...
User user = obj.toJavaObject(User.class);
List<User> users = array.toJavaList(User.class);*/
}

@Test
public void testJavaObject2JSON() {
// 将Java对象序列化为JSON
Object data = "...";
String text = JSON.toJSONString(data);
byte[] text2 = JSON.toJSONBytes(data);

User user = new User();
user.id = 2;
user.name = "FastJson2";
String text3 = JSON.toJSONString(user);
byte[] bytes = JSON.toJSONBytes(user);
}

@Test
public void testJSONB() {

// 将JavaBean对象序列化JSONB
User user = new User();
byte[] bytes = JSONB.toBytes(user);
byte[] bytes2 = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);

// 将JSONB数据解析为JavaBean
User user2 = JSONB.parseObject(bytes, User.class);
User user3 = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportArrayToBean);

}

@Test
public void testJsonPath() throws IOException {

InputStream is = Fastjson2Test.class.getClassLoader().getResourceAsStream("json/JsonPathDemo.json");
String str = IOUtils.toString(is, "UTF-8");
Object ff = JSONPath.extract(str, "$.store.book[*].author");

JSONReader jsonReader = JSONReader.of(str);
JSONPath jsonPath = JSONPath.of("$.store.bicycle.color");
Object fff = jsonPath.extract(jsonReader);

// 3.2.1 使用JSONPath读取部分数据
String text = "...";
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

JSONReader parser = JSONReader.of(text);
Object result = path.extract(parser);

// 3.2.2 使用JSONPath读取部分byte[]的数据
byte[] bytes = "...".getBytes();
JSONPath path2 = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

JSONReader parser2 = JSONReader.of(bytes);
Object result2 = path2.extract(parser2);

// 3.2.3 使用JSONPath读取部分byte[]的数据
JSONPath path3 = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

JSONReader parser3 = JSONReader.ofJSONB(bytes); // 注意这里使用ofJSONB方法
Object result3 = path3.extract(parser3);

}

public class User {
private int id;
private String name;
private int age;
}

}
Loading

0 comments on commit 630f97a

Please sign in to comment.