public void LoadObject(object o, Dictionary <string, object> h) { if (o is IJsonSerializerLoading) { (o as IJsonSerializerLoading).Loading(h as Dictionary <string, object>); } foreach (PropertyInfo pi in o.GetType().GetProperties()) { if (!ShallSave(pi)) { continue; } string name = Name(pi); if (pi.CanWrite && h.ContainsKey(name)) { JsonSerializerVirtual v = pi.Get <JsonSerializerVirtual>(); if (IsReference(pi)) { SaveReference(o, pi.Name, h[name]); } else { object value = FromJson(o, h[name], pi.PropertyType, v); pi.SetValue(o, value, null); } } } foreach (FieldInfo fi in o.GetType().GetFields()) { if (!ShallSave(fi)) { continue; } string name = Name(fi); if (h.ContainsKey(name)) { JsonSerializerVirtual v = fi.Get <JsonSerializerVirtual>(); if (IsReference(fi)) { SaveReference(o, fi.Name, h[name]); } else { object value = FromJson(o, h[name], fi.FieldType, v); fi.SetValue(o, value); } } } if (o is IJsonSerializerLoadCompleted) { (o as IJsonSerializerLoadCompleted).LoadCompleted(h as Dictionary <string, object>); } }
public Dictionary <string, object> SaveObject(object o, JsonSerializerVirtual virt) { Dictionary <string, object> h = new Dictionary <string, object>(); foreach (PropertyInfo pi in o.GetType().GetProperties()) { if (!ShallSave(pi)) { continue; } object item = ObjectToSave(pi, pi.GetValue(o, null)); JsonSerializerVirtual v = pi.Get <JsonSerializerVirtual>(); if (item != null) { h[Name(pi)] = ToJson(item, v); } } foreach (FieldInfo fi in o.GetType().GetFields()) { if (!ShallSave(fi)) { continue; } object item = ObjectToSave(fi, fi.GetValue(o)); JsonSerializerVirtual v = fi.Get <JsonSerializerVirtual>(); if (item != null) { h[Name(fi)] = ToJson(item, v); } } if (virt != null) { h[virt.ClassPropertyName] = o.GetType().FullName; } return(h); }
object FromJson(object parent, object o, Type t, JsonSerializerVirtual virt) { if (t.IsSubclassOf(typeof(Enum))) { return(Enum.Parse(t, o as string)); } else if (t == typeof(TimeSpan)) { return(new TimeSpan((long)((double)o * TimeSpan.TicksPerSecond))); } else if (o is string || o is int || o is bool) { return(o); } else if (o is double) { if (t == typeof(double) || t == typeof(double?)) { return((double)o); } else if (t == typeof(float) || t == typeof(float?)) { return((float)(double)o); } else if (t == typeof(int) || t == typeof(int?)) { return((int)(double)o); } else if (t == typeof(Object)) { return(o); } else { throw new Exception("error"); } } else if (t == typeof(DateTime) || t == typeof(DateTime?)) { return(DateTimeExtensions.FromDictionary(o as Dictionary <string, object>)); } else if (typeof(System.Collections.IDictionary).IsAssignableFrom(t)) { Type key_type = t.GetGenericArguments()[0]; Type value_type = t.GetGenericArguments()[1]; System.Collections.IDictionary res = t.GetConstructor(Type.EmptyTypes).Invoke(null) as System.Collections.IDictionary; foreach (var item in o as Dictionary <string, object> ) { res[FromJson(parent, item.Key, key_type, virt)] = FromJson(parent, item.Value, value_type, virt); } return(res); } else if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(HashSet <>)) { var value_type = t.GetGenericArguments()[0]; var object_enumerable = o; var cast_method = typeof(Enumerable).GetMethod("Cast"); var cast_to_value_type_method = cast_method.MakeGenericMethod(value_type); var typed_enumerable = cast_to_value_type_method.Invoke(null, new[] { object_enumerable }); var hash_set_constructor = t.GetConstructor(new[] { typed_enumerable.GetType() }); Debug.Assert(hash_set_constructor != null); var hash_set = hash_set_constructor.Invoke(new[] { typed_enumerable }); return(hash_set); } else if (o is List <object> ) { if (t.IsArray) { Array res = Array.CreateInstance(t.GetElementType(), (o as List <object>).Count); int i = 0; foreach (object item in o as List <object> ) { res.SetValue(FromJson(parent, item, t.GetElementType(), virt), i); ++i; } return(res); } else { Type inner_type = t.GetGenericArguments()[0]; System.Collections.IList res = t.GetConstructor(Type.EmptyTypes).Invoke(null) as System.Collections.IList; foreach (object item in o as List <object> ) { res.Add(FromJson(parent, item, inner_type, virt)); } return(res); } } else if (o is Dictionary <string, object> ) { if (virt != null) { t = virt.GetType(o as Dictionary <string, object>); } object res = Construct(parent, t); LoadObject(res, o as Dictionary <string, object>); return(res); } else { throw new Exception("error"); } }
object ToJson(object o, JsonSerializerVirtual virt) { if (o is int) { return((double)(int)o); } else if (o is float) { return((double)(float)o); } else if (o is double) { return((double)o); } else if (o is string || o is bool || o == null) { return(o); } else if (o is DateTime) { return((o as DateTime?).GetValueOrDefault().ToDictionary()); } else if (o is Enum) { return(o.ToString()); } else if (o is TimeSpan) { return(((TimeSpan)o).TotalSeconds); } else if (o is System.Collections.IList) { List <object> a = new List <object>(); foreach (var item in o as System.Collections.IEnumerable) { a.Add(ToJson(item, virt)); } return(a); } else if (o is System.Collections.IDictionary) { Dictionary <string, object> h = new Dictionary <string, object>(); foreach (System.Collections.DictionaryEntry item in o as System.Collections.IDictionary) { h[(string)item.Key] = ToJson(item.Value, virt); } return(h); } else if (o.GetType().IsGenericType&& o.GetType().GetGenericTypeDefinition() == typeof(HashSet <>)) { var a = new List <object>(); foreach (var item in (System.Collections.IEnumerable)o) { a.Add(ToJson(item, virt)); } return(a); } else { return(SaveObject(o, virt)); } }