ChainedPolicy

Chains serialization policy.

Constructs a serialization policy that given a type T will apply the first compatible policy toRepresentation and fromRepresentation functions. Policies are evaluated left-to-right according to isPolicySerializable.

Members

Aliases

ChainedPolicy
alias ChainedPolicy = Primary
Undocumented in source.
ChainedPolicy
alias ChainedPolicy = ChainedPolicy!(ChainedPolicyImpl!(Primary, Fallbacks[0]), Fallbacks[1..$])
Undocumented in source.

Examples

import std.conv;

// To be represented as the boxed value when serialized
static struct Box(T) {
	T value;
}
// Also to berepresented as the boxed value when serialized, but has
// a different way to access the value.
static struct Box2(T) {
	private T v;
	ref T get() {
		return v;
	}
}
template BoxPol(S)
{
	auto toRepresentation(S s) {
		return s.value;
	}

	S fromRepresentation(typeof(toRepresentation(S.init)) v) {
		return S(v);
	}
}
template Box2Pol(S)
{
	auto toRepresentation(S s) {
		return s.get();
	}

	S fromRepresentation(typeof(toRepresentation(S.init)) v) {
		S s;
		s.get() = v;
		return s;
	}
}
alias ChainPol = ChainedPolicy!(BoxPol, Box2Pol);
static assert(!isPolicySerializable!(BoxPol, Box2!int));
static assert(!isPolicySerializable!(Box2Pol, Box!int));
static assert(isPolicySerializable!(ChainPol, Box!int));
static assert(isPolicySerializable!(ChainPol, Box2!int));

See Also

vibe.data.serialization.serializeWithPolicy

Meta