14 Şubat 2018 Çarşamba

Gson GsonBuilder Sınıfı

constructor
Şöyle yaparız.
GsonBuilder builder = new GsonBuilder();
create metodu
Şöyle yaparız.
Gson gson = builder.create();
excludeFieldsWithoutExposeAnnotation metodu
@Expose ile işaretli olmayan alanları dahil etmez. Şöyle yaparız.
Gson gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
registerTypeAdapter metodu - JsonDeserializer
Örnek
Şöyle yaparız.
class BooleanJsonDeserializer implements JsonDeserializer<Boolean> {
  @Override
  public Boolean deserialize(JsonElement json, Type typeOfT,
    JsonDeserializationContext context) throws JsonParseException {
    JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
    if (jsonPrimitive.isBoolean()) {
      return jsonPrimitive.getAsBoolean();
    } else {
      return jsonPrimitive.getAsInt() == 0 ? false : true;
    }
  }
}

Gson gson = new GsonBuilder()
  .registerTypeAdapter(Boolean.class, new BooleanJsonDeserializer()).create();
Örnek
Şöyle yaparız
Gson gson = 
new GsonBuilder()
  .registerTypeAdapter(CommentData.class, new JsonDeserializer<CommentData>() {
    @Override
    public CommentData deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {
      ...
    }
  })
  .serializeNulls()
  .create();
registerTypeAdapter metodu - JsonDeserializer + JsonSerializer
Şöyle yaparız.
public class FooAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T>
{

  @Override
  public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObject = new JsonObject();
    ...
    return jsonObject;
  }

  @Override
  public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
  throws JsonParseException
  {
    ...
    return context.deserialize(...,...);
  }
  
}
Şöyle yaparız.
builder.registerTypeAdapter(Foo.class, new FooAdapter<Foo>());
registerTypeAdapter metodu -TypeAdapter
Şöyle yaparız.
builder.registerTypeAdapter(BigDecimal.class, new TypeAdapter<BigDecimal>() {
  @Override
  public BigDecimal read(JsonReader reader) throws IOException {

    if (reader.peek() == JsonToken.STRING) {
      String stringNum = reader.nextString();
      if (stringNum == null || stringNum.isEmpty()) {
        return null;
      } else {                      
        return new BigDecimal(stringNum);
      }

    } else {
      return null;
    }
  }

  @Override
  public void write(JsonWriter writer, BigDecimal num) throws IOException {
    // TODO Auto-generated method stub

  }
});
setExclusionStrategies metodu
Şöyle yaparız.
builder.setExclusionStrategies(new ExclusionStrategy() {
  @Override
  public boolean shouldSkipField(FieldAttributes f) {
    ...
  }

  @Override
  public boolean shouldSkipClass(Class<?> clazz) {
    ...
  }
});
serializeNulls metodu
Şöyle yaparız.
Gson gson = new GsonBuilder().serializeNulls().create();
setPrettyPrinting metodu
Şöyle yaparız.
builder.setPrettyPrinting();



Hiç yorum yok:

Yorum Gönder