Skip to content

Cannot deserialize types using std::ratio #1105

Description

@GabrielDav

Given
void from_json(const json& j, std::ratio<60>& r) { // parsing }
I expect to get parsed value back when used like this:
auto data = j.at("Data").get<std::ratio<60>>()
However, instead I'm getting a compilation error:

could not find from_json() method in T's namespace

This problem impacts usage of std::chrono::duration and std::crhono::time_point

This is really weird, because I copied implementation of std::ratio and renamed struct to rat

template<intmax_t _Nx,
		intmax_t _Dx = 1>
		struct rat
	{	// holds the ratio of _Nx to _Dx
		static_assert(_Dx != 0,
			"zero denominator");
		static_assert(-INTMAX_MAX <= _Nx,
			"numerator too negative");
		static_assert(-INTMAX_MAX <= _Dx,
			"denominator too negative");

		static constexpr intmax_t num = _Sign_of<_Nx>::value
			* _Sign_of<_Dx>::value * _Abs<_Nx>::value / _Gcd<_Nx, _Dx>::value;

		static constexpr intmax_t den = _Abs<_Dx>::value / _Gcd<_Nx, _Dx>::value;

		typedef rat<num, den> type;
	};

void from_json(const json& j, rat<60>& r)
	{
            // parsing
	}

auto data = j.at("Data").get<rat<60>>();

Surprisingly this works as expected.
I'm using Visual Studio 2017 msvc compiler. Project uses std::chrono across solution without any issues.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions