vibe.d beta banner
get vibe.d
0.10.1

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

Function deserializeWithPolicy

Deserializes and returns a serialized value, interpreting values according to Policy when possible.

T deserializeWithPolicy(Serializer, alias Policy, T, ARGS...) (
  ARGS args
);

serialized_data can be either an input range or a value containing the serialized data, depending on the type of serializer used.

See Also

JsonSerializer, JsonStringSerializer, BsonSerializer

Example

import vibe.data.json;
import std.meta : AliasSeq;

template SizePol(T)
	if (__traits(allMembers, T) == AliasSeq!("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)
	@safe {
		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;
}

Json serializedI = "1x2";
SizeI sizeI = deserializeWithPolicy!(JsonSerializer, SizePol, SizeI)(serializedI);
assert(sizeI.x == 1);
assert(sizeI.y == 2);

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

Sönke Ludwig

Copyright

© 2013-2016 rejectedsoftware e.K.

License

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