Simply your TypeAdapter
is wrong. Replace it with:
public void write(JsonWriter writer, MyClass object) throws IOException {
if (object == null) {
writer.nullValue();
return;
}
writer.beginObject();
writer.name("type").value(object.getType().toString()); //this is the enum
writer.endObject();
}
In your code, you create a string from the enum doing a JSON serialization. This produces "ENUM_VALUE"
(gson.toJson(object.getType())
), and then it is serialized again into a string so the result is \"ENUM_VALUE\"
.
In my code, I get the string representation of the enum using the toString()
method so no additional quotes are created.