[Serializable, ProtoContract()] public class TTT { [TDataMember, ProtoMember(1)] public string s = "compact"; [TDataMember, ProtoMember(2)] public bool f = true; [TDataMember, ProtoMember(3)] public string s2 = "schema"; [TDataMember, ProtoMember(4)] public short i = 0; }
PlatformNotSupportedException: On-the-fly enum serializer generation is not supported in Unity iOS. Use pre-generated serializer instead. MsgPack.Serialization.ReflectionSerializers.ReflectionSerializerHelper.CreateReflectionEnuMessagePackSerializer[State] (MsgPack.Serialization.SerializationContext context) MsgPack.Serialization.MessagePackSerializer.CreateReflectionInternal[State] (MsgPack.Serialization.SerializationContext context) MsgPack.Serialization.MessagePackSerializer.CreateReflectionInternal (MsgPack.Serialization.SerializationContext context, System.Type targetType) MsgPack.Serialization.SerializationContext.GetSerializer (System.Type targetType, System.Object providerParameter) MsgPack.Serialization.ReflectionSerializers.ReflectionSerializerHelper.GetMetadata (MsgPack.Serialization.SerializationContext context, System.Type targetType, System.Func`2[]& getters, System.Action`2[]& setters, System.Reflection.MemberInfo[]& memberInfos, MsgPack.Serialization.DataMemberContract[]& contracts, MsgPack.Serialization.IMessagePackSerializer[]& serializers) MsgPack.Serialization.ReflectionSerializers.ReflectionObjectMessagePackSerializer`1[TestS+TestC]..ctor (MsgPack.Serialization.SerializationContext context) MsgPack.Serialization.MessagePackSerializer.CreateReflectionInternal[TestC] (MsgPack.Serialization.SerializationContext context) MsgPack.Serialization.SerializationContext.GetSerializer[TestC] (System.Object providerParameter) MsgPack.Serialization.SerializationContext.GetSerializer[TestC] () /// [,] string ArgumentException: 'System.String[,]' is not compatible for 'System.String[]'. Parameter name: objectTree MsgPack.Serialization.MessagePackSerializer`1[System.String[]].MsgPack.Serialization.IMessagePackSerializer.PackTo (MsgPack.Packer packer, System.Object objectTree) MsgPack.Serialization.ReflectionSerializers.ReflectionObjectMessagePackSerializer`1[TestS+TestC].PackMemberValue (MsgPack.Packer packer, .TestC objectTree, Int32 index) MsgPack.Serialization.ReflectionSerializers.ReflectionObjectMessagePackSerializer`1[TestS+TestC].PackToCore (MsgPack.Packer packer, .TestC objectTree) MsgPack.Serialization.MessagePackSerializer`1[TestS+TestC].PackTo (MsgPack.Packer packer, .TestC objectTree) MsgPack.Serialization.MessagePackSerializer`1[TestS+TestC].Pack (System.IO.Stream stream, .TestC objectTree) /// etc SerializationException: Non generic collection may contain only MessagePackObject type. MsgPack.Serialization.DefaultSerializers.NonGenericEnumerableSerializerBase`1[T].PackToCore (MsgPack.Packer packer, .T objectTree) MsgPack.Serialization.MessagePackSerializer`1[T].MsgPack.Serialization.IMessagePackSerializer.PackTo (MsgPack.Packer packer, System.Object objectTree) MsgPack.Serialization.ReflectionSerializers.ReflectionCollectionSerializer`1[System.Collections.ArrayList].PackToCore (MsgPack.Packer packer, System.Collections.ArrayList objectTree) MsgPack.Serialization.MessagePackSerializer`1[System.Collections.ArrayList].MsgPack.Serialization.IMessagePackSerializer.PackTo (MsgPack.Packer packer, System.Object objectTree) MsgPack.Serialization.ReflectionSerializers.ReflectionObjectMessagePackSerializer`1[TestS+TestC].PackMemberValue (MsgPack.Packer packer, .TestC objectTree, Int32 index) MsgPack.Serialization.ReflectionSerializers.ReflectionObjectMessagePackSerializer`1[TestS+TestC].PackToCore (MsgPack.Packer packer, .TestC objectTree) MsgPack.Serialization.MessagePackSerializer`1[TestS+TestC].PackTo (MsgPack.Packer packer, .TestC objectTree) MsgPack.Serialization.MessagePackSerializer`1[TestS+TestC].Pack (System.IO.Stream stream, .TestC objectTree)
NotSupportedException: Multi-dimension arrays are supported ProtoBuf.Meta.MetaType.ResolveListTypes (ProtoBuf.Meta.TypeModel model, System.Type type, System.Type& itemType, System.Type& defaultType) ProtoBuf.Meta.MetaType.ApplyDefaultBehaviour (Boolean isEnum, ProtoBuf.ProtoMemberAttribute normalizedAttribute) ProtoBuf.Meta.MetaType.ApplyDefaultBehaviour () ProtoBuf.Meta.RuntimeTypeModel.FindOrAddAuto (System.Type type, Boolean demand, Boolean addWithContractOnly, Boolean addEvenIfAutoDisabled) ProtoBuf.Meta.RuntimeTypeModel.GetKey (System.Type type, Boolean demand, Boolean getBaseKey)
TTT c = new TTT(); TSerizalization serizalization = new TSerizalization(); bytes = serizalization.Serizalize(c, true); System.IO.File.WriteAllBytes("d:\\s.dat", bytes); Debug.LogError("T complete " + bytes.Length ); json = JsonConvert.SerializeObject(c); System.IO.File.WriteAllText("d:\\s.json", json); Debug.LogError("J complete " + json.Length); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formater = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formater.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full; System.IO.MemoryStream mstream = new System.IO.MemoryStream(); formater.Serialize(mstream, c); Debug.LogError("B complete " + mstream.ToArray().Length); System.IO.File.WriteAllBytes("d:\\s2.dat", mstream.ToArray()); mstream = new System.IO.MemoryStream(); var serializer = MsgPack.Serialization.SerializationContext.Default.GetSerializer<TTT>(); serializer.Pack(mstream, c); System.IO.File.WriteAllBytes("d:\\s3.dat", mstream.ToArray()); Debug.LogError("M complete " + mstream.ToArray().Length); mstream = new System.IO.MemoryStream(); ProtoBuf.Serializer.Serialize<TTT>(mstream, c); System.IO.File.WriteAllBytes("d:\\s4.dat", mstream.ToArray()); Debug.LogError("P complete " + mstream.ToArray().Length);
public class TMap { public Dictionary<Type, short> StandartTypes { get; protected set; } public Dictionary<Type, short> DataBase { get; protected set; } public Dictionary<short, Type> DataBaseTags { get; protected set; } ... }
public class TData : TContainerBase { public object value; public List<TData> childrens = new List<TData>(); ... }
public class TContainerBase { public short Tag { get; protected set; } public int ArrayRank { get; protected set; } public List<int> ArrayDimension { get; protected set; } ...
public class TContainer : TContainerBase { public int Size { get; protected set; } public List<object> List { get; protected set; } ...
public abstract class TReaderBase { public abstract T Read<T>(byte[] bytes, Assembly assembly); } public abstract class TWriterBase { public abstract byte[] Write(TMap map, TData data); }
public class TSerizalization { protected TMap map; protected TWriterBase writer; protected TReaderBase reader; public TSerizalization() { writer = new TBinaryWriter(); reader = new TBinaryReader(); } public virtual byte[] Serialize(object target, bool callBeforeSerializationMethods = false); public virtual T Deserialize<T>(byte[] bytes, Assembly assembly, bool callAfterDeserializationMethods = false); protected virtual TData Read(object obj) }
public interface IClass { } [System.Serializable ] public class TestC : IClass { [To2dnd.TDataMember] public int a = 10; [To2dnd.TDataMember] public int b = 12; [To2dnd.TDataMember] public string s= "Hello World"; [To2dnd.TDataMember] public State state = State.Close; [To2dnd.TDataMember] public DateTime dt = new DateTime(); [To2dnd.TDataMember] public Type type = typeof(IClass); [To2dnd.TDataMember] public string[,] arr = new string[,] { {"1111", "2222", "3333", "4444" }, {"aaaa", "bbbb", "cccc", "dddd" }, {"321", "32", "2qfs", "12f" } }; [To2dnd.TDataMember] public object classD = new TestC2(); [To2dnd.TDataMember] public TestC1[] array1 = new TestC1[] { new TestC1(), new TestC2(), new TestC2() }; [To2dnd.TDataMember] public ArrayList arr2 = new ArrayList( new string[]{ "list1", "list2" }); [To2dnd.TDataMember] public List<string> list = new List<string>() { "list Item 1", "List Item 2" }; [To2dnd.TDataMember] public Dictionary<string, int> dic = new Dictionary<string, int>() { {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4} }; [To2dnd.TDataMember] public Hashtable ht = new Hashtable() { {"H one", 1}, {"H two", 2}, {"H three", 3}, {"H four", 4} }; [To2dnd.TDataMember] public SortedList<string, int> sl = new SortedList<string, int>() { {"S one", 1}, {"S two", 2}, {"S three", 3}, {"S four", 4} }; [To2dnd.TDataMember] public Dictionary<string, List<string>> dic3 = new Dictionary<string,List<string>>() { {">> 1", new List<string>(){"a1", "a2", "a3"} }, {">> 2", new List<string>(){"b1", "b2", "b3"} }, {">> 3", new List<string>(){"c1", "c2", "c3"} } }; [To2dnd.TDataMember] public List<List<string>> l = new List<List<string>>() { new List<string>(){"a1", "a2", "a3"}, new List<string>(){"b1", "b2", "b3"}, }; [ProtoMember(16)] public Dictionary<string, Dictionary<string, string>> dic4 = new Dictionary<string, Dictionary<string, string>>() { {">> 1", new Dictionary<string, string>() { { "a1", "a2"}, { "a2", "a3"} } }, {">> 2", new Dictionary<string, string>() { { "a1", "a2"}, { "a2", "a3"} } } }; [ProtoMember(17)] public Dictionary<string, Dictionary<string, string>> Dic4 {get; protected set;} [ProtoMember(18)] public Dictionary<string, object> dic333 = new Dictionary<string, object>() { {":@", new List<string>(){"1", "2", "3"}}, {":@2", new TestC2()}, {":@222", "sff"} }; public TestC() { Dic4 = new Dictionary<string,Dictionary<string,string>>() { {">> 1", new Dictionary<string, string>() { { "a1", "a2"}, { "a2", "a3"} } }, {">> 2", new Dictionary<string, string>() { { "a1", "a2"}, { "a2", "a3"} } } }; } } [Serializable] public class TestC1 : IClass { [To2dnd.TDataMember] public float value1 = 10; [To2dnd.TDataMember] public float value2 = 12; } [Serializable ] public class TestC2 : TestC1 { [To2dnd.TDataMember] public float a1 = 10; [To2dnd.TDataMember] public float b2 = 12; [To2dnd.TDataMember] public string str = "Class 1"; [To2dnd.TDataMember] public State state = State.Close; public TestC2() { } [TAfterDeserialization] public void After() { } [TBeforeSerialization] public void Before() { } } public class TestC33 { [To2dnd.TDataMember] public float b2 = 12; [To2dnd.TDataMember] public TestC2 tt = new TestC2(); [ To2dnd.TDataMember] public TestC1[] array1 = new TestC1[] { new TestC1(), new TestC2(), new TestC2() }; [To2dnd.TDataMember] public object classD = new TestC2(); [To2dnd.TDataMember] public Type type = typeof(IClass); }
Source: https://habr.com/ru/post/255399/
All Articles