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

Comparing data type with value_t::number_integer fails #1783

Closed
kubkodev opened this issue Oct 8, 2019 · 2 comments
Closed

Comparing data type with value_t::number_integer fails #1783

kubkodev opened this issue Oct 8, 2019 · 2 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@kubkodev
Copy link

kubkodev commented Oct 8, 2019

I'm trying to make template function that stores json integer value as int (signed). I had no problem with unsigned, however comparing type for signed seems to fail. What am I doing wrong? Using 3.5.0 version, thanks for any help.

//works fine
template<typename Td>
typename std::enable_if<std::is_integral<Td>::value && std::is_unsigned<Td>::value, bool>::type TryGetJsonValue(nlohmann::json const &data, Td &dest)
{
	if (data.type() != nlohmann::json::value_t::number_unsigned)
		return false;

	dest = data.get<Td>();
	return true;
}

template<typename Td>
typename std::enable_if<std::is_integral<Td>::value && std::is_signed<Td>::value, bool>::type TryGetJsonValue(nlohmann::json const &data, Td &dest)
{
	if (data.type() != nlohmann::json::value_t::number_integer) //here
		return false;

	dest = data.get<Td>();
	return true;
}
@kubkodev kubkodev changed the title Comparing data type with number_integer fails Comparing data type with value_t::number_integer fails Oct 8, 2019
@tete17
Copy link
Contributor

tete17 commented Oct 8, 2019

Hi @kubkodev

I am sorry but those std::enable_if only make sure the second argument is of the type you are expecting but it says nothing about the actual json object you are passing. As an example, the following code would call your second function and would do the correct thing.

#include <nlohmann/json.hpp>
int main()
{
    int dest = 0;
    nlohmann::json data = "This json is just a string";
    TryGetJsonValue(data, dest); // This will be false;
}

Are you sure the json you are passing contains a signed integer? Could you please provide a small example/json object in which the type is not correct?

@stale
Copy link

stale bot commented Nov 7, 2019

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