Home > Mobile >  Unable to clone a JSON Array properly using C Poco library
Unable to clone a JSON Array properly using C Poco library

Time:09-21

I would like to clone json objects. In order to do the job, I've developped two functions. One for objects and one for arrays.

The method that's clone JSON::Object::Ptr works well. Here is the code

Poco::JSON::Object::Ptr CJsonHelper::cloneJson(Poco::JSON::Object::Ptr obj)
{
    Poco::JSON::Object::Iterator it;
    Poco::JSON::Object::Ptr ptr = new Poco::JSON::Object;
    // loop and copy
    for(it = obj->begin(); it != obj->end(); it  )
        ptr->set(it->first, it->second);

    return ptr;
}

The method that's clone JSON::Array::Ptr works but when I stringify the object that's contains the array, I got following error :

Can not convert to std::string

Here is the code of the function to clone array that's doesn't work

Poco::JSON::Array::Ptr CJsonHelper::cloneJson(Poco::JSON::Array::Ptr obj)
{
    Poco::JSON::Array::Ptr copy = new Poco::JSON::Array;
    // loop and copy
    for (auto item = obj->begin(); item != obj->end();   item)
    {
        copy->add(item);
    }

    return copy;
}

The object pass to function is previsoulsy parsed from faile and is valid

{
  "items" : [
  {
    "name" : "object0",
    "position" : {
      "x" : "700.0",
      "y" : "0.0",
      "z" : "250.0"
    } 
  }
  ]
}

In the clonse method if I replace the

copy->add(item);

by

copy->add(item->extract<Poco::JSON::Object::Ptr>());

it's working well but only in this case, I need it to be generic.

I'm sure I've missed something that I don't found because it is working well in clone object method.

CodePudding user response:

I've finally found my mistake !

I've missed a * to go through the auto iterator

copy->add(item);

becomes

copy->add(*item);

I post the updated function if it can help someone

Poco::JSON::Array::Ptr CJsonHelper::cloneJson(Poco::JSON::Array::Ptr obj)
{
    Poco::JSON::Array::Ptr copy = new Poco::JSON::Array;
    // loop and copy
    for (auto item = obj->begin(); item != obj->end();   item)
    {
        copy->add(*item);
    }

    return copy;
}
  • Related