Arduinojson: Use of Deleted Function - Using JsonObject as member variable

Created on 29 May 2016  路  4Comments  路  Source: bblanchon/ArduinoJson

Hello - I threw a small library on top of this to allow saving/loading configuration from a json file, and I was hoping to be able to expose a JsonObject through my config class so I could just interact with it as config.jsonObj["value"]... I was able to get the class working with the Buffer and Object defined within the init() and save() functions, but when I try to pull the Buffer and Object into member variables, it fails with the below errors.

  • use of deleted function 'ArduinoJson::JsonObject& ArduinoJson::JsonObject::operator=(const ArduinoJson::JsonObject&)'
  • 'ArduinoJson::JsonObject& ArduinoJson::JsonObject::operator=(const ArduinoJson::JsonObject&)' is implicitly deleted because the default definition would be ill-formed
  • 'ArduinoJson::Internals::ReferenceType& ArduinoJson::Internals::ReferenceType::operator=(const ArduinoJson::Internals::ReferenceType&)' is private

I have the whole code posted in a gist here https://gist.github.com/bepursuant/fc558c18f409271583fe3d3331fe8b5a , but the offending line is:

this->jsonObj = this->jsonBuf.parseObject(buf.get());

I'm sure my question stems from a misunderstanding of the buffers/scoping in C++, or of exactly how this library should be used... so I apologize upfront for the probably somewhat ignorant question.

question

All 4 comments

Hi,

It's a rule of the C++ language: you cannot reassign a reference.
As a consequence, if you have a member of type reference, you have to set it in the member initializer list. Unfortunately, this is not possible in your case.

The solution is to use a pointer instead of a reference:

class ConfigFile{
  public:
    JsonObject& jsonObject() {
        return *jsonObjectPtr;
    }

  private:
    JsonObject* jsonObjPtr;
};

However, I think it would be more clever to use a JsonVariant, as it would wrap the pointer for you, and would also support arrays:

class ConfigFile{
  public:
    JsonObject& jsonObject() {
        return jsonVariant;
    }

  private:
    JsonVariant jsonVariant;
};

Regards.

@bepursuant Any progress? Can I close the issue?

@bblanchon Yes, as usual your library has been a fantastic help, but you've been fantastic supporting it as well! Again, apologies for the semi-unrelated-to-your-library question, but I truly appreciate your willingness to help anyway :) I was able to get my library working using the JsonVariant method you mentioned, which worked with almost no additional modifications.

@bepursuant I am trying to do something similar. Would you be willing to share the gist for the changed code?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zixzixzix picture zixzixzix  路  5Comments

mhazley picture mhazley  路  4Comments

kylegordon picture kylegordon  路  6Comments

koffienl picture koffienl  路  3Comments

czuvich picture czuvich  路  7Comments