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

std::complex type #1510

Closed
mipac opened this issue Mar 11, 2019 · 9 comments
Closed

std::complex type #1510

mipac opened this issue Mar 11, 2019 · 9 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@mipac
Copy link

mipac commented Mar 11, 2019

How can we serialize std::complex type ?
And vector<complex> in particular ?

I make to_json/from_json functions, but I wonder if there is a better way.

@nlohmann
Copy link
Owner

std::complex is not supported out of the box. So, yes, you need to define to_json/from_json functions. There, you need to define how you want to represent the values.

@gregmarr
Copy link
Contributor

Specifically, you need to define them for std::complex. If you do that, then std::vector<std::complex> comes for free.

@mipac
Copy link
Author

mipac commented Mar 12, 2019

thx for your replies,

I can't make it work for free with vector ;)
can you post a sample code?

@nlohmann
Copy link
Owner

How does your code for std::complex look like?

@mipac
Copy link
Author

mipac commented Mar 12, 2019

... It works when I put the code in std namespace... sorry

it could help:

namespace std {

template< class T > to_json(json &j, const std::complex< T > &p) {
  j = json {to_string(p.real()) + "+i" + to_string(p.imag())};
}

template< class T > from_json(const json &j, std::complex< T > &p) {
}

}

...
vector<complex<float>> v = { {1,1}, {2,2}};
json j(v);

Do you have an efficient way to deserialize ;)

@nlohmann
Copy link
Owner

How about

#include "json.hpp"
#include <iostream>
#include <complex>

using json = nlohmann::json;

namespace std {
    
    template< class T > void to_json(json &j, const std::complex< T > &p) {
        j = json {p.real(), p.imag()};
    }
    
    template< class T > void from_json(const json &j, std::complex< T > &p) {
        p.real(j.at(0));
        p.imag(j.at(1));
    }
}

int main() {
    std::vector<std::complex<float>> v1 = { {1,2}, {3,4}};
    json j(v1);
    
    std::cout << j << std::endl;
    
    auto v2 = j.get<std::vector<std::complex<float>>>();
    std::cout << std::boolalpha << (v1 == v2) << std::endl;
}

@mipac
Copy link
Author

mipac commented Mar 12, 2019

ok tested and approved!
A big thank you ;)

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 12, 2019
@nlohmann
Copy link
Owner

You're welcome!

@danra
Copy link

danra commented Nov 17, 2023

Adding my vote for supporting std::complex<T> out of the box. Some canonical default would work for 99% for the use-cases and would lower friction.

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

4 participants