当前位置: 首页>>代码示例>>Java>>正文


Java TypeAdapter类代码示例

本文整理汇总了Java中com.google.gson.TypeAdapter的典型用法代码示例。如果您正苦于以下问题:Java TypeAdapter类的具体用法?Java TypeAdapter怎么用?Java TypeAdapter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


TypeAdapter类属于com.google.gson包,在下文中一共展示了TypeAdapter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: customDecoder

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Test
public void customDecoder() throws Exception {
  GsonDecoder decoder = new GsonDecoder(Arrays.<TypeAdapter<?>>asList(upperZone));

  List<Zone> zones = new LinkedList<Zone>();
  zones.add(new Zone("DENOMINATOR.IO."));
  zones.add(new Zone("DENOMINATOR.IO.", "ABCD"));

  Response response =
      Response.builder()
              .status(200)
              .reason("OK")
              .headers(Collections.<String, Collection<String>>emptyMap())
              .body(zonesJson, UTF_8)
              .build();
  assertEquals(zones, decoder.decode(response, new TypeToken<List<Zone>>() {
  }.getType()));
}
 
开发者ID:wenwu315,项目名称:XXXX,代码行数:19,代码来源:GsonCodecTest.java

示例2: writable

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> Range<Comparable<Object>> writable(TypeAdapter<T> adapter, Range<T> range) {
  if (range.hasLowerBound() && range.hasUpperBound()) {
    return Range.range(
        (Comparable<Object>) writable(adapter, range.lowerEndpoint()),
        range.lowerBoundType(),
        (Comparable<Object>) writable(adapter, range.upperEndpoint()),
        range.upperBoundType());
  } else if (range.hasLowerBound()) {
    return Range.downTo(
        (Comparable<Object>) writable(adapter, range.lowerEndpoint()),
        range.lowerBoundType());
  } else if (range.hasUpperBound()) {
    return Range.upTo(
        (Comparable<Object>) writable(adapter, range.upperEndpoint()),
        range.upperBoundType());
  }
  throw new AssertionError();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:20,代码来源:Support.java

示例3: create

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
  if (typeToken.getRawType() != Timestamp.class) {
    return null;
  }

  final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);
  return (TypeAdapter<T>) new TypeAdapter<Timestamp>() {
    @Override public Timestamp read(JsonReader in) throws IOException {
      Date date = dateTypeAdapter.read(in);
      return date != null ? new Timestamp(date.getTime()) : null;
    }

    @Override public void write(JsonWriter out, Timestamp value) throws IOException {
      dateTypeAdapter.write(out, value);
    }
  };
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:19,代码来源:TypeAdapters.java

示例4: getTypeAdapter

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> fieldType, JsonAdapter annotation) {
  Class<?> value = annotation.value();
  if (TypeAdapter.class.isAssignableFrom(value)) {
        Class<TypeAdapter<?>> typeAdapter = (Class<TypeAdapter<?>>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapter)).construct();
  }
  if (TypeAdapterFactory.class.isAssignableFrom(value)) {
        Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapterFactory))
        .construct()
        .create(gson, fieldType);
  }

  throw new IllegalArgumentException(
      "@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:19,代码来源:JsonAdapterAnnotationTypeAdapterFactory.java

示例5: create

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<?> rawType = type.getRawType();
    TypeAdapter<Map> mapAdapter = gson.getAdapter(Map.class);
    TypeAdapter<Collection> collectionAdapter = gson.getAdapter(Collection.class);
    if (Location.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new LocationTypeAdapter();
    } else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new ConfigurationSerializableAdapter(mapAdapter);
    } else if (GUISignature.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new GUISignatureTypeAdapter(mapAdapter, collectionAdapter);
    } else if (MaterialAndData.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new MaterialAndDataAdapter();
    }
    return null;
}
 
开发者ID:EntryPointKR,项目名称:MCLibrary,代码行数:17,代码来源:BukkitTypeAdapterFactory.java

示例6: assertRegisterTypeAdapter

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Test
public void assertRegisterTypeAdapter() {
    Gson beforeRegisterGson = GsonFactory.getGson();
    GsonFactory.registerTypeAdapter(GsonFactoryTest.class, new TypeAdapter() {
        
        @Override
        public Object read(final JsonReader in) throws IOException {
            return null;
        }
        
        @Override
        public void write(final JsonWriter out, final Object value) throws IOException {
            out.jsonValue("test");
        }
    });
    assertThat(beforeRegisterGson.toJson(new GsonFactoryTest()), is("{}"));
    assertThat(GsonFactory.getGson().toJson(new GsonFactoryTest()), is("test"));
}
 
开发者ID:elasticjob,项目名称:elastic-job-cloud,代码行数:19,代码来源:GsonFactoryTest.java

