wangxiaowei0303 / rapidjson

Automatically exported from code.google.com/p/rapidjson
MIT License
0 stars 0 forks source link

Error getting members with object values smth #24

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I have the following json text:
{
    "type1": 6,
    "name": "widget",
    "fields": {
        "sku": {
            "type2": 0,
            "value": 5
        }
    }
}

2. I parse the entire text with no errors and save it as "Document d;"
3. Next when I access the fields of the root object, I get ->

//Make it easier to transfer over as method arg;
const rapidjson::Value *src = &d;

--> if (!src->HasMember("name")) { Error; } // This is ok.
--> const String name((*src)["name"].GetString()); // This is ok.

--> const rapidjson::Value& fields = (*src)["fields"]; // This is ok.
--> if (!fields.IsObject()) { Error; } // This is ok.
--> assert(fields.HasMember("sku")) // This fails.

gdb log for the fields object says:

$25 = (rapidjson::Value &) @0xdd2644: {
  data_ = {
    s = {
      str = 0xdd25e0 "\270", <incomplete sequence \335>, 
      length = 0, 
      hashcode = 0
    }, 
    n = {
      {
        i = 14493152, 
        padding = "\000\000\000"
      }, 
      {
        u = 14493152, 
        padding2 = "\000\000\000"
      }, 
      i64 = 14493152, 
      u64 = 14493152, 
      d = 7.160568503155354e-317
    }, 
    o = {
      members = 0xdd25e0, 
      size = 0, 
      capacity = 0
    }, 
    a = {
      elements = 0xdd25e0, 
      size = 0, 
      capacity = 0
    }
}, 
  flags_ = 3, 
  static kDefaultArrayCapacity = <optimized out>, 
  static kDefaultObjectCapacity = <optimized out>
}

The size of the members here is 0, but if I try:

for (rapidjson::Value::ConstMemberIterator mIt = fields.MemberEnd();
     mIt != fields.MemberBegin();
     --mIt) {

   const rapidjson::Value::Member& member = *mIt;
}

Here member.name.GetString() returns "type2", which is the inner type(inside 
the nested object). Somehow "sku" name is unusable. 

What version of the product are you using? On what operating system?
Built on ubuntu 11.10 x64 using both clang and gcc.

I also noticed a similar problem with the array value:

{
   "type1" : 7,
   "value1" : [
      {
         "type2" : 5,
         "value2" : "a string"
      }
   ]
}

Here when I get value1 using:
const rapidjson::Value& v1 = (*src)["value1"]; // This is ok.
if (!fields.IsArray()) { Error; } // This is ok.

for (rapidjson::Value::ConstValueIterator vIt = v1.End();
     vIt != v1.Begin();
     --vIt) {
    const rapidjson::Value& jsonE = *vIt;
}

Here jsonE value when printed shows that it has no flags, meaning the field 
flags_ is 0.
As far as I saw there is some actual content there but the object itself cannot 
be tested whether it is an object or not, since it always return false;

Could you please try these samples out.
Thanks,
    Petko.

Original issue reported on code.google.com by ppadev...@gmail.com on 8 May 2012 at 12:48

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
My mistake, the end should not be a valid element of the list/object.

Original comment by ppadev...@gmail.com on 10 May 2012 at 10:27

GoogleCodeExporter commented 9 years ago

Original comment by milo...@gmail.com on 10 May 2012 at 2:17