All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.immutables.fixture.GsonAdaptersJsonIgnore Maven / Gradle / Ivy

There is a newer version: 2.10.1
Show newest version
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.ParametersAreNonnullByDefault;
import javax.annotation.processing.Generated;

/**
 * A {@code TypeAdapterFactory} that handles all of the immutable types generated under {@code JsonIgnore}.
 * @see ImmutableJsonIgnore
 */
@SuppressWarnings({"all", "MethodCanBeStatic"})
@ParametersAreNonnullByDefault
@Generated("org.immutables.processor.ProxyProcessor")
@org.immutables.value.Generated(from = "org.immutables.fixture", generator = "Gsons")
public final class GsonAdaptersJsonIgnore implements TypeAdapterFactory {
  @SuppressWarnings({"unchecked", "rawtypes"}) // 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)";
  }

  @org.immutables.value.Generated(from = "JsonIgnore", generator = "Gsons")
  @SuppressWarnings({"unchecked", "rawtypes"}) // 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 {
      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 {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
      }
      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 - 2024 Weber Informatics LLC | Privacy Policy