Custom JSON serializer for MessageCodec

535 views
Skip to first unread message

Konstantinos Liakos

unread,
Nov 2, 2016, 5:53:14 AM11/2/16
to vert.x
I am trying to send a message over the event bus to a sockjs client. The message is a POJO, so I register a MessageCodec as in the example https://github.com/vert-x3/vertx-examples/blob/ea6e07945baa9abec6d6f4f98404c727cc8c342e/core-examples/src/main/java/io/vertx/example/core/eventbus/messagecodec/util/CustomMessageCodec.java

However I am replacing the JSON serialization code with a custom solution(using Gson).

JsonObject jsonToEncode = new JsonObject();
jsonToEncode
.put("statusCode", myPojo.getStatusCode());
jsonToEncode
.put("resultCode", myPojo.getResultCode());
jsonToEncode
.put("summary", myPojo.getSummary());
String jsonToStr = jsonToEncode.encode();

Replaced with:

StringjsonStr = new GsonBuilder().create().toJson(myPojo);



This results to error:

SEVERE: Unhandled exception
java.lang.IllegalStateException: Illegal type in JsonObject: class MyPOJO
at io.vertx.core.json.Json.checkAndCopy(Json.java:125)
at io.vertx.core.json.JsonObject.put(JsonObject.java:645)
at io.vertx.ext.web.handler.sockjs.impl.EventBusBridgeImpl.deliverMessage(EventBusBridgeImpl.java:343)
at io.vertx.ext.web.handler.sockjs.impl.EventBusBridgeImpl.lambda$null$9(EventBusBridgeImpl.java:246)
at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:213)
at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:192)
at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$3(EventBusImpl.java:503)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$3(ContextImpl.java:359)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:339)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:393)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742)
at java.lang.Thread.run(Thread.java:745)




Konstantinos Liakos

unread,
Nov 2, 2016, 6:37:04 AM11/2/16
to vert.x
Actually it seems that the MessageCodec is not invoked at all. Can't find out why.

Paulo Lopes

unread,
Nov 2, 2016, 8:53:24 AM11/2/16
to vert.x
Did you register your codec?

vertx.eventBus().registerDefaultCodec(MyPOJO.class, new MyPOJOMessageCodec(engine));

Konstantinos Liakos

unread,
Nov 2, 2016, 9:23:32 AM11/2/16
to vert.x
I did register the codec the way you mention it. But not passing anything to the constructor.  What exactly is this engine object you pass to the constructor?

Konstantinos Liakos

unread,
Nov 2, 2016, 9:33:10 AM11/2/16
to vert.x
Did another test: copied the CustomMessage classes from the custom message coded example to the RealTime example https://github.com/vert-x3/vertx-examples/tree/master/web-examples/src/main/java/io/vertx/example/web/realtime

I register it the same way, and get the very same error. 

Failed to handleMessage 
java.lang.IllegalStateException: Illegal type in JsonObject: class io.vertx.example.web.realtime.util.CustomMessage
at io.vertx.core.json.Json.checkAndCopy(Json.java:125)
at io.vertx.core.json.JsonObject.put(JsonObject.java:645)
at io.vertx.ext.web.handler.sockjs.impl.EventBusBridgeImpl.deliverMessage(EventBusBridgeImpl.java:343)
at io.vertx.ext.web.handler.sockjs.impl.EventBusBridgeImpl.lambda$null$9(EventBusBridgeImpl.java:246)
at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:212)
at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:191)
at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$3(EventBusImpl.java:503)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:316)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:418)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:440)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)
at java.lang.Thread.run(Thread.java:745)

Unhandled exception 
java.lang.IllegalStateException: Illegal type in JsonObject: class io.vertx.example.web.realtime.util.CustomMessage
at io.vertx.core.json.Json.checkAndCopy(Json.java:125)
at io.vertx.core.json.JsonObject.put(JsonObject.java:645)
at io.vertx.ext.web.handler.sockjs.impl.EventBusBridgeImpl.deliverMessage(EventBusBridgeImpl.java:343)
at io.vertx.ext.web.handler.sockjs.impl.EventBusBridgeImpl.lambda$null$9(EventBusBridgeImpl.java:246)
at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:212)
at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:191)
at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$3(EventBusImpl.java:503)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:316)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:418)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:440)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)
at java.lang.Thread.run(Thread.java:745)

Paulo Lopes

unread,
Nov 2, 2016, 9:34:24 AM11/2/16
to vert.x
Sorry that was a copy paste issue, there is no need to pass anything to the constructor.

Also is your MyPOJO a real class? I mean not an interface or a abstract class? because the match is done by the exact class type.

Konstantinos Liakos

unread,
Nov 2, 2016, 9:43:56 AM11/2/16
to vert.x
Look my answer above, I did the same test by using the MessageCodec from the examples. And get the same failure. What am I missing here? Aren't codecs supposed to also work over via the sockjs bridge?

Paulo Lopes

unread,
Nov 2, 2016, 10:48:48 AM11/2/16
to vert.x
I can't see what you're doing wrong since you refer first to MyPOJO but there is no MyPOJO code and the error you showed is because there is an non JSON valid type inside the JsonObject. Could you share a reproducer?

Konstantinos Liakos

unread,
Nov 2, 2016, 10:52:51 AM11/2/16
to vert.x
Yes I have mixed a couple of examples here...

So you do say that custom codecs do work over the SockJS bridge? Is this designed to work?

If yes, I will set up a github repo with an example reproducing the case.

Konstantinos Liakos

unread,
Nov 2, 2016, 11:02:27 AM11/2/16
to vert.x
After reading the code, I get the impression that SockJS messages over the event bus bridge are always implicitly JsonObject.



On Wednesday, 2 November 2016 16:48:48 UTC+2, Paulo Lopes wrote:

Konstantinos Liakos

unread,
Nov 4, 2016, 4:45:36 PM11/4/16
to vert.x
Can anyone confirm if the custom codecs are supposed to work over the SockJS event bridge?

Konstantinos Liakos

unread,
Nov 5, 2016, 6:32:09 AM11/5/16
to vert.x
Here is the full example for anyone interested looking into this: https://github.com/kliakos/vertx-sockjs-example
Reply all
Reply to author
Forward
0 new messages