com.google.gson.Gson.getAdapter()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(572)

本文整理了Java中com.google.gson.Gson.getAdapter()方法的一些代码示例,展示了Gson.getAdapter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Gson.getAdapter()方法的具体详情如下:
包路径:com.google.gson.Gson
类名称:Gson
方法名:getAdapter

Gson.getAdapter介绍

[英]Returns the type adapter for type.
[中]返回类型的类型适配器。

代码示例

代码示例来源:origin: immutables/immutables

private List<TypeAdapter<? extends T>> lookupAdapters() {
 List<TypeAdapter<? extends T>> adapters = new ArrayList<>(subtypes.length);
 for (TypeToken<? extends T> subtype : subtypes) {
  adapters.add(gson.getAdapter(subtype));
 }
 return adapters;
}

代码示例来源:origin: square/retrofit

@Override
 public Converter<?, RequestBody> requestBodyConverter(Type type,
   Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
  TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
  return new GsonRequestBodyConverter<>(gson, adapter);
 }
}

代码示例来源:origin: square/retrofit

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
  Retrofit retrofit) {
 TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
 return new GsonResponseBodyConverter<>(gson, adapter);
}

代码示例来源:origin: immutables/immutables

@Override
 public <T> Codec<T> get(Class<T> clazz) {
  return codecFromTypeAdapter(clazz, gson.getAdapter(clazz));
 }
};

代码示例来源:origin: json-path/JsonPath

@Override
public <T> T map(Object source, Class<T> targetType, Configuration configuration) {
  if(source == null){
    return null;
  }
  try {
    return factory.call().getAdapter(targetType).fromJsonTree((JsonElement) source);
  } catch (Exception e){
    throw new MappingException(e);
  }
}

代码示例来源:origin: stackoverflow.com

public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
  @Override
  @SuppressWarnings("unchecked")
  public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    return (HibernateProxy.class.isAssignableFrom(type.getRawType())
        ? (TypeAdapter<T>) new HibernateProxyTypeAdapter((TypeAdapter)gson.getAdapter(TypeToken.get(type.getRawType().getSuperclass()))) 
   : null);
  }
};
private final TypeAdapter<Object> delegate;

private HibernateProxyTypeAdapter(TypeAdapter<Object> delegate) {
  this.delegate = delegate;
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }
  delegate.write(out, ((HibernateProxy) value).getHibernateLazyInitializer()
      .getImplementation());
}

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

@Override
public Parser<ResponseBody, ?> responseBodyParser(Type type) {
  TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
  return new GsonResponseBodyParser<>(gson, adapter);
}

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

@Override
public Parser<?, RequestBody> requestBodyParser(Type type) {
  TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
  return new GsonRequestBodyParser<>(gson, adapter);
}

代码示例来源:origin: immutables/immutables

@SuppressWarnings("unchecked")
@Override
public Object read(Gson gson, Type type, InputStream stream) throws IOException {
 @Nullable JsonReader reader = null;
 try {
  reader = createJsonReader(new BufferedReader(new InputStreamReader(stream, CHARSET_NAME)));
  options.setReaderOptions(reader);
  return gson.getAdapter((TypeToken<Object>) TypeToken.get(type)).read(reader);
 } catch (IOException ex) {
  throw ex;
 } catch (Exception ex) {
  throw new IOException(ex);
 }
}

代码示例来源:origin: com.squareup.retrofit2/converter-gson

@Override
 public Converter<?, RequestBody> requestBodyConverter(Type type,
   Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
  TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
  return new GsonRequestBodyConverter<>(gson, adapter);
 }
}

代码示例来源:origin: com.squareup.retrofit2/converter-gson

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
  Retrofit retrofit) {
 TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
 return new GsonResponseBodyConverter<>(gson, adapter);
}

代码示例来源:origin: json-path/JsonPath

@Override
  public <T> T map(Object source, TypeRef<T> targetType, Configuration configuration) {
    if(source == null){
      return null;
    }
    try {
      return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source);
    } catch (Exception e){
      throw new MappingException(e);
    }
  }
}

代码示例来源:origin: square/wire

