Skip to content
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

update readFromStream example #1572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 30 additions & 5 deletions example/readFromStream/readFromStream.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
#include "json/json.h"
#include <fstream>
#include <iostream>
/** \brief Parse from stream, collect comments and capture error info.
/** \brief Parse from stream, collect comments, access data, and capture error info.
* Example Usage:
* $g++ readFromStream.cpp -ljsoncpp -std=c++11 -o readFromStream
* $./readFromStream
* // comment head
* {
* [
* // comment before
* "key" : "value"
* }
* {
* "key" :
* {
* "id" : 1,
* "val" : "value 1"
* }
* },
* {
* "key" :
* {
* "id" : 2,
* "val" : "value 2"
* }
* }
* ] // comment tail
* // comment after
* // comment tail
* 1
* value 1
* 2
* value 2
*/
int main(int argc, char* argv[]) {
Json::Value root;
Expand All @@ -26,5 +42,14 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE;
}
std::cout << root << std::endl;

for (Json::Value::const_iterator it = root.begin(); it != root.end(); ++it) {
int id = it->get("key", "").get("id", "").asInt();
std::string val = it->get("key", "").get("val", "").asString();

std::cout << id << std::endl;
std::cout << val << std::endl;
}

return EXIT_SUCCESS;
}
18 changes: 15 additions & 3 deletions example/readFromStream/withComment.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// comment head
{
[
// comment before
"key" : "value"
{
"key" : {
"id": 1,
"val": "value 1"
}

},
{
"key" : {
"id": 2,
"val": "value 2"
}
}
// comment after
}// comment tail
]// comment tail
Loading