graham.reeds
graham.reeds

Reputation: 16476

Convert a MemberIterator to Value

In C++ you can easily get the underlying value from the iterator. ie:

std::vector<int> numbers = { 1, 2, 3, 4, 5 };

for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); ++it) {
    int i = *it;
    std::cout << "i = " << i << std::endl;
}

However using rapidjson I am having trouble in getting the underlying value from ConstMemberIterator:

rapidjson::Value* values;
for (rapidjson::Value::ConstMemberIterator it = values->MemberBegin(); it != values->MemberEnd(); ++it) {
    rapidjson::Value v = *it; // ERROR!
}

error: conversion from ‘rapidjson::GenericMemberIterator<true, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocatorrapidjson::CrtAllocator >::ValueType {aka const rapidjson::GenericMemberrapidjson::UTF8<, rapidjson::MemoryPoolAllocatorrapidjson::CrtAllocator >}’ to non-scalar type ‘rapidjson::Value {aka rapidjson::GenericValuerapidjson::UTF8< >}’ requested rapidjson::Value v = *it;

How do I get the underlying value so I can do further operations on it?

The only way I have found is to convert the value to a string and then reparse the string as a document and go in via document[it->name.GetString()] since there also seemingly no way to convert a value to a document.

Upvotes: 3

Views: 1666

Answers (1)

Jean-Benoit Harvey
Jean-Benoit Harvey

Reputation: 404

If you're using C++11, you could try and use auto...

rapidjson::Value* values;
for (rapidjson::Value::ConstMemberIterator it = values->MemberBegin(); it != values->MemberEnd(); ++it) {
    auto v = *it;
}

Upvotes: 2

Related Questions