vibe.d beta banner
get vibe.d

Asynchronous I/O that doesn’t get in your way, written in D

Function serializeWithPolicy

Serializes a value with the given serializer, representing values according to Policy when possible.

auto serializeWithPolicy(Serializer, alias Policy, T, ARGS...) (
  auto ref T value,
  ARGS args

void serializeWithPolicy(Serializer, alias Policy, T) (
  ref Serializer serializer,
  auto ref T value

The serializer must have a value result for the first form to work. Otherwise, use the range based form.

See Also

JsonSerializer, JsonStringSerializer, BsonSerializer



template SizePol(T)
	if (__traits(allMembers, T) == TypeTuple!("x", "y"))
	import std.conv;
	import std.array;

	static string toRepresentation(T value) @safe {
		return to!string(value.x) ~ "x" ~ to!string(value.y);

	static T fromRepresentation(string value) {
		string[] fields = value.split('x');
		alias fieldT = typeof(T.x);
		auto x = to!fieldT(fields[0]);
		auto y = to!fieldT(fields[1]);
		return T(x, y);

static struct SizeI {
	int x;
	int y;
SizeI sizeI = SizeI(1,2);
Json serializedI = serializeWithPolicy!(JsonSerializer, SizePol)(sizeI);
assert(serializedI.get!string == "1x2");

static struct SizeF {
	float x;
	float y;
SizeF sizeF = SizeF(0.1f,0.2f);
Json serializedF = serializeWithPolicy!(JsonSerializer, SizePol)(sizeF);
assert(serializedF.get!string == "0.1x0.2");

Sönke Ludwig


© 2013-2016 rejectedsoftware e.K.


Subject to the terms of the MIT license, as written in the included LICENSE.txt file.