java - Elegant mapping from POJOs to vertx.io's JsonObject? -
i working on vertx.io application , wanted use provide mongo api data storage. have rather clunky abstraction on top of stock jsonobject classes get
, set
methods replaced things like:
this.backingobject.get(key_for_this_property);
this , now, won't scale particularly well. seems dirty, when using nested arrays or objects. example, if want able fill fields when actual data known, have check if array exists, , if doesn't create , store in object. can add element list. example:
if (this.backingobject.getjsonarray(key_list) == null) { this.backingobject.put(key_list, new jsonarray()); } this.backingobject.getjsonarray(key_list).add(p.getbackingobject());
i have thought potential solutions don't particularly of them. namely, could use gson or similar library annotation support handle loading object purposes of manipulating data in code, , using serialize , unserialize function of both gson , vertx convert between formats (vertx load data -> json string -> gson parse json pojos -> make changes -> serialize json string -> parse vertx , save)
that's gross , inefficient workflow. come sort of abstract wrapper extends/implements vertx json library passes functionality through gson, seems lot of work.
is there way achieve more friendly , maintainable serialization using vertx?
not sure if i've understood correctly, sounds you're trying find simple way of converting pojos jsonobject?
so, have lots of pojos send on eventbus
jsonobject
s
i've found easiest way use vert.x
json
class has loads of helper methods convert / json
strings
jsonobject jsonobject = new jsonobject(json.encode(mypojo);
sometimes need add custom (de)serializers, stick jackson
- vert.x
using work out of box.
what do, provide interface following:
public jsonobjectserializable { public jsonobject tojson(); }
and our pojos need sent on eventbus
have implement interface.
then our eventbus
sending code looks (simplified):
public <t extends jsonobjectserializable> response<t> dispatch(t eventpayload);
also, don't unit test pojos, adding interface
encourages developers unit test conversion.
hope helps,
will
Comments
Post a Comment