From e9ccbe01453908cd1753dbe882ffe5349f82cd93 Mon Sep 17 00:00:00 2001 From: dota17 <50514813+dota17@users.noreply.github.com> Date: Wed, 18 Sep 2019 04:30:00 +0800 Subject: [PATCH] Create an example directory and add some code examples. (#944) * update example directory * modify some compile error. * update with clang-format * update * update * add_definitions("../include/json") # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Wed Jul 10 21:26:16 2019 +0800 # # On branch code_example # Your branch is up-to-date with 'origin/code_example'. # # Changes to be committed: # modified: example/CMakeLists.txt # * change CMakeLists.txt * update streamWrite.cpp * update * Update readFromStream.cpp * fix typo --- CMakeLists.txt | 2 ++ example/CMakeLists.txt | 29 ++++++++++++++++++ example/README.md | 13 ++++++++ example/readFromStream/errorFormat.json | 3 ++ example/readFromStream/readFromStream.cpp | 30 ++++++++++++++++++ example/readFromStream/withComment.json | 6 ++++ example/readFromString/readFromString.cpp | 37 +++++++++++++++++++++++ example/streamWrite/streamWrite.cpp | 22 ++++++++++++++ example/stringWrite/stringWrite.cpp | 33 ++++++++++++++++++++ src/test_lib_json/jsontest.cpp | 4 +-- src/test_lib_json/main.cpp | 2 +- 11 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 example/CMakeLists.txt create mode 100644 example/README.md create mode 100644 example/readFromStream/errorFormat.json create mode 100644 example/readFromStream/readFromStream.cpp create mode 100644 example/readFromStream/withComment.json create mode 100644 example/readFromString/readFromString.cpp create mode 100644 example/streamWrite/streamWrite.cpp create mode 100644 example/stringWrite/stringWrite.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b4eb830..9f2bab5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -187,3 +187,5 @@ add_subdirectory( src ) #install the includes add_subdirectory( include ) +#install the example +add_subdirectory( example ) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..c8eba53 --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,29 @@ +#vim: et ts =4 sts = 4 sw = 4 tw = 0 +cmake_minimum_required(VERSION 3.1) + +set(EXAMPLES + readFromString + readFromStream + stringWrite + streamWrite + ) +add_definitions(-D_GLIBCXX_USE_CXX11_ABI) +set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS}) + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra ") +else() + add_definitions( + -D_SCL_SECURE_NO_WARNINGS + -D_CRT_SECURE_NO_WARNINGS + -D_WIN32_WINNT=0x601 + -D_WINSOCK_DEPRECATED_NO_WARNINGS) +endif() + +foreach (example ${EXAMPLES}) + add_executable(${example} ${example}/${example}.cpp) + target_include_directories(${example} PUBLIC ${CMAKE_SOURCE_DIR}/include) + target_link_libraries(${example} jsoncpp_lib) +endforeach() + +add_custom_target(examples ALL DEPENDS ${EXAMPLES}) diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..b1ae4c8 --- /dev/null +++ b/example/README.md @@ -0,0 +1,13 @@ +***NOTE*** + +If you get linker errors about undefined references to symbols that involve types in the `std::__cxx11` namespace or the tag +`[abi:cxx11]` then it probably indicates that you are trying to link together object files that were compiled with different +values for the _GLIBCXX_USE_CXX11_ABI marco. This commonly happens when linking to a third-party library that was compiled with +an older version of GCC. If the third-party library cannot be rebuilt with the new ABI, then you need to recompile your code with +the old ABI,just like: +**g++ stringWrite.cpp -ljsoncpp -std=c++11 -D_GLIBCXX_USE_CXX11_ABI=0 -o stringWrite** + +Not all of uses of the new ABI will cause changes in symbol names, for example a class with a `std::string` member variable will +have the same mangled name whether compiled with the older or new ABI. In order to detect such problems, the new types and functions +are annotated with the abi_tag attribute, allowing the compiler to warn about potential ABI incompatibilities in code using them. +Those warnings can be enabled with the `-Wabi-tag` option. diff --git a/example/readFromStream/errorFormat.json b/example/readFromStream/errorFormat.json new file mode 100644 index 0000000..2d13290 --- /dev/null +++ b/example/readFromStream/errorFormat.json @@ -0,0 +1,3 @@ +{ + 1: "value" +} \ No newline at end of file diff --git a/example/readFromStream/readFromStream.cpp b/example/readFromStream/readFromStream.cpp new file mode 100644 index 0000000..358d2ca --- /dev/null +++ b/example/readFromStream/readFromStream.cpp @@ -0,0 +1,30 @@ +#include "json/json.h" +#include +#include +/** \brief Parse from stream, collect comments and capture error info. + * Example Usage: + * $g++ readFromStream.cpp -ljsoncpp -std=c++11 -o readFromStream + * $./readFromStream + * // comment head + * { + * // comment before + * "key" : "value" + * } + * // comment after + * // comment tail + */ +int main(int argc, char* argv[]) { + Json::Value root; + std::ifstream ifs; + ifs.open(argv[1]); + + Json::CharReaderBuilder builder; + builder["collectComments"] = true; + JSONCPP_STRING errs; + if (!parseFromStream(builder, ifs, &root, &errs)) { + std::cout << errs << std::endl; + return EXIT_FAILURE; + } + std::cout << root << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/readFromStream/withComment.json b/example/readFromStream/withComment.json new file mode 100644 index 0000000..7a8c828 --- /dev/null +++ b/example/readFromStream/withComment.json @@ -0,0 +1,6 @@ +// comment head +{ + // comment before + "key" : "value" + // comment after +}// comment tail diff --git a/example/readFromString/readFromString.cpp b/example/readFromString/readFromString.cpp new file mode 100644 index 0000000..753f9c9 --- /dev/null +++ b/example/readFromString/readFromString.cpp @@ -0,0 +1,37 @@ +#include "json/json.h" +#include +/** + * \brief Parse a raw string into Value object using the CharReaderBuilder + * class, or the legacy Reader class. + * Example Usage: + * $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString + * $./readFromString + * colin + * 20 + */ +int main() { + const std::string rawJson = R"({"Age": 20, "Name": "colin"})"; + const int rawJsonLength = static_cast(rawJson.length()); + constexpr bool shouldUseOldWay = false; + JSONCPP_STRING err; + Json::Value root; + + if (shouldUseOldWay) { + Json::Reader reader; + reader.parse(rawJson, root); + } else { + Json::CharReaderBuilder builder; + const std::unique_ptr reader(builder.newCharReader()); + if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root, + &err)) { + std::cout << "error" << std::endl; + return EXIT_FAILURE; + } + } + const std::string name = root["Name"].asString(); + const int age = root["Age"].asInt(); + + std::cout << name << std::endl; + std::cout << age << std::endl; + return EXIT_SUCCESS; +} diff --git a/example/streamWrite/streamWrite.cpp b/example/streamWrite/streamWrite.cpp new file mode 100644 index 0000000..6f7f797 --- /dev/null +++ b/example/streamWrite/streamWrite.cpp @@ -0,0 +1,22 @@ +#include "json/json.h" +#include +/** \brief Write the Value object to a stream. + * Example Usage: + * $g++ streamWrite.cpp -ljsoncpp -std=c++11 -o streamWrite + * $./streamWrite + * { + * "Age" : 20, + * "Name" : "robin" + * } + */ +int main() { + Json::Value root; + Json::StreamWriterBuilder builder; + const std::unique_ptr writer(builder.newStreamWriter()); + + root["Name"] = "robin"; + root["Age"] = 20; + writer->write(root, &std::cout); + + return EXIT_SUCCESS; +} diff --git a/example/stringWrite/stringWrite.cpp b/example/stringWrite/stringWrite.cpp new file mode 100644 index 0000000..d501ba9 --- /dev/null +++ b/example/stringWrite/stringWrite.cpp @@ -0,0 +1,33 @@ +#include "json/json.h" +#include +/** \brief Write a Value object to a string. + * Example Usage: + * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite + * $./stringWrite + * { + * "action" : "run", + * "data" : + * { + * "number" : 1 + * } + * } + */ +int main() { + Json::Value root; + Json::Value data; + constexpr bool shouldUseOldWay = false; + root["action"] = "run"; + data["number"] = 1; + root["data"] = data; + + if (shouldUseOldWay) { + Json::FastWriter writer; + const std::string json_file = writer.write(root); + std::cout << json_file << std::endl; + } else { + Json::StreamWriterBuilder builder; + const std::string json_file = Json::writeString(builder, root); + std::cout << json_file << std::endl; + } + return EXIT_SUCCESS; +} diff --git a/src/test_lib_json/jsontest.cpp b/src/test_lib_json/jsontest.cpp index c0b5296..4c59d07 100644 --- a/src/test_lib_json/jsontest.cpp +++ b/src/test_lib_json/jsontest.cpp @@ -378,8 +378,8 @@ void Runner::preventDialogOnCrash() { _CrtSetReportHook(&msvcrtSilentReportHook); #endif // if defined(_MSC_VER) - // @todo investigate this handler (for buffer overflow) - // _set_security_error_handler +// @todo investigate this handler (for buffer overflow) +// _set_security_error_handler #if defined(_WIN32) // Prevents the system from popping a dialog for debugging if the diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index b0106bd..4897f8c 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -2615,7 +2615,7 @@ JSONTEST_FIXTURE(IteratorTest, const) { Json::Value const v; JSONTEST_ASSERT_THROWS( Json::Value::iterator it(v.begin()) // Compile, but throw. - ); + ); Json::Value value;