Skip to content

Subclassing json() & error on recursive load #1201

@Dariusz1989

Description

@Dariusz1989

Hey

So I have subclassed json with another class so that in future if I want to change the container class/type I can just replace that within my class and not have to go over entire app replacing all containers.

Essentially class looks like this :

class myObject : public json {
public:
    myObject(){};
    ~myObject() {};
    friend QDebug operator<<(QDebug stream, const myObject &metaData);
};
Q_DECLARE_METATYPE(myObject);

Now the issue is when I have a json file I read from file, that has some name/id/etc information + children which is of the same structure.

The initial object load file, but the children throw me an error.

Say if I do
data["name] < this prints name.
if I do data["children"].count() < this gets me proper count
if I do data["children"][0]["name] < this gets me name of child - all great.

But if I try to pass that object to newly created class to configure it. Say child class I get this error :
Essentially loops goes as follow:

void objectTemplate::setData(myObject data) {
	mName = data["name"].get<std::string>();
	for (int x = 0; x < data["children"].size(); ++x) {
		std::cout << "sending child to be generated" << data["children"][x];
		myTempalte objectTemplate;
		objectTemplate.setData(data["children"][x]); /// this errors out here
		mChildTemplates.push_back(objectTemplate);
	}
}

Errors starts here:

void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
{
	if (JSON_UNLIKELY(not j.is_array()))
	{
		JSON_THROW(type_error::create(302, "type must be array, but is " +
			std::string(j.type_name())));
	}

	from_json_array_impl(j, arr, priority_tag<2> {});
}

Its driving me nuts, as far as I debug it, everything is of proper myObject class & have data whenever I print it. But the second I try to generate child from json["children"]array I get an crash :- (

Does this look familiar to any one?

TIA
Btw I have no idea why formating is off ;[

Edit1. Thanks Nlohmann for formating fix! : -)
I also did test with using json, instead of myObject class and everyhing works. So there is a weird issue when I try to load children as my subclass class thini ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: help neededthe issue needs help to proceedstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions