-
Notifications
You must be signed in to change notification settings - Fork 7
ISerializer interface
The ISerializer interface as used by the ClientSideGameStorageAsset to (de)serialize data. The ClientSideGameStorageAsset has internal support for xml serialization, so the interface is mostly used to get access to a Json Serializer of choice.
- For Unity ISerialize can use the build-in JsonUtility class.
- For Xamarin Newtonsoft.net is more appropriate as it's often already a dependency of some package being used.
-
Note: JsonUtility only supports fields (no properties), is fast but limited.
-
Note: This interface wil be changed a bit (removal of the generic paramater of Deserialize as it might result in compilation issues caused by code stripping during the IL2CPP phase of compilation for iOS.
#region ISerializer Members public object Deserialize<T>(string text, SerializingFormat format) { // Debug.Log(text); // Debug.Log(typeof(T).FullName); return JsonUtility.FromJson<T>(text); } public string Serialize(object obj, SerializingFormat format) { return JsonUtility.ToJson(obj); } public bool Supports(SerializingFormat format) { switch (format) { //case SerializingFormat.Binary: // return false; case SerializingFormat.Xml: return false; case SerializingFormat.Json: return true; } return false; } #endregion ISerializer Members
-
Xml is handled in the ClientSideGameStorageAsset using the .Net 3.5 XmlSerializer class, but can be handled in the bridge as well if neccesary.
-
Binary serialization is not supported due to portability issues (portable assemblies).
-
When using .Net the creation of an XmlSerializer for a particular class has a one-time overhead that can be time-wise quite large. Mono does not seem to have this issue.
#region ISerializer Members /// <summary> /// Supports the given format. /// </summary> /// /// <param name="format"> Describes the format to use. </param> /// /// <returns> /// true if it succeeds, false if it fails. /// </returns> public bool Supports(SerializingFormat format) { switch (format) { //case SerializingFormat.Binary: // return false; case SerializingFormat.Xml: return false; case SerializingFormat.Json: return true; } return false; } /// <summary> /// Deserialize this object to the given textual representation and format. /// </summary> /// /// <param name="text"> The text to deserialize. </param> /// <param name="type"> The type to deserialize. </param> /// <param name="format"> Describes the format to use. </param> /// /// <returns> /// An object. /// </returns> public object Deserialize<T>(string text, SerializingFormat format) { return JsonConvert.DeserializeObject(text, typeof(T)); } /// <summary> /// Serialize this object to the given textual representation and format. /// </summary> /// /// <param name="obj"> The object to serialize. </param> /// <param name="format"> Describes the format to use. </param> /// /// <returns> /// A string. /// </returns> public string Serialize(object obj, SerializingFormat format) { return JsonConvert.SerializeObject(obj, Formatting.Indented); } #endregion ISerializer Members