當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。