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); } }
/// <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); }
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)); } }
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); } }
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); } }
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)); } }
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); } }