private dynamic Parse(JToken token, Type t, DeserializeAttribute attr = null) { bool deserialize = attr?.Json ?? false; bool deserializeItems = attr?.JsonItems ?? false; if (token.Type == JTokenType.Object) { if (typeof(IDictionary).IsAssignableFrom(t)) { return(ParseDictionary((JObject)token, t, deserializeItems)); } return(ParseObject((JObject)token, t)); } else if (token.Type == JTokenType.Array) { return(ParseArray((JArray)token, t, deserializeItems)); } else if (token.Type == JTokenType.Boolean || token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return(token.ToObject(t)); } else if (token.Type == JTokenType.String) { string s = token.ToObject <string>(); if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(EnumWrapper <>)) { return(Activator.CreateInstance(t, s)); } else if (t == typeof(DateTime)) { return(DateTime.Parse(s)); } if (!deserialize) { return(s); } return(Parse(JToken.Parse(s), t)); } else if (token.Type == JTokenType.Null) { return(null); } else if (token.Type == JTokenType.Date) { return(token.Value <DateTime>()); } else { throw new Exception("Invalid token type " + token.Type); } }
public object Reconstruct(object o, DeserializeAttribute attr = null) { object result = null; if (o == null) { result = null; } else { Type t = o.GetType(); if (ReflectionUtil.IsBoxed(o) || o is string) { result = o; } else if (o is IDictionary) { result = ReconstructDictionary((IDictionary)o, attr?.JsonItems ?? false); } else if (o is ICollection || ReflectionUtil.ImplementsGenericInterface(o.GetType(), typeof(ICollection <>))) { result = ReconstructCollection(o, attr?.JsonItems ?? false); } else if (o.GetType().IsGenericType&& o.GetType().GetGenericTypeDefinition() == typeof(EnumWrapper <>)) { return(o.ToString()); } else { result = ReconstructObject(o); } } if (attr != null && result != null && attr.Json) { return(JsonConvert.SerializeObject(result)); } return(result); }