-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <prncoprs@163.com> Co-authored-by: Qianqian Fang <fangqq@gmail.com>
- Loading branch information
1 parent
a8a547d
commit 6a73920
Showing
102 changed files
with
1,990 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
docs/examples/byte_container_with_subtype__byte_container_with_subtype.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
// define a byte container based on std::vector | ||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>; | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
// (1) create empty container | ||
auto c1 = byte_container_with_subtype(); | ||
|
||
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}}; | ||
|
||
// (2) create container | ||
auto c2 = byte_container_with_subtype(bytes); | ||
|
||
// (3) create container with subtype | ||
auto c3 = byte_container_with_subtype(bytes, 42); | ||
|
||
std::cout << json(c1) << "\n" << json(c2) << "\n" << json(c3) << std::endl; | ||
} |
3 changes: 3 additions & 0 deletions
3
docs/examples/byte_container_with_subtype__byte_container_with_subtype.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{"bytes":[],"subtype":null} | ||
{"bytes":[202,254,186,190],"subtype":null} | ||
{"bytes":[202,254,186,190],"subtype":42} |
21 changes: 21 additions & 0 deletions
21
docs/examples/byte_container_with_subtype__clear_subtype.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
// define a byte container based on std::vector | ||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>; | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}}; | ||
|
||
// create container with subtype | ||
auto c1 = byte_container_with_subtype(bytes, 42); | ||
|
||
std::cout << "before calling clear_subtype(): " << json(c1) << '\n'; | ||
|
||
c1.clear_subtype(); | ||
|
||
std::cout << "after calling clear_subtype(): " << json(c1) << '\n'; | ||
} |
2 changes: 2 additions & 0 deletions
2
docs/examples/byte_container_with_subtype__clear_subtype.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
before calling clear_subtype(): {"bytes":[202,254,186,190],"subtype":42} | ||
after calling clear_subtype(): {"bytes":[202,254,186,190],"subtype":null} |
19 changes: 19 additions & 0 deletions
19
docs/examples/byte_container_with_subtype__has_subtype.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
// define a byte container based on std::vector | ||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>; | ||
|
||
int main() | ||
{ | ||
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}}; | ||
|
||
// create container | ||
auto c1 = byte_container_with_subtype(bytes); | ||
|
||
// create container with subtype | ||
auto c2 = byte_container_with_subtype(bytes, 42); | ||
|
||
std::cout << std::boolalpha << "c1.has_subtype() = " << c1.has_subtype() | ||
<< "\nc2.has_subtype() = " << c2.has_subtype() << std::endl; | ||
} |
2 changes: 2 additions & 0 deletions
2
docs/examples/byte_container_with_subtype__has_subtype.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
c1.has_subtype() = false | ||
c2.has_subtype() = true |
22 changes: 22 additions & 0 deletions
22
docs/examples/byte_container_with_subtype__set_subtype.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
// define a byte container based on std::vector | ||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>; | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}}; | ||
|
||
// create container without subtype | ||
auto c = byte_container_with_subtype(bytes); | ||
|
||
std::cout << "before calling set_subtype(42): " << json(c) << '\n'; | ||
|
||
// set the subtype | ||
c.set_subtype(42); | ||
|
||
std::cout << "after calling set_subtype(42): " << json(c) << '\n'; | ||
} |
2 changes: 2 additions & 0 deletions
2
docs/examples/byte_container_with_subtype__set_subtype.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
before calling set_subtype(42): {"bytes":[202,254,186,190],"subtype":null} | ||
after calling set_subtype(42): {"bytes":[202,254,186,190],"subtype":42} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
// define a byte container based on std::vector | ||
using byte_container_with_subtype = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>; | ||
|
||
int main() | ||
{ | ||
std::vector<std::uint8_t> bytes = {{0xca, 0xfe, 0xba, 0xbe}}; | ||
|
||
// create container | ||
auto c1 = byte_container_with_subtype(bytes); | ||
|
||
// create container with subtype | ||
auto c2 = byte_container_with_subtype(bytes, 42); | ||
|
||
std::cout << "c1.subtype() = " << c1.subtype() | ||
<< "\nc2.subtype() = " << c2.subtype() << std::endl; | ||
|
||
// in case no subtype is set, return special value | ||
assert(c1.subtype() == static_cast<byte_container_with_subtype::subtype_type>(-1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
c1.subtype() = 18446744073709551615 | ||
c2.subtype() = 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
// tagged byte string | ||
std::vector<std::uint8_t> vec = {{0xd8, 0x42, 0x44, 0xcA, 0xfe, 0xba, 0xbe}}; | ||
|
||
// cbor_tag_handler_t::error throws | ||
try | ||
{ | ||
auto b_throw_on_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::error); | ||
} | ||
catch (json::parse_error& e) | ||
{ | ||
std::cout << e.what() << std::endl; | ||
} | ||
|
||
// cbor_tag_handler_t::ignore ignores the tag | ||
auto b_ignore_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::ignore); | ||
std::cout << b_ignore_tag << std::endl; | ||
|
||
// cbor_tag_handler_t::store stores the tag as binary subtype | ||
auto b_store_tag = json::from_cbor(vec, true, true, json::cbor_tag_handler_t::store); | ||
std::cout << b_store_tag << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0xD8 | ||
{"bytes":[202,254,186,190],"subtype":null} | ||
{"bytes":[202,254,186,190],"subtype":66} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
std::cout << std::boolalpha | ||
<< "one < two : " << json::default_object_comparator_t{}("one", "two") << "\n" | ||
<< "three < four : " << json::default_object_comparator_t{}("three", "four") << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
one < two : true | ||
three < four : false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
// create JSON value with invalid UTF-8 byte sequence | ||
json j_invalid = "ä\xA9ü"; | ||
try | ||
{ | ||
std::cout << j_invalid.dump() << std::endl; | ||
} | ||
catch (json::type_error& e) | ||
{ | ||
std::cout << e.what() << std::endl; | ||
} | ||
|
||
std::cout << "string with replaced invalid characters: " | ||
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::replace) | ||
<< "\nstring with ignored invalid characters: " | ||
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore) | ||
<< '\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9 | ||
string with replaced invalid characters: "ä�ü" | ||
string with ignored invalid characters: "äü" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
// create byte vector | ||
std::vector<std::uint8_t> v = {0x7B, 0x69, 0x07, 0x63, 0x6F, 0x6D, 0x70, 0x61, | ||
0x63, 0x74, 0x54, 0x69, 0x06, 0x73, 0x63, 0x68, | ||
0x65, 0x6D, 0x61, 0x69, 0x00, 0x7D | ||
}; | ||
|
||
// deserialize it with BJData | ||
json j = json::from_bjdata(v); | ||
|
||
// print the deserialized JSON value | ||
std::cout << std::setw(2) << j << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"compact": true, | ||
"schema": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
auto alloc = json::get_allocator(); | ||
using traits_t = std::allocator_traits<decltype(alloc)>; | ||
|
||
json* j = traits_t::allocate(alloc, 1); | ||
traits_t::construct(alloc, j, "Hello, world!"); | ||
|
||
std::cout << *j << std::endl; | ||
|
||
traits_t::destroy(alloc, j); | ||
traits_t::deallocate(alloc, j, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"Hello, world!" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
// different JSON Pointers | ||
json::json_pointer ptr1("/foo/0"); | ||
json::json_pointer ptr2("/a~1b"); | ||
|
||
// implicit conversion to string | ||
std::string s; | ||
s += ptr1; | ||
s += "\n"; | ||
s += ptr2; | ||
|
||
std::cout << s << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/foo/0 | ||
/a~1b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <iostream> | ||
#include <nlohmann/json.hpp> | ||
|
||
using json = nlohmann::json; | ||
|
||
int main() | ||
{ | ||
json::json_pointer::string_t s = "This is a string."; | ||
|
||
std::cout << s << std::endl; | ||
|
||
std::cout << std::boolalpha << std::is_same<json::json_pointer::string_t, json::string_t>::value << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This is a string. | ||
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.