示例7: write

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override public void write(JsonWriter out, Object value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }

  TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass());
  if (typeAdapter instanceof ObjectTypeAdapter) {
    out.beginObject();
    out.endObject();
    return;
  }

  typeAdapter.write(out, value);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:17,代码来源:ObjectTypeAdapter.java

示例8: gatherParsers

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Override
public void gatherParsers(GsonBuilder builder) {
    builder.registerTypeHierarchyAdapter(TrickType.class, new TypeAdapter<TrickType>() {
        @Override
        public TrickType read(JsonReader in) throws IOException {
            TrickType type =  TrickType.byId.get(in.nextString());
            if (type == null) {
                return TrickType.STRING;
            }
            return type;
        }
        
        @Override
        public void write(JsonWriter out, TrickType value) throws IOException {
            out.value(value.getId());
        }
    });
}
 
开发者ID:tterrag1098,项目名称:MCBot,代码行数:19,代码来源:CommandTrick.java

示例9: newFactory

import com.google.gson.TypeAdapter; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactory(
    final Class<TT> unboxed, final Class<TT> boxed, final TypeAdapter<? super TT> typeAdapter) {
  return new TypeAdapterFactory() {
    @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
      Class<? super T> rawType = typeToken.getRawType();
      return (rawType == unboxed || rawType == boxed) ? (TypeAdapter<T>) typeAdapter : null;
    }
    @Override public String toString() {
      return "Factory[type=" + boxed.getName()
          + "+" + unboxed.getName() + ",adapter=" + typeAdapter + "]";
    }
  };
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:15,代码来源:TypeAdapters.java

示例10: getFieldAdapter

import com.google.gson.TypeAdapter; //导入依赖的package包/类
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
    JsonAdapter annotation = (JsonAdapter) field.getAnnotation(JsonAdapter.class);
    if (annotation != null) {
        TypeAdapter<?> adapter = JsonAdapterAnnotationTypeAdapterFactory.getTypeAdapter(this
                .constructorConstructor, gson, fieldType, annotation);
        if (adapter != null) {
            return adapter;
        }
    }
    return gson.getAdapter((TypeToken) fieldType);
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:12,代码来源:ReflectiveTypeAdapterFactory.java

示例11: create

import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (type.getRawType() == Object.class) {
        return (TypeAdapter<T>) new NumberPredictObjectTypeAdapter(gson);
    }
    return null;
}
 
开发者ID:EntryPointKR,项目名称:MCLibrary,代码行数:8,代码来源:NumberPredictObjectTypeAdapterFactory.java

示例12: create

import com.google.gson.TypeAdapter; //导入依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    Class<? super T> rawType = typeToken.getRawType();
    if (!Collection.class.isAssignableFrom(rawType)) {
        return null;
    }
    Type elementType = C$Gson$Types.getCollectionElementType(type, rawType);
    return new Adapter(gson, elementType, gson.getAdapter(TypeToken.get(elementType)), this
            .constructorConstructor.get(typeToken));
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:11,代码来源:CollectionTypeAdapterFactory.java

示例13: newFactoryForMultipleTypes

import com.google.gson.TypeAdapter; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactoryForMultipleTypes(final Class<TT> base,
    final Class<? extends TT> sub, final TypeAdapter<? super TT> typeAdapter) {
  return new TypeAdapterFactory() {
    @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
      Class<? super T> rawType = typeToken.getRawType();
      return (rawType == base || rawType == sub) ? (TypeAdapter<T>) typeAdapter : null;
    }
    @Override public String toString() {
      return "Factory[type=" + base.getName()
          + "+" + sub.getName() + ",adapter=" + typeAdapter + "]";
    }
  };
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:15,代码来源:TypeAdapters.java

示例14: mutationSetAdapter

import com.google.gson.TypeAdapter; //导入依赖的package包/类
private <T> TypeAdapter<T> mutationSetAdapter(TypeAdapter<T> delegate) {

			return new TypeAdapter<T>() {
				public void write(JsonWriter out, T value) throws IOException {
					delegate.write(out, value);
				}

				@SuppressWarnings("unchecked")
				public T read(JsonReader reader) throws IOException {
					final TypeAdapter<ArrayList<Mutation>> arrayListAdapter =
						gson.getAdapter(new TypeToken<ArrayList<Mutation>>(){});
					return (T) new MutationSet(arrayListAdapter.read(reader));
				}
			};
		}
 
开发者ID:hivdb,项目名称:sierra,代码行数:16,代码来源:Json.java

示例15: lookupAdapters

import com.google.gson.TypeAdapter; //导入依赖的package包/类
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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:8,代码来源:ExpectedSubtypesAdapter.java


注:本文中的com.google.gson.TypeAdapter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。