📝 overwork example handling

This commit is contained in:
Niels Lohmann 2021-12-28 23:48:08 +01:00
parent be1eafa12c
commit da38cca316
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
16 changed files with 86 additions and 58 deletions

View File

@ -81,7 +81,7 @@ int main()
json j_list(c_list);
// create an array from std::forward_list
std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
std::forward_list<std::int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist);
// create an array from std::array

View File

@ -7,11 +7,11 @@ using json = nlohmann::json;
int main()
{
// create byte vector
std::vector<uint8_t> v = {0x1b, 0x00, 0x00, 0x00, 0x08, 0x63, 0x6f, 0x6d,
0x70, 0x61, 0x63, 0x74, 0x00, 0x01, 0x10, 0x73,
0x63, 0x68, 0x65, 0x6d, 0x61, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};
std::vector<std::uint8_t> v = {0x1b, 0x00, 0x00, 0x00, 0x08, 0x63, 0x6f, 0x6d,
0x70, 0x61, 0x63, 0x74, 0x00, 0x01, 0x10, 0x73,
0x63, 0x68, 0x65, 0x6d, 0x61, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};
// deserialize it with BSON
json j = json::from_bson(v);

View File

@ -7,10 +7,10 @@ using json = nlohmann::json;
int main()
{
// create byte vector
std::vector<uint8_t> v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d,
0x61, 0x00
};
std::vector<std::uint8_t> v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d,
0x61, 0x00
};
// deserialize it with CBOR
json j = json::from_cbor(v);

View File

@ -7,10 +7,10 @@ using json = nlohmann::json;
int main()
{
// create byte vector
std::vector<uint8_t> v = {0x82, 0xa7, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
0x74, 0xc3, 0xa6, 0x73, 0x63, 0x68, 0x65, 0x6d,
0x61, 0x00
};
std::vector<std::uint8_t> v = {0x82, 0xa7, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
0x74, 0xc3, 0xa6, 0x73, 0x63, 0x68, 0x65, 0x6d,
0x61, 0x00
};
// deserialize it with MessagePack
json j = json::from_msgpack(v);

View File

@ -7,10 +7,10 @@ using json = nlohmann::json;
int main()
{
// create byte vector
std::vector<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
};
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 UBJSON
json j = json::from_ubjson(v);

View File

@ -2,7 +2,7 @@
"compiler": {
"c++": "201103",
"family": "clang",
"version": "13.0.0 (clang-1300.0.29.3)"
"version": "13.0.0 (clang-1300.0.29.30)"
},
"copyright": "(C) 2013-2021 Niels Lohmann",
"name": "JSON for Modern C++",

View File

@ -7,7 +7,7 @@ using json = nlohmann::json;
int main()
{
// a JSON text given as std::vector
std::vector<uint8_t> text = {'[', '1', ',', '2', ',', '3', ']', '\0'};
std::vector<std::uint8_t> text = {'[', '1', ',', '2', ',', '3', ']', '\0'};
// parse and serialize JSON
json j_complete = json::parse(text);

View File

@ -1,15 +0,0 @@
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// a JSON text given as std::vector
std::vector<uint8_t> text = {'[', '1', ',', '2', ',', '3', ']', '\0'};
// parse and serialize JSON
json j_complete = json::parse(text.begin(), text.end());
std::cout << std::setw(4) << j_complete << "\n\n";
}

View File

@ -1,6 +0,0 @@
[
1,
2,
3
]

View File

@ -10,7 +10,7 @@ int main()
json j = R"({"compact": true, "schema": 0})"_json;
// serialize it to BSON
std::vector<uint8_t> v = json::to_bson(j);
std::vector<std::uint8_t> v = json::to_bson(j);
// print the vector content
for (auto& byte : v)

View File

@ -10,7 +10,7 @@ int main()
json j = R"({"compact": true, "schema": 0})"_json;
// serialize it to CBOR
std::vector<uint8_t> v = json::to_cbor(j);
std::vector<std::uint8_t> v = json::to_cbor(j);
// print the vector content
for (auto& byte : v)

View File

@ -10,7 +10,7 @@ int main()
json j = R"({"compact": true, "schema": 0})"_json;
// serialize it to MessagePack
std::vector<uint8_t> v = json::to_msgpack(j);
std::vector<std::uint8_t> v = json::to_msgpack(j);
// print the vector content
for (auto& byte : v)

View File

@ -23,7 +23,7 @@ int main()
json j = R"({"compact": true, "schema": false})"_json;
// serialize it to UBJSON
std::vector<uint8_t> v = json::to_ubjson(j);
std::vector<std::uint8_t> v = json::to_ubjson(j);
// print the vector content
for (auto& byte : v)
@ -36,11 +36,11 @@ int main()
json array = {1, 2, 3, 4, 5, 6, 7, 8};
// serialize it to UBJSON using default representation
std::vector<uint8_t> v_array = json::to_ubjson(array);
std::vector<std::uint8_t> v_array = json::to_ubjson(array);
// serialize it to UBJSON using size optimization
std::vector<uint8_t> v_array_size = json::to_ubjson(array, true);
std::vector<std::uint8_t> v_array_size = json::to_ubjson(array, true);
// serialize it to UBJSON using type optimization
std::vector<uint8_t> v_array_size_and_type = json::to_ubjson(array, true, true);
std::vector<std::uint8_t> v_array_size_and_type = json::to_ubjson(array, true, true);
// print the vector contents
for (auto& byte : v_array)

View File

@ -101,6 +101,20 @@ Linear.
--8<-- "examples/operator__equal.output"
```
??? example
The example demonstrates comparing several JSON types against the null pointer (JSON `#!json null`).
```cpp
--8<-- "examples/operator__equal__nullptr_t.cpp"
```
Output:
```json
--8<-- "examples/operator__equal__nullptr_t.output"
```
## Version history
- Added in version 1.0.0.

View File

@ -39,18 +39,33 @@ Linear.
## Examples
The example demonstrates comparing several JSON
types.
??? example
The example demonstrates comparing several JSON types.
```cpp
--8<-- "examples/operator__notequal.cpp"
```
```cpp
--8<-- "examples/operator__notequal.cpp"
```
Output:
```json
--8<-- "examples/operator__notequal.output"
```
Output:
??? example
```json
--8<-- "examples/operator__notequal.output"
```
The example demonstrates comparing several JSON types against the null pointer (JSON `#!json null`).
```cpp
--8<-- "examples/operator__notequal__nullptr_t.cpp"
```
Output:
```json
--8<-- "examples/operator__notequal__nullptr_t.output"
```
## Version history

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
import glob
import os.path
def check_structure():
@ -95,7 +96,26 @@ def check_structure():
if required_header not in existing_headers:
print(f'{file}:{lineno+1}: Error: required header "{required_header}" was not found!')
print(f'\nchecked {len(files)} files')
def check_examples():
example_files = sorted(glob.glob('../../examples/*.cpp'))
markdown_files = sorted(glob.glob('**/*.md', recursive=True))
# check if every example file is used in at least one markdown file
for example_file in example_files:
example_file = os.path.join('examples', os.path.basename(example_file))
found = False
for markdown_file in markdown_files:
content = ' '.join(open(markdown_file).readlines())
if example_file in content:
found = True
break
if not found:
print(f'{example_file}: Error: example file is not used in any documentation file!')
if __name__ == '__main__':
check_structure()
check_examples()