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

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

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

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.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.