How to handle null values for JsonArray?

67 views
Skip to first unread message

javadevmtl

unread,
Sep 26, 2016, 12:17:44 PM9/26/16
to vert.x
Hi using the JsonArray to pass around parameters to my proxy service, in similar manner to the JDBCClient when calling a prepared statement...

Say I received the following fields...

final String email = body.getString("email");

final String password = body.getString("password");

final String phone = body.getString("phone");


JsonArray params = new JsonArray().add(email).add(password).add(phone);

Lets pretend that phone is optional. The getString will assign a null value. But JsonArray.add() (I looked at the source it rejects nulls) does not accept null values. You have to specifically call .addNull()

Is there a way to do this without nasty IF ELSE?

Unfortunately the underlying service requires that all parameters are passed even if they are null.


Thomas SEGISMONT

unread,
Sep 26, 2016, 12:26:09 PM9/26/16
to ve...@googlegroups.com
Maybe stupid question, why not use a JsonObject?

--
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+unsubscribe@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/5d52f4c7-f727-47eb-87b3-369044687699%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Smith

unread,
Sep 26, 2016, 12:47:19 PM9/26/16
to ve...@googlegroups.com
JsonArray keeps order of values, JsonObject doesn't guarantee order of fields(I know for sure for JsonObject). Or am I wrong?

The underlying service signature someFunction(String name, Object...)

So it is easy to do this...

JsonArray parameters = new JsonArray();
...

Object[] parameterz = new Object[parameters.size()];

int parameterIndex = 0;

for(Object o : parameters) {

    parameterz[parameterIndex++] = o;

}






--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/uIrE1ZWT2-c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+unsubscribe@googlegroups.com.

javadevmtl

unread,
Sep 26, 2016, 1:02:12 PM9/26/16
to vert.x
Actually it's 100% JsonArray does preserve order JsonObject doesn't.

javadevmtl

unread,
Sep 27, 2016, 11:57:16 AM9/27/16
to vert.x
Any thoughts on this? Thanks

javadevmtl

unread,
Sep 27, 2016, 11:57:59 AM9/27/16
to vert.x
I guess I can write a wrapper around JsonArray and do the check myself.
Reply all
Reply to author
Forward
0 new messages