-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Comments
|
Specifically, you need to define them for |
thx for your replies, I can't make it work for free with vector ;) |
How does your code for |
... It works when I put the code in std namespace... sorry it could help:
Do you have an efficient way to deserialize ;) |
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;
} |
ok tested and approved! |
You're welcome! |
Adding my vote for supporting |
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.
The text was updated successfully, but these errors were encountered: