Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.Collections.Concurrent;
- using System.IO;
- using System.Xml.Serialization;
- namespace Contracts.Helpers
- {
- public static class SerialisationHelper
- {
- #region Member Variables
- private readonly static ConcurrentDictionary<Type, XmlSerializer> _xmlSerialiser;
- private readonly static JsonSerializerSettings _jsonSettings;
- #endregion
- #region Constructors
- /// <summary>
- /// Initialises the static instance of the <see cref="SerialisationHelper"/> class.
- /// </summary>
- static SerialisationHelper()
- {
- _xmlSerialiser = new ConcurrentDictionary<Type, XmlSerializer>();
- _jsonSettings = new JsonSerializerSettings()
- {
- TypeNameHandling = TypeNameHandling.Auto,
- ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
- PreserveReferencesHandling = PreserveReferencesHandling.Objects,
- ObjectCreationHandling = ObjectCreationHandling.Auto
- };
- }
- #endregion
- #region Methods
- /// <summary>
- /// Serialises an object.
- /// </summary>
- /// <param name="objectToSerialise">Object to serialise.</param>
- /// <param name="serialiseAsJson">True if to serialise as JSON, otherwise will serialise as XML.</param>
- /// <returns></returns>
- public static string Serialise(object objectToSerialise, bool serialiseAsJson = true)
- {
- string result = null;
- if (serialiseAsJson)
- {
- result = JsonConvert.SerializeObject(objectToSerialise, Formatting.Indented, _jsonSettings);
- }
- else
- {
- using (var writer = new StringWriter())
- {
- XmlSerializer serialiser = GetXmlSerialiser(objectToSerialise.GetType());
- serialiser.Serialize(writer, objectToSerialise);
- result = writer.ToString();
- }
- }
- return result;
- }
- /// <summary>
- /// Deserialises an object.
- /// </summary>
- /// <param name="serialisedObject">Object to deserialise</param>
- /// <param name="deserialiseFromJson">True if to deserialise from JSON, otherwise will deserialise from XML.</param>
- /// <returns></returns>
- public static TResult Deserialise<TResult>(string serialisedObject, bool deserialiseFromJson = true) where TResult : class
- {
- TResult result = null;
- if (deserialiseFromJson)
- {
- try
- {
- result = JsonConvert.DeserializeObject<TResult>(serialisedObject, _jsonSettings);
- }
- catch(JsonSerializationException)
- {
- result = null;
- }
- catch (JsonReaderException)
- {
- result = null;
- }
- }
- else
- {
- XmlSerializer serialiser = GetXmlSerialiser(typeof(TResult));
- using (var reader = new StringReader(serialisedObject))
- {
- try
- {
- result = serialiser.Deserialize(reader) as TResult;
- }
- catch (InvalidOperationException)
- {
- result = null;
- }
- }
- }
- return result;
- }
- /// <summary>
- /// Gets an XML Serialiser instance for handling the given object type.
- /// </summary>
- /// <param name="objectType"></param>
- /// <returns></returns>
- private static XmlSerializer GetXmlSerialiser(Type objectType)
- {
- XmlSerializer result = null;
- _xmlSerialiser.TryGetValue(objectType, out result);
- if (result == null)
- {
- result = _xmlSerialiser.AddOrUpdate(objectType, new XmlSerializer(objectType), (ot, s) => s);
- }
- return result;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement