netty-socketio icon indicating copy to clipboard operation
netty-socketio copied to clipboard

EventListener Can not parse JSON string to Java Object

Open zhuoran opened this issue 11 years ago • 5 comments

In my case I used JacksonJsonSupport to parse json object

config.setJsonSupport(new JacksonJsonSupport());
config.getJsonSupport().addEventMapping("", "message", ChatEvent.class);
            ....
server.addEventListener("message", ChatEvent.class,chatEventListener);

when I received user data from ChatEventListener and I always get a error!

This problem occurs only when the Android client, JavaScript client has no problem

Android Client Code Both of these two methods are parse failed

ChatEvent data = new ChatEvent();
mSocket.emit("message", JSON.toJSONString(data));

ChatEvent data = new ChatEvent();
mSocket.emit("message", data);

Any Idea for this?

Thx

···java [com.corundumstudio.socketio.JsonSupportWrapper]-[ERROR] Can't read value: ["message","{"action":1,"content":"ffc","date":1437811543560,"from":1,"nickName":"test","roomId":"1001","uid":"AYGQNVGEQOZ95POR"}"] for type: class com.corundumstudio.socketio.protocol.Event com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.xxx.chat.protocol.ChatEvent] from String value ('{"action":1,"content":"ffc","date":1437811543560,"from":1,"nickName":"test","roomId":"1001","uid":"AYGQNVGEQOZ95POR"}'); no single-String constructor/factory method at [Source: io.netty.buffer.ByteBufInputStream@21a1f581; line: 1, column: 11] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:757) at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:277) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:289) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1133) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:135) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126) at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3023) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1637) at com.corundumstudio.socketio.protocol.JacksonJsonSupport$EventDeserializer.deserialize(JacksonJsonSupport.java:194) at com.corundumstudio.socketio.protocol.JacksonJsonSupport$EventDeserializer.deserialize(JacksonJsonSupport.java:155) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2192) at com.corundumstudio.socketio.protocol.JacksonJsonSupport.readValue(JacksonJsonSupport.java:337) at com.corundumstudio.socketio.JsonSupportWrapper.readValue(JsonSupportWrapper.java:52) at com.corundumstudio.socketio.protocol.PacketDecoder.parseBody(PacketDecoder.java:303) at com.corundumstudio.socketio.protocol.PacketDecoder.decode(PacketDecoder.java:184) at com.corundumstudio.socketio.protocol.PacketDecoder.decodePackets(PacketDecoder.java:150) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:65) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:36) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:187) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at com.corundumstudio.socketio.transport.WebSocketTransport.channelRead(WebSocketTransport.java:94) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.transport.PollingTransport.channelRead(PollingTransport.java:109) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.handler.AuthorizeHandler.channelRead(AuthorizeHandler.java:115) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:108) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:182) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) at java.lang.Thread.run(Thread.java:745) 2015-07-25 16:03:05.042 [com.corundumstudio.socketio.handler.InPacketHandler]-[ERROR] Error during data processing. Client sessionId: d2a0be61-0ab0-437f-9c1e-2b325ae316d2, data: java.lang.NullPointerException at com.corundumstudio.socketio.protocol.PacketDecoder.parseBody(PacketDecoder.java:304) at com.corundumstudio.socketio.protocol.PacketDecoder.decode(PacketDecoder.java:184) at com.corundumstudio.socketio.protocol.PacketDecoder.decodePackets(PacketDecoder.java:150) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:65) at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:36) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:187) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at com.corundumstudio.socketio.transport.WebSocketTransport.channelRead(WebSocketTransport.java:94) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.transport.PollingTransport.channelRead(PollingTransport.java:109) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at com.corundumstudio.socketio.handler.AuthorizeHandler.channelRead(AuthorizeHandler.java:115) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:108) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:182) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) at java.lang.Thread.run(Thread.java:745)

zhuoran avatar Jul 25 '15 09:07 zhuoran

Hi @zhuoran did you find any solution for this problem

nebyan avatar Dec 28 '16 06:12 nebyan

Hi @zhuoran, I have the same problem, do you fixed it??

atlas1119 avatar Jun 15 '17 06:06 atlas1119

Android client use json parser is "org.json",when emit message,you should like this: ChatEvent data = new ChatEvent(); mSocket.emit("message", new JSONObject(data));

jiangbaojun avatar Aug 31 '18 09:08 jiangbaojun

@jiangbaojun is right, i also added a correction to his tho

mSocket.emit("message", new JSONObject(new Gson().toJson(data)));

try and convert the object you are trying to send to a string using GSON library

eshiett1995 avatar Dec 23 '18 14:12 eshiett1995

my Solution: the bean add constructor by createFromString like this:

`class TestBean {

private String x;
private String y;
private String z;

public TestBean() {}

public TestBean(String beanStr) {
	// StdValueInstantiator.createFromString
	// _fromStringCreator
	JSONObject parseObject = JSON.parseObject(beanStr);
	setX(parseObject.getString("x"));
	setY(parseObject.getString("y"));
	setZ(parseObject.getString("z"));
}`

you can grab the packet By wrieshark Look at the differences between JS and Android package

the package Sketch Map:

image

JS-Client package: image

java-client package: image

jingzhongyue avatar Feb 20 '19 09:02 jingzhongyue