Created
November 15, 2011 13:54
-
-
Save JamesMaroney/1367121 to your computer and use it in GitHub Desktop.
Serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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