Exemplo n.º 1
0
        public override void WriteObject(Stream stream, object obj, SerializationContext context)
        {
            IEnumerable enumerable = (IEnumerable)obj;

            foreach (var item in enumerable)
            {
                FullSerializer.Serialize(stream, item, context);
            }
        }
 public void SerializeAndDeserialize_should_return_the_same_object(object obj)
 {
     using (var stream = new MemoryStream())
     {
         FullSerializer.Serialize(stream, obj);
         stream.Position = 0;
         object result = FullSerializer.Deserialize(stream);
         Assert.Equal(obj, result);
     }
 }
Exemplo n.º 3
0
        /// <summary> Serialize the data using json format. </summary>
        public static string Serialize(object data, bool isPretty = false)
        {
            // using Unity built in solution
            // return JsonUtility.ToJson(data, isPretty);

            //-------------------------------------------------------

            //using Fullserializer
            var type = data.GetType();

            return(FullSerializer.Serialize(type, data, isPretty));
        }
Exemplo n.º 4
0
        /// <summary> Deserialize the data from string json format. </summary>
        public static T Deserialize <T>(string json) where T : class
        {
            //using Unity built in solution
            //return JsonUtility.FromJson<T>(json);

            //-------------------------------------------------------

            //using Fullserializer
            var type = typeof(T);

            return(FullSerializer.Deserialize(type, json) as T);
        }
Exemplo n.º 5
0
        public override object Read(Stream stream, DeserializationContext context)
        {
            Type   pointerType = stream.ReadType();
            object obj         = FullSerializer.Deserialize(stream, context);
            IntPtr intPtr      = Marshal.AllocHGlobal(Marshal.SizeOf(pointerType.GetElementType()));

            Marshal.StructureToPtr(obj, intPtr, false);
            unsafe
            {
                return(Pointer.Box(intPtr.ToPointer(), pointerType));
            }
        }
Exemplo n.º 6
0
        public override void Fill(Stream stream, object obj, DeserializationContext context)
        {
            Type type = obj.GetType();

            FieldInfo[] fields = type.GetFields(FieldsBindingFlags);

            foreach (FieldInfo t in fields)
            {
                object fieldValue = FullSerializer.Deserialize(stream, context);
                t.SetValue(obj, fieldValue);
            }
        }
Exemplo n.º 7
0
 public unsafe void SerializeAndDeserialize_should_return_pointer_to_object_that_is_equal()
 {
     using (var stream = new MemoryStream())
     {
         double  d   = 3.4;
         double *ptr = &d;
         FullSerializer.Serialize(stream, Pointer.Box(ptr, typeof(double *)));
         stream.Position = 0;
         object  result    = FullSerializer.Deserialize(stream);
         double *resultPtr = (double *)Pointer.Unbox(result);
         Assert.Equal(d, *resultPtr);
     }
 }
Exemplo n.º 8
0
        public override void WriteObjectWithoutReference(Stream stream, object obj, SerializationContext context)
        {
            Type type = obj.GetType();

            stream.WriteString(type.FullName);

            FieldInfo[] fields = type.GetFields(FieldsBindingFlags);

            foreach (FieldInfo t in fields)
            {
                FullSerializer.Serialize(stream, t.GetValue(obj), context);
            }
        }
        public void Serialize_and_deserialize_array_with_cycles_should_handle_cycle()
        {
            object[] x = new object[1];
            x[0] = x;

            using (var stream = new MemoryStream())
            {
                FullSerializer.Serialize(stream, x);
                stream.Position = 0;
                object result = FullSerializer.Deserialize(stream);

                Assert.True(result is object[] resultX && resultX.Length == 1 && resultX[0].Equals(resultX));
            }
        }
        public void Serialize_and_deserialize_list_with_cycles_should_handle_cycle()
        {
            List <object> x = new List <object>();

            x.Add(x);

            using (var stream = new MemoryStream())
            {
                FullSerializer.Serialize(stream, x);
                stream.Position = 0;
                object result = FullSerializer.Deserialize(stream);

                Assert.True(result is List <object> resultX && resultX.Count == 1 && resultX[0].Equals(resultX));
            }
        }
        public void Serialize_and_deserialize_tree_node_data_with_cycles_should_handle_cycle()
        {
            TreeNode x = new TreeNode();
            TreeNode y = new TreeNode();

            x.Parent = y;
            y.Parent = x;

            using (var stream = new MemoryStream())
            {
                FullSerializer.Serialize(stream, x);
                stream.Position = 0;
                object result = FullSerializer.Deserialize(stream);

                Assert.True(result is TreeNode resultX && resultX.Id.Equals(x.Id) && resultX.Parent.Id.Equals(x.Parent.Id));
            }
        }
Exemplo n.º 12
0
        public override void WriteObject(Stream stream, object obj, SerializationContext context)
        {
            Type type = obj.GetType();

            FieldInfo[] fields = type.GetFields(CustomObjectTypeHandler.FieldsBindingFlags);
            FieldInfo   pointerTypeFieldInfo = fields[1];
            object      pointerTypeObj       = pointerTypeFieldInfo.GetValue(obj);
            Type        pointerType          = (Type)pointerTypeObj;

            stream.WriteString(pointerType.FullName);
            Type elementType = pointerType.GetElementType();

            unsafe
            {
                object o = Marshal.PtrToStructure(new IntPtr(Pointer.Unbox(obj)), elementType);
                FullSerializer.Serialize(stream, o);
            }
        }
Exemplo n.º 13
0
        public override void Fill(Stream stream, object obj, DeserializationContext context)
        {
            Array array = (Array)obj;

            int[] lengths = new int[array.Rank];
            for (int i = 0; i < lengths.Length; i++)
            {
                lengths[i] = array.GetLength(i);
            }

            int[][] indexes = new[] { new int[0] };
            indexes = lengths.Select(l => Enumerable.Range(0, l).ToArray()).Aggregate(indexes, (current, length) => Aggregate(current, length).ToArray());

            foreach (var index in indexes)
            {
                array.SetValue(FullSerializer.Deserialize(stream, context), index);
            }
        }