There’s a bug in the current version (1.0.10) related to serializing null values in arrays because it doesn’t call WriteFormatting() in the WriteNull() function.
it would be nice to have some Exception in case of trying to deserialize stream which Position != 0. First of all i tried to serialize and deserialize some object just from that MemoryStream and it took me some time to get that stream must be in zero position to deserialization work.
Example in documentation is simple but not actually helpful. RectSerializer code seems more useful.
I can’t get how it is designed to serialize MemoryStream properly.
I have a code like this:
public MemoryStream gameRoomData;
......
writer.WriteMember("g");
writer.WriteValue(src.gameRoomData, typeof(MemoryStream));
writer.WriteObjectEnd();
Trying to execute it generates Exception:
The only way i could find to make it work was this hack in JsonWriterExtentions.cs:
var actualValueType = value.GetType();
//dirty hack start
if (valueType == typeof(System.IO.MemoryStream))
{
actualValueType = typeof(System.IO.Stream);
}
//dirty hack end
var serializer = writer.Context.GetSerializerForType(actualValueType);
Currently it’s throwing “Unexpected token ‘EndOfStream’” it’s quite descriptive.
Yep, description is quite short. It’s hard for me writing documentation in english.
I make few changed related to Stream serialization in source code on GitHub, but your code still will not work without changes. It’s throws exception because it’s don’t know how to serialize MemoryStream, only Stream(base class of MemoryStream) and you should:
a) use WriteValue(memoryStream, typeof(Stream))
b) use class with Stream property/field
c) make hint to serializer how to serialize MemoryStream
class Program
{
public class MyClass
{
public MemoryStream mySteam;
}
public static void Main()
{
// prepare context
var context = new SerializationContext
{
Serializers = { { typeof(MemoryStream), StreamSerializer.Instance } },
// or
SerializerFactory = type => (type == typeof(MemoryStream)) ? StreamSerializer.Instance : null,
// this will suppress _type member
Options = SerializationOptions.SuppressTypeInformation
};
var myObject = new MyClass
{
mySteam = new MemoryStream(...)
};
var tmpStream = new MemoryStream();
MsgPack.Serialize(myObject, tmpStream, context);
// reading MyClass
tmpStream.Position = 0;
myObject = MsgPack.Deserialize<MyClass>(tmpStream, context);
// reading MyClass manually
tmpStream.Position = 0;
var reader = new MsgPackReader(tmpStream, context);
reader.ReadObjectBegin();
if (reader.ReadMember() != "mySteam") throw JsonSerializationException.UnexpectedMemberName(reader.Value.AsString, "mySteam", reader);
var readedStream = (MemoryStream)reader.ReadValue(typeof(MemoryStream));
reader.ReadObjectEnd(nextToken: false);
}
}
And you should’t write custom TypeSerializers for your classes if they too complex for serialization. It’s better to declare “data storage” class and put all data into it. Writing custom TypeSerializer is quite advanced solution.
Hi, i’m new to unity, i used binary formater to do save and load before. then after learned a bit about other method to save and load, i found option like bf (ofcourse), json (the readable ones), and msgpack (the smallest ones). if you don’t mind, can you give me example (complete ones) on save() and load() function in unity using persistentdatapath and msgpack please… thanks a lot.
Hi @DenisZykov
When import to Unity 2018.3.0f2, its throw below error:
Assets\Plugins\GameDevWare.Serialization\Metadata\ReflectionUtils.cs(86,34): error CS0246: The type or namespace name 'DynamicMethod' could not be found (are you missing a using directive or an assembly reference?)
Unity 2018.3.0f2 which default script runtime version is .NET 4.x, and SRV .NET 3.5.x was deprecated, so will be remove in the future. Please fix this ! Thanks in advanced