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

Implement iterator for JSONHashTable and JSONArray #4

Closed
shreyasbharath opened this issue Jul 3, 2014 · 3 comments
Closed

Implement iterator for JSONHashTable and JSONArray #4

shreyasbharath opened this issue Jul 3, 2014 · 3 comments
Assignees
Milestone

Comments

@shreyasbharath
Copy link

Something like this -

for ( auto i = jsonHashTable.begin(), i != jsonHashTable.end(); i++ ) {
    cout << "Key " << i->first;
    cout << "Value" << i->second;
}

for ( auto i = jsonArray.begin(), i != jsonArray.end(); i++ ) {
    cout << "Value" << *i;
}

I think this will involve writing a custom iterator (see http://www.cplusplus.com/reference/iterator/) for these two classes, hopefully it isn't too difficult.

@bblanchon bblanchon added this to the 3.0 milestone Jul 19, 2014
@bblanchon bblanchon self-assigned this Jul 19, 2014
@bblanchon
Copy link
Owner

The original design of the parser API doesn't allow such feature.
So I implemented this feature as a part of a new API for the parser.

Old parser API:

JsonHashTable root = parser.parseHashTable(json);

char*  sensor    = root.getString("sensor");
long   time      = root.getLong("time");
double latitude  = root.getArray("data").getDouble(0);
double longitude = root.getArray("data").getDouble(1);

New parser API:

JsonObject root = parser.parse(json);

char*  sensor    = root["sensor"];
long   time      = root["time"];
double latitude  = root["data"][0];
double longitude = root["data"][1];

Iterators, with C++11 syntax:

for (auto i : jsonHashTable) {
    cout << "Key " << i.key();
    cout << "Value" << (const char*)i->value();
}

for (const char* i : jsonArray) {
    cout << "Value" << i;
}

The code is available in the new-parser-api branch

Please give me your feedback.

@bblanchon bblanchon modified the milestone: 3.0 Jul 24, 2014
@bblanchon
Copy link
Owner

I merged the branch into master, it's now part of version 3.0

@shreyasbharath
Copy link
Author

Thank you, we will upgrade to the new version soon!

Repository owner locked and limited conversation to collaborators Sep 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants