25-Mar-2013
NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the
FAQ for more details.
I've been using the
JSON-RPC library for some time now to write the server for my game, thus far I've only had a need to transport simple objects back to the client, but recently I've had a need send a collection of objects back. This is where I found that JSON-RPC falls short if you try to use the standard Java collections.
I've tried several things, first I used a Collection, which resulted in the following exception:
Caused by: java.lang.IllegalArgumentException: abstract class or interface not allowed : interface java.util.Collection
at org.json.rpc.commons.GsonTypeChecker.isValidType(GsonTypeChecker.java:75)
at org.json.rpc.commons.GsonTypeChecker.isValidType(GsonTypeChecker.java:128)
Then I thought if I used a concrete class, it would fix that, but alas, the following exception is thrown:
Caused by: java.lang.IllegalArgumentException: parametrized classes not allowed : class java.util.ArrayList
at org.json.rpc.commons.GsonTypeChecker.isValidType(GsonTypeChecker.java:82)
at org.json.rpc.commons.GsonTypeChecker.isValidType(GsonTypeChecker.java:128)
So what's a parametrized class? Well that's to do with Java Generics, the ArrayList I had was actually something like an ArrayList
and the JSON-RPC library doesn't know how to handle that.
It looks like all of the Java collections now are parametrized, so using them will not work. The only thing I found that would work was to send an array of my objects instead. This is a bit messy since it means I have to allocate my array on the server side with a known size, with a collection I was just using lazy loading, which saved me some hassle.
So to sum up, my object that was being returned to the client had to change from this:
private List<MyOtherClass> otherClass;
to this:
private MyOtherClass[] otherClass;
-i
A quick disclaimer...
Although I put in a great effort into researching all the topics I cover, mistakes can happen.
If you spot something out of place, please do let me know.
All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle).
Use of any information contained in this blog post/article is subject to
this disclaimer.
Igor Kromin
Other posts you may like...