using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ICD.Common.Utils.Json { /// /// Simple wrapper for serialization of an object and its type. /// public sealed class JsonItemWrapper { private const string TYPE_TOKEN = "t"; private const string ITEM_TOKEN = "i"; private readonly string m_ItemTypeString; private readonly object m_Item; /// /// Gets the string representation of the item type. Returns null if the item is null. /// public string ItemTypeString { get { return m_ItemTypeString; } } /// /// Gets the Type of the item. Returns null if the item is null. /// public Type ItemType { get { return string.IsNullOrEmpty(m_ItemTypeString) ? null : Type.GetType(m_ItemTypeString); } } /// /// Gets the wrapped item. /// public object Item { get { return string.IsNullOrEmpty(m_ItemTypeString) ? null : m_Item; } } /// /// Constructor. /// /// public JsonItemWrapper(object item) { m_ItemTypeString = item == null ? null : item.GetType().FullName; m_Item = item; } /// /// Writes the JsonItemWrapper as a JObject. /// /// public void Write(JsonWriter writer) { if (writer == null) throw new ArgumentNullException("writer"); writer.WriteStartObject(); writer.WritePropertyName(TYPE_TOKEN); writer.WriteValue(m_ItemTypeString); writer.WritePropertyName(ITEM_TOKEN); writer.WriteValue(JsonConvert.SerializeObject(m_Item)); writer.WriteEndObject(); } /// /// Reads the JToken back to the wrapped object. /// /// /// public static object ReadToObject(JToken token) { if (token == null) throw new ArgumentNullException("token"); string typeString = (string)token.SelectToken(TYPE_TOKEN); if (string.IsNullOrEmpty(typeString)) return null; string itemString = (string)token.SelectToken(ITEM_TOKEN); Type type = Type.GetType(typeString); return JsonConvert.DeserializeObject(itemString, type); } } }