0

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();
}
LoserEXE
  • 33
  • 5

1 Answers1

2

You could define one TypeAdapter per complex object defined in your json structure. This could reduce code inside your actual TypeAdapter and deport the creation of sub-object in sub-TypeAdapter.

DescriptionTypaAdapter.class

public class DescriptionTypaAdapter extends TypeAdapter<Description> {
    
    @Override
    public void write(JsonWriter jsonWriter, Description description) throws IOException {
        jsonWriter.beginObject();
        
        jsonWriter.name("extra");
        jsonWriter.beginArray();
        // Call specific TypeAdapter to handle transformation of Extra object to Json
        TypeAdapter<Extra> extraTypeAdapter = new Gson().getAdapter(Extra.class);
        for (Extra extra : description.getExtras()) {
            extraTypeAdapter.write(jsonWriter, extra);
        }
        jsonWriter.endArray();
        
        jsonWriter.name("text");
        jsonWriter.value(description.getText());
        
        jsonWriter.endObject();
        
    }
}

ExtraTypaAdapter.class

public class ExtraAdapter extends TypeAdapter<Extra> {
    
    @Override
    public void write(JsonWriter jsonWriter, Extra extra) throws IOException {
        jsonWriter.beginObject();
        // Put here Extra Json build  
        if (extra.isBold()) {
            jsonWriter.name("bold");
            jsonWriter.value(true);
        }
        ...
        
        jsonWriter.endObject();
    }
}
  • This. This kind of adapter basically realizes object->string serialization and string->object deserialization. This way, you can access you JSON object as `object.property.property`, including having the correct types automatically. – Torge Rosendahl Sep 25 '22 at 22:23