Skip to content

Commit 6a73920

Browse files
nlohmannprncoprsfangq
authored
Complete documentation for 3.11.0 (#3464)
* 👥 update contributor and sponsor list * 🚧 document BJData format * 🚧 document BJData format * 📝 clarified documentation of [json.exception.parse_error.112] * ✏️ adjust titles * 📝 add more examples * 🚨 adjust warnings for index.md files * 📝 add more examples * 🔥 remove example for deprecated code * 📝 add missing enum entry * 📝 overwork table for binary formats * ✅ add test to create table for binary formats * 📝 fix wording in example * 📝 add more examples * Update iterators.md (#3481) * ✨ add check for overloads to linter #3455 * 👥 update contributor list * 📝 add more examples * 📝 fix documentation * 📝 add more examples * 🎨 fix indentation * 🔥 remove example for destructor * 📝 overwork documentation * Updated BJData documentation, #3464 (#3493) * update bjdata.md for #3464 * Minor edit * Fix URL typo * Add info on demoting ND array to a 1-D optimized array when singleton dimension Co-authored-by: Chaoqi Zhang <[email protected]> Co-authored-by: Qianqian Fang <[email protected]>
1 parent a8a547d commit 6a73920

File tree

102 files changed

+1990
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1990
-247
lines changed

README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ There are myriads of [JSON](https://json.org) libraries out there, and each may
5454

5555
- **Trivial integration**. Our whole code consists of a single header file [`json.hpp`](https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp). That's it. No library, no subproject, no dependencies, no complex build system. The class is written in vanilla C++11. All in all, everything should require no adjustment of your compiler flags or project settings.
5656

57-
- **Serious testing**. Our class is heavily [unit-tested](https://github.com/nlohmann/json/tree/develop/tests/src) and covers [100%](https://coveralls.io/r/nlohmann/json) of the code, including all exceptional behavior. Furthermore, we checked with [Valgrind](https://valgrind.org) and the [Clang Sanitizers](https://clang.llvm.org/docs/index.html) that there are no memory leaks. [Google OSS-Fuzz](https://github.com/google/oss-fuzz/tree/master/projects/json) additionally runs fuzz tests against all parsers 24/7, effectively executing billions of tests so far. To maintain high quality, the project is following the [Core Infrastructure Initiative (CII) best practices](https://bestpractices.coreinfrastructure.org/projects/289).
57+
- **Serious testing**. Our code is heavily [unit-tested](https://github.com/nlohmann/json/tree/develop/tests/src) and covers [100%](https://coveralls.io/r/nlohmann/json) of the code, including all exceptional behavior. Furthermore, we checked with [Valgrind](https://valgrind.org) and the [Clang Sanitizers](https://clang.llvm.org/docs/index.html) that there are no memory leaks. [Google OSS-Fuzz](https://github.com/google/oss-fuzz/tree/master/projects/json) additionally runs fuzz tests against all parsers 24/7, effectively executing billions of tests so far. To maintain high quality, the project is following the [Core Infrastructure Initiative (CII) best practices](https://bestpractices.coreinfrastructure.org/projects/289).
5858

5959
Other aspects were not so important to us:
6060

@@ -69,6 +69,10 @@ See the [contribution guidelines](https://github.com/nlohmann/json/blob/master/.
6969

7070
You can sponsor this library at [GitHub Sponsors](https://github.com/sponsors/nlohmann).
7171

72+
### :office: Corporate Sponsor
73+
74+
[![](https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Codacy-logo-black.svg/320px-Codacy-logo-black.svg.png)](https://github.com/codacy)
75+
7276
### :label: Named Sponsors
7377

7478
- [Michael Hartmann](https://github.com/reFX-Mike)
@@ -1599,6 +1603,20 @@ I deeply appreciate the help of the following people.
15991603
- [Dirk Stolle](https://github.com/striezel) fixed typos in documentation.
16001604
- [Daniel Albuschat](https://github.com/daniel-kun) corrected the parameter name in the `parse` documentation.
16011605
- [Prince Mendiratta](https://github.com/Prince-Mendiratta) fixed a link to the FAQ.
1606+
- [Florian Albrechtskirchinger](https://github.com/falbrechtskirchinger) implemented `std::string_view` support for object keys and made dozens of other improvements.
1607+
- [Qianqian Fang](https://github.com/fangq) implemented the Binary JData (BJData) format.
1608+
- [pketelsen](https://github.com/pketelsen) added macros `NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT` and `NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT`.
1609+
- [DarkZeros](https://github.com/DarkZeros) adjusted to code to not clash with Arduino defines.
1610+
- [flagarde](https://github.com/flagarde) fixed the output of `meta()` for MSVC.
1611+
- [Giovanni Cerretani](https://github.com/gcerretani) fixed a check for `std::filesystem`.
1612+
- [Dimitris Apostolou](https://github.com/rex4539) fixed a typo.
1613+
- [Ferry Huberts](https://github.com/fhuberts) fixed a typo.
1614+
- [Michael Nosthoff](https://github.com/heinemml) fixed a typo.
1615+
- [JungHoon Lee](https://github.com/jhnlee) fixed a typo.
1616+
- [Faruk D.](https://github.com/fdiblen) fixed the CITATION.CFF file.
1617+
- [Andrea Cocito](https://github.com/puffetto) added a clarification on macro usage to the documentation.
1618+
- [Krzysiek Karbowiak](https://github.com/kkarbowiak) refactored the tests to use `CHECK_THROWS_WITH_AS`.
1619+
- [Chaoqi Zhang](https://github.com/prncoprs) fixed a typo.
16021620

16031621
Thanks a lot for helping out! Please [let me know](mailto:[email protected]) if I forgot someone.
16041622

@@ -1613,7 +1631,7 @@ The library itself consists of a single header file licensed under the MIT licen
16131631
- [**Artistic Style**](http://astyle.sourceforge.net) for automatic source code indentation
16141632
- [**Clang**](https://clang.llvm.org) for compilation with code sanitizers
16151633
- [**CMake**](https://cmake.org) for build automation
1616-
- [**Codacity**](https://www.codacy.com) for further [code analysis](https://www.codacy.com/app/nlohmann/json)
1634+
- [**Codacy**](https://www.codacy.com) for further [code analysis](https://www.codacy.com/app/nlohmann/json)
16171635
- [**Coveralls**](https://coveralls.io) to measure [code coverage](https://coveralls.io/github/nlohmann/json)
16181636
- [**Coverity Scan**](https://scan.coverity.com) for [static analysis](https://scan.coverity.com/projects/nlohmann-json)
16191637
- [**cppcheck**](http://cppcheck.sourceforge.net) for static analysis
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
// define a byte container based on std::vector
5+
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
6+
7+
using json = nlohmann::json;
8+
9+
int main()
10+
{
11+
// (1) create empty container
12+
auto c1 = byte_container_with_subtype();
13+
14+
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}};
15+
16+
// (2) create container
17+
auto c2 = byte_container_with_subtype(bytes);
18+
19+
// (3) create container with subtype
20+
auto c3 = byte_container_with_subtype(bytes, 42);
21+
22+
std::cout << json(c1) << "\n" << json(c2) << "\n" << json(c3) << std::endl;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"bytes":[],"subtype":null}
2+
{"bytes":[202,254,186,190],"subtype":null}
3+
{"bytes":[202,254,186,190],"subtype":42}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
// define a byte container based on std::vector
5+
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
6+
7+
using json = nlohmann::json;
8+
9+
int main()
10+
{
11+
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}};
12+
13+
// create container with subtype
14+
auto c1 = byte_container_with_subtype(bytes, 42);
15+
16+
std::cout << "before calling clear_subtype(): " << json(c1) << '\n';
17+
18+
c1.clear_subtype();
19+
20+
std::cout << "after calling clear_subtype(): " << json(c1) << '\n';
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
before calling clear_subtype(): {"bytes":[202,254,186,190],"subtype":42}
2+
after calling clear_subtype(): {"bytes":[202,254,186,190],"subtype":null}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
// define a byte container based on std::vector
5+
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
6+
7+
int main()
8+
{
9+
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}};
10+
11+
// create container
12+
auto c1 = byte_container_with_subtype(bytes);
13+
14+
// create container with subtype
15+
auto c2 = byte_container_with_subtype(bytes, 42);
16+
17+
std::cout << std::boolalpha << "c1.has_subtype() = " << c1.has_subtype()
18+
<< "\nc2.has_subtype() = " << c2.has_subtype() << std::endl;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
c1.has_subtype() = false
2+
c2.has_subtype() = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
// define a byte container based on std::vector
5+
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
6+
7+
using json = nlohmann::json;
8+
9+
int main()
10+
{
11+
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}};
12+
13+
// create container without subtype
14+
auto c = byte_container_with_subtype(bytes);
15+
16+
std::cout << "before calling set_subtype(42): " << json(c) << '\n';
17+
18+
// set the subtype
19+
c.set_subtype(42);
20+
21+
std::cout << "after calling set_subtype(42): " << json(c) << '\n';
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
before calling set_subtype(42): {"bytes":[202,254,186,190],"subtype":null}
2+
after calling set_subtype(42): {"bytes":[202,254,186,190],"subtype":42}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
// define a byte container based on std::vector
5+
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>;
6+
7+
int main()
8+
{
9+
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}};
10+
11+
// create container
12+
auto c1 = byte_container_with_subtype(bytes);
13+
14+
// create container with subtype
15+
auto c2 = byte_container_with_subtype(bytes, 42);
16+
17+
std::cout << "c1.subtype() = " << c1.subtype()
18+
<< "\nc2.subtype() = " << c2.subtype() << std::endl;
19+
20+
// in case no subtype is set, return special value
21+
assert(c1.subtype() == static_cast<byte_container_with_subtype::subtype_type>(-1));
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
c1.subtype() = 18446744073709551615
2+
c2.subtype() = 42

docs/examples/cbor_tag_handler_t.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
// tagged byte string
9+
std::vector<std::uint8_t> vec = {{0xd8, 0x42, 0x44, 0xcA, 0xfe, 0xba, 0xbe}};
10+
11+
// cbor_tag_handler_t::error throws
12+
try
13+
{
14+
auto b_throw_on_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::error);
15+
}
16+
catch (json::parse_error& e)
17+
{
18+
std::cout << e.what() << std::endl;
19+
}
20+
21+
// cbor_tag_handler_t::ignore ignores the tag
22+
auto b_ignore_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::ignore);
23+
std::cout << b_ignore_tag << std::endl;
24+
25+
// cbor_tag_handler_t::store stores the tag as binary subtype
26+
auto b_store_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::store);
27+
std::cout << b_store_tag << std::endl;
28+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0xD8
2+
{"bytes":[202,254,186,190],"subtype":null}
3+
{"bytes":[202,254,186,190],"subtype":66}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
std::cout << std::boolalpha
9+
<< "one < two : " << json::default_object_comparator_t{}("one", "two") << "\n"
10+
<< "three < four : " << json::default_object_comparator_t{}("three", "four") << std::endl;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
one < two : true
2+
three < four : false

docs/examples/error_handler_t.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
// create JSON value with invalid UTF-8 byte sequence
9+
json j_invalid = "ä\xA9ü";
10+
try
11+
{
12+
std::cout << j_invalid.dump() << std::endl;
13+
}
14+
catch (json::type_error& e)
15+
{
16+
std::cout << e.what() << std::endl;
17+
}
18+
19+
std::cout << "string with replaced invalid characters: "
20+
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::replace)
21+
<< "\nstring with ignored invalid characters: "
22+
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore)
23+
<< '\n';
24+
}

docs/examples/error_handler_t.output

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9
2+
string with replaced invalid characters: "ä�ü"
3+
string with ignored invalid characters: "äü"

docs/examples/from_bjdata.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include <nlohmann/json.hpp>
4+
5+
using json = nlohmann::json;
6+
7+
int main()
8+
{
9+
// create byte vector
10+
std::vector<std::uint8_t> v = {0x7B, 0x69, 0x07, 0x63, 0x6F, 0x6D, 0x70, 0x61,
11+
0x63, 0x74, 0x54, 0x69, 0x06, 0x73, 0x63, 0x68,
12+
0x65, 0x6D, 0x61, 0x69, 0x00, 0x7D
13+
};
14+
15+
// deserialize it with BJData
16+
json j = json::from_bjdata(v);
17+
18+
// print the deserialized JSON value
19+
std::cout << std::setw(2) << j << std::endl;
20+
}

docs/examples/from_bjdata.output

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"compact": true,
3+
"schema": 0
4+
}

docs/examples/get_allocator.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
auto alloc = json::get_allocator();
9+
using traits_t = std::allocator_traits<decltype(alloc)>;
10+
11+
json* j = traits_t::allocate(alloc, 1);
12+
traits_t::construct(alloc, j, "Hello, world!");
13+
14+
std::cout << *j << std::endl;
15+
16+
traits_t::destroy(alloc, j);
17+
traits_t::deallocate(alloc, j, 1);
18+
}

docs/examples/get_allocator.output

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Hello, world!"

docs/examples/iterator_wrapper.cpp

-29
This file was deleted.

docs/examples/iterator_wrapper.output

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
// different JSON Pointers
9+
json::json_pointer ptr1("/foo/0");
10+
json::json_pointer ptr2("/a~1b");
11+
12+
// implicit conversion to string
13+
std::string s;
14+
s += ptr1;
15+
s += "\n";
16+
s += ptr2;
17+
18+
std::cout << s << std::endl;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/foo/0
2+
/a~1b
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
json::json_pointer::string_t s = "This is a string.";
9+
10+
std::cout << s << std::endl;
11+
12+
std::cout << std::boolalpha << std::is_same<json::json_pointer::string_t, json::string_t>::value << std::endl;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is a string.
2+
true

docs/examples/json_pointer__to_string.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ int main()
1919
json::json_pointer ptr11("/ ");
2020
json::json_pointer ptr12("/m~0n");
2121

22-
2322
std::cout << ptr1.to_string() << '\n'
2423
<< ptr2.to_string() << '\n'
2524
<< ptr3.to_string() << '\n'

0 commit comments

Comments
 (0)