Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token readed 'BeginArray' while 'BeginObject' is expected. #25

Open
Mol0ko opened this issue Sep 28, 2021 · 6 comments
Open

Comments

@Mol0ko
Copy link

Mol0ko commented Sep 28, 2021

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:

Снимок экрана 2021-09-28 в 10 42 13

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.

@deniszykov
Copy link
Owner

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]);

@Mol0ko
Copy link
Author

Mol0ko commented Sep 28, 2021

@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]
  ],
  []
]

@deniszykov
Copy link
Owner

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

@BinhNguyenPSA
Copy link

I have the same problem, I found that if I remove the attribute [DataContract] everything will work ok.

@BinhNguyenPSA
Copy link

@deniszykov could you please write the "write custom object serializer for tuples and register it in" you mention above :-)

@deniszykov
Copy link
Owner

@BinhNguyenPSA sorry I have no plans to expand this package. I gladly accept PR with new object serializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants