GithubHelp home page GithubHelp logo

tywo45 / fastjson2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alibaba/fastjson2

0.0 0.0 0.0 121.26 MB

🚄 FASTJSON2 is a Java JSON library with excellent performance.

License: Apache License 2.0

Shell 0.01% Java 99.57% Go 0.02% Kotlin 0.38% HTML 0.02%

fastjson2's Introduction

Java CI Codecov Maven Central GitHub release Java support License Gitpod Ready-to-Code Last SNAPSHOT GitHub Stars GitHub Forks user repos GitHub Contributors

📖 English Documentation | 📖 中文文档
本项目的Issues会被同步沉淀至阿里云开发者社区

FASTJSON v2

FASTJSON 2是一个性能极致并且简单易用的Java JSON库。

fastjson logo

1. 使用准备

1.1 添加依赖

fastjson v2中,groupId1.x不一样,是com.alibaba.fastjson2

Maven:

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2</artifactId>
    <version>2.0.48</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2:2.0.48'
}

可以在 maven.org 查看最新可用的版本。

1.2 其他模块

Fastjson v1兼容模块

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

Maven:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.48</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.alibaba:fastjson:2.0.48'
}

Fastjson Kotlin集成模块

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

  • Maven:
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-kotlin</artifactId>
    <version>2.0.48</version>
</dependency>

酌情添加标准库(kotlin-stdlib)、反射库(kotlin-reflect), 其中若使用数据类(data class)、通过构造函数传入参数则添加反射库。

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>${kotlin-version}</version>
</dependency>

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-reflect</artifactId>
    <version>${kotlin-version}</version>
</dependency>
  • Kotlin Gradle:
dependencies {
    implementation("com.alibaba.fastjson2:fastjson2-kotlin:2.0.48")
}
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
    implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version")
}

Fastjson Extension扩展模块

如果项目使用SpringFramework等框架,可以使用fastjson-extension模块,使用方式参考 SpringFramework Support

Maven:

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension-spring5</artifactId>
    <version>2.0.48</version>
</dependency>
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension-spring6</artifactId>
    <version>2.0.48</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring5:2.0.48'
}
dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring6:2.0.48'
}

2. 简单使用

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

2.1 将JSON解析为JSONObject

Java:

String text = "...";
JSONObject data = JSON.parseObject(text);

byte[] bytes = ...;
JSONObject data = JSON.parseObject(bytes);

Kotlin:

import com.alibaba.fastjson2.*

val text = ... // String
val data = text.parseObject()

val bytes = ... // ByteArray
val data = bytes.parseObject() // JSONObject

2.2 将JSON解析为JSONArray

Java:

String text = "...";
JSONArray data = JSON.parseArray(text);

Kotlin:

import com.alibaba.fastjson2.*

val text = ... // String
val data = text.parseArray() // JSONArray

2.3 将JSON解析为Java对象

Java:

String text = "...";
User data = JSON.parseObject(text, User.class);

Kotlin:

import com.alibaba.fastjson2.*

val text = ... // String
val data = text.to<User>() // User
val data = text.parseObject<User>() // User

2.4 将Java对象序列化为JSON

Java:

Object data = "...";
String text = JSON.toJSONString(data);
byte[] text = JSON.toJSONBytes(data);

Kotlin:

import com.alibaba.fastjson2.*

val data = ... // Any
val text = text.toJSONString() // String
val bytes = text.toJSONByteArray() // ByteArray

2.5 使用JSONObjectJSONArray

2.5.1 获取简单属性

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

int id = obj.getIntValue("id");
String name = obj.getString("name");
String text = "[2, \"fastjson2\"]";
JSONArray array = JSON.parseArray(text);

int id = array.getIntValue(0);
String name = array.getString(1);

2.5.2 读取JavaBean

Java:

JSONArray array = ...
JSONObject obj = ...

User user = array.getObject(0, User.class);
User user = obj.getObject("key", User.class);

Kotlin:

val array = ... // JSONArray
val obj = ... // JSONObject

val user = array.to<User>(0)
val user = obj.to<User>("key")

2.5.3 转为JavaBean

Java:

JSONArray array = ...
JSONObject obj = ...

User user = obj.toJavaObject(User.class);
List<User> users = array.toJavaList(User.class);

Kotlin:

val array = ... // JSONArray
val obj = ... // JSONObject

val user = obj.to<User>() // User
val users = array.toList<User>() // List<User>

2.6 将JavaBean对象序列化为JSON

Java:

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

User user = new User();
user.id = 2;
user.name = "FastJson2";

String text = JSON.toJSONString(user);
byte[] bytes = JSON.toJSONBytes(user);

Kotlin:

class User(
    var id: Int,
    var name: String
)

val user = User()
user.id = 2
user.name = "FastJson2"

val text = user.toJSONString() // String
val bytes = user.toJSONByteArray() // ByteArray

序列化结果:

{
    "id"   : 2,
    "name" : "FastJson2"
}

3. 进阶使用

3.1 使用JSONB

3.1.1 将JavaBean对象序列化JSONB

User user = ...;
byte[] bytes = JSONB.toBytes(user);
byte[] bytes = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);

3.1.2 将JSONB数据解析为JavaBean

byte[] bytes = ...
User user = JSONB.parseObject(bytes, User.class);
User user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);

3.2 使用JSONPath

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 = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

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

3.2.3 使用JSONPath读取部分byte[]的数据

byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

JSONReader parser = JSONReader.ofJSONB(bytes); // 注意这里使用ofJSONB方法
Object result = path.extract(parser);

Star History

Star History Chart

fastjson2's People

Contributors

wenshao avatar dependabot[bot] avatar victorzeng avatar kraity avatar oldratlee avatar yanxutao89 avatar wsxe9988 avatar rowstop avatar mek1986 avatar codeplayer avatar magicfollower avatar jiangqiang1996 avatar wyyl1 avatar kdl1217 avatar howardlei avatar gabrielhwang avatar shenfeng312 avatar seasonpanpan avatar moshicoco avatar krishnanand5 avatar liaozan avatar bodhili avatar yinwoods avatar danght avatar lhaox avatar cnscoo avatar zhao2018mr avatar a1batr0ss avatar scrollsyou avatar xiezheng-xd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.