- Home /
Serialize and deserialize class to Byte[] on IOS
How can i serialize and deserialize a class to a Byte Array on IOS.
I mean that I write something like
byte[] SerializeBytes = SerializeObject<ClassToSerialiteIn> (InstanceOfTheClassToSerialize);
and
ClassToSerialiteIn someClass = DeserializeObject<ClassToSerialiteIn> (SerializedBytes);
Answer by Alanisaac · Feb 07, 2015 at 07:41 PM
Not sure from your question if you actually want a byte array or a string, but here is a solution for a byte array. The answer should be applicable to Mono as well:
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
// ...
object obj = new object ();
byte[] bytes;
IFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, obj);
bytes = stream.ToArray();
}
Note that you probably shouldn't be using byte arrays for persistence of classes, since it won't be compatible with any changes to the class or to the Mono .NET framework, and won't be portable to any other platforms. I'd only use it if you're transporting data between the exact same version of the same application. Otherwise you might want to look into JSON, XML, or even a serverless database.
In the way i wrote it (see Code below), it works in the Editor but not on IOS. I think stuff like IFormatter just does not work on IOS.
public static byte[] SerializeObject <T>(T serializableObject)
{
T obj = serializableObject;
IFormatter formatter = new BinaryFormatter();
using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream())
{
formatter.Serialize(stream, obj);
return stream.ToArray();
}
}
public static T DeserializeObject <T>(byte[] serilizedBytes)
{
IFormatter formatter = new BinaryFormatter ();
using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream(serilizedBytes))
{
return (T) formatter.Deserialize (stream);
}
}
But with the power of X$$anonymous$$L I have managed to write this version, which works on IOS.
public static byte[] SerializeObject <T>(T serializableObject)
{
T obj = serializableObject;
using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream())
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
x.Serialize (stream,obj);
return stream.ToArray ();
}
}
public static T DeserializeObject <T>(byte[] serilizedBytes)
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer (typeof(T));
using ($$anonymous$$emoryStream stream = new $$anonymous$$emoryStream(serilizedBytes))
{
return (T) x.Deserialize (stream);
}
}
Answer by BytesCrafter · Feb 22, 2017 at 05:09 AM
I tested the script of @alfalfasprout. Yeah! its working. Now what is the sample used of this? First, for example: you want to send an entire class with variables for sync player prefab on the current device to other device which have the clone of the said playerPrefab in Multiplayer Implementation. Just sharing hopefully this help.
Your answer
Follow this Question
Related Questions
Fastest C# Byte[] to String conversion? 1 Answer
Multiple Cars not working 1 Answer
Using System.Reflection with iOS 2 Answers
Texture2D.ReadPixels not working on iOS (GL error 0x0502) 1 Answer
Distribute terrain in zones 3 Answers