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