From da38cca31683bc961b4d37bc346d8c07576e1c0c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 28 Dec 2021 23:48:08 +0100 Subject: [PATCH] :memo: overwork example handling --- doc/examples/basic_json__CompatibleType.cpp | 2 +- doc/examples/from_bson.cpp | 10 +++--- doc/examples/from_cbor.cpp | 8 ++--- doc/examples/from_msgpack.cpp | 8 ++--- doc/examples/from_ubjson.cpp | 8 ++--- doc/examples/meta.output | 2 +- ...contiguouscontainer__parser_callback_t.cpp | 2 +- ...parse__iteratortype__parser_callback_t.cpp | 15 --------- ...se__iteratortype__parser_callback_t.output | 6 ---- doc/examples/to_bson.cpp | 2 +- doc/examples/to_cbor.cpp | 2 +- doc/examples/to_msgpack.cpp | 2 +- doc/examples/to_ubjson.cpp | 8 ++--- doc/mkdocs/docs/api/basic_json/operator_eq.md | 14 ++++++++ doc/mkdocs/docs/api/basic_json/operator_ne.md | 33 ++++++++++++++----- doc/mkdocs/scripts/check_structure.py | 22 ++++++++++++- 16 files changed, 86 insertions(+), 58 deletions(-) delete mode 100644 doc/examples/parse__iteratortype__parser_callback_t.cpp delete mode 100644 doc/examples/parse__iteratortype__parser_callback_t.output diff --git a/doc/examples/basic_json__CompatibleType.cpp b/doc/examples/basic_json__CompatibleType.cpp index d39fd7a43..e2f01aa63 100644 --- a/doc/examples/basic_json__CompatibleType.cpp +++ b/doc/examples/basic_json__CompatibleType.cpp @@ -81,7 +81,7 @@ int main() json j_list(c_list); // create an array from std::forward_list - std::forward_list c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; + std::forward_list c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; json j_flist(c_flist); // create an array from std::array diff --git a/doc/examples/from_bson.cpp b/doc/examples/from_bson.cpp index df71dd37e..c9d9fdfa6 100644 --- a/doc/examples/from_bson.cpp +++ b/doc/examples/from_bson.cpp @@ -7,11 +7,11 @@ using json = nlohmann::json; int main() { // create byte vector - std::vector 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 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); diff --git a/doc/examples/from_cbor.cpp b/doc/examples/from_cbor.cpp index e49987e55..e685329ef 100644 --- a/doc/examples/from_cbor.cpp +++ b/doc/examples/from_cbor.cpp @@ -7,10 +7,10 @@ using json = nlohmann::json; int main() { // create byte vector - std::vector v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63, - 0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d, - 0x61, 0x00 - }; + std::vector 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); diff --git a/doc/examples/from_msgpack.cpp b/doc/examples/from_msgpack.cpp index 67fdc454b..5c2183f1e 100644 --- a/doc/examples/from_msgpack.cpp +++ b/doc/examples/from_msgpack.cpp @@ -7,10 +7,10 @@ using json = nlohmann::json; int main() { // create byte vector - std::vector v = {0x82, 0xa7, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63, - 0x74, 0xc3, 0xa6, 0x73, 0x63, 0x68, 0x65, 0x6d, - 0x61, 0x00 - }; + std::vector 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); diff --git a/doc/examples/from_ubjson.cpp b/doc/examples/from_ubjson.cpp index a84057ed1..1e85e4e36 100644 --- a/doc/examples/from_ubjson.cpp +++ b/doc/examples/from_ubjson.cpp @@ -7,10 +7,10 @@ using json = nlohmann::json; int main() { // create byte vector - std::vector 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 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); diff --git a/doc/examples/meta.output b/doc/examples/meta.output index 9a18fab3a..9cc5cf29f 100644 --- a/doc/examples/meta.output +++ b/doc/examples/meta.output @@ -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++", diff --git a/doc/examples/parse__contiguouscontainer__parser_callback_t.cpp b/doc/examples/parse__contiguouscontainer__parser_callback_t.cpp index 14cfa2514..6eb409bba 100644 --- a/doc/examples/parse__contiguouscontainer__parser_callback_t.cpp +++ b/doc/examples/parse__contiguouscontainer__parser_callback_t.cpp @@ -7,7 +7,7 @@ using json = nlohmann::json; int main() { // a JSON text given as std::vector - std::vector text = {'[', '1', ',', '2', ',', '3', ']', '\0'}; + std::vector text = {'[', '1', ',', '2', ',', '3', ']', '\0'}; // parse and serialize JSON json j_complete = json::parse(text); diff --git a/doc/examples/parse__iteratortype__parser_callback_t.cpp b/doc/examples/parse__iteratortype__parser_callback_t.cpp deleted file mode 100644 index 1b5e23d4e..000000000 --- a/doc/examples/parse__iteratortype__parser_callback_t.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include - -using json = nlohmann::json; - -int main() -{ - // a JSON text given as std::vector - std::vector 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"; -} diff --git a/doc/examples/parse__iteratortype__parser_callback_t.output b/doc/examples/parse__iteratortype__parser_callback_t.output deleted file mode 100644 index 74633e808..000000000 --- a/doc/examples/parse__iteratortype__parser_callback_t.output +++ /dev/null @@ -1,6 +0,0 @@ -[ - 1, - 2, - 3 -] - diff --git a/doc/examples/to_bson.cpp b/doc/examples/to_bson.cpp index fd2884f13..2dd448768 100644 --- a/doc/examples/to_bson.cpp +++ b/doc/examples/to_bson.cpp @@ -10,7 +10,7 @@ int main() json j = R"({"compact": true, "schema": 0})"_json; // serialize it to BSON - std::vector v = json::to_bson(j); + std::vector v = json::to_bson(j); // print the vector content for (auto& byte : v) diff --git a/doc/examples/to_cbor.cpp b/doc/examples/to_cbor.cpp index 1237ca86f..3d85deb5a 100644 --- a/doc/examples/to_cbor.cpp +++ b/doc/examples/to_cbor.cpp @@ -10,7 +10,7 @@ int main() json j = R"({"compact": true, "schema": 0})"_json; // serialize it to CBOR - std::vector v = json::to_cbor(j); + std::vector v = json::to_cbor(j); // print the vector content for (auto& byte : v) diff --git a/doc/examples/to_msgpack.cpp b/doc/examples/to_msgpack.cpp index 99cc9eda2..76bb22f14 100644 --- a/doc/examples/to_msgpack.cpp +++ b/doc/examples/to_msgpack.cpp @@ -10,7 +10,7 @@ int main() json j = R"({"compact": true, "schema": 0})"_json; // serialize it to MessagePack - std::vector v = json::to_msgpack(j); + std::vector v = json::to_msgpack(j); // print the vector content for (auto& byte : v) diff --git a/doc/examples/to_ubjson.cpp b/doc/examples/to_ubjson.cpp index 06b2abba8..2329cdd05 100644 --- a/doc/examples/to_ubjson.cpp +++ b/doc/examples/to_ubjson.cpp @@ -23,7 +23,7 @@ int main() json j = R"({"compact": true, "schema": false})"_json; // serialize it to UBJSON - std::vector v = json::to_ubjson(j); + std::vector 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 v_array = json::to_ubjson(array); + std::vector v_array = json::to_ubjson(array); // serialize it to UBJSON using size optimization - std::vector v_array_size = json::to_ubjson(array, true); + std::vector v_array_size = json::to_ubjson(array, true); // serialize it to UBJSON using type optimization - std::vector v_array_size_and_type = json::to_ubjson(array, true, true); + std::vector v_array_size_and_type = json::to_ubjson(array, true, true); // print the vector contents for (auto& byte : v_array) diff --git a/doc/mkdocs/docs/api/basic_json/operator_eq.md b/doc/mkdocs/docs/api/basic_json/operator_eq.md index 027444acd..49f96b1c0 100644 --- a/doc/mkdocs/docs/api/basic_json/operator_eq.md +++ b/doc/mkdocs/docs/api/basic_json/operator_eq.md @@ -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. diff --git a/doc/mkdocs/docs/api/basic_json/operator_ne.md b/doc/mkdocs/docs/api/basic_json/operator_ne.md index 477d40ffc..e94da9b76 100644 --- a/doc/mkdocs/docs/api/basic_json/operator_ne.md +++ b/doc/mkdocs/docs/api/basic_json/operator_ne.md @@ -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 diff --git a/doc/mkdocs/scripts/check_structure.py b/doc/mkdocs/scripts/check_structure.py index 8f0379209..cacc51cb4 100644 --- a/doc/mkdocs/scripts/check_structure.py +++ b/doc/mkdocs/scripts/check_structure.py @@ -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()