java - 如何使用高性能解析器在 Java 中解析 JSON?

标签 java json

我的输入是一个数组,大约有。每秒接收 2,000 个元素,有时每秒接收数次。数组的每个元素都有 3 个大数。

Jackson (com.fasterxml) 是亚毫秒级应用程序中最慢的部分,需要 15 毫秒(平均)。慢函数是objectMapper.readValue(text, MyDto.class);

使用基于子字符串的自定义 JSON 解析算法时,需要几微秒。使用 ObjectMapper 是 15 毫秒。

通过子字符串解析 JSON 是一种不好的做法,因为代码冗长且容易出现错误。

您将使用什么进行 JSON 解析?要求是非常快的算法。

https://github.com/ngs-doo/dsl-json我找到了 DSL json,但不知道如何让它将带有 JSON 的字符串解析到我的 DTO 中。我还没有找到一种简单快速的算法来将 JSON 从 String 解析为 DTO。

编辑:要解析的输入位于:

https://pastebin.com/831YtBdq

代码:

public class BitstampOrderBook {
    private long timestamp;
    private List<List<BigDecimal>> bids;
    private List<List<BigDecimal>> asks;

    public BitstampOrderBook() {

    }

    public BitstampOrderBook(long timestamp, List<List<BigDecimal>> bids, List<List<BigDecimal>> asks) {
        this.timestamp = timestamp;
        this.bids = bids;
        this.asks = asks;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public List<List<BigDecimal>> getBids() {
        return bids;
    }

    public List<List<BigDecimal>> getAsks() {
        return asks;
    }
}

public class BitstampOrder {
    private BigDecimal price;

    private BigDecimal amount;

    private String datetime;

    private int id;

    @SerializedName("order_type")
    private int orderType;

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getOrderType() {
        return orderType;
    }

    public void setOrderType(int orderType) {
        this.orderType = orderType;
    }

    @Override
    public String toString() {
        return "BitStampOrder{" +
                "price=" + price +
                ", amount=" + amount +
                ", datetime='" + datetime + '\'' +
                ", id='" + id + '\'' +
                ", orderType='" + orderType + '\'' +
                '}';
    }
}

主要内容:

BitstampOrderBook orderBook = objectMapper.readValue(jsonString, BitstampOrderBook.class);

JProfiler( jackson ):

https://i.imgur.com/mjdbDQe.png

编辑 2:

JProfiler(Gson):

https://i.imgur.com/WcHVhhd.png

从JProfiler可以看出,Gson比Jackson快了好几倍。什么会比 Gson 更快?

最佳答案

  1. 不要使用字符串作为输入,而是立即解析您从网络或磁盘读取的缓冲字节。
  2. 不要绑定(bind)到 BigDecimal,如果您需要以相同的精度将其值序列化回 JSON,则根据您的规则使用双原语 + 高效格式化程序。
  3. 为价格/金额对的内部数组使用 2 字段类。
  4. 切换到 Scala 并使用 jsoniter-scala (声明:我是这个了不起的图书馆的贡献者)。

完成所有这些步骤,在一个线程中解析样本时,您将获得超过每秒 400Mb 的速度。

查看解析 mostly the same kind of JSON (arrays of 2-value sub-arrays of numbers)GeoJSONReading 的结果here .

关于java - 如何使用高性能解析器在 Java 中解析 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52394559/

相关文章:

java - Gson 解析错误 预期为 BEGIN_ARRAY,但实际为 STRING

java - 如何匹配多个对象

java - 当我关闭连接时,Spring Reactive : java. io.IOException : An established connection was aborted by the software in your host machine,

java - 为什么我收到 AttachFile(File) is undefined for the type MimeBodyPart?

java - 使用反射检查方法是否是接口(interface)的重写方法

php - MySQL查询返回json名称值

php - 当我将 JSON 发送到 WEB 服务器时,发生错误 : org. json.JSONException:字符 3 处的输入结束

javascript - 如何使用 JSON 数组填充数据表?

json - 解码 JSON,因为它仍在通过 net/http 流入

json - 获取json对象