Error while getting value from MapValue.. Please help me.

112 views
Skip to first unread message

nausath ali

unread,
Jul 31, 2013, 10:32:46 AM7/31/13
to msgpa...@googlegroups.com
Hi,
    I am trying to serialize map using messagpack.write(map). During deserialization using messagepack.read(byte[]) i got MapValue. But I cannot fetch the values using MapValue.get(key).. Look this problem below

        HashMap<Object,String> map = new HashMap<Object, String>();
        map.put(1,"ONE");
        map.put(2,"TWO");

        MessagePack m= new MessagePack();
        byte[] b = m.write(map);
      MessagePack m1 = new MessagePack();
      MapValue value = (MapValue)m1.read(b);
      System.out.println(value);// here I am getting {1:"ONE",2:"TWO"}

     System.out.println( value.get(1)); // printing the value for key 1. I am getting null.

     Please help on this.. Thanking you.
    
   Nausadh

   
 

Muga Nishizawa

unread,
Jul 31, 2013, 10:55:15 PM7/31/13
to msgpa...@googlegroups.com
Hi Nausadh,

MapValue type is one of abstract representation in MessagePack for
Java. MapValue is different from your "map" object. The key and value
types are Value type. You can serialize Map<Integer, String> objects
as following:

import static org.msgpack.template.Templates.TInteger;
import static org.msgpack.template.Templates.TString;
import static org.msgpack.template.Templates.tMap;

import java.util.HashMap;
import java.util.Map;

import org.msgpack.MessagePack;
import org.msgpack.template.Template;

public class Main {
public static void main(String[] args) throws Exception {
// We unrecommend that you serialize/deserialize Object types
with MessagePack.
Map<Integer,String> src = new HashMap<Integer, String>();
src.put(1,"ONE");
src.put(2,"TWO");


MessagePack msgpack = new MessagePack();
// create template object, which is serializer/deserializer
for Map<Integer, String> object.
Template<Map<Integer, String>> tmpl = tMap(TInteger, TString);

byte[] bytes = msgpack.write(src, tmpl);
Map<Integer, String> dst = msgpack.read(bytes, tmpl);

System.out.println(dst);
System.out.println(dst.get(1));
}
}

----

Thanks,
Muga
> --
> You received this message because you are subscribed to the Google Groups
> "MessagePack Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to msgpack-dev...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

nausath ali

unread,
Aug 1, 2013, 5:14:22 AM8/1/13
to msgpa...@googlegroups.com

Thanks muga,,

  But i am having a Map<Object,Object>... how can i serialize it .. Any idea?
Nausadh.ar
Reply all
Reply to author
Forward
0 new messages