Skip to content

Commit e9ccbe0

Browse files
dota17baylesj
authored andcommitted
Create an example directory and add some code examples. (open-source-parsers#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
1 parent 21e3d21 commit e9ccbe0

File tree

11 files changed

+178
-3
lines changed

11 files changed

+178
-3
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,5 @@ add_subdirectory( src )
187187
#install the includes
188188
add_subdirectory( include )
189189

190+
#install the example
191+
add_subdirectory( example )

example/CMakeLists.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#vim: et ts =4 sts = 4 sw = 4 tw = 0
2+
cmake_minimum_required(VERSION 3.1)
3+
4+
set(EXAMPLES
5+
readFromString
6+
readFromStream
7+
stringWrite
8+
streamWrite
9+
)
10+
add_definitions(-D_GLIBCXX_USE_CXX11_ABI)
11+
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
12+
13+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
14+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra ")
15+
else()
16+
add_definitions(
17+
-D_SCL_SECURE_NO_WARNINGS
18+
-D_CRT_SECURE_NO_WARNINGS
19+
-D_WIN32_WINNT=0x601
20+
-D_WINSOCK_DEPRECATED_NO_WARNINGS)
21+
endif()
22+
23+
foreach (example ${EXAMPLES})
24+
add_executable(${example} ${example}/${example}.cpp)
25+
target_include_directories(${example} PUBLIC ${CMAKE_SOURCE_DIR}/include)
26+
target_link_libraries(${example} jsoncpp_lib)
27+
endforeach()
28+
29+
add_custom_target(examples ALL DEPENDS ${EXAMPLES})

example/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
***NOTE***
2+
3+
If you get linker errors about undefined references to symbols that involve types in the `std::__cxx11` namespace or the tag
4+
`[abi:cxx11]` then it probably indicates that you are trying to link together object files that were compiled with different
5+
values for the _GLIBCXX_USE_CXX11_ABI marco. This commonly happens when linking to a third-party library that was compiled with
6+
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
7+
the old ABI,just like:
8+
**g++ stringWrite.cpp -ljsoncpp -std=c++11 -D_GLIBCXX_USE_CXX11_ABI=0 -o stringWrite**
9+
10+
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
11+
have the same mangled name whether compiled with the older or new ABI. In order to detect such problems, the new types and functions
12+
are annotated with the abi_tag attribute, allowing the compiler to warn about potential ABI incompatibilities in code using them.
13+
Those warnings can be enabled with the `-Wabi-tag` option.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
1: "value"
3+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "json/json.h"
2+
#include <fstream>
3+
#include <iostream>
4+
/** \brief Parse from stream, collect comments and capture error info.
5+
* Example Usage:
6+
* $g++ readFromStream.cpp -ljsoncpp -std=c++11 -o readFromStream
7+
* $./readFromStream
8+
* // comment head
9+
* {
10+
* // comment before
11+
* "key" : "value"
12+
* }
13+
* // comment after
14+
* // comment tail
15+
*/
16+
int main(int argc, char* argv[]) {
17+
Json::Value root;
18+
std::ifstream ifs;
19+
ifs.open(argv[1]);
20+
21+
Json::CharReaderBuilder builder;
22+
builder["collectComments"] = true;
23+
JSONCPP_STRING errs;
24+
if (!parseFromStream(builder, ifs, &root, &errs)) {
25+
std::cout << errs << std::endl;
26+
return EXIT_FAILURE;
27+
}
28+
std::cout << root << std::endl;
29+
return EXIT_SUCCESS;
30+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// comment head
2+
{
3+
// comment before
4+
"key" : "value"
5+
// comment after
6+
}// comment tail
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "json/json.h"
2+
#include <iostream>
3+
/**
4+
* \brief Parse a raw string into Value object using the CharReaderBuilder
5+
* class, or the legacy Reader class.
6+
* Example Usage:
7+
* $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
8+
* $./readFromString
9+
* colin
10+
* 20
11+
*/
12+
int main() {
13+
const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
14+
const int rawJsonLength = static_cast<int>(rawJson.length());
15+
constexpr bool shouldUseOldWay = false;
16+
JSONCPP_STRING err;
17+
Json::Value root;
18+
19+
if (shouldUseOldWay) {
20+
Json::Reader reader;
21+
reader.parse(rawJson, root);
22+
} else {
23+
Json::CharReaderBuilder builder;
24+
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
25+
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
26+
&err)) {
27+
std::cout << "error" << std::endl;
28+
return EXIT_FAILURE;
29+
}
30+
}
31+
const std::string name = root["Name"].asString();
32+
const int age = root["Age"].asInt();
33+
34+
std::cout << name << std::endl;
35+
std::cout << age << std::endl;
36+
return EXIT_SUCCESS;
37+
}

example/streamWrite/streamWrite.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "json/json.h"
2+
#include <iostream>
3+
/** \brief Write the Value object to a stream.
4+
* Example Usage:
5+
* $g++ streamWrite.cpp -ljsoncpp -std=c++11 -o streamWrite
6+
* $./streamWrite
7+
* {
8+
* "Age" : 20,
9+
* "Name" : "robin"
10+
* }
11+
*/
12+
int main() {
13+
Json::Value root;
14+
Json::StreamWriterBuilder builder;
15+
const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
16+
17+
root["Name"] = "robin";
18+
root["Age"] = 20;
19+
writer->write(root, &std::cout);
20+
21+
return EXIT_SUCCESS;
22+
}

example/stringWrite/stringWrite.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "json/json.h"
2+
#include <iostream>
3+
/** \brief Write a Value object to a string.
4+
* Example Usage:
5+
* $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
6+
* $./stringWrite
7+
* {
8+
* "action" : "run",
9+
* "data" :
10+
* {
11+
* "number" : 1
12+
* }
13+
* }
14+
*/
15+
int main() {
16+
Json::Value root;
17+
Json::Value data;
18+
constexpr bool shouldUseOldWay = false;
19+
root["action"] = "run";
20+
data["number"] = 1;
21+
root["data"] = data;
22+
23+
if (shouldUseOldWay) {
24+
Json::FastWriter writer;
25+
const std::string json_file = writer.write(root);
26+
std::cout << json_file << std::endl;
27+
} else {
28+
Json::StreamWriterBuilder builder;
29+
const std::string json_file = Json::writeString(builder, root);
30+
std::cout << json_file << std::endl;
31+
}
32+
return EXIT_SUCCESS;
33+
}

src/test_lib_json/jsontest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ void Runner::preventDialogOnCrash() {
378378
_CrtSetReportHook(&msvcrtSilentReportHook);
379379
#endif // if defined(_MSC_VER)
380380

381-
// @todo investigate this handler (for buffer overflow)
382-
// _set_security_error_handler
381+
// @todo investigate this handler (for buffer overflow)
382+
// _set_security_error_handler
383383

384384
#if defined(_WIN32)
385385
// Prevents the system from popping a dialog for debugging if the

src/test_lib_json/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2615,7 +2615,7 @@ JSONTEST_FIXTURE(IteratorTest, const) {
26152615
Json::Value const v;
26162616
JSONTEST_ASSERT_THROWS(
26172617
Json::Value::iterator it(v.begin()) // Compile, but throw.
2618-
);
2618+
);
26192619

26202620
Json::Value value;
26212621

0 commit comments

Comments
 (0)