Skip to content

Instantly share code, notes, and snippets.

@masayuki038
Created September 11, 2011 15:09
Show Gist options
  • 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));
}
}
}
@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