InvalidOperationException: code is invalid. code:10 format:positive fixint
MessagePack.Decoders.InvalidStringSegment.Read (System.Byte[] bytes, Int32 offset, System.Int32& readSize) (at /Scripts/Utils/MessagePack/MessagePackBinary.cs:5213)
MessagePack.MessagePackBinary.ReadStringSegment (System.Byte[] bytes, Int32 offset, System.Int32& readSize) (at /Scripts/Utils/MessagePack/MessagePackBinary.cs:1939)
MessagePack.Formatters.com_eb_mm_game_TrophyConfigFormatter.Deserialize (System.Byte[] , Int32 , IFormatterResolver , System.Int32& )
MessagePack.Formatters.DictionaryFormatterBase`4[System.Int32,com.eb.mm.game.TrophyConfig,System.Collections.Generic.Dictionary`2[System.Int32,com.eb.mm.game.TrophyConfig],System.Collections.Generic.Dictionary`2[System.Int32,com.eb.mm.game.TrophyConfig]].Deserialize (System.Byte[] bytes, Int32 offset, IFormatterResolver formatterResolver, System.Int32& readSize) (at /Scripts/Utils/MessagePack/Formatters/DictionaryFormatter.cs:339)
MessagePack.MessagePackSerializer.Deserialize[Dictionary`2] (System.Byte[] bytes, IFormatterResolver resolver) (at /Scripts/Utils/MessagePack/MessagePackSerializer.cs:133)
MessagePack.MessagePackSerializer.Deserialize[Dictionary`2] (System.Byte[] bytes) (at /Scripts/Utils/MessagePack/MessagePackSerializer.cs:124)
com.eb.mm.game.GameDataManager+<loadTrophyCfg>c__Iterator0.MoveNext () (at /Scripts/GameData/GameDataManager.cs:44)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
I use msgpack-python to dumps a simple dict {1:{'id':1, 'name':'haha','exp': 10 }}
I got this error when i unpack in unity.
Perhaps the binary from python and the type to convert do not match.
First of all, please check the exact binary format with MessagePackSerializer.ToJson.
I've checked these code, it works.
import msgpack
data = {1: {"id":1, "name" :"haha", "exp":10}};
bin = msgpack.packb(data)
f = open("test.bin", "wb")
f.write(bin)
f.close()
class Program
{
static void Main(string[] args)
{
var path = @"test.bin";
var bin = File.ReadAllBytes(path);
// JSON printout key as string so there is no way to check if the key is int....
// {"1":{"id":1,"name":"haha","exp":10}}
Console.WriteLine(MessagePackSerializer.ToJson(bin));
// ok to deserialize
var check = MessagePackSerializer.Deserialize<Dictionary<int, TrophyConfig>>(bin);
}
}
[MessagePackObject(true)]
public class TrophyConfig
{
public int id { get; set; }
public string name { get; set; }
public int exp { get; set; }
}
If anyone else finds this page as the only result of googling "code is invalid. code:10 format:positive fixint", I have a solution for you:
If you were trying to serialize in another language (for example: C++ or Python) and deserialize in C# and you get this error, you were trying to serialize the number "10" and you opened the file in text mode, not in binary mode.
If you open a file in text mode in Windows then every n (ASCII 10) will have r (ASCII 13) prepended if it doesn鈥檛 exist. So the number "10" was treated as "line feed" or "n" and Windows decided to prepend "carriage return" or "r" before it, because it wants "rn" in files opened in text mode. Now your file has a 13 followed by a 10 and when deserializing that in C# the MessagePack will read the 13 and give you error when it encounters the next number 10.
Perhaps the binary from python and the type to convert do not match.
First of all, please check the exact binary format withMessagePackSerializer.ToJson.I've checked these code, it works.
import msgpack data = {1: {"id":1, "name" :"haha", "exp":10}}; bin = msgpack.packb(data) f = open("test.bin", "wb") f.write(bin) f.close()class Program { static void Main(string[] args) { var path = @"test.bin"; var bin = File.ReadAllBytes(path); // JSON printout key as string so there is no way to check if the key is int.... // {"1":{"id":1,"name":"haha","exp":10}} Console.WriteLine(MessagePackSerializer.ToJson(bin)); // ok to deserialize var check = MessagePackSerializer.Deserialize<Dictionary<int, TrophyConfig>>(bin); } } [MessagePackObject(true)] public class TrophyConfig { public int id { get; set; } public string name { get; set; } public int exp { get; set; } }I moving Json to MessagePack,
I got MessagePack.MessagePackSerializationException: code is invalid. code: 10 format: positive fixint When trying to Deserialize a Stream.
How to fix that?
Thank!
Did you serialize the stream yourself? Are you using a custom formatter? If you're using a custom formatter, make sure you always read and write exactly one msgpack token.
Did you serialize the stream yourself? Are you using a custom formatter? If you're using a custom formatter, make sure you always read and write exactly one msgpack token.
Thank for your reply!
I read HttpResponseMessage from API and use .Content.ReadAsStreamAsync() to convert stream
then I put Stream in MessagePackSerializer.Deserialize
Well that error shows up when what you try to deserialize doesn't match what was serialized. Or when you're using a custom formatter that has a bug in it.
Well that error shows up when what you try to deserialize doesn't match what was serialized. Or when you're using a custom formatter that has a bug in it.
My app worked fine with Json, now I鈥檓 moving to MessagePack with MyModel
Deserialize<MyModel>(stream)
Have a good day!
My app worked fine with Json
That means nothing. It's an entirely different serialization system. And I can't help you if you don't answer my questions or provide repro code.
My app worked fine with Json
That means nothing. It's an entirely different serialization system. And I can't help you if you don't answer my questions or provide repro code.
Thank @AArnott !
I鈥檒l follow your guide about Serialize before Deserialize. And I鈥檒l will come back to tell you soon.
Same problem, I use a custom formatter.
Same answer, @Caskia. This is a general error that happens any time your formatter expects to deserialize something that doesn't match what was previously serialized. This is either an incompatibility between two implementations or a bug in the formatter. We would need a repro to help you in investigating it.
Most helpful comment
If anyone else finds this page as the only result of googling "code is invalid. code:10 format:positive fixint", I have a solution for you:
If you were trying to serialize in another language (for example: C++ or Python) and deserialize in C# and you get this error, you were trying to serialize the number "10" and you opened the file in text mode, not in binary mode.
If you open a file in text mode in Windows then every n (ASCII 10) will have r (ASCII 13) prepended if it doesn鈥檛 exist. So the number "10" was treated as "line feed" or "n" and Windows decided to prepend "carriage return" or "r" before it, because it wants "rn" in files opened in text mode. Now your file has a 13 followed by a 10 and when deserializing that in C# the MessagePack will read the 13 and give you error when it encounters the next number 10.