Groovy support: JsonObject with Maps containing GString produces unexpected JSON

262 views
Skip to first unread message

Nikolai Raitsev

unread,
Jun 5, 2016, 10:35:07 AM6/5/16
to vert.x
Hi,

just found, that creating of JsonObject in Groovy with map containing GStrings as values, is serialized to not expected json values.

this

def x = 12
def y = 5
def m = [val: "$x:$y"]

JsonObject json = new JsonObject(m)
println json


results in

{"val":{"values":[12,5],"strings":["",":",""],"bytes":"MTI6NQ==","valueCount":2}}

instead of expected

{"val":"12:5"}


I'm using Vertx version 3.2.1

In Vertx 2 this issue was solved 2014 with https://github.com/vert-x/mod-lang-groovy/issues/59.

Is it planned to solve this Issue in Vertx 3 with own version of groovish JsonObject? Or what are the best practice with groovy maps (which may contain lazy interpeted values) and JsonObject in vertx?

Best regards,

Nikolai


Julien Viet

unread,
Jun 5, 2016, 11:30:09 AM6/5/16
to ve...@googlegroups.com
Hi,

the 2.x does not solve this problem in general, it rather solves how GString are serialized over the event bus, which solves this problem in case of the event bus.

One generic solution for this problem is to use this setup:

Json.mapper.registerModule(new SimpleModule().addSerializer(GString.class, new StdSerializer<GString>(GString.class) {
@Override
void serialize(GString value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeString(value.toString());
}
}));

This will correctly map a Groovy string to a plain string.

Another way it could be solved is via the usage of the Groovy “as” operator (http://docs.groovy-lang.org/latest/html/documentation/index.html#_coercion_operator) so one would do

[val: :”$x:$y”] as JsonObject

that JsonObject can be properly initialized from the Map.

this can be made automatic using Groovy extensions (http://mrhaki.blogspot.fr/2013/01/groovy-goodness-adding-extra-methods.html)



--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/da30157b-466e-4693-983f-b363a3dc9478%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nikolai Raitsev

unread,
Jun 5, 2016, 2:43:28 PM6/5/16
to vert.x
Hi Julien,

many thanks for prompt answer.

I'll try to setup my environment with GString-Serializer. But do you think, such serialization should be a standard, if groovy-lang module is used?

I mean, maybe this Serializer should be registered in some of io.vertx.lang.groovy.* startup class?

Best regards,

Nikolai

Julien Viet

unread,
Jun 5, 2016, 4:12:14 PM6/5/16
to ve...@googlegroups.com
yes maybe it’s a good idea too…

it should not break existing code (I don’t see somebody relying on this).

I don’t know what others think of this, it would be good that others comment on this and give their opinion.

Reply all
Reply to author
Forward
0 new messages