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

Can't have a json type as a property in an arbitrary type #2078

Closed
TheOnlyArtz opened this issue May 1, 2020 · 3 comments
Closed

Can't have a json type as a property in an arbitrary type #2078

TheOnlyArtz opened this issue May 1, 2020 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@TheOnlyArtz
Copy link

TheOnlyArtz commented May 1, 2020

I'm trying to have a struct which can old json type as one of it's properties and it throws an error, for some reason.

namespace DataSerialization
{
	struct GeneralResponse
	{
		int op;
		json d;
	};

	void from_json(const json& j, const GeneralResponse& response)
	{
		j.at("op").get_to(response.op);
               // ERROR HERE
		j.at("d").get_to(response.d);
	}

	void to_json(json& j, const GeneralResponse& response)
	{
		j = json{ {"op", response.op}, {"d", response.d} };
	}
}

image

@nlohmann
Copy link
Owner

nlohmann commented May 1, 2020

In the from_json function, your type must not be const.

@TheOnlyArtz
Copy link
Author

In the from_json function, your type must not be const.

Aight, but still, same error occurs, I've "found" a workaround by using
response.d = j["d"].get<json>();

@nlohmann
Copy link
Owner

nlohmann commented May 1, 2020

You can simplify this with

response.d = j["d"];

or

response.d = j.at("d");

The get_to function is meant to derive the type for conversions and does not work with the json type itself, see the documentation.

The signature is

template<typename ValueType,
         detail::enable_if_t <
             not detail::is_basic_json<ValueType>::value and
             detail::has_from_json<basic_json_t, ValueType>::value,
             int> = 0>
ValueType & get_to(ValueType& v) const noexcept(noexcept(
            JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants