0

I'm trying to compare rapidjson documents but it's giving an error message that I'm not sure how to fix (see above).

This is the rapidjson:

static const std::string& JSON()
    {
        static const std::string j =
        "{"
            "\"SimpleCompany:Manager\":{"
                "\"read\":\"true\""
                "\"update\":\"true\""
                "\"delete\":\"true\""
                "\"insert\":\"false\""
            "},"
            "\"SimpleCompany:Manager\":{"
                "\"read\":\"true\""
                "\"update\":\"true\""
                "\"delete\":\"false\""
                "\"insert\":\"false\""
            "},"
        "}";

        return j;
    }

This is where I try to compare two documents with presumably the same contents:

rapidjson::StringStream strStream(JSON().c_str());
rapidjson::Document origDocument;
origDocument.ParseStream(strStream); //newDocument obtained other way
ASSERT_TRUE(newDocument["read"] == origDocument["read"]); //error no operator [] matches these operands
ASSERT_TRUE(strcmp(newDocument["read"] , origDocument["read"])); //error no operator [] matches these operands
const rapidjson::Value& a1 = newDocument["read"]; //error no operator [] matches these operands

Any idea how to correctly compare their values? I tried two ways, but they didn't like the [.

Milo says that there's an equality operator. It looks like I'm comparing things like the rapidjson tutorial, although they are comparing the key with expected value, and I'm comparing keys of two documents for equality, which seems ok to me.

Community
  • 1
  • 1
Michele
  • 3,617
  • 12
  • 47
  • 81

2 Answers2

0

I ran into a similar issue. Things to check/try:

1) Do you have using namespace rapidjson;in your relevant codeblock? I assume not because I see you using rapidjson:: in your orgiDocument declaration. I believe part of this issue is as simple as a namespacing collision. (The compiler doesn't know you're trying to use rapidjson's overloaded []operator...I think.)

2) The second thing I'd suggest is to try using newDocument["read"].GetBool() to retrieve the value for the "read" key. I've been having issues trying to get my code to work while following that tutorial as well, and using those 'Get' methods are the only way I've been able to actually get values returned.

Hope this helps!

Mough
  • 56
  • 4
0
  1. What is the type of newDocument, and how do you create it? I tried running your code (by creating NewDocument the same way as origDocument) and the first == assertion worked. If newDocument is a regular rapidjson::Document, then the line const rapidjson::Value& a1 = newDocument["read"]; should compile.

  2. The second assertion is trying to strcmp two rapidjson Value objects, so I think it's expected that doesn't compile. You should instead get theconst char* contents from the values using .GetString() on each of them.

Just to make clear, what you want to do should work:

  • The operator[] for a Document (which is actually a subclass of GenericValue) returns a GenericValue object reference.

  • You can compare GenericValues with the expected operators == and !=. See the section "Equal-to and not-equal-to operators" in the docs.

FluxLemur
  • 1,827
  • 16
  • 18