Skip to content

Instantly share code, notes, and snippets.

@masayuki038
Created September 11, 2011 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masayuki038/1209689 to your computer and use it in GitHub Desktop.
Save masayuki038/1209689 to your computer and use it in GitHub Desktop.
Serializing HashMap By MessagePack for Java.
@Test
public void testSerliazingMapByMessagePack(){
Map<String, Object> map = new HashMap<String, Object>();
map.put("int", 1);
map.put("long", 1L);
map.put("date", new Date());
map.put("string", "test");
byte[] buffer = MessagePack.pack(map);
for (byte b : buffer) {
System.out.print(Integer.toHexString(b & 0xFF) + " ");
}
System.out.println();
System.out.println("buffer.length: " + buffer.length);
System.out.println();
InputStream in = new ByteArrayInputStream(buffer);
Unpacker unpacker = new Unpacker(in);
for(Object obj : unpacker){
MapType mapType = (MapType)obj;
Map<MessagePackObject, MessagePackObject> ret = mapType.asMap();
for(MessagePackObject key : ret.keySet()){
String keyStr = key.asString();
Object value = ret.get(key);
System.out.println(String.format("%s: %s", keyStr, value));
}
}
}
@masayuki038
Copy link
Author

result:
84 a3 69 6e 74 1 a6 73 74 72 69 6e 67 a4 74 65 73 74 a4 64 61 74 65 cf 0 0 1 32 59 9 51 f5 a4 6c 6f 6e 67 1
buffer.length: 38

int: org.msgpack.object.ShortIntegerTypeIMPL@1
long: org.msgpack.object.ShortIntegerTypeIMPL@1
date: org.msgpack.object.LongIntegerTypeIMPL@590950c7
string: org.msgpack.object.RawType@445c13

@Anand-J-Kadhi
Copy link

Which msgpack version are you using ? @masayuki038

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment