Unpacking objects with lists

98 views
Skip to first unread message

Peter Steele

unread,
Oct 30, 2012, 11:28:53 AM10/30/12
to msg...@googlegroups.com
I have a Python class defined as follows:

class DiscoverReply(Packet):
    def __init__(self):
        self.transactionId = 0
        self.id = 0
        self.disklist = [];
        
    def serialize(self):
        packer = msgpack.Packer()
        return packer.pack([self.transactionId, self.id, self.disklist])

The serialized data is sent to a Java application which unpacks the data into an equivalent Java class. My class is defined as:

@Message
public class DiscoverReply extends PacketData {
    private long transactionId;
    private long id;
    private List<Long> disklist;
    ...
}

The deserialization code I've written for the Python byte stream I receive in my Java app looks something like this:

MessagePack msgpack = new MessagePack();
Unpacker unpacker = msgpack.createBufferUnpacker(bytes);
DiscoverReply data = unpacker.read(DiscoverReply.class);

However, when the byte stream is processed by this code, I get an exception:

org.msgpack.MessageTypeException: org.msgpack.MessageTypeException: Unexpected raw value

The exact same code works fine if I remove the disklist member from both sides of the exchanging, leaving only simple types. It appears that the Java unpack code doesn't like the way I've packed the data on the Python side. What's the correct way to pack a Python object that contains a list. I'll probably end up with a more complex structure than even this, possibly a list of other objects and not just a list of longs. What's the correct way to serialize data in Python so that it is compatible with Java classes that have been flagged with @Message?

Peter Steele

unread,
Oct 31, 2012, 7:52:53 AM10/31/12
to msg...@googlegroups.com
This turned out to be a mistake on my part. When I constructed the DiscoverReply object on the Python side, I was supposed to assign a list of numeric values to the disklist field. However, the manner in which I generated this list produced a list of strings, not a list of numbers. MsgPack encodes strings as raw values, and the unexpected raw value was the Java MsgPack unpacker complaining about encountering a string in the disklist field where it expected a number. I was thinking it was complaining about the list itself, and that's what threw me. As soon as I corrected the data on the Python side, the problem was resolved.

So, as is so often the case, user error...


Reply all
Reply to author
Forward
0 new messages