Created
August 5, 2013 11:27
-
-
Save muga/6155233 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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