Ejemplo n.º 1
0
        private SerializationStatus DeserializeObject(SerializationInstance instance, Type t, SerializationInfo s_info, StreamingContext context)
        {
            object obj = t.GetConstructor(Type.EmptyTypes)?.Invoke(Array.Empty <object>());

            if (obj == null)
            {
                return(SerializationStatus.Failure);
            }

            Dictionary <string, MemberInfo> map = t.GetMembers().Where(mi => CheckGetAndSet(mi, false, true, true)).ToDictionary(mi => mi.Name);

            foreach (SerializationEntry entry in s_info)
            {
                if (!map.TryGetValue(entry.Name, out MemberInfo mi))
                {
                    continue;
                }

                object value = s_info.GetValue(entry.Name, GetMemberType(mi));

                SetValue(obj, mi, value);
            }

            return(SerializationStatus.Deserialized(obj));
        }
Ejemplo n.º 2
0
        private SerializationStatus DeserializeSerializable(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            object obj = FormatterServices.GetUninitializedObject(t);

            InvokeOnSerialize(obj, context, false, false);

            if (typeof(ISerializable).IsAssignableFrom(t) && TryDeserializeISerializable(obj, info, context))
            {
                InvokeOnSerialize(obj, context, false, true);
                return(SerializationStatus.Deserialized(obj));
            }

            Dictionary <string, MemberInfo> map = FormatterServices.GetSerializableMembers(t, context).ToDictionary(m => m.Name);

            List <MemberInfo> found  = new List <MemberInfo>();
            List <object>     values = new List <object>();

            foreach (SerializationEntry entry in info)
            {
                if (!map.TryGetValue(entry.Name, out MemberInfo mi))
                {
                    continue;
                }

                found.Add(mi);
                values.Add(info.GetValue(entry.Name, GetMemberType(mi)));
            }

            obj = FormatterServices.PopulateObjectMembers(obj, found.ToArray(), values.ToArray());

            InvokeOnSerialize(obj, context, false, true);
            return(SerializationStatus.Deserialized(obj));
        }
Ejemplo n.º 3
0
        private SerializationStatus DeserializeContract(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            HashSet <string> items = new HashSet <string>();

            foreach (SerializationEntry entry in info)
            {
                items.Add(entry.Name);
            }

            IEnumerable <Tuple <MemberInfo, DataMemberAttribute> > members = t.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                                                             .Where(mi => CheckGetAndSet(mi, false, true, false)).Select(mi => Tuple.Create(mi, mi.GetCustomAttribute <DataMemberAttribute>()))
                                                                             .Where(tup => tup.Item2 != null).OrderBy(tup => tup.Item2.Order);

            object obj = FormatterServices.GetUninitializedObject(t);

            foreach (Tuple <MemberInfo, DataMemberAttribute> tup in members)
            {
                MemberInfo          mi  = tup.Item1;
                DataMemberAttribute dma = tup.Item2;

                string name = dma.IsNameSetExplicitly ? dma.Name : mi.Name;
                if (dma.IsRequired && !items.Contains(name))
                {
                    return(SerializationStatus.Failure);
                }

                object value = info.GetValue(name, GetMemberType(mi));

                SetValue(obj, mi, value);
            }

            InvokeOnSerialize(obj, context, false, true);
            return(SerializationStatus.Deserialized(obj));
        }
Ejemplo n.º 4
0
        public SerializationStatus Deserialize(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            if (Deserializer == null || !Deserializer(Deep ? instance.CreateLoopbackInfo(info) : info, context, out object value))
            {
                return(SerializationStatus.Failure);
            }

            return(SerializationStatus.Deserialized(value));
        }
Ejemplo n.º 5
0
        private SerializationStatus DeserializeSerializable(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            object obj = FormatterServices.GetUninitializedObject(t);

            InvokeOnSerialize(obj, context, false, false);

            if (typeof(ISerializable).IsAssignableFrom(t) && TryDeserializeISerializable(obj, info, context))
            {
                InvokeOnSerialize(obj, context, false, true);
                return(SerializationStatus.Deserialized(obj));
            }

            Dictionary <string, MemberInfo> map = FormatterServices.GetSerializableMembers(t, context).ToDictionary(m => m.Name);

            List <MemberInfo> found  = new List <MemberInfo>();
            List <object>     values = new List <object>();

            List <MemberInfo> missing     = new List <MemberInfo>();
            List <ulong>      missing_ids = new List <ulong>();

            foreach (SerializationEntry entry in info)
            {
                if (!map.TryGetValue(entry.Name, out MemberInfo mi))
                {
                    continue;
                }

                SerializedObject so = (SerializedObject)info.GetValue(entry.Name, typeof(SerializedObject));

                if (instance.TryGetOrDeserialize(GetMemberType(mi), so, out object val))
                {
                    found.Add(mi);
                    values.Add(val);
                }
                else
                {
                    missing.Add(mi);
                    missing_ids.Add(so.Id);
                }
            }

            obj = FormatterServices.PopulateObjectMembers(obj, found.ToArray(), values.ToArray());

            if (missing_ids.Count == 0)
            {
                InvokeOnSerialize(obj, context, false, true);
                return(SerializationStatus.Deserialized(obj));
            }

            return(SerializationStatus.FixupLater(obj, CreateSerializableFixup(missing.ToArray(), context), missing_ids.ToArray()));
        }
