Home » U++ Library support » U++ Core » How to serialize a new data type
How to serialize a new data type [message #52578] |
Wed, 23 October 2019 10:58 |
|
koldo
Messages: 3358 Registered: August 2008
|
Senior Veteran |
|
|
I am lost. This sample to serialize a std::complex compiles in MSVC but gets this error with MinGW:
error: 'struct std::complex<double>' has no member named 'Jsonize'
#include <Core/Core.h>
using namespace Upp;
template <class T>
void Jsonize(JsonIO& io, std::complex<T>& var) {
T re, im;
if (io.IsStoring()) {
re = var.real();
im = var.imag();
}
io("re", re)("im", im);
if (io.IsLoading()) {
var.real(re);
var.imag(im);
}
}
struct Demo {
std::complex<double> val;
void Jsonize(JsonIO& io) {io("val", val);}
};
CONSOLE_APP_MAIN
{
Demo demo;
demo.val = std::complex<double>(1, 2);
StoreAsJsonFile(demo, "filename.json");
}
Best regards
Iñaki
|
|
|
Re: How to serialize a new data type [message #52580 is a reply to message #52578] |
Wed, 23 October 2019 13:46 |
Oblivion
Messages: 1093 Registered: August 2007
|
Senior Contributor |
|
|
Hello Koldo,
On Linux with GCC I get the same error. The problem seems to be namespace.
Try this one:
#include <Core/Core.h>
namespace Upp { // <--
template<class T>
void Jsonize(JsonIO& io, std::complex<T>& var)
{
T re, im;
if (io.IsStoring()) {
re = var.real();
im = var.imag();
}
io("re", re)("im", im);
if (io.IsLoading()) {
var.real(re);
var.imag(im);
}
}
}
using namespace Upp;
struct Demo {
std::complex<double> val;
void Jsonize(JsonIO& io)
{
io("val", val);
}
};
CONSOLE_APP_MAIN
{
Demo demo;
demo.val = std::complex<double>(1, 2);
StoreAsJsonFile(demo, "filename.json");
}
This works on GCC and CLANG (on Linux).
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Wed, 23 October 2019 13:49] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Sat May 04 14:03:47 CEST 2024
Total time taken to generate the page: 0.02081 seconds
|