Created
November 6, 2015 06:34
-
-
Save ReubenBond/932e7a96785d4e1d6fc3 to your computer and use it in GitHub Desktop.
Orleans JSON (JObject, etc) Serialization
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
/// <summary> | |
/// Provides support for serializing JSON values. | |
/// </summary> | |
[RegisterSerializer] | |
public class OrleansJsonSerialization | |
{ | |
/// <summary> | |
/// Initializes static members of the <see cref="OrleansJsonSerialization"/> class. | |
/// </summary> | |
static OrleansJsonSerialization() | |
{ | |
Register(); | |
} | |
/// <summary> | |
/// The deep copier. | |
/// </summary> | |
/// <param name="original"> | |
/// The original. | |
/// </param> | |
/// <returns> | |
/// The copy. | |
/// </returns> | |
public static object DeepCopier(object original) | |
{ | |
return original; | |
} | |
/// <summary> | |
/// Serializes an object to a stream. | |
/// </summary> | |
/// <param name="obj"> | |
/// The object being serialized. | |
/// </param> | |
/// <param name="stream"> | |
/// The stream to serialize to. | |
/// </param> | |
/// <param name="expected"> | |
/// The expected type. | |
/// </param> | |
public static void Serialize(object obj, BinaryTokenStreamWriter stream, Type expected) | |
{ | |
var str = JsonConvert.SerializeObject(obj, expected, SerializationSettings.JsonConfig); | |
SerializationManager.SerializeInner(str, stream, typeof(string)); | |
} | |
/// <summary> | |
/// Deserializes a JSON object. | |
/// </summary> | |
/// <param name="expected"> | |
/// The expected type. | |
/// </param> | |
/// <param name="stream"> | |
/// The stream. | |
/// </param> | |
/// <returns> | |
/// The deserialized object. | |
/// </returns> | |
public static object Deserialize(Type expected, BinaryTokenStreamReader stream) | |
{ | |
var str = (string)SerializationManager.DeserializeInner(typeof(string), stream); | |
return JsonConvert.DeserializeObject(str, expected, SerializationSettings.JsonConfig); | |
} | |
/// <summary> | |
/// Registers this class with the <see cref="SerializationManager"/>. | |
/// </summary> | |
public static void Register() | |
{ | |
var jsonTypes = new[] | |
{ typeof(JObject), typeof(JArray), typeof(JToken), typeof(JValue), typeof(JProperty), typeof(JConstructor) }; | |
foreach (var type in jsonTypes) | |
{ | |
SerializationManager.Register(type, DeepCopier, Serialize, Deserialize); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment