开发者

Serializing array types and arrays in Jackson

i have a task serializing a class like RPC开发者_如何学JAVA message to JSON using Jackson in Java. I have to say that i´m a complete newbie to Jackson. Now what i´m trying to do is to serialize array type into JSON.

I have:

 ObjectMapper mapper = new ObjectMapper(); 

a message is then put into HashMap (simplified)

 LinkedHashMap<String,Object> map = new LinkedHashMap<String, Object>();
 if(msg.getSignal())
     map.put("signal",msg.getMethodName());
 else {
     map.put("method", msg.getMethodName());
     map.put("retT", msg.getReturnType()); //returns Class<?> type
 }

 return mapper.writeValueAsString(wrapper);

for method name "add" and return type int[], this results in:

{"method":"add","retT":"[I"}

Can anyhone please help me how to achieve "[int]" instead of "[I"?


I assume that 'msg.getReturnType()' returns Class; and if so, Jackson will just call toString() on it. If so, you would want to instead do conversion yourself, to get actual String value you want.

You can also simplify code a bit, since ObjectMapper has 'writeValueAsString()' method:

return mapper.writeValueAsString(wrapper);

which will internally handle creation of StringWriter and JsonGenerator, to achieve what you are doing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