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 » Community » U++ community news and announcements » Jsonize/Xmlize with lambda (and common template example)
Jsonize/Xmlize with lambda (and common template example) [message #50556] Thu, 15 November 2018 09:36
mirek is currently offline  mirek
Messages: 11825
Registered: November 2005
Ultimate Member
It is now possible to use lambda definition of structure of data or array element with jsonizing/xmlizing. The new reference/IzeLambda show this as well as method of using single method for both JSON and XML:

#include <Core/Core.h>

using namespace Upp;

struct Item {
	int  value;
};

struct Data {
	Array<Item> array;
	Point       p;

	template <class IO>
	void Ize(IO& io) { // define single template function for both JSON and XML
		io
			.Var("p", p, [=] (IO& io, Point& m) { // use lambda to define how to 'ize' structure
				io("X", m.x)("Y", m.y);
			})
			.Array("values", array, [=] (IO& io, Item& m) { // use lambda to define how to 'ize' elements
				io("value", m.value);
			}, "element") // this is ignored in Json, provides tag of single element
		;
	}
	
	void Xmlize(XmlIO& io)   { Ize(io); }
	void Jsonize(JsonIO& io) { Ize(io); }
};

CONSOLE_APP_MAIN
{
	StdLogSetup(LOG_COUT|LOG_FILE);

	Data data;
	data.array.Add().value = 12345;
	data.p.x = 1;
	data.p.y = 2;
	
	LOG(StoreAsXML(data));
	LOG(StoreAsJson(data));
}


Note that, while not shown here, the important feature of using lambda (instead of Xmlize method) is the ability to define different structure, possibly even based on serialized data - this was in fact the original motivation for this...
Previous Topic: CoWorkNX
Next Topic: Painter refactored/optimized
Goto Forum:
  


Current Time: Fri Apr 19 12:46:34 CEST 2019

Total time taken to generate the page: 0.00911 seconds