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

Error thrown when parsing in a subclass #2124

Closed
paulaltin opened this issue May 19, 2020 · 4 comments
Closed

Error thrown when parsing in a subclass #2124

paulaltin opened this issue May 19, 2020 · 4 comments
Labels
kind: bug state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@paulaltin
Copy link

paulaltin commented May 19, 2020

  • What is the issue you have?

When I create an instance of a subclass of nlohmann::json, attempting to deserialize a string to it using json::parse throws an error.

  • Please describe the steps to reproduce the issue.

Given:

class json_subclass : public json {};

then the following all work as expected:

json a = "{}"_json;
json b(json::parse("{}"));
json c = json::parse("{}");
json d; d = json::parse("{}");

but any of the following throw an error:

json_subclass a = "{}"_json;
json_subclass b(json::parse("{}"));
json_subclass c = json::parse("{}");
json_subclass d; d = json::parse("{}");

The error thrown is [json.exception.type_error.302] type must be array, but is object.

  • What is the expected behavior?

Deserializing a string to a nlohmann::json subclass should work.

  • And what is the actual behavior instead?

An error is thrown: [json.exception.type_error.302] type must be array, but is object.

  • Which compiler and operating system are you using?

Mac OS X 10.14.6
Xcode 11.3.1 (11C505)
Apple clang version 11.0.0 (clang-1100.0.33.17)

  • Did you use a released version of the library or the version from the develop branch?

Release 3.7.3.

  • Other notes

This may be related to issue #608, although I'm unable to reproduce the exact problem described there.

@paulaltin
Copy link
Author

paulaltin commented May 19, 2020

It works if I change the subclass definition to the following:

class json_subclass : public json
{
public:
    using json::json;
    json_subclass(const json& j) : json(j) { }
    json_subclass& operator=(const json& j) { json::operator=(j); return *this; }
};

@nlohmann
Copy link
Owner

The class is not really intended to subclass, I'm afraid.

@paulaltin
Copy link
Author

Ok, fair enough. In that case, perhaps the class should be marked final? Or at least this could be documented in the Readme.

@stale
Copy link

stale bot commented Jun 20, 2020

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 Jun 20, 2020
@stale stale bot closed this as completed Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug 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

2 participants