Пример #1
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));
        }
Пример #2
0
        private SerializationStatus SerializeContract(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
        {
            Type t = obj.GetType();

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

            InvokeOnSerialize(obj, context, true, false);

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

                string name = attr.IsNameSetExplicitly ? attr.Name : mi.Name;

                object value = GetValue(obj, mi);

                if (!attr.IsRequired && !attr.EmitDefaultValue && value == DefaultValue(GetMemberType(mi)))
                {
                    continue;
                }

                info.AddValue(name, value);
            }

            InvokeOnSerialize(obj, context, true, true);

            return(SerializationStatus.Success);
        }
Пример #3
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));
        }
Пример #4
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));
        }
Пример #5
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));
        }
Пример #6
0
        private SerializationStatus SerializeObject(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
        {
            foreach (MemberInfo mi in obj.GetType().GetMembers().Where(mi => CheckGetAndSet(mi, true, false, true)))
            {
                info.AddValue(mi.Name, GetValue(obj, mi));
            }

            return(SerializationStatus.Success);
        }
Пример #7
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()));
        }
Пример #8
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));
        }
Пример #9
0
        public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
        {
            SerializationInfo proxy = Deep ? new SerializationInfo(obj.GetType(), new FormatterConverter()) : null;

            if (Serializer == null || !Serializer(Deep ? proxy : info, context, obj))
            {
                return(SerializationStatus.Failure);
            }

            if (Deep)
            {
                instance.CopyAndSerialize(proxy, info);
            }
            return(SerializationStatus.Success);
        }
Пример #10
0
        public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
        {
            Type t = obj.GetType();

            if (t.IsSerializable)
            {
                return(SerializeSerializable(instance, obj, info, context));
            }

            if (t.CustomAttributes.Any(d => typeof(DataContractAttribute).IsAssignableFrom(d.AttributeType)))
            {
                return(SerializeContract(instance, obj, info, context));
            }

            return(SerializeObject(instance, obj, info, context));
        }
Пример #11
0
        public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
        {
            if (!(obj is Array array))
            {
                return(SerializationStatus.Failure);
            }

            SerializedObject[] serialized_array = new SerializedObject[array.Length];

            for (int i = 0; i < array.Length; i++)
            {
                serialized_array[i] = instance.Serialize(array.GetValue(i));
            }

            info.AddValue("array", serialized_array);

            return(SerializationStatus.Success);
        }
Пример #12
0
        public SerializationStatus Deserialize(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
        {
            if (t.IsInterface || t.IsAbstract)
            {
                return(SerializationStatus.Failure);
            }

            if (t.IsSerializable)
            {
                return(DeserializeSerializable(instance, t, info, context));
            }

            if (t.CustomAttributes.Any(d => typeof(DataContractAttribute).IsAssignableFrom(d.AttributeType)))
            {
                return(DeserializeContract(instance, t, info, context));
            }

            return(DeserializeObject(instance, t, info, context));
        }
Пример #13
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 }));
        }
Пример #14
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));
        }
Пример #15
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));
        }
Пример #16
0
        public SerializationStatus SerializeSerializable(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
        {
            InvokeOnSerialize(obj, context, true, false);
            if (obj is ISerializable serializable)
            {
                serializable.GetObjectData(info, context);
                InvokeOnSerialize(obj, context, true, true);
                return(SerializationStatus.Success);
            }

            MemberInfo[] infos   = FormatterServices.GetSerializableMembers(obj.GetType(), context);
            object[]     objects = FormatterServices.GetObjectData(obj, infos);

            for (int i = 0; i < infos.Length; i++)
            {
                info.AddValue(infos[i].Name, objects[i]);
            }

            InvokeOnSerialize(obj, context, true, true);

            return(SerializationStatus.Success);
        }
Пример #17
0
 public SerializationStatus Deserialize(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
 => SerializationStatus.Failure;
Пример #18
0
 public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
 => SerializationStatus.Failure;
Пример #19
0
 public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
 {
     info.AddValue("value", ((Guid)obj).ToString());
     return(SerializationStatus.Success);
 }
Пример #20
0
 public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
 => SerializationStatus.UseAlternativeType(RedirectTo);
Пример #21
0
 public SerializationStatus Deserialize(SerializationInstance instance, Type t, SerializationInfo info, StreamingContext context)
 => SerializationStatus.Deserialized(Guid.Parse(info.GetString("value")));
Пример #22
0
        public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
        {
            Type[] args = obj.GetType().GetGenericArguments();

            GetAllValues(obj, out object keys, out object values, args[0], args[1]);
Пример #23
0
 public SerializationStatus Serialize(SerializationInstance instance, object obj, SerializationInfo info, StreamingContext context)
 {
     info.AddValue("s_value", (string)obj, typeof(string));
     return(SerializationStatus.Success);
 }