Skip to content

Instantly share code, notes, and snippets.

@muga
Created August 5, 2013 11:27
Show Gist options
  • Save muga/6155233 to your computer and use it in GitHub Desktop.
Save muga/6155233 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
import org.msgpack.MessagePack;
import org.msgpack.type.IntegerValue;
import org.msgpack.type.MapValue;
import org.msgpack.type.RawValue;
import org.msgpack.type.ValueFactory;
public class Sample {
public static void main(String[] args) throws Exception {
MessagePack msgpack = new MessagePack();
Map<Object, Object> src = new HashMap<Object, Object>();
src.put("name", "nausadh");
src.put("age", 22);
src.put("dateofbirth", 1332938923948l);
/**
* serialize
*/
byte[] bytes = msgpack.write(src);
MapValue dstValueMap = msgpack.read(bytes).asMapValue();
/**
* deserialize/convert value object into string object.
* RawValue object represents type of byte array or String in MessagePack for Java.
*/
// name
RawValue nameValue = dstValueMap.get(ValueFactory.createRawValue("name")).asRawValue();
System.out.println("name: " + nameValue.getString());
// age
IntegerValue ageValue = dstValueMap.get(ValueFactory.createRawValue("age")).asIntegerValue();
System.out.println("age: " + ageValue.getInt());
// dateofbirth
IntegerValue dobValue = dstValueMap.get(ValueFactory.createRawValue("dateofbirth")).asIntegerValue();
System.out.println("dateofbirth: " + dobValue.getLong());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment