qjson

August 18, 2025 · View on GitHub

A fast, efficient JSON parsing and serialization library designed for modern C++ applications.

Features

🚀Ultra-Fast Performance

  • 6x faster parsing than nlohmann/json
  • Nearly 3x faster serialization than nlohmann/json
  • Zero-copy view mode provides additional 2x performance boost

âš¡ Memory Efficient

  • Two memory policies: copy and view modes
  • View mode eliminates unnecessary string copying
  • Compact internal storage reduces memory footprint

🛠 Modern C++ Design

  • Fully utilizes C++17 features
  • Template-based design with type safety
  • constexpr optimizations for compile-time computation
  • Exception-safe design

Performance Benchmarks

Performance comparison with popular JSON libraries on the same hardware:

  • Test environment: Intel i7-12700H @ 2.68GHz, 100,000 Iterations

JSON File Structure Statistics


FilenameSize(bytes)elementCountobjectCountarrayCountnumberCountstringCounttrueCountfalseCountnullCount
canana.json225105116717045604511112612000
citm_catalog.json17272041190810937104511439226604001263
twitter.json6315145681264105021091809934524461946

1. Parsing Performance (Parse, in MB/s)

JSON Librarycanada.jsoncitmcatalog.jsontwitter.json
Nlohmann (C++11)103.945368.416181.022
QJson (View+Pool)807.3601574.7521387.693
QJson (Copy+Pool)695.8731232.0051073.545
QJson (View)731.1891030.138930.848
QJson (Copy)377.089612.111431.727
RapidJSON_AutoUTF387.924394.442262.079
RapidJSON_FullPrec246.6981344.645525.990
RapidJSON_Insitu564.0491597.663751.883
RapidJSON_Iterative550.4541138.348520.085
RapidJSON593.8501574.752564.971

2. Stringification Performance (Stringify, in MB/s)

JSON Librarycanada.jsoncitmcatalog.jsontwitter.json
Nlohmann (C++11)48.947517.821306.805
QJson (View+Pool)2172.8444666.2613811.764
QJson (Copy+Pool)1953.3844653.0793717.646
QJson (View)2036.7834887.8043885.540
QJson (Copy)1984.0754692.8493885.540
RapidJSON_AutoUTF190.6881359.068562.333
RapidJSON_FullPrec287.4622549.8301123.617
RapidJSON_Insitu300.4162278.271965.158
RapidJSON_Iterative299.8702541.9601129.941
RapidJSON302.0222569.7191229.099

3. Statistics Performance (Statistics, in MB/s)

JSON Librarycanada.jsoncitmcatalog.jsontwitter.json
Nlohmann (C++11)4923.7834537.7141942.770
QJson (View+Pool)5740.02510359.6863717.646
QJson (Copy+Pool)5261.6909863.4143672.309
QJson (View)6925.06311850.2882448.206
QJson (Copy)5590.54610981.2672448.206
RapidJSON_AutoUTF6446.75514707.05413383.526
RapidJSON_FullPrec6168.87813959.23812547.056
RapidJSON_Insitu6446.75514839.55013383.526
RapidJSON_Iterative6389.19514323.39213092.580
RapidJSON6408.26714323.39212814.015

Summary & Observations

  • Parsing Performance:
    QJson View and RapidJSONInsitu deliver the best parsing speeds across most files. Nlohmann is consistently slower.
  • Stringification Performance:
    QJson and RapidJSONFullPrec perform best on complex files like twitter.json. simdjson and SonicJSON show impressive speeds on citmcatalog.json.
  • Statistics Performance:
    The RapidJSON family dominates, especially on citmcatalog.json, reaching speeds above 15,000 MB/s.

Quick Start

Building from Source

git clone git@github.com:qinyonghang/json.git
cd json
mkdir build && cd build
cmake ..
make
make install

Using CMake

find_package(qjson REQUIRED)
target_link_libraries(${TARGET_NAME} PRIVATE qlib::json)

Parsing JSON

#include "qlib/json.h"

using namespace qlib;
json_view_t json;
result = json::parse(&json, begin, end);
if (0 != result) {
    std::cout << "json::parse return " << result << std::endl;
    break;
}

// get string
auto name = json["name"].get<string_t>();
std::cout << "name: " << name << std::endl;

// get array
auto& array = json["array"].array();
for (auto& item : array) {
    std::cout << item.get<string_t>() << std::endl;
}

// get object
auto& object = json["object"].object();
for (auto& [key, value] : object) {
    std::cout << key << ": " << value.get<string_t>() << std::endl;
}

Memory Policies

Copy Mode(json_t)

using namespace qlib;
json_t json;
result = json::parse(&json, begin, end);
if (0 != result) {
    std::cout << "json::parse return " << result << std::endl;
    break;
}
  • Creates data copies
  • Suitable for scenarios requiring JSON data modification
  • Data independent with safe lifetime

View Mode(json_view_t)

using namespace qlib;
json_view_t json;
result = json::parse(&json, begin, end);
if (0 != result) {
    std::cout << "json::parse return " << result << std::endl;
    break;
}
  • Zero-copy, references original data
  • Up to 2x performance improvement
  • Suitable for read-only scenarios, requires original data lifetime management

High-Performance Usage Tips

  1. Prefer view mode: Use json_view_t for read-only operations for optimal performance
  2. Avoid deep copying: Use move semantics std::move() when possible

Dependencies

  • C++14
  • Standard library only (no external dependencies)

Designed for C++ developers who demand ultimate performance