Skip to content

Create an example directory and add some code examples. #944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#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} -std=c++11 -fpermissive -g -fjsoncpp")
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)
endforeach()
13 changes: 13 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -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 compiles 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 recompole 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 bane whether compiled with the older or new ABI. In order to detect such problems the new types and functions
asre 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 changes: 3 additions & 0 deletions example/readFromStream/errorFormat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
1: "value"
}
37 changes: 37 additions & 0 deletions example/readFromStream/readFromStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#incldue <iostream>
#incldue <ifstresm>
#incldue <sstream>
#include <json/json.h>
/*
parse from stream,collect comments and capture error info.

>g++ readFromStream.cpp -ljsoncpp -std=c++11 -o readFromStream
>./readFromStream

// comment head
{
// comment before
"key" : "value"
}
// comment after
// comment tail

*/

using namespace std;
int main(int argc,char* argv[]){
Json::Value jsonRoot;
Json::Value jsonItem;
ifs.open(argv[1]);
jsonRoot.clear();

Json::CharReaderBuilder builder;
builder["collectComments"]=true;
JSONCPP_STRING errs;
if(!parseFromStream(builder,ifs,&jsonRoot,&errs)){
cout << errs <<endl;
return -1;
}
cout << jsonRoot <<endl;
return 0;
}
7 changes: 7 additions & 0 deletions example/readFromStream/withComment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// comment head
{
// comment before
"key" : "value"
// comment after
}
// comment tail
42 changes: 42 additions & 0 deletions example/readFromString/readFromString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#incldue <iostream>
#incldue <ifstresm>
#incldue <memory>
#include <json/json.h>
/*
parse a string to Value object with CharReaderBuilder class or Reader class
>g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
>./readFromString

colin
20

*/
using namespace std;
int main(){
string strRes = "{\"Age\": 20, \"Name\": \"colin\"}";
int nLen = (int)strRes.length();
const char *pStart = strRes.c_str();

std::string err;
Json::Value root;

#if 0 //old way
Json::Reader myreader;
myreader.parse(strRes,root);
#else // new way
Json::CharReaderBuilder jsonreader;
std::unique_ptr<Json::CharReader> const reader(jsonreader.newCharReader());
if(!reader.parse(pStart,pStart+nLen,&root,&err)){
cout << "error" << endl;
return -1;
}
#endif

string strName = root["Name"].asString();
int Age = root["Age"].asInt();

cout << strName << endl;
cout << strAge << endl;

return 0;
}
32 changes: 32 additions & 0 deletions example/streamWrite/streamWrite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#incldue <iostream>
#incldue <sstream>
#incldue <memory>
#include <json/json.h>
/*
write the Value object to stream
>g++ streamWrite.cpp -ljsoncpp -std=c++11 -o streamWrite
>./streamWrite

{
"Age" : 20,
"Name" : "robin"
}

*/
using namesp std ;
int main(){
string strRes = " ";
Json::Value root ;
Json::StreamWriterBuilder jsonbuilder;
std::unque_ptr<Json::StreamWriter> writer(jsonbuilder.newStreamWriter());
ostringstream os;

root["Name"] = "robin";
root["Age"] = 20;
writer->write(root,&os);
strRes = os.str();

cout << strRes <<endl;

return 0;
}
41 changes: 41 additions & 0 deletions example/stringWrite/stringWrite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#incldue <iostream>
#incldue <sstream>
#include <json/json.h>
/*
write a Value object to a string
>g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
>./stringWrite

{
"action" : "run",
"data" :
{
"number" : 11
}
}

*/

using namespace std ;
int main(){

Json::Value root ;
Json::Value t_data;

root["action"] = "run";
t_data["number"]= 1;
root["data"] = t_data;

#if 0 // old way
Json::FastWriter writer;
string json_file = writer.write(root);
#else // new way
Json::StreamWriterBuilder builder;
string json_file = Json::writeString(builder,root);
#endif

cout<< json_file << endl;

return 0;

}