Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

| Views: 3137
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:
public class MyClass {
private List<MyOtherClass> otherClass;
public MyClass() {}
...
}


to this:
public class MyClass {
private MyOtherClass[] otherClass;
public MyClass() {}
...
}


-i

Have comments or feedback on what I wrote? Please share them below! Found this useful? Consider sending me a small tip.
comments powered by Disqus
Other posts you may like...