Let's say I have a some basic types:

```
public class Base {
    String a = "a";
    Nested nested = new Nested()
}

public class Nested {
    String b = "b",
}

```

Serialising this with GSON, I get:

```
{
   "a": "a",
   "nested": {
      "b": "b"    
   }
}

```

This is all good, but what if I want to preserve the object type as well, according to the following:

```
{
    "Base":
    {
       "a": "a",
       "nested": {
           "Nested": {
               "b": "b"  
           }  
       }
    }
}

```

I could try writing a custom serializer to solve this (Just assume an equivalent one is written for Nested as well):

```
new JsonSerializer<Base>() {
    @Override
    public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
         Map<String, Base> selfMap = new HashMap<>();
         selfMap.put(src.getClass().getSimpleName(), src);
         return context.serialize(selfMap);
     }
}

```

The problem with this, however, is of course that I get stuck in an infinite recursion when serializing the base type.

Is there any other way to solve this?