So I'm pretty new to Gson as a library and Java in general. I feel that there is a better way to write the write function. the reason I had to create a custom adapter was because the json could sometimes return a string instead of a object. Anyway here is my code :)
@Override
public void write(JsonWriter jsonWriter, Description description) throws IOException {
jsonWriter.beginObject();
jsonWriter.name("extra");
jsonWriter.beginArray();
List<Extra> extra = description.getExtra();
for (int i = 0; i < extra.size(); i++) {
writeExtra(jsonWriter, extra.get(i));
}
jsonWriter.endArray();
jsonWriter.name("text");
jsonWriter.value(description.getText());
jsonWriter.endObject();
}
private void writeExtra(JsonWriter jsonWriter, Extra extra) throws IOException {
jsonWriter.beginObject();
if (extra.isBold()) {
jsonWriter.name("bold");
jsonWriter.value(true);
}
if (extra.isStrikeThrough()) {
jsonWriter.name("strikeThrough");
jsonWriter.value(true);
}
if (extra.getColor() != null) {
jsonWriter.name("color");
jsonWriter.value(extra.getColor());
}
if (extra.getExtra() != null) {
jsonWriter.name("extra");
jsonWriter.beginArray();
for (int i = 0; i < extra.getExtra().size(); i++) {
writeExtra(jsonWriter, extra.getExtra().get(i));
}
jsonWriter.endArray();
}
if (extra.getText() != null) {
jsonWriter.name("text");
jsonWriter.value(extra.getText());
}
jsonWriter.endObject();
}