The best, easiest way is to wrap the JSON itself in a container containing this type.
For instance:
{ "foo":"bar" }
becomes
{ "type":"FooContainer" "value": { "foo":"bar" } }
However, you said that you cannot do this, so I will not go into details about how to actually deserialize. If you can make this limited change, you can write a TypeAdapterFactory and let Gson do the rest of the work by using the passed Gson object in the create() method and gson.fromJson() in the read method, which will be much less code than the method below.
Think about what really needs to happen here. Somehow you need to determine what data is in the MQ Websphere objects and use it to figure it out. Finding it this way is going to take the code, analyze what data you received, and spit out the correct object.
The first problem you will encounter is that you do not want to do this:
Object myObj = gson.fromJson(jsonString, Object.class);
This means that you are redefining the default base deserializer, which can have all kinds of unpleasant side effects. It is better to wrap the behavior in a TypeAdapter for a kind of custom object:
public class Wrapper { public Object data;
I will leave you casting / generics. There are many ways to do this, but I recommend the Visitor Template as a good choice, or perhaps create an object of type enum . It would be better if all the resulting object types implemented some kind of interface to make it easier, but, again, this is not your main problem.
Your main problem is that they really detect these classes in code. Now that we have a wrapper class, we can use this:
Wrapper myWrapper = gson.fromJson(jsonString, Wrapper.class);
Now each class generated is the same class, so Gson is happy. But where do we set the rules? They go in your regular TypeAdapter :
public class WrapperTypeAdapter extends TypeAdapter<Wrapper> { @Override public final void write(JsonWriter out, Wrapper value) throws IOException { if(value == null) { out.nullValue(); return; }
In the read method, when you are stream tokens, you can use the rules based on what you expect from the JSON tokens themselves to decide which class it will be, and then build a new object. I recommend breaking this down into many methods like MyFoo readMyFoo(JsonReader) , MyBar readMyBar(JsonReader) , etc. I find it difficult to go into much more detailed information without knowing more about JSON itself, but this should be enough to get you started.