casting a JsonArray to a specific type

3,647 views
Skip to first unread message

Alexander Lehmann

unread,
Dec 28, 2014, 6:23:25 PM12/28/14
to ve...@googlegroups.com
I wonder if there is a smarter way to cast a JsonArray to a List of specific type.

Currently I use (List<String>)json.getJsonArray("recipients").getList(), which works and giving an exception if either the entry is not an array or the array elements are not Strings, but this gives a warning about unchecked operations (which is obviously correct). Can this be done another way so that it works without a warning?


Jordan Halterman

unread,
Dec 28, 2014, 7:06:20 PM12/28/14
to ve...@googlegroups.com
Since JsonArray returns List and not List<T> you only have two options: suppress the warnings or manually convert the list in a foreach loop.

Note that you can use @SuppressWarnings("unchecked") on local variables to limit the scope.

In the second option, just loop through the list and create a new List<String> by calling toString() on each element. Of course, this is wildly inefficient if you're casting the list a lot, and it would most definitely be better to deal with the warnings.

Also, if you're using Java 8 you can make the List to List<String> conversion a little prettier with the stream API. Something like...

List<String> list = array.toList().stream().collect(Collectors.mapping(value -> value.toString()), Collectors::toList);

Might have that a little wrong since I typed that from my phone :-)

On Dec 28, 2014, at 3:23 PM, Alexander Lehmann <alex...@gmail.com> wrote:

I wonder if there is a smarter way to cast a JsonArray to a List of specific type.

Currently I use (List<String>)json.getJsonArray("recipients").getList(), which works and giving an exception if either the entry is not an array or the array elements are not Strings, but this gives a warning about unchecked operations (which is obviously correct). Can this be done another way so that it works without a warning?


--
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.
For more options, visit https://groups.google.com/d/optout.

swami....@ishafoundation.org

unread,
Apr 1, 2015, 9:05:58 AM4/1/15
to ve...@googlegroups.com
+1

I frequently need to do this... (usually JsonArray to/from Set<String>)  would be great if the v3.0 api could make this a little more simple

Geoffrey Clements

unread,
Apr 2, 2015, 8:31:05 AM4/2/15
to ve...@googlegroups.com
From memory:

json.getJsonArray("recipients").stream().map(s -> (String) s).collect(Collectors.toSet());

I think that should do it and return a Set<String>. Modify the map() call to convert each object to the type you want the generic set to specialize on.

The Java 8 Stream API, in combination with lambdas, is so useful.


geoff

Christian Villamizar Lamus

unread,
Apr 27, 2021, 4:47:59 AM4/27/21
to vert.x
In case someone is interested, when it is a more complex type.


JsonArray jarr = new JsonArray(messageAsyncResult.result().bodyAsString());

List<Customer> cl = (List<Customer>) jarr.getList().stream().map(s -> DatabindCodec.mapper().convertValue(s, Customer.class)).collect(Collectors.toList());

Konstantin Smirnov

unread,
Apr 27, 2021, 1:00:00 PM4/27/21
to vert.x
Use groovy, where you can do a lot of magic tricks out-of-box:

jsonArray.collect{ it as MyClass }
or
jsonArray.collect{ new MyClass( it ) }
Reply all
Reply to author
Forward
0 new messages