- Home /
Serialize a list on iPhone
Hey, I'm having trouble with serializing on iPhone, specifically a generic list (List(float)). I've hunted around the forums and as far as I can tell this should work. But it's throwing an exception when trying it:
Couldn't write gameCenterCache: Exception caught: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ExecutionEngineException: Attempting to JIT compile method 'List`1__TypeMetadata:.ctor ()' while running with --aot-only.
at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata (System.Type type) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) [0x00000] in <filename unknown>:0
at Serializer.SerializeObject (System.String filename, .GameCenterCache gameCenterCache) [0x0001f] in /Users/teacup42729/Dropbox/development/TouchScreenTest/Assets/Wind/Scripts/IO/Serializer.cs:24
It seems to work fine in the editor on windows and mac, the file is created and can be read back again. I've also done tests with other object types, my own custom class works fine and any base classes, and basic arrays, and also lists and dictionaries, everything works fine in editor, but Lists and dictionaries won't on the iphone.
Anyone got any ideas? Thanks!
This is my code:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable()]
public class GameCenterCache : ISerializable
{
GameCenterAchievementSerial single;
List<float> testList;
float[] testArray;
public GameCenterCache()
{
single = new GameCenterAchievementSerial("woop", 56f);
testList = new List<float>();
testList.Add(43.4f);
testList.Add(23.3f);
testArray = new float[]{4f, 2f, 9f};
}
public GameCenterCache(SerializationInfo info, StreamingContext context)
{
single = (GameCenterAchievementSerial)info.GetValue("single", typeof(GameCenterAchievementSerial));
testList = (List<float>)info.GetValue("testList", typeof(List<float>));
testArray = (float[])info.GetValue("testArray", typeof(float[]));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("single", single);
info.AddValue("testList", testList);
info.AddValue("testArray", testArray);
}
}
and
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System;
using System.Reflection;
public class Serializer
{
public Serializer()
{
}
public void SerializeObject(string filename, GameCenterCache gameCenterCache)
{
Debug.Log(filename);
try
{
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Binder = new VersionDeserializationBinder();
bFormatter.Serialize(stream, gameCenterCache);
stream.Close();
}
catch (Exception e)
{
Debug.LogWarning("Couldn't write gameCenterCache: Exception caught: " + e);
}
}
public GameCenterCache DeSerializeObject(string filename)
{
Debug.Log(filename);
try
{
Stream stream = File.OpenRead(filename);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Binder = new VersionDeserializationBinder();
GameCenterCache gameCenterCache = (GameCenterCache)bFormatter.Deserialize(stream);
stream.Close();
return gameCenterCache;
}
catch (Exception e)
{
Debug.LogWarning("Couldn't read gameCenterCache: Exception caught: " + e);
return new GameCenterCache();
}
}
}
public sealed class VersionDeserializationBinder : SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName)
{
if ( !string.IsNullOrEmpty(assemblyName) && !string.IsNullOrEmpty(typeName))
{
Type typeToDeserialize = null;
assemblyName = Assembly.GetExecutingAssembly().FullName;
typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
return typeToDeserialize;
}
return null;
}
}
I am seeing this same issue, I am interested to see if anyone has figured out a fix for this yet.
I never found a real solution to this, I ended up creating functions that converted my lists and dictionaries into basic arrays. So when I want to save I convert to array, and when I load I convert back into dictionaries/lists. This works fine for me at the moment, but I wish I had a proper solution.
Answer by antonholmquist · Dec 03, 2013 at 08:04 PM
This issue can be fixed by adding this line before doing any serialization:
``` Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes"); ```
Holy shmokes, I wouldn't have believed it if I didn't see it with my own eyes!
Good knowledge, antonholmquist.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
List of ScriptableObjects lost on project reload 1 Answer
using classes in List<>'s 1 Answer
Passing lists to serialize and deserialize methods for XML parsing 0 Answers
Calling for two variables in a list 1 Answer