private object SerializeInternal(object obj, bool pretty, Type asType) { if (obj == null) { return(null); } Formats.IPrototype proto = BuildPrototype(obj, asType, 0); if (proto == null) { return(null); } return(_format.SerializePrototype(proto, pretty)); }
public T DeserializeInternal <T>(object input) { Formats.IPrototype proto = _format.DeserializePrototype(input); if (proto == null) { return(default(T)); } object result = BindPrototype(proto, typeof(T), 0); if (result == null) { return(default(T)); } return((T)result); }
private Formats.IPrototype BuildPrototype(object obj, Type nominalType, int depth) { if (obj == null || depth > MaxDepth) { return(null); } Formats.IPrototype proto = _format.CreatePrototype(); if (proto == null) { return(null); } IList <Tuple <MemberInfo, object> > values = _reflector.GetSerializableValues(obj); foreach (Tuple <MemberInfo, object> value in values) { if (string.IsNullOrEmpty(value.Item1.Name)) { continue; } Type nominalMemberType = _reflector.GetNominalMemberType(value.Item1); object valueObj = BuildValue(value.Item2, nominalMemberType, depth + 1); if (valueObj == null) { continue; } proto.SetChild(value.Item1.Name, valueObj); } Type actualType = obj.GetType(); if (nominalType != null && nominalType != actualType) { string actualTypeName = GetPolymorphicTypeName(actualType, nominalType); if (!string.IsNullOrEmpty(actualTypeName)) { proto.SetTypeName(actualTypeName, values.Select((v) => v.Item1.Name).ToList()); } } return(proto); }
private object BindPrototype(Formats.IPrototype proto, Type type, int depth) { if (proto == null || type == null || depth > MaxDepth) { return(null); } string actualTypeName = proto.GetTypeName(); if (!string.IsNullOrEmpty(actualTypeName)) { Type actualType = GetPolymorphicType(actualTypeName); if (actualType != null && _reflector.IsCompatibleType(actualType, type)) { type = actualType; } } IList <Tuple <MemberInfo, Type, object> > memberValues = new List <Tuple <MemberInfo, Type, object> >(); IList <MemberInfo> members = _reflector.GetSerializableMembers(type); if (members != null && members.Count > 0) { foreach (MemberInfo member in members) { if (!proto.GetChild(member.Name, out object memberValue)) { continue; } Type nominalMemberType = _reflector.GetNominalMemberType(member); if (nominalMemberType == null) { continue; } memberValue = BindValue(memberValue, nominalMemberType, depth + 1); memberValues.Add(Tuple.Create(member, nominalMemberType, memberValue)); } } return(CreateInstance(type, memberValues)); }