msgpack-unity3d
msgpack-unity3d copied to clipboard
Unexpected token readed 'BeginArray' while 'BeginObject' is expected.
Hi. I am using masgpack along with Best Http/2 in Unity project to deserialize SignalR messages. I used this guide to set it. I can't fix the following exception:
{"tid":1,"div":"WebSocketTransport","msg":"OnMessage(byte[])
","ex": [{"msg": "Unexpected token readed 'BeginArray' while 'BeginObject' is expected.",
"stack": " at GameDevWare.Serialization.Serializers.ObjectSerializer.Deserialize
(GameDevWare.Serialization.IJsonReader reader) [0x00026] in ...
Here is my code for SignalR hub connection:
_connection = new HubConnection(url, new MessagePackProtocol(), options);
_connection.On<StepResponseMessage>("Step", (step) =>
{
Debug.Log("Step.result: " + step.Result);
});
Here is response message model:
[DataContract]
public class StepResponseMessage
{
[DataMember(Order = 0)]
public int Turn { get; set; }
[DataMember(Order = 1)]
public long TimeMicroseconds { get; set; }
[DataMember(Order = 2)]
public IReadOnlyList<MatchCollection> Changes { get; set; }
[DataMember(Order = 3)]
public IReadOnlyList<AttackInfo> Player { get; set; }
[DataMember(Order = 4)]
public IReadOnlyList<RecoveryInfo> PlayerRecovery { get; set; }
[DataMember(Order = 5)]
public IReadOnlyList<AttackInfo> Enemy { get; set; }
[DataMember(Order = 6)]
public IReadOnlyList<RecoveryInfo> EnemyRecovery { get; set; }
[DataMember(Order = 7)]
public string? Result { get; set; }
[DataMember(Order = 8)]
public IReadOnlyList<AbilityInfo> PlayerAbilities { get; set; }
[DataMember(Order = 9)]
public IReadOnlyList<AbilityInfo> EnemyAbilities { get; set; }
[DataMember(Order = 10)]
public IReadOnlyList<GenericAbilityActionInfo> AbilityActions { get; set; }
}
Without _connection.On<StepResponseMessage>
call I get the following message to message _connection.OnMessage
callback:
I am sure that StepResponseMessage corresponds to the messagepack object from arguments. I am using v2.4.3 from asset store.
Could you help me with it? Am I do something wrong? Thanks.
Hi @Mol0ko!
This is must be Dictionary<KeyT, ValueT>
representation problem. Some serializers serialize them as arrays of [key, value]
tuples, and some as arrays of { "key": xxx, "value": xxx }
objects.
Can you transform message.arguments[0]
to JSON and review find where such Dictionary is located and how it is represented (tuples/objects etc).
GameDevWare.Serialization.Json.SerializeToString(message.arguments[0]);
@deniszykov thanks for your answer. Here is an example of GameDevWare.Serialization.Json.SerializeToString(message.arguments[0]);
:
[
1,
1632823892325699,
[
[
[
[
[3,0,1,3,1,3],
[2,0,0,3,2,3],
[3,2,2,0,1,0],
[2,1,0,2,1,1],
[3,3,2,1,3,3]
]
],
[]
]
],
[],
[],
[
[0,1,1.2,66,2664,null,null],
[1,1,1.2,22,2642,null,null]
],
[],
null,
[
[0,1,false,false],
[1,1,false,false],
[2,1,false,false],
[3,1,false,false],
[4,1,false,false]
],
[
[0,1,false,false],
[1,1,false,false],
[2,1,false,false],
[3,1,false,false],
[4,1,false,false]
],
[]
]
He is problem, all your object are represented as tuples [a,b,c,d]
instead of objects { a: x, b: x, c: x, d: x}
.
You need to tune server-size serializer to send object or write custom object serializer for tuples and register it in SerializationContext.SerializerFactory
I have the same problem, I found that if I remove the attribute [DataContract] everything will work ok.
@deniszykov could you please write the "write custom object serializer for tuples and register it in" you mention above :-)
@BinhNguyenPSA sorry I have no plans to expand this package. I gladly accept PR with new object serializer.