Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
koldo is currently offline  koldo
Messages: 3355
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 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
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: Smile

#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


[Updated on: Wed, 23 October 2019 13:49]

Report message to a moderator

Re: How to serialize a new data type [message #52582 is a reply to message #52580] Wed, 23 October 2019 16:21 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
You are totally right. Thank you very much!

Best regards
Iñaki
Re: How to serialize a new data type [message #52583 is a reply to message #52582] Thu, 24 October 2019 09:02 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
BTW, Jsonize and similar stuff (Serialize, GetHashValue...) is the reason for existence of Complex.
Re: How to serialize a new data type [message #52585 is a reply to message #52583] Thu, 24 October 2019 10:14 Go to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
OK. I will use it.

Best regards
Iñaki
Previous Topic: [SOLVED] Vector Bug
Next Topic: Long time running program and Vector
Goto Forum:
  


Current Time: Fri Mar 29 10:37:57 CET 2024

Total time taken to generate the page: 0.01134 seconds