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

Deserialization "object" type #13

Open
Sashok9203 opened this issue Oct 24, 2019 · 5 comments
Open

Deserialization "object" type #13

Sashok9203 opened this issue Oct 24, 2019 · 5 comments

Comments

@Sashok9203
Copy link

Sashok9203 commented Oct 24, 2019

Example:
object test;
int i = 12;
test = i;
MessagePackFormatter formatter = new MessagePackFormatter();
byte[] bytes = formatter.Serialize(test);
test = formatter.Deserialize< object >(bytes);
Debug.Log((int)test); ---- error-----
Debug.Log(Convert.ToInt32(test)); - ---works---

if in the code change int to float code works
With unity types(Vector3,Quanternion and others), the error is the same ...

Am I doing something wrong?

@taka-oyama
Copy link
Owner

taka-oyama commented Oct 25, 2019

Unfortunately, MessagePack doesn't preserve the type for integer values.
This is what's happening in the background.

  • It encodes 12 as byte type, since that uses the least amount of space for integer values.
  • It tries to decode it as object but message pack format sees that it's store as byte so it decodes it as byte.

So if you try Debug.Log((byte)test); it will work.
The reason why Convert.ToInt32 works is because it's converting byte to Int32 internally.

@taka-oyama
Copy link
Owner

I suggest you do explicit deserialization by using generics instead of using casting.

formatter.Deserialize<int>(bytes);

@Sashok9203
Copy link
Author

Sashok9203 commented Oct 25, 2019

In my case, this "formatter.Deserialize< int >(bytes);" -
this is not my option .
In my code where I use msgpack-unity ,i am serializing a dictionary with objects (Dictionary< int,object >)
accordingly, I cannot use for each value explicit deserialization.
Will have to replace the "object" with "byte[]" in the dictionary,and serialize each value separately ...
Thanks for the help.:)

@taka-oyama
Copy link
Owner

I will try and see if I can add an option to make this work

@Sashok9203
Copy link
Author

It would be nice :)
Do not drop this project(msgpack-unity), it is very useful for simple tasks

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

2 participants