In messagepack, error while getting value from MapValue.. Please help me

血红的双手。 提交于 2019-12-11 03:08:59

问题


I'm 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,Object> map = new HashMap<Object, Object>();
  map.put(1,"ONE");
  map.put("ONE","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


回答1:


You need to use ValueFactory and convert key to use a Value interface. It's not really intuitive

// instead of value.get(1) use following
System.out.println(value.get(ValueFactory.createIntegerValue(1)));

// if the key would be a String use:
System.out.println(value.get(ValueFactory.createRawValue("key")));


来源:https://stackoverflow.com/questions/17993145/in-messagepack-error-while-getting-value-from-mapvalue-please-help-me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!