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

Addint support for complex type #2207

Closed
pfeatherstone opened this issue Jun 22, 2020 · 6 comments
Closed

Addint support for complex type #2207

pfeatherstone opened this issue Jun 22, 2020 · 6 comments
Labels
kind: enhancement/improvement solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)

Comments

@pfeatherstone
Copy link
Contributor

Could we add official support for complex type. I don't know if this requires a fully blown PR as the code is so small:

namespace std {
    template<typename R, typename std::enable_if<std::is_arithmetic<R>::value>::type* = nullptr>
    void to_json(nlohmann::json& j, const complex<R>& d)
    {
        j = {d.real(), d.imag()};
    }

    template<typename R, typename std::enable_if<std::is_arithmetic<R>::value>::type* = nullptr>
    void from_json(const nlohmann::json& j, complex<R>& d)
    {
        d.real(j.at(0).get<R>());
        d.imag(j.at(1).get<R>());
    }
}
@nlohmann
Copy link
Owner

I'm hesitant, because this would define the serialization format for a standard type where there is not canonic serialization. You propose serializing as array, but an object would also make sense, e.g.

{"complex": {"real": 1, "imag": 0}}

Though the user could override the defaults, I am not sure whether the library should propose a default.

Any opinions on this?

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Jun 22, 2020
@pfeatherstone
Copy link
Contributor Author

Good point. It seems msgpack also doesn’t define a spec for complex numbers. Weird. As engineers we use them almost just as often as real numbers so why do these specs not care about complex numbers??

@pfeatherstone
Copy link
Contributor Author

I guess if you start doing that, then people will want quaternions and other exotic things and then it quickly becomes a math serialisation library.

@nlohmann
Copy link
Owner

The good thing is that you can realize the conversion with the few lines above, and I think this issue should pop up in any search for issues with the word "complex".

@pfeatherstone
Copy link
Contributor Author

As a json string i would expect to see something like

{
"z": 1 + 2i
}

For example. But no other json library, including the built-in javascript JSON library, would understand that without adding custom parsers. We need to open an issue on the JSON spec if that exists.

@nlohmann
Copy link
Owner

The JSON specification is unlikely to ever change. The RFC has been updated every once in a while, but never were new types added.

@nlohmann nlohmann added solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) and removed state: please discuss please discuss the issue or vote for your favorite option labels Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

No branches or pull requests

2 participants