Ejemplo n.º 6
0
        private SerializationStatus DeserializeContract(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            HashSet <string> items = new HashSet <string>();

            foreach (SerializationEntry entry in info)
            {
                items.Add(entry.Name);
            }

            IEnumerable <Tuple <MemberInfo, DataMemberAttribute> > members = t.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                                                             .Where(mi => CheckGetAndSet(mi, false, true, false)).Select(mi => Tuple.Create(mi, mi.GetCustomAttribute <DataMemberAttribute>()))
                                                                             .Where(tup => tup.Item2 != null).OrderBy(tup => tup.Item2.Order);

            object obj = FormatterServices.GetUninitializedObject(t);

            List <MemberInfo> unassigned_members = new List <MemberInfo>();
            List <ulong>      unassigned_ids     = new List <ulong>();

            foreach (Tuple <MemberInfo, DataMemberAttribute> tup in members)
            {
                MemberInfo          mi  = tup.Item1;
                DataMemberAttribute dma = tup.Item2;

                string name = dma.IsNameSetExplicitly ? dma.Name : mi.Name;
                if (dma.IsRequired && !items.Contains(name))
                {
                    return(SerializationStatus.Failure);
                }

                SerializedObject so = (SerializedObject)info.GetValue(name, typeof(SerializedObject));

                if (!instance.TryGetOrDeserialize(GetMemberType(mi), so, out object value) || unassigned_members.Count > 0)
                {
                    unassigned_members.Add(mi);
                    unassigned_ids.Add(so.Id);
                    continue;
                }

                SetValue(obj, mi, value);
            }

            if (unassigned_members.Count > 0)
            {
                return(SerializationStatus.FixupLater(obj, CreateObjectFixup(unassigned_members.ToArray(), context), unassigned_ids.ToArray()));
            }

            InvokeOnSerialize(obj, context, false, true);
            return(SerializationStatus.Deserialized(obj));
        }
Ejemplo n.º 7
0
        public SerializationStatus Deserialize(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            Type[] args             = t.GetGenericArguments();
            Type   key_array_type   = args[0].MakeArrayType();
            Type   value_array_type = args[1].MakeArrayType();

            object o = Activator.CreateInstance(t);

            SerializedObject serialized_keys   = (SerializedObject)info.GetValue("keys", typeof(SerializedObject));
            SerializedObject serialized_values = (SerializedObject)info.GetValue("values", typeof(SerializedObject));

            //bitwise & operator used to make sure both run
            if (instance.TryGetOrDeserializeFully(key_array_type, serialized_keys, out object keys) & instance.TryGetOrDeserializeFully(value_array_type, serialized_values, out object values))
            {
                AddAllValues(o, keys, values, args[0], args[1]);
                return(SerializationStatus.Deserialized(o));
            }

            return(SerializationStatus.FixupLater(o, CreateFixup(args[0], args[1]), new ulong[] { serialized_keys.Id, serialized_values.Id }));
        }
Ejemplo n.º 8
0
        public SerializationStatus Deserialize(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            if (!t.IsArray)
            {
                return(SerializationStatus.Failure);
            }

            if (t == typeof(Array))
            {
                return(SerializationStatus.Failure);
            }

            SerializedObject[] serialized_array = (SerializedObject[])info.GetValue("array", typeof(SerializedObject[]));

            Type e_type = t.GetElementType();

            Array array = Array.CreateInstance(e_type, serialized_array.Length);

            List <int>   missing_indexes = new List <int>();
            List <ulong> missing_ids     = new List <ulong>();

            for (int i = 0; i < serialized_array.Length; i++)
            {
                if (!instance.TryGetOrDeserialize(e_type, serialized_array[i], out object value))
                {
                    missing_ids.Add(serialized_array[i].Id);
                    missing_indexes.Add(i);
                    continue;
                }

                array.SetValue(value, i);
            }

            if (missing_indexes.Count > 0)
            {
                return(SerializationStatus.FixupLater(array, CreateArrayFixup(missing_indexes.ToArray()), missing_ids.ToArray()));
            }

            return(SerializationStatus.Deserialized(array));
        }
Ejemplo n.º 9
0
        private SerializationStatus DeserializeObject(SerializationInstance instance, Type t, SerializationInfo s_info, StreamingContext context)
        {
            object obj = t.GetConstructor(Type.EmptyTypes)?.Invoke(Array.Empty <object>());

            if (obj == null)
            {
                return(SerializationStatus.Failure);
            }

            Dictionary <string, MemberInfo> map = t.GetMembers().Where(mi => CheckGetAndSet(mi, false, true, true)).ToDictionary(mi => mi.Name);

            List <ulong>      missing_ids     = new List <ulong>();
            List <MemberInfo> missing_members = new List <MemberInfo>();

            foreach (SerializationEntry entry in s_info)
            {
                if (!map.TryGetValue(entry.Name, out MemberInfo mi))
                {
                    continue;
                }

                SerializedObject so = (SerializedObject)s_info.GetValue(entry.Name, typeof(SerializedObject));

                if (!instance.TryGetOrDeserialize(GetMemberType(mi), so, out object value))
                {
                    missing_members.Add(mi);
                    missing_ids.Add(so.Id);
                    continue;
                }

                SetValue(obj, mi, value);
            }

            if (missing_members.Count > 0)
            {
                return(SerializationStatus.FixupLater(obj, CreateObjectFixup(missing_members.ToArray(), context), missing_ids.ToArray()));
            }

            return(SerializationStatus.Deserialized(obj));
        }
Ejemplo n.º 10
0
 public SerializationStatus Deserialize(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
 => SerializationStatus.Deserialized(Guid.Parse(info.GetString("value")));