Coder Social home page Coder Social logo

xza-m / fastjson2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alibaba/fastjson2

1.0 0.0 0.0 2.36 MB

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

License: Apache License 2.0

Java 99.90% Kotlin 0.07% Shell 0.02%

fastjson2's Introduction

Java CI Codecov Maven Central GitHub release Java support License Gitpod Ready-to-Code

1. FASTJSON v2

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

  • 支持JSON/JSONB两种协议,JSONPath是一等公民;
  • 支持全量解析和部分解析;
  • 支持Java服务端、客户端Android、大数据场景。

fastjson

相关文档:

2. 使用前准备

2.1 Maven依赖

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

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

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

2.2 fastjson v1的兼容包

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

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

2.3 常用类和方法

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

package com.alibaba.fastjson2;

class JSON {
    // 将字符串解析成JSONObject
    static JSONObject parseObject(String str);

    // 将字符串解析成JSONArray
    static JSONArray parseArray(String str);

    // 将字符串解析成Java对象
    static T parseObject(byte[] utf8Bytes, Class<T> objectClass);

    // 将Java对象输出成字符串
    static String toJSONString(Object object);

    // 将Java对象输出成UTF8编码的byte[]
    static byte[] toJSONBytes(Object object);
}

class JSONB {
    // 将jsonb格式的byte[]解析成Java对象
    static T parseObject(byte[] jsonbBytes, Class<T> objectClass);

    // 将Java对象输出成jsonb格式的byte[]
    static byte[] toBytes(Object object);
}

class JSONObject {
    Object get(String key);
    int getIntValue(String key);
    Integer getInteger(String key);
    long getLongValue(String key);
    Long getLong(String key);
    T getObject(String key, Class<T> objectClass);

    // 将JSONObject对象转换为Java对象
    T toJavaObject(Class<T> objectClass);
}

class JSONArray {
    Object get(int index);
    int getIntValue(int index);
    Integer getInteger(int index);
    long getLongValue(int index);
    Long getLong(int index);
    T getObject(int index, Class<T> objectClass);
}

class JSONPath {
    // 构造JSONPath
    static JSONPath of(String path);

    // 根据path直接解析输入,会部分解析优化,不会全部解析
    Object extract(JSONReader jsonReader);

    // 根据path对对象求值
    Object eval(Object rootObject);
}

class JSONReader {
    // 构造基于String输入的JSONReader
    static JSONReader of(String str);

    // 构造基于utf8编码byte数组输入的JSONReader
    static JSONReader of(byte[] utf8Bytes);

    // 构造基于char[]输入的JSONReader
    static JSONReader of(char[] chars);

    // 构造基于json格式byte数组输入的JSONReader
    static JSONReader ofJSONB(byte[] jsonbBytes)
}

3. 读取JSON对象

String str = "{\"id\":123}";
JSONObject jsonObject = JSON.parseObject(str);
int id = jsonObject.getIntValue("id");
String str = "[\"id\", 123]";
JSONArray jsonArray = JSON.parseArray(str);
String name = jsonArray.getString(0);
int id = jsonArray.getIntValue(1);

4. 将JavaBean对象生成JSON

4.1 将JavaBean对象生成JSON格式的字符串

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

Product product = new Product();
product.id = 1001;
product.name = "DataWorks";

JSON.toJSONString(product);

// 生成如下的结果
{
    "id"   : 1001,
    "name" : "DataWorks"
}

JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
// 生成如下的结果
[1001, "DataWorks"]

4.2 将JavaBean对象生成UTF8编码的byte[]

Product product = ...;
byte[] utf8JSONBytes = JSON.toJSONBytes(product);

4.3 将JavaBean对象生成JSONB格式的byte[]

Product product = ...;
byte[] jsonbBytes = JSONB.toBytes(product);

byte[] jsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);

5. 读取JavaBean

5.1 将字符串读取成JavaBean

String str = "{\"id\":123}";
Product product = JSON.parseObject(str, Product.class);

5.2 将UTF8编码的byte[]读取成JavaBean

byte[] utf8Bytes = "{\"id\":123}".getBytes(StandardCharsets.UTF_8);
Product product = JSON.parseObject(utf8Bytes, Product.class);

5.3 将JSONB数据读取成JavaBean

byte[] jsonbBytes = ...
Product product = JSONB.parseObject(jsonbBytes, Product.class);

Product product = JSONB.parseObject(jsonbBytes, Product.class, JSONReader.Feature.SupportBeanArrayMapping);

6. 使用JSONPath

6.1 使用JSONPath部分读取数据

String str = ...;

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

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

6.2 使用JSONPath读取部分utf8Bytes的数据

byte[] utf8Bytes = ...;

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

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

6.3 使用JSONPath读取部分jsonbBytes的数据

byte[] jsonbBytes = ...;

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

JSONReader parser = JSONReader.ofJSONB(jsonbBytes); // 注意,这是利用ofJSONB方法
Object result = path.extract(parser);

fastjson2's People

Contributors

wenshao avatar oldratlee avatar victorzeng avatar kraity avatar wsxe9988 avatar kdl1217 avatar

Stargazers

xza 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.