-
Notifications
You must be signed in to change notification settings - Fork 173
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
Documentation on unpacking of dynamic types could use some examples #13
Comments
Hi You can get MessagePackObject manually through Unpacker.Data property, or you can use MessagePackObject type property in your deserialization target object. Then, you can use MessagePackObject.UnderlyingType property or IsXxx properties to get the convertible type. BTW, the entire code (with descriptive assertions) for dynamic typing looks like following: // Gets dict which stores dynamic key/value pairs as dictionary object.
Dictionary<MessagePackObject, MessagePackObject> mpoDict = MessagePackSerializer.Create<Dictionary<MessagePackObject, MessagePackObject>>().UnpackFrom(...);
Debug.Assert( dict.Keys.All( v => v.IsTypeOf<String>() );
// Converts dict keys. Note that this line assumes there are no key duplications.
Dictionary<String, MessagePackObject> dict= mpoDict.ToDictionary( kv => (String)kv.Key, kv => kv.Value );
MessaePackObject theValue;
if ( dict.TryGetValue( "error", out theValue ) )
{
// The data must be error.
Debug.Assert( theValue.IsTypeOf<String>() );
// Convert utf-8 binaries to a String object.
var error = (String) theValue;
...
}
else if ( dict.TryGetValue( "results", out theValue ) )
{
Debug.Assert( theData.IsList );
// First get values as list.
IList<MessagePackObject> values = (IList<MessagePackObject>)theValue;
Debug.Assert( !values.Any() || values.All( v => v.IsTypeOf<Double>()) );
// Convert values.
var results = values.Select( v => (double )v );
...
} Thanks! Side note: I guess the source code of MPO looked like complex for numerics compatibility. By this feature, you can use floats or ints in 'results' field because MPO supports 'natural' numerics conversion. MPO just stores binary primitives and its type code for scalars, or object reference for non-scalar values, and makes efforts to handle numerics conversion properly. |
Thanks, this is useful. Having that in the documentation would have helped. |
I updated wiki to describe issue14 related topic, and polymorphic scenario. https://github.com/msgpack/msgpack-cli/wiki/Dynamic-type-handling |
I tried the sample code, and it does not work for me. I created a sample MsgPack message using the following Python code:
Feeding the output of the code above into this:
... I get the following exception:
on the line that calls theValue.AsList(). Inspecting the Let me know if you can reproduce this or now. Thanks! |
However, the following program works fine:
(The To my (potentially uneducated) eye, this looks like a bug... |
Sorry for slow response. It seems to be serializer bug of MPO as your smart eyes found. (You does not have to be so modest :) ) To workaround this, you can rewrite your code (from line 4 to line 9) as following: // Because you know it should be treated as Dictionary, so just unpack it.
var dict = Unpacking.UnpackDictionary( rawReply ).Value;
I will fix this bug on next release. Thank you for your careful reporting! |
I fixed this issue and released as 0.4.1. |
There is a sample code now. |
Hello,
I am transmitting a dict over MessagePack from Python to C#, which either has a key with name "results", pointing to a list of doubles, or a key "error", pointing to a string. I thought the "dynamic" API of the C# MessagePack library would allow me to programmatically unpack this, but after a fair bit of time trying different things and reading some of the source code of the library, I gave up.
I got things working by creating a helper class with the right member variables and passing that to Unpack(). But I think the documentation in https://github.com/msgpack/msgpack-cli/wiki/Messagepackobject could be improved, as I really wasn't able to figure out the "dynamic" way of doing what I wanted from said documentation. For one, adding a few examples of unpacking dynamic, multi-level data using the MessagePackObject, etc. API would be really helpful. Thanks!
The text was updated successfully, but these errors were encountered: