Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclassing json() & error on recursive load #1201

Closed
Dariusz1989 opened this issue Aug 18, 2018 · 4 comments
Closed

Subclassing json() & error on recursive load #1201

Dariusz1989 opened this issue Aug 18, 2018 · 4 comments
Labels
state: help needed the issue needs help to proceed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@Dariusz1989
Copy link

Dariusz1989 commented Aug 18, 2018

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 ?

@nlohmann
Copy link
Owner

I haven't tried you code yet, but I fear that the library is currently not designed to be subclassed. You can configure it partly via template arguments, but also by defining translations from and to arbitrary types.

@patrickelectric
Copy link

@Dariusz1989 any update on it ?

@Dariusz1989
Copy link
Author

Dariusz1989 commented Sep 29, 2018

@patrickelectric

A snap so sorry, I saw the reply and went on with my R&D. I think I ended up just passing json rather than my wrapper to solve this temporary. But I was unable to use my class as a recursive pass sadly. When I have a moment I'll provide a full test example to show how/when it breaks. Perhaps nlohmann or some other smart person will be able to help :- )

@stale
Copy link

stale bot commented Oct 29, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Oct 29, 2018
@stale stale bot closed this as completed Nov 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: help needed the issue needs help to proceed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants