@Override
  public JsonElement serialize(Holder src, Type type, JsonSerializationContext context) {
    JsonObject root = new JsonObject();
    JsonElement value = context.serialize(src.value());

    root.addProperty("id", src.id());

    if (value.isJsonObject()) {
      value.getAsJsonObject().addProperty(Holder.TYPE_PROPERTY, src.value().getClass().getName());
    }

    root.add(VALUE_PROPERTY, value);
    return root;
  }
Custom serializer which allows to (JSON) store different types of objects inside same class :
{@link Holder}
mongo/test/org/immutables/mongo/fixture/holder/HolderJsonSerializer.java