MessagePack-CSharp/MessagePack-CSharp

How to avoid cast and multiple Deserialize in code.

SachinTichkule opened this issue · 1 comments

It make more complicated to get json from string

//Client side
        string output = MessagePackSerializer.ConvertToJson(uwr.downloadHandler.data);
        JSONNode json = JSON.Parse(output);

        Debug.Log((string)json["message"]);
        Debug.Log((string)json["users"][0]);
        Debug.Log((string)json["users"][1]);
        Debug.Log(json["users"][2]);

        var data = MessagePackSerializer.Deserialize<string>(uwr.downloadHandler.data);
        this is not working i got error when use this method

//Server side
import pkg from 'msgpack-lite';
const responseMsgPack = pkg.encode({
    message: 'The following users have been added to the Database:',
    users: userWithID
});
res.set('Content-Type', 'application/x-msgpack');
res.send(responseMsgPack);

how to avoid casting and multiple deserialize

Your JSON conversion and subsequent logging suggests you expect a structured object with two properties. But then you tell MessagePackSerializer.Deserialize to deserialize it as if it were a single string instead of a structured object. That would be why it throws where you say it isn't working. You need to declare a C# type to resemble the schema sent by the server and deserialize as that.

As for deserializing multiple times, that's easy enough to avoid: stop converting to JSON. Just deserialize to your C# type and then log based on the deserialized object's contents.

Please use a Discussion for such questions in the future.