Adjust JSON Pointer examples (#3622)

* 📝 adjust JSON Pointer examples

* 👷 add test for documentation

* 📝 note platform-dependent output on some examples
This commit is contained in:
Niels Lohmann 2022-07-29 14:28:37 +02:00 committed by GitHub
parent 6576c3f776
commit d1d79b930d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 86 additions and 56 deletions

View File

@ -147,3 +147,12 @@ jobs:
run: python -m pip install reuse run: python -m pip install reuse
- name: REUSE lint - name: REUSE lint
run: reuse lint run: reuse lint
ci_test_documentation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: cmake
run: cmake -S . -B build -DJSON_CI=On
- name: build
run: cmake --build build --target ci_test_documentation

View File

@ -934,6 +934,16 @@ add_custom_target(ci_icpc
COMMENT "Compile and test with ICPC" COMMENT "Compile and test with ICPC"
) )
###############################################################################
# test documentation
###############################################################################
add_custom_target(ci_test_documentation
COMMAND make check_output_portable -j8
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/docs
COMMENT "Check that all examples compile and create the desired output"
)
############################################################################### ###############################################################################
# Clean up all generated files. # Clean up all generated files.
############################################################################### ###############################################################################

View File

@ -11,13 +11,13 @@ EXAMPLES = $(wildcard examples/*.cpp)
# create output from a stand-alone example file # create output from a stand-alone example file
%.output: %.cpp %.output: %.cpp
make $(<:.cpp=) CPPFLAGS="-I $(SRCDIR)" CXXFLAGS="-std=c++11" $(MAKE) $(<:.cpp=) CPPFLAGS="-I $(SRCDIR)" CXXFLAGS="-std=c++11"
./$(<:.cpp=) > $@ ./$(<:.cpp=) > $@
rm $(<:.cpp=) rm $(<:.cpp=)
# compare created output with current output of the example files # compare created output with current output of the example files
%.test: %.cpp %.test: %.cpp
make $(<:.cpp=) CPPFLAGS="-I $(SRCDIR)" CXXFLAGS="-std=c++11" $(MAKE) $(<:.cpp=) CPPFLAGS="-I $(SRCDIR)" CXXFLAGS="-std=c++11"
./$(<:.cpp=) > $@ ./$(<:.cpp=) > $@
diff $@ $(<:.cpp=.output) diff $@ $(<:.cpp=.output)
rm $(<:.cpp=) $@ rm $(<:.cpp=) $@
@ -28,6 +28,10 @@ create_output: $(EXAMPLES:.cpp=.output)
# check output of all stand-alone example files # check output of all stand-alone example files
check_output: $(EXAMPLES:.cpp=.test) check_output: $(EXAMPLES:.cpp=.test)
# check output of all stand-alone example files (exclude files with platform-dependent output.)
# This target is used in the CI (ci_test_documentation).
check_output_portable: $(filter-out examples/meta.test examples/max_size.test examples/std_hash.test examples/basic_json__CompatibleType.test,$(EXAMPLES:.cpp=.test))
clean: clean:
rm -fr $(EXAMPLES:.cpp=) rm -fr $(EXAMPLES:.cpp=)
$(MAKE) clean -C docset $(MAKE) clean -C docset

View File

@ -10,6 +10,6 @@ int main()
json::json_pointer ptr2("/foo/0"); json::json_pointer ptr2("/foo/0");
// call empty() // call empty()
std::cout << "last reference token of " << ptr1 << " is " << ptr1.back() << '\n' std::cout << "last reference token of \"" << ptr1 << "\" is \"" << ptr1.back() << "\"\n"
<< "last reference token of " << ptr2 << " is " << ptr2.back() << std::endl; << "last reference token of \"" << ptr2 << "\" is \"" << ptr2.back() << "\"" << std::endl;
} }

View File

@ -1,2 +1,2 @@
last reference token of "/foo" is foo last reference token of "/foo" is "foo"
last reference token of "/foo/0" is 0 last reference token of "/foo/0" is "0"

View File

@ -13,8 +13,8 @@ int main()
// call empty() // call empty()
std::cout << std::boolalpha std::cout << std::boolalpha
<< ptr0 << ": " << ptr0.empty() << '\n' << "\"" << ptr0 << "\": " << ptr0.empty() << '\n'
<< ptr1 << ": " << ptr1.empty() << '\n' << "\"" << ptr1 << "\": " << ptr1.empty() << '\n'
<< ptr2 << ": " << ptr2.empty() << '\n' << "\"" << ptr2 << "\": " << ptr2.empty() << '\n'
<< ptr3 << ": " << ptr3.empty() << std::endl; << "\"" << ptr3 << "\": " << ptr3.empty() << std::endl;
} }

View File

@ -7,17 +7,17 @@ int main()
{ {
// create a JSON pointer // create a JSON pointer
json::json_pointer ptr("/foo"); json::json_pointer ptr("/foo");
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
// append a JSON Pointer // append a JSON Pointer
ptr /= json::json_pointer("/bar/baz"); ptr /= json::json_pointer("/bar/baz");
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
// append a string // append a string
ptr /= "fob"; ptr /= "fob";
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
// append an array index // append an array index
ptr /= 42; ptr /= 42;
std::cout << ptr << std::endl; std::cout << "\"" << ptr << "\"" << std::endl;
} }

View File

@ -9,11 +9,11 @@ int main()
json::json_pointer ptr("/foo"); json::json_pointer ptr("/foo");
// append a JSON Pointer // append a JSON Pointer
std::cout << ptr / json::json_pointer("/bar/baz") << '\n'; std::cout << "\"" << ptr / json::json_pointer("/bar/baz") << "\"\n";
// append a string // append a string
std::cout << ptr / "fob" << '\n'; std::cout << "\"" << ptr / "fob" << "\"\n";
// append an array index // append an array index
std::cout << ptr / 42 << std::endl; std::cout << "\"" << ptr / 42 << "\"" << std::endl;
} }

View File

@ -12,7 +12,7 @@ int main()
// call parent_pointer() // call parent_pointer()
std::cout << std::boolalpha std::cout << std::boolalpha
<< "parent of " << ptr1 << " is " << ptr1.parent_pointer() << '\n' << "parent of \"" << ptr1 << "\" is \"" << ptr1.parent_pointer() << "\"\n"
<< "parent of " << ptr2 << " is " << ptr2.parent_pointer() << '\n' << "parent of \"" << ptr2 << "\" is \"" << ptr2.parent_pointer() << "\"\n"
<< "parent of " << ptr3 << " is " << ptr3.parent_pointer() << std::endl; << "parent of \"" << ptr3 << "\" is \"" << ptr3.parent_pointer() << "\"" << std::endl;
} }

View File

@ -7,15 +7,15 @@ int main()
{ {
// create empty JSON Pointer // create empty JSON Pointer
json::json_pointer ptr("/foo/bar/baz"); json::json_pointer ptr("/foo/bar/baz");
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
// call pop_back() // call pop_back()
ptr.pop_back(); ptr.pop_back();
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
ptr.pop_back(); ptr.pop_back();
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
ptr.pop_back(); ptr.pop_back();
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
} }

View File

@ -7,15 +7,15 @@ int main()
{ {
// create empty JSON Pointer // create empty JSON Pointer
json::json_pointer ptr; json::json_pointer ptr;
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
// call push_back() // call push_back()
ptr.push_back("foo"); ptr.push_back("foo");
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
ptr.push_back("0"); ptr.push_back("0");
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
ptr.push_back("bar"); ptr.push_back("bar");
std::cout << ptr << '\n'; std::cout << "\"" << ptr << "\"\n";
} }

View File

@ -19,16 +19,16 @@ int main()
json::json_pointer ptr11("/ "); json::json_pointer ptr11("/ ");
json::json_pointer ptr12("/m~0n"); json::json_pointer ptr12("/m~0n");
std::cout << ptr1.to_string() << '\n' std::cout << "\"" << ptr1.to_string() << "\"\n"
<< ptr2.to_string() << '\n' << "\"" << ptr2.to_string() << "\"\n"
<< ptr3.to_string() << '\n' << "\"" << ptr3.to_string() << "\"\n"
<< ptr4.to_string() << '\n' << "\"" << ptr4.to_string() << "\"\n"
<< ptr5.to_string() << '\n' << "\"" << ptr5.to_string() << "\"\n"
<< ptr6.to_string() << '\n' << "\"" << ptr6.to_string() << "\"\n"
<< ptr7.to_string() << '\n' << "\"" << ptr7.to_string() << "\"\n"
<< ptr8.to_string() << '\n' << "\"" << ptr8.to_string() << "\"\n"
<< ptr9.to_string() << '\n' << "\"" << ptr9.to_string() << "\"\n"
<< ptr10.to_string() << '\n' << "\"" << ptr10.to_string() << "\"\n"
<< ptr11.to_string() << '\n' << "\"" << ptr11.to_string() << "\"\n"
<< ptr12.to_string() << std::endl; << "\"" << ptr12.to_string() << "\"" << std::endl;
} }

View File

@ -1,12 +1,12 @@
""
/foo "/foo"
/foo/0 "/foo/0"
/ "/"
/a~1b "/a~1b"
/c%d "/c%d"
/e^f "/e^f"
/g|h "/g|h"
/i\j "/i\j"
/k"l "/k"l"
/ "/ "
/m~0n "/m~0n"

View File

@ -30,5 +30,5 @@ int main()
doc.patch_inplace(patch); doc.patch_inplace(patch);
// output patched document // output patched document
std::cout << "After\n" << std::setw(4) << doc << std::endl; std::cout << "\nAfter\n" << std::setw(4) << doc << std::endl;
} }

View File

@ -317,6 +317,8 @@ basic_json(basic_json&& other) noexcept;
--8<-- "examples/basic_json__CompatibleType.output" --8<-- "examples/basic_json__CompatibleType.output"
``` ```
Note the output is platform-dependent.
??? example "Example: (5) create a container (array or object) from an initializer list" ??? example "Example: (5) create a container (array or object) from an initializer list"
The example below shows how JSON values are created from initializer lists. The example below shows how JSON values are created from initializer lists.

View File

@ -40,7 +40,7 @@ string elements the JSON value can store which is `1`.
??? example ??? example
The following code calls `max_size()` on the different value types. Note the output is implementation specific. The following code calls `max_size()` on the different value types.
```cpp ```cpp
--8<-- "examples/max_size.cpp" --8<-- "examples/max_size.cpp"
@ -52,6 +52,8 @@ string elements the JSON value can store which is `1`.
--8<-- "examples/max_size.output" --8<-- "examples/max_size.output"
``` ```
Note the output is platform-dependent.
## Version history ## Version history
- Added in version 1.0.0. - Added in version 1.0.0.

View File

@ -32,8 +32,7 @@ Constant.
??? example ??? example
The following code shows an example output of the `meta()` The following code shows an example output of the `meta()` function.
function.
```cpp ```cpp
--8<-- "examples/meta.cpp" --8<-- "examples/meta.cpp"
@ -45,6 +44,8 @@ Constant.
--8<-- "examples/meta.output" --8<-- "examples/meta.output"
``` ```
Note the output is platform-dependent.
## See also ## See also
- [**NLOHMANN_JSON_VERSION_MAJOR**/**NLOHMANN_JSON_VERSION_MINOR**/**NLOHMANN_JSON_VERSION_PATCH**](../macros/nlohmann_json_version_major.md) - library version information - [**NLOHMANN_JSON_VERSION_MAJOR**/**NLOHMANN_JSON_VERSION_MINOR**/**NLOHMANN_JSON_VERSION_PATCH**](../macros/nlohmann_json_version_major.md) - library version information

View File

@ -26,6 +26,8 @@ type of the JSON value is taken into account to have different hash values for `
--8<-- "examples/std_hash.output" --8<-- "examples/std_hash.output"
``` ```
Note the output is platform-dependent.
## Version history ## Version history
- Added in version 1.0.0. - Added in version 1.0.0.