Go to file
薛磊 7796353f4e 仓库转正删除patch
Signed-off-by: 薛磊 <xuelei3@huawei.com>
2023-09-21 14:44:21 +08:00
benches 【ylong_json】copyright 注释修改 2023-07-21 09:51:50 +08:00
docs 【ylong_json】更新 README & RELEASE_NOTE & user_guide 2023-07-17 21:25:32 +08:00
examples 【ylong_json】copyright 注释修改 2023-07-21 09:51:50 +08:00
figures 修改 ylong_json readme 问题。 2023-09-07 19:44:33 +08:00
src 【ylong_json】修复 serde adapt 反序列解析 objcect 逻辑问题 & 补充 serde adapt 反序列化的 sdv 测试 2023-08-08 11:02:02 +08:00
tests 【ylong_json】修复 serde adapt 反序列解析 objcect 逻辑问题 & 补充 serde adapt 反序列化的 sdv 测试 2023-08-08 11:02:02 +08:00
BUILD.gn 修改 ylong_json readme 问题。 2023-09-07 19:44:33 +08:00
bundle.json 【ylong_json】配置 patches 相关信息 & 修复 BUILD.gn, bundle.json 2023-07-25 15:29:20 +08:00
Cargo.toml 【ylong_json】构建方式修正 && OAT.xml 修改 2023-08-29 10:14:07 +08:00
LICENSE ylong_json code init 2023-06-19 17:44:09 +08:00
OAT.xml 【ylong_json】构建方式修正 && OAT.xml 修改 2023-08-29 10:14:07 +08:00
README_zh.md 仓库转正删除patch 2023-09-21 14:44:21 +08:00
README.md 仓库转正删除patch 2023-09-21 14:44:21 +08:00
RELEASE_NOTE.md 【ylong_json】更新 README & RELEASE_NOTE & user_guide 2023-07-17 21:25:32 +08:00

ylong_json

Introduction

ylong_json is a general JSON syntax parsing library that provides functions for converting JSON text to and from specific data structures.

ylong_json in OpenHarmony

structure The following is a description of the key fields in the above figure:

  • Application Layer: The application layer provides specific functions to users.
  • App: Various applications need to use the functions of the system service layer.
  • System Service Layer: System service layer, which provides system services to upper-layer applications.
  • system services: Various system services require the use of JSON related functions.
  • ylong_json: System component, providing common JSON serialization and deserialization capabilities to related components of the system service layer.
  • serde: third-party library for efficient and versatile serialization and deserialization of Rust data structures.

ylong_json Internal architecture diagram

structure ylong_json is mainly divided into three submodules: JsonValue submodule, serde submodule, and C-ffi submodule.

The JsonValue submodule provides a basic data structure JsonValue. JsonValue supports serializing itself into JSON text in either indented or compact format. Any syntactically correct JSON text can also be deserialized into a corresponding JsonValue data structure. JsonValue supports addition, deletion, modification and query, and you can use the specified interface to change the data content in JsonValue. JsonValue supports all data types in JSON syntax: null, boolean, number, string, array, object, and implements all its functions according to ECMA-404. For array and object grammatical structures, JsonValue provides a variety of underlying data structures for different usage scenarios, for example, for array structures, it supports the underlying use of Vec or LinkedList, for object , supports the use of Vec, LinkedList or Btree as its underlying layer. On different underlying data structures, array and object will reflect different creation and query performance, for example, object based on Btree data structure has higher performance in query, LinkedList or LinkedList or Vec has high performance in terms of creation.

The serde submodule provides procedural macro functions based on the Serialize and Deserialize traits provided by the serde third-party library, which can support fast conversion of user structures and JSON text. The advantage of serde compared to JsonValue is that it is easy to use. Users do not need to convert the JSON text to JsonValue and then extract the specified data from it to generate the Rust structure. They only need to set Serialize' to the structure. and Deserialize process macro tags can be used to serialize the interface structure provided in ylong_json into JSON text, or convert the corresponding JSON text into a user structure.

The C-ffi module provides a C interface layer based on the JsonValue module, which facilitates users to use the C interface to call the functions of the ylong_json library.

Directory

ylong_json
├─ benches                                # Benche test files
├─ docs                                   # Description documents
├─ examples                               # ylong_json code example
├─ figures                                # ylong_json structure charts
├─ src
│  ├─ value                               # Array and Object type definitions and related methods
│  ├─ adapter.rs                          # Adapts to the C interface implementation
│  ├─ consts.rs                           # Some definitions of constants and tables
│  ├─ deserializer.rs                     # Deserialization implementation of the adaptation serde
│  ├─ encoder.rs                          # Serialization implementation for the `JsonValue` type
│  ├─ error.rs                            # Error type definition, helpful to identify the problem
│  ├─ link_list.rs                        # LinkedList type definition and related methods
│  ├─ serializer_compact.rs               # Serialization implementation of the adaptation serde
│  ├─ states.rs                           # Deserialization implementation for the `JsonValue` type
│  └─ value.rs                            # JsonValue type definition and related methods
└─ tests                                  # Test directory

Build

Use Cargo

  1. Add ylong_json to the dependency of Cargo.toml
[dependencies]
ylong_json = { git = "https://gitee.com/openharmony-sig/commonlibrary_rust_ylong_json.git" }

Use gn

  1. Add ylong_json in bundle.json
“deps” {
    “components”: ["ylong_json"]
}
  1. Add ylong_json:lib in BUILD.gn
external_deps = ["ylong_json:lib"]

User Guide

See user_guide

Performance test

The following tests are from nativejson-benchmark

The test environment information is as follows:

OS: Ubuntu 7.3.-16ubuntu3
Processor: Intel(R) Xeon(R) Gold 6278C CPU @ 2.60GHz
CPU(s): 8
Memory8.0 G

Software versions tested:

cJSON 1.7.16

Test Results:

======= ylong-json ==== parse | stringify ====
canada.json            200 MB/s  90 MB/s 
citm_catalog.json      450 MB/s  300 MB/s 
twitter.json           340 MB/s  520 MB/s

======== cJSON ======== parse | stringify ====
canada.json            55 MB/s    11 MB/s 
citm_catalog.json      260 MB/s   170 MB/s 
twitter.json           210 MB/s   210 MB/s

Description of test results:

Three test files are provided in the nativejson-benchmark test. Among them, canada.json contains a large number of number structures, the various data types of citm_catalog.json are relatively average, and twitter.json exists Various UTF-8 characters. To ensure test fairness, ylong_json enables list_object, list_array and ascii_only feature. The list_object and list_array features are mainly to ensure consistency with the cJSON data structure, and both are implemented using linked lists. ascii_only feature is to ensure consistent processing logic for UTF-8 characters, cJSON does not handle UTF-8 characters.

The testing process is as follows:

  • Read the content of the file into the memory, and get the content of the file content.
  • Call the specified JSON library deserialization interface to generate the corresponding JSON structure data.
  • Call the serialization interface of the JSON structure to generate the output content result.
  • Using content, loop deserialization generates JSON structure 100 times, taking the smallest processing time t1.
  • Using data, serialize and generate JSON text 100 times, taking the smallest processing time t2.
  • Calculate the parsing speed, the deserialization time is the length of content divided by t1, and the serialization time is the length of the JSON text divided by t2.