mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2024-12-13 10:22:55 +01:00
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
This commit is contained in:
parent
21e3d21243
commit
e9ccbe0145
@ -187,3 +187,5 @@ add_subdirectory( src )
|
|||||||
#install the includes
|
#install the includes
|
||||||
add_subdirectory( include )
|
add_subdirectory( include )
|
||||||
|
|
||||||
|
#install the example
|
||||||
|
add_subdirectory( example )
|
||||||
|
29
example/CMakeLists.txt
Normal file
29
example/CMakeLists.txt
Normal file
@ -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})
|
13
example/README.md
Normal file
13
example/README.md
Normal file
@ -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.
|
3
example/readFromStream/errorFormat.json
Normal file
3
example/readFromStream/errorFormat.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
1: "value"
|
||||||
|
}
|
30
example/readFromStream/readFromStream.cpp
Normal file
30
example/readFromStream/readFromStream.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include "json/json.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
/** \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;
|
||||||
|
}
|
6
example/readFromStream/withComment.json
Normal file
6
example/readFromStream/withComment.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// comment head
|
||||||
|
{
|
||||||
|
// comment before
|
||||||
|
"key" : "value"
|
||||||
|
// comment after
|
||||||
|
}// comment tail
|
37
example/readFromString/readFromString.cpp
Normal file
37
example/readFromString/readFromString.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include "json/json.h"
|
||||||
|
#include <iostream>
|
||||||
|
/**
|
||||||
|
* \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<int>(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<Json::CharReader> 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;
|
||||||
|
}
|
22
example/streamWrite/streamWrite.cpp
Normal file
22
example/streamWrite/streamWrite.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "json/json.h"
|
||||||
|
#include <iostream>
|
||||||
|
/** \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<Json::StreamWriter> writer(builder.newStreamWriter());
|
||||||
|
|
||||||
|
root["Name"] = "robin";
|
||||||
|
root["Age"] = 20;
|
||||||
|
writer->write(root, &std::cout);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
33
example/stringWrite/stringWrite.cpp
Normal file
33
example/stringWrite/stringWrite.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "json/json.h"
|
||||||
|
#include <iostream>
|
||||||
|
/** \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;
|
||||||
|
}
|
@ -378,8 +378,8 @@ void Runner::preventDialogOnCrash() {
|
|||||||
_CrtSetReportHook(&msvcrtSilentReportHook);
|
_CrtSetReportHook(&msvcrtSilentReportHook);
|
||||||
#endif // if defined(_MSC_VER)
|
#endif // if defined(_MSC_VER)
|
||||||
|
|
||||||
// @todo investigate this handler (for buffer overflow)
|
// @todo investigate this handler (for buffer overflow)
|
||||||
// _set_security_error_handler
|
// _set_security_error_handler
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
// Prevents the system from popping a dialog for debugging if the
|
// Prevents the system from popping a dialog for debugging if the
|
||||||
|
@ -2615,7 +2615,7 @@ JSONTEST_FIXTURE(IteratorTest, const) {
|
|||||||
Json::Value const v;
|
Json::Value const v;
|
||||||
JSONTEST_ASSERT_THROWS(
|
JSONTEST_ASSERT_THROWS(
|
||||||
Json::Value::iterator it(v.begin()) // Compile, but throw.
|
Json::Value::iterator it(v.begin()) // Compile, but throw.
|
||||||
);
|
);
|
||||||
|
|
||||||
Json::Value value;
|
Json::Value value;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user