@SuppressWarnings("unchecked")
@Override public @Nullable M read(JsonReader in) throws IOException {
 if (in.peek() == JsonToken.NULL) {
  in.nextNull();
  return null;
 }
 TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
 B builder = messageAdapter.newBuilder();
 in.beginObject();
 while (in.peek() != JsonToken.END_OBJECT) {
  String name = in.nextName();
  FieldBinding<M, B> fieldBinding = fieldBindings.get(name);
  if (fieldBinding == null) {
   in.skipValue();
  } else {
   JsonElement element = elementAdapter.read(in);
   Object value = parseValue(fieldBinding, element);
   fieldBinding.set(builder, value);
  }
 }
 in.endObject();
 return builder.build();
}

代码示例来源:origin: immutables/immutables

@SuppressWarnings("unchecked")
 @Override
 public Object read(Gson gson, Type type, InputStream stream) throws IOException {
  @Nullable JsonReader reader = null;
  try {
   reader = new JsonParserReader(JSON_FACTORY.createParser(stream));
   options.setReaderOptions(reader);
   return gson.getAdapter((TypeToken<Object>) TypeToken.get(type)).read(reader);
  } catch (IOException ex) {
   throw ex;
  } catch (Exception ex) {
   throw new IOException(ex);
  } finally {
   if (reader != null) {
    try {
     // note that stream is not closed here as per JSON_FACTORY configuration
     reader.close();
    } catch (IOException ex) {
     // ignore io exception of reader close as not important
    }
   }
  }
 }
}

代码示例来源:origin: immutables/immutables

@SuppressWarnings({"resource", "unchecked"})
 public static <T> T fromJson(String string, Class<T> type) {
  TypeAdapter<Object> adapter = GSON.getAdapter((TypeToken<Object>) TypeToken.get(type));
  try {
   if (adapter instanceof com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.Adapter) {
    throw new IllegalStateException("Immutable adapters not registered");
   }
   JsonParserReader reader = new JsonParserReader(JSON_FACTORY.createParser(string));
   return GSON.fromJson(reader, type);
  } catch (IOException ex) {
   throw Throwables.propagate(ex);
  }
 }
}

代码示例来源:origin: nickbutcher/plaid

@Override
public Converter<ResponseBody, ?> responseBodyConverter(
    Type type, Annotation[] annotations, Retrofit retrofit) {
  // This converter requires an annotation providing the name of the payload in the envelope;
  // if one is not supplied then return null to continue down the converter chain.
  final String payloadName = getPayloadName(annotations);
  if (payloadName == null) return null;
  final TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
  return (Converter<ResponseBody, Object>) body -> {
    try (JsonReader jsonReader = gson.newJsonReader(body.charStream())) {
      jsonReader.beginObject();
      while (jsonReader.hasNext()) {
        if (payloadName.equals(jsonReader.nextName())) {
          return adapter.read(jsonReader);
        } else {
          jsonReader.skipValue();
        }
      }
      return null;
    } finally {
      body.close();
    }
  };
}

代码示例来源:origin: immutables/immutables

@SuppressWarnings({"resource", "unchecked"})
public static String toJson(Object object) {
 TypeAdapter<Object> adapter = GSON.getAdapter((TypeToken<Object>) TypeToken.get(object.getClass()));
 try {
  StringWriter stringWriter = new StringWriter();
  JsonGeneratorWriter writer = new JsonGeneratorWriter(JSON_FACTORY.createGenerator(stringWriter));
  if (adapter instanceof com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.Adapter) {
   throw new IllegalStateException("Immutable adapters not registered");
  }
  GSON.toJson(object, object.getClass(), writer);
  writer.close();
  return stringWriter.toString();
 } catch (IOException ex) {
  throw Throwables.propagate(ex);
 }
}

代码示例来源:origin: immutables/immutables

@Test
public void reflectiveTypeAdapter() {
 check(!GsonCodecs.isReflectiveTypeAdapter(new GsonBuilder().create().getAdapter(BigDecimal.class)));
}

代码示例来源:origin: immutables/immutables

@Test
public void unmarshalNull() {
 check(Marshaling.fromJson("null", NonGrouped.Cadabra.class)).isNull();
 check(Marshaling.getGson().getAdapter(NonGrouped.Cadabra.class).toJsonTree(null)).isA(JsonNull.class);
}

代码示例来源:origin: immutables/immutables

@Test
public void unmarshalWrappedNull() {
 check(Marshaling.fromJson("null", NonGrouped.Buagra.class).buagra()).isNull();
 check(Marshaling.getGson().getAdapter(NonGrouped.Buagra.class).toJsonTree(ImmutableBuagra.of(null)))
   .isA(JsonNull.class);
}

相关文章