Skip to content

Instantly share code, notes, and snippets.

@JamesMaroney
Created November 15, 2011 13:54
Show Gist options
  • Save JamesMaroney/1367121 to your computer and use it in GitHub Desktop.
Save JamesMaroney/1367121 to your computer and use it in GitHub Desktop.
Serializer
public static class Serializer
{
private static JsonSerializer ServerToServerInstanceBacker;
private static JsonSerializer ServerToClientBacker;
private static JsonSerializer ClientToServerBacker;
public static JsonSerializer ServerToServerInstance { get { return ServerToServerInstanceBacker = ServerToServerInstanceBacker ?? Get(); } }
public static JsonSerializer ServerToClientInstance {
get {
return ServerToClientBacker = ServerToClientBacker ?? Get(
ravenConvention => ravenConvention.JsonContractResolver = new CamelCasePropertyNamesContractResolver(),
jsonSerializer => {
jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
jsonSerializer.TypeNameHandling = TypeNameHandling.None;
});
}
}
public static JsonSerializer ClientToServerInstance {
get {
return ClientToServerBacker = ClientToServerBacker ?? Get(
ravenConvention => ravenConvention.JsonContractResolver = new CamelCasePropertyNamesContractResolver(),
jsonSerializer => {
jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
jsonSerializer.TypeNameHandling = TypeNameHandling.None;
});
}
}
private static JsonSerializer Get(Action<DocumentConvention> configureConventions = null, Action<JsonSerializer> configureSerializer = null) {
var ravenConvention = new DocumentConvention();
ravenConvention.IdentityTypeConvertors.Add(new NullableGuidConverter());
if (null != configureConventions) configureConventions(ravenConvention);
JsonSerializer jsonSerializer = ravenConvention.CreateSerializer();
if (null != configureSerializer) configureSerializer(jsonSerializer);
return jsonSerializer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment