0

To all those that are familiar with rapidjson i have the following issue:
I have a certain function that accepts as parameter a date and if that date exists in the json file the method does some operations and if not some other operations.
Generally it looks like this: (not actual code more like pseudo)

Function:

void updateData(string date) {
    //
    //code to turn date from string to const char* (tested)
    //
    if (v.HasMember(date)) { //v is a value
        Value d;
        d=v[date];
        //
        //code that involves getting data from d (d is object) using HasMember
        //
    } else {
        //generic code that has nothing to do with json
    }

JSON file:

{
    "main": {
        "v": {
           "2014-10-02" : {
                //some fields
           },
           "2014-10-03" : {
                //some fields
           }
         }
     }    
}

So the first time that i call updateData for date "2014-10-02" it runs correctly(executes the if part).
The problem is when i call updateData for another date (like "2014-10-03" that is supposed to work well) it always executes the wrong part(else part) and even when i switch back to the first date it still executes the else part. (while popping many assertions (mostly isString())).
So is HasMember really the problem here, in the sense that it is maybe altering the object?
And are there any alternative ways to search for a member, other than that?
Any tip is appreciated...

loukwn
  • 174
  • 2
  • 14

1 Answers1

1

Its hard to tell without the actual code, but I think problem might be that you are treating "v" as a Value instead of an Object. "v" isn't a value, its the name of the object. So what you have is a nested object. In order to do this I think you would have to used MemberIterators and iterate through the child objects in the v object.

rapidjson has a pretty good example on how to use iterators.

there is also this question here, which has a pretty good answer on how to use nested objects

Retrieving a nested object inside a JSON string using rapidjson

Community
  • 1
  • 1
Mike Howard
  • 333
  • 1
  • 2
  • 11
  • so basically the only way to interact with an object's members is with iterators? – loukwn Oct 02 '14 at 21:28
  • For nested objects this is only way I've got it to work, although in my case, it also happens to be the way that make the most sense. – Mike Howard Oct 02 '14 at 23:25