chadaustin / sajson

Lightweight, extremely high-performance JSON parser for C++11
MIT License
565 stars 41 forks source link

sajson::document and sajson::value cannot be member variable #8

Closed ASCE1885 closed 7 years ago

ASCE1885 commented 9 years ago

I wanna wrap sajson,so I declare my class like this:

class HFJsonArray;
class HFJsonObject
{
public:

    explicit HFJsonObject(HFString& jsonString);
    virtual ~HFJsonObject();

public:
    HFInt32 GetInteger(HFString& key);
    HFDouble GetDouble(HFString& key);
    HFBool GetBoolean(HFString& key);
    HFString& GetString(HFString& key);
    HFJsonObject& GetJsonObject(HFString& key);
    HFJsonArray& GetJsonArray(HFString& key);

private:
    const sajson::document mDocument;
    const sajson::value& mValue;
};

In the cpp file:

HFJsonObject::HFJsonObject(HFString& jsonString)
{
    HFChar* str = (HFChar*)jsonString.GetBuffer();
    sajson::literal lt = sajson::literal((const char*)str);
    sajson::document doc = sajson::parse<sajson::literal>(lt);
    mValue = doc.get_root();
}

and compile error occur:

mValue = doc.get_root(); // semantic error: No viable overloaded '='

but this would work:

sajson::value myValue = doc.get_root();

as a result, I cannot implements the other function like GetInteger, GetDouble etc.. because I cannot get the sajson::value variable

chadaustin commented 9 years ago

You can't assign to a const reference in the constructor (e.g. mValue). Consider writing your constructor like this:

HFJsonObject::HFJsonObject(HFString& jsonString)
    : mDocument(sajson::literal((const char*)jsonString.GetBuffer()))
    , mValue(mDocument.get_root())
{}

Does that work?