json README

The project's README from github.com/nlohmann/json. As one markdown file: README.md.

JSON for Modern C++

Build Status Ubuntu macOS Windows Coverage Status Coverity Scan Build Status Codacy Badge Cirrus CI Fuzzing Status Try online Documentation GitHub license GitHub Releases Packaging status GitHub Downloads GitHub Issues Average time to resolve an issue CII Best Practices OpenSSF Scorecard Backup Status GitHub Sponsors REUSE status Discord

Design goals

There are myriads of JSON libraries out there, and each may even have its reason to exist. Our class had these design goals:

Other aspects were not so important to us:

See the contribution guidelines for more information.

Sponsors

You can sponsor this library at GitHub Sponsors.

:raising_hand: Priority Sponsor

:label: Named Sponsors

Further support

The development of the library is further supported by JetBrains by providing free access to their IDE tools.

JetBrains logo.

Thanks everyone!

Support

:question: If you have a question, please check if it is already answered in the FAQ or the Q&A section. If not, please ask a new question there.

:books: If you want to learn more about how to use the library, check out the rest of the README, have a look at code examples, or browse through the help pages.

:construction: If you want to understand the API better, check out the API Reference or have a look at the quick reference below.

:bug: If you found a bug, please check the FAQ if it is a known issue or the result of a design decision. Please also have a look at the issue list before you create a new issue. Please provide as much information as possible to help us understand and reproduce your issue.

There is also a docset for the documentation browsers Dash, Velocity, and Zeal that contains the full documentation as an offline resource.

Quick reference

Full API documentation

Examples

Here are some examples to give you an idea how to use the class.

Besides the examples below, you may want to:

→ Check the documentation
→ Browse the standalone example files
→ Read the full API Documentation with self-contained examples for every function

Read JSON from a file

The json class provides an API for manipulating a JSON value. To create a json object by reading a JSON file:

#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

// ...

std::ifstream f("example.json");
json data = json::parse(f);

If using modules (enabled with NLOHMANN_JSON_BUILD_MODULES), this example becomes:

import std;
import nlohmann.json;

using json = nlohmann::json;

// ...

std::ifstream f("example.json");
json data = json::parse(f);

Creating json objects from JSON literals

Assume you want to hard-code this literal JSON value as a json object:

{
  "pi": 3.141,
  "happy": true
}

There are various options:

// Using (raw) string literals and json::parse
json ex1 = json::parse(R"(
  {
    "pi": 3.141,
    "happy": true
  }
)");

// Using user-defined (raw) string literals
using namespace nlohmann::literals;
json ex2 = R"(
  {
    "pi": 3.141,
    "happy": true
  }
)"_json;

// Using initializer lists
json ex3 = {
  {"happy", true},
  {"pi", 3.141},
};

JSON as a first-class data type

Here are some examples to give you an idea how to use the class.

Assume you want to create the JSON object

{
  "pi": 3.141,
  "happy": true,
  "name": "Niels",
  "nothing": null,
  "answer": {
    "everything": 42
  },
  "list": [1, 0, 2],
  "object": {
    "currency": "USD",
    "value": 42.99
  }
}

With this library, you could write:

// create an empty structure (null)
json j;

// add a number stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;

// add a Boolean stored as bool
j["happy"] = true;

// add a string stored as std::string
j["name"] = "Niels";

// add another null object by passing nullptr
j["nothing"] = nullptr;

// add an object inside the object
j["answer"]["everything"] = 42;

// add an array stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };

// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };

// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
  {"pi", 3.141},
  {"happy", true},
  {"name", "Niels"},
  {"nothing", nullptr},
  {"answer", {
    {"everything", 42}
  }},
  {"list", {1, 0, 2}},
  {"object", {
    {"currency", "USD"},
    {"value", 42.99}
  }}
};

Note that in all these cases, you never need to "tell" the compiler which JSON value type you want to use. If you want to be explicit or express some edge cases, the functions json::array() and json::object() will help:

// a way to express the empty array []
json empty_array_explicit = json::array();

// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();

// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json array_not_object = json::array({ {"currency", "USD"}, {"value", 42.99} });

Serialization / Deserialization

To/from strings

You can create a JSON value (deserialization) by appending _json to a string literal:

// create object from string literal
json j = "{ \"happy\": true, \"pi\": 3

json (nlohmann/json)

Searching Database…

50.8K ·GitHub
这是什么?一个面向现代 C++ 的 JSON 库(nlohmann/json),让你可以直接在 C++ 代码中用直观、类似 STL 的语法读取、创建和操作 JSON 数据。
这是框架还是只需引入的库?它是一个纯头文件库:全部实现都在一个文件 json.hpp 中,无需单独构建,也无需添加子项目。
我该如何把它添加到项目中?只需将 json.hpp 放进项目并用 #include 引入,或通过 CMake、包管理器(vcpkg、Conan 等)或 Pkg-config 引入;也可以通过 NLOHMANN_JSON_BUILD_MODULES 选项,以 C++20 模块的形式使用。
需要任何外部依赖或特殊的编译器选项吗?不需要任何外部依赖,它使用标准 C++11 编写,在编译器的默认设置下即可工作。
它是现有最快的 JSON 库吗?不是。作者明确把易于集成放在原始速度和内存效率之前,因此其他 JSON 库的性能可能会超过它。
除了普通 JSON,它还支持哪些与 JSON 相关的标准和格式?它支持 JSON Pointer(RFC 6901)和 JSON Patch(RFC 6902),用于定位文档,以及对文档进行差异比较和打补丁;还支持 JSON Merge Patch,以及用于紧凑序列化的二进制格式 BSON、CBOR、MessagePack、UBJSON 和 BJData。
我能否把自己的 C++ 类自动转换为 JSON,或从 JSON 转换回来?可以。该库支持与任意用户自定义类型之间的隐式转换,包括为枚举特化转换,这些都通过其「与任意类型相互转换」机制实现。
这个库的测试做得怎么样?它声称达到 100% 的代码覆盖率,并进行了大量单元测试;已使用 Valgrind 和 Clang Sanitizers 检查内存泄漏,并由 Google OSS-Fuzz 持续进行模糊测试。
我能直接用 std::vector 或 std::map 之类的 STL 容器来构建 JSON 吗?可以。该库原生支持从 STL 容器转换;如果你已经会使用 std::vector 或 std::map,那么你也就已经掌握了所需的大部分 API。
Creator
nlohmann
Size
304.9 MB
Stars
50.8K
Age
13y
Commits · 90 Days