org.immutables.fixture.GsonAdaptersJsonIgnore Maven / Gradle / Ivy
package org.immutables.fixture;
import com.google.gson.*;
import com.google.gson.reflect.*;
import com.google.gson.stream.*;
import java.io.IOException;
import javax.annotation.Generated;
import javax.annotation.ParametersAreNonnullByDefault;
/**
* A {@code TypeAdapterFactory} that handles all of the immutable types generated under {@code JsonIgnore}.
* @see ImmutableJsonIgnore
*/
@SuppressWarnings("all")
@Generated({"Gsons.generator", "org.immutables.fixture.JsonIgnore"})
@ParametersAreNonnullByDefault
public final class GsonAdaptersJsonIgnore implements TypeAdapterFactory {
@SuppressWarnings({"unchecked", "raw"}) // safe unchecked, types are verified in runtime
@Override
public TypeAdapter create(Gson gson, TypeToken type) {
if (JsonIgnoreTypeAdapter.adapts(type)) {
return (TypeAdapter) new JsonIgnoreTypeAdapter(gson);
}
return null;
}
@Override
public String toString() {
return "GsonAdaptersJsonIgnore(JsonIgnore)";
}
@SuppressWarnings({"unchecked", "raw"}) // safe unchecked, types are verified in runtime
private static class JsonIgnoreTypeAdapter extends TypeAdapter {
JsonIgnoreTypeAdapter(Gson gson) {
}
static boolean adapts(TypeToken> type) {
return JsonIgnore.class == type.getRawType()
|| ImmutableJsonIgnore.class == type.getRawType();
}
@Override
public void write(JsonWriter out, JsonIgnore value) throws IOException {
if (value == null) {
out.nullValue();
} else {
writeJsonIgnore(out, value);
}
}
@Override
public JsonIgnore read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
return readJsonIgnore(in);
}
private void writeJsonIgnore(JsonWriter out, JsonIgnore instance)
throws IOException {
out.beginObject();
out.name("value");
out.value(instance.value());
out.endObject();
}
private JsonIgnore readJsonIgnore(JsonReader in)
throws IOException {
ImmutableJsonIgnore.Builder builder = ImmutableJsonIgnore.builder();
in.beginObject();
while (in.hasNext()) {
eachAttribute(in, builder);
}
in.endObject();
return builder.build();
}
private void eachAttribute(JsonReader in, ImmutableJsonIgnore.Builder builder)
throws IOException {
String attributeName = in.nextName();
switch (attributeName.charAt(0)) {
case 'v':
if ("value".equals(attributeName)) {
readInValue(in, builder);
return;
}
break;
default:
}
in.skipValue();
}
private void readInValue(JsonReader in, ImmutableJsonIgnore.Builder builder)
throws IOException {
builder.value(in.nextInt());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy