Comments (8)
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.
As you said, they are not obvious so documents should be improved, and I will provide documentation for 'using MPO to interpret dynamic type field'.
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.
from msgpack-cli.
Thanks, this is useful. Having that in the documentation would have helped.
from msgpack-cli.
I updated wiki to describe issue14 related topic, and polymorphic scenario.
https://github.com/msgpack/msgpack-cli/wiki/Dynamic-type-handling
from msgpack-cli.
I tried the sample code, and it does not work for me. I created a sample MsgPack message using the following Python code:
import msgpack
s = msgpack.dumps({'results': [1.0, 2.0]})
print s.encode('base64')
Feeding the output of the code above into this:
var s = "gadyZXN1bHRzkss/8AAAAAAAAMtAAAAAAAAAAA==";
byte[] rawReply = System.Convert.FromBase64String(s);
// Gets an object which stores dynamic key/value pairs as dictionary object.
Dictionary<MessagePackObject, MessagePackObject> mpoDict =
MessagePackSerializer.Create<Dictionary<MessagePackObject, MessagePackObject>>().Unpack(new MemoryStream(rawReply));
// 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 );
MessagePackObject theValue;
if (dict.TryGetValue( "error", out theValue ) )
{
// The data must be error.
Debug.Assert(theValue.IsTypeOf<String>() ?? false);
// Convert utf-8 binaries to a String object.
var error = (String)theValue;
}
else if ( dict.TryGetValue( "results", out theValue ) )
{
Debug.Assert(theValue.IsList);
// First get values as list.
IList<MessagePackObject> values = theValue.AsList();
// Convert values.
var results = values.Select( v => (double?)v ?? -1000.0 );
}
... I get the following exception:
InvalidOperationException: Do not convert System.Byte (binary:0x2) MessagePackObject to System.Collections.Generic.IList`1[MsgPack.MessagePackObject].
on the line that calls theValue.AsList(). Inspecting the dict
variable, it indeed seems to be a dict with a key of "results" and a corresponding value of a MessagePackObject for the number 2.
Let me know if you can reproduce this or now. Thanks!
from msgpack-cli.
However, the following program works fine:
public class ReplyMessage
{
// ReSharper disable InconsistentNaming
public double[] results;
public string error;
// ReSharper restore InconsistentNaming
}
void Main()
{
var s = "gadyZXN1bHRzkss/8AAAAAAAAMtAAAAAAAAAAA==";
byte[] rawReply = System.Convert.FromBase64String(s);
var serializer = MessagePackSerializer.Create<ReplyMessage>();
var stream = new MemoryStream(rawReply);
var replyMsg = serializer.Unpack(stream);
if (replyMsg.error != null)
{
Console.WriteLine("Exception!");
}
else
{
replyMsg.results.Dump();
}
}
(The Dump()
call is a Linqpad thing... Feel free to replace it by Console.WriteLine(), etc.)
To my (potentially uneducated) eye, this looks like a bug...
from msgpack-cli.
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;
Unpacking
is utility providing convenient static APIs around Unpacker
, and it does not use serialization stack (this means it is faster than MessagePackSerializer
for this case).
I will fix this bug on next release. Thank you for your careful reporting!
from msgpack-cli.
I fixed this issue and released as 0.4.1.
from msgpack-cli.
There is a sample code now.
https://github.com/msgpack/msgpack-cli/blob/master/samples/Samples/Sample02_HandlingDynamicObject.cs
from msgpack-cli.
Related Issues (20)
- Add a non-generic override to SerializerRepository
- EnumMemberAttribute does not work. (expectedly)
- Type Object in MessagePack HOT 1
- GetMemberName() causes NullReferenceException when using MessagePackSerializer.Get<T>() in Xamarin iOS
- [Unity][il2cpp]'class' is not registered in this resolver,resolver.StandardResolver HOT 2
- How to serialize and deserialize a List member in a class HOT 2
- Problem deserializing byte array - InvalidOperationException caused by Decoders.InvalidArrayHeader.Read call HOT 4
- about LZ4 HOT 1
- Unable to cast object of type on netcore HOT 1
- Custom Serialisation docs appear to be out of date. HOT 1
- PackAsync & UnpackAsync not working when passing data continuously using for loop c# HOT 1
- The unpacked value is not expected type HOT 2
- Authenticode Signing? HOT 2
- DateTime serialization and deserialization timezone HOT 3
- Enum Deserialization Not working? HOT 1
- Serializer.Unpack Continuosly Stream of Data => High Memory Usage HOT 3
- Serialization / deserialization speed optimizations HOT 2
- Serialization field order by declaration order HOT 3
- Add additional TFM do dependencies can be optimised HOT 2
- Set additional package metadata
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack-cli.