lengjingzju / json

LJSON is a C implemented JSON library that is much faster than cJSON and substantially faster than RapidJSON, it is currently the fastest general-purpose JSON library and supports all JSON5 features. LJSON supports JSON parsing, printing and editing, provides DOM and SAX APIs, and I/O supports string and file.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LJSON Description

中文版

LJSON is a C implemented JSON library that is much faster than cJSON and substantially faster than RapidJSON, it is currently the fastest general-purpose JSON library and supports all JSON5 features. LJSON supports JSON parsing, printing and editing, provides DOM and SAX APIs, and I/O supports string and file, it fully supports the test cases of nativejson-benchmark. By default, LJSON uses the personally developed ldouble algorithm to print double to string. Compared with the standard library, it may only be the 15th decimal place difference. It is currently the fastest double to string algorithm; users can also choose the personally optimized grisu2 algorithm or dragonbox algorithm.

Features

  • Faster: Print and parse faster than both cJSON and RapidJSON, up to 32 times faster than CJSON and 1.5 time faster than Rapid JSON, refer to below test results
  • Lighter: Provide a variety of methods to save memory, such as pool memory, file parsing while reading, file writing while printing, and SAX APIs. It can make memory usage a constant
  • Stronger: Support DOM and SAX-style APIs, provide APIs for JSON in classic mode and memory pool mode, support string and file as input and output, is extended to support long long integer and hexadecimal number
  • More friendly: C language implementation, does not depend on any other library, does not contain platform-related code, only one header file and source file, and the interface corresponding to cJSON. the code logic is clearer than any other JSON libraries
  • JSON5: Supports all JSON5 features, such as hexadecimal digits, comments, array and object tail element comma, single quoted string/key and unquoted key

Compile and Run

Compile Method

  • Compile directly
gcc -o ljson json.c -jnum.c json_test.c -lm -O2 -ffunction-sections -fdata-sections -W -Wall
make O=<output path> && make O=<output path> DESTDIR=<install path>
  • Cross Compile
make O=<output path> CROSS_COMPILE=<tool prefix> && make O=<output path> DESTDIR=<install path>
  • Select double to string algorithm gcc -DJSON_DTOA_ALGORITHM=n, n may be 0 / 1 / 2 / 3
    • 0: Personal implementation of ldouble algorithm: faster than Google's default implementation of grisu2 129% , faster than Tencent optimized grisu2 implementation 33%, faster than sprintf 14.6 times
    • 1: C standard library sprintf
    • 2: Personal optimized grisu2 algorithm: Google's grisu2 default implementation is 5.7 times faster than sprintf, Tencent optimized grisu2 implementation is 9.1 times faster than sprintf, LJSON optimized implementation is faster than sprintf 11.4 times
    • 3: Personal optimized dragonbox algorithm: the speed performance is slower than ldouble algorithm, but faster than grisu2 algorithm

Run Method

./json <json filename> <test index:0-7>

Debug Method

  • Set the value of the variable JSON_ERROR_PRINT_ENABLE in json.c to 1 and then re-compile

Parse Config

  • #define JSON_PARSE_SKIP_COMMENT 1 : Whether to allow C-like single-line comments and multi-line comments(JSON5 feature)
  • #define JSON_PARSE_LAST_COMMA 1 : Whether to allow comma in last element of array or object(JSON5 feature)
  • #define JSON_PARSE_EMPTY_KEY 0 : Whether to allow empty key
  • #define JSON_PARSE_SPECIAL_CHAR 1 : Whether to allow special characters such as newline in the string(JSON5 feature)
  • #define JSON_PARSE_SPECIAL_QUOTES 1 : Whether to allow single quoted string/key and unquoted key(JSON5 feature)
  • #define JSON_PARSE_HEX_NUM 1 : Whether to allow HEX number(JSON5 feature)
  • #define JSON_PARSE_SPECIAL_NUM 1 :Whether to allow special number such as starting with '.', '+', '0', for example: +99 .1234 10. 001(JSON5 feature)
  • #define JSON_PARSE_SPECIAL_DOUBLE 1 : Whether to allow special double such as NaN, Infinity, -Infinity(JSON5 feature)
  • #define JSON_PARSE_SINGLE_VALUE 1 : Whether to allow json starting with non-array and non-object
  • #define JSON_PARSE_FINISHED_CHAR 0 : Whether to allow characters other than spaces after finishing parsing

Note:

  • It 100% matches the test cases of nativejson-benchmark when only JSON_PARSE_EMPTY_KEY is set to 1, all others are set to 0.
  • Setting JSON_PARSE_SKIP_COMMENT and JSON_PARSE_SPECIAL_QUOTES to 1 will significantly affect the parsing speed.

Speed Test

Note: 'O2' optimization level and default option compilation, the test files come from the nativejson-benchmark project

Test Platform: Ambarella CV25M Board | CPU: ARM CortexA53 | OS: Linux-5.15
Test Result: LJSON parses 475% faster and prints 2836% faster than cJSON, LJSON parses 131% faster and prints 147% faster than RapidJSON

AARCH64-Linux Test Result

Test Platform: PC | CPU: Intel i7-10700 | OS: Ubuntu 18.04 (VirtualBox)
Test Result: LJSON parses 560% faster and prints 3184% faster than cJSON, LJSON parses 75% faster and prints 133% faster than RapidJSON

x86_64-Linux Test Result

ldouble-x86_64 Test Result

Contact

About

LJSON is a C implemented JSON library that is much faster than cJSON and substantially faster than RapidJSON, it is currently the fastest general-purpose JSON library and supports all JSON5 features. LJSON supports JSON parsing, printing and editing, provides DOM and SAX APIs, and I/O supports string and file.

License:MIT License


Languages

Language:C 99.5%Language:Makefile 0.5%