com.squareup.moshi.Json.name()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(95)

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

Json.name介绍

暂无

代码示例

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

FallbackEnumJsonAdapter(Class<T> enumType, T defaultValue) {
 this.enumType = enumType;
 this.defaultValue = defaultValue;
 try {
  constants = enumType.getEnumConstants();
  nameStrings = new String[constants.length];
  for (int i = 0; i < constants.length; i++) {
   T constant = constants[i];
   Json annotation = enumType.getField(constant.name()).getAnnotation(Json.class);
   String name = annotation != null ? annotation.name() : constant.name();
   nameStrings[i] = name;
  }
  options = JsonReader.Options.of(nameStrings);
 } catch (NoSuchFieldException e) {
  throw new AssertionError(e);
 }
}

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

EnumJsonAdapter(Class<T> enumType, @Nullable T fallbackValue, boolean useFallbackValue) {
 this.enumType = enumType;
 this.fallbackValue = fallbackValue;
 this.useFallbackValue = useFallbackValue;
 try {
  constants = enumType.getEnumConstants();
  nameStrings = new String[constants.length];
  for (int i = 0; i < constants.length; i++) {
   String constantName = constants[i].name();
   Json annotation = enumType.getField(constantName).getAnnotation(Json.class);
   String name = annotation != null ? annotation.name() : constantName;
   nameStrings[i] = name;
  }
  options = JsonReader.Options.of(nameStrings);
 } catch (NoSuchFieldException e) {
  throw new AssertionError("Missing field in " + enumType.getName(), e);
 }
}

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

EnumJsonAdapter(Class<T> enumType) {
 this.enumType = enumType;
 try {
  constants = enumType.getEnumConstants();
  nameStrings = new String[constants.length];
  for (int i = 0; i < constants.length; i++) {
   T constant = constants[i];
   Json annotation = enumType.getField(constant.name()).getAnnotation(Json.class);
   String name = annotation != null ? annotation.name() : constant.name();
   nameStrings[i] = name;
  }
  options = JsonReader.Options.of(nameStrings);
 } catch (NoSuchFieldException e) {
  throw new AssertionError("Missing field in " + enumType.getName(), e);
 }
}

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

/** Creates a field binding for each of declared field of {@code type}. */
private void createFieldBindings(
  Moshi moshi, Type type, Map<String, FieldBinding<?>> fieldBindings) {
 Class<?> rawType = Types.getRawType(type);
 boolean platformType = Util.isPlatformType(rawType);
 for (Field field : rawType.getDeclaredFields()) {
  if (!includeField(platformType, field.getModifiers())) continue;
  // Look up a type adapter for this type.
  Type fieldType = resolve(type, rawType, field.getGenericType());
  Set<? extends Annotation> annotations = Util.jsonAnnotations(field);
  String fieldName = field.getName();
  JsonAdapter<Object> adapter = moshi.adapter(fieldType, annotations, fieldName);
  // Create the binding between field and JSON.
  field.setAccessible(true);
  // Store it using the field's name. If there was already a field with this name, fail!
  Json jsonAnnotation = field.getAnnotation(Json.class);
  String name = jsonAnnotation != null ? jsonAnnotation.name() : fieldName;
  FieldBinding<Object> fieldBinding = new FieldBinding<>(name, field, adapter);
  FieldBinding<?> replaced = fieldBindings.put(name, fieldBinding);
  if (replaced != null) {
   throw new IllegalArgumentException("Conflicting fields:\n"
     + "    " + replaced.field + "\n"
     + "    " + fieldBinding.field);
  }
 }
}

代码示例来源:origin: com.squareup.moshi/moshi

EnumJsonAdapter(Class<T> enumType) {
 this.enumType = enumType;
 try {
  constants = enumType.getEnumConstants();
  nameStrings = new String[constants.length];
  for (int i = 0; i < constants.length; i++) {
   T constant = constants[i];
   Json annotation = enumType.getField(constant.name()).getAnnotation(Json.class);
   String name = annotation != null ? annotation.name() : constant.name();
   nameStrings[i] = name;
  }
  options = JsonReader.Options.of(nameStrings);
 } catch (NoSuchFieldException e) {
  throw new AssertionError("Missing field in " + enumType.getName(), e);
 }
}

代码示例来源:origin: xing/xing-android-sdk

SafeEnumJsonAdapter(Class<T> enumType) {
  this.enumType = enumType;
  try {
    T[] constants = enumType.getEnumConstants();
    //noinspection CollectionWithoutInitialCapacity
    nameConstantMap = new LinkedHashMap<>();
    nameStrings = new String[constants.length];
    for (int i = 0, size = constants.length; i < size; i++) {
      T constant = constants[i];
      Json annotation = enumType.getField(constant.name()).getAnnotation(Json.class);
      String name = annotation != null ? annotation.name() : constant.name();
      nameConstantMap.put(name, constant);
      nameStrings[i] = name;
    }
  } catch (NoSuchFieldException e) {
    throw new AssertionError("Missing field in " + enumType.getName(), e);
  }
}

代码示例来源:origin: com.squareup.moshi/moshi-adapters

EnumJsonAdapter(Class<T> enumType, @Nullable T fallbackValue, boolean useFallbackValue) {
 this.enumType = enumType;
 this.fallbackValue = fallbackValue;
 this.useFallbackValue = useFallbackValue;
 try {
  constants = enumType.getEnumConstants();
  nameStrings = new String[constants.length];
  for (int i = 0; i < constants.length; i++) {
   String constantName = constants[i].name();
   Json annotation = enumType.getField(constantName).getAnnotation(Json.class);
   String name = annotation != null ? annotation.name() : constantName;
   nameStrings[i] = name;
  }
  options = JsonReader.Options.of(nameStrings);
 } catch (NoSuchFieldException e) {
  throw new AssertionError("Missing field in " + enumType.getName(), e);
 }
}

代码示例来源:origin: serj-lotutovici/moshi-lazy-adapters

FallbackEnumJsonAdapter(Class<T> enumType, String fallback) {
 fallbackConstant = Enum.valueOf(enumType, fallback);
 this.enumType = enumType;
 try {
  constants = enumType.getEnumConstants();
  nameStrings = new String[constants.length];
  for (int i = 0; i < constants.length; i++) {
   T constant = constants[i];
   Json annotation = enumType.getField(constant.name()).getAnnotation(Json.class);
   String name = annotation != null ? annotation.name() : constant.name();
   nameStrings[i] = name;
  }
  options = JsonReader.Options.of(nameStrings);
 } catch (NoSuchFieldException e) {
  throw new AssertionError(e);
 }
}

代码示例来源:origin: com.squareup.moshi/moshi

/** Creates a field binding for each of declared field of {@code type}. */
private void createFieldBindings(
  Moshi moshi, Type type, Map<String, FieldBinding<?>> fieldBindings) {
 Class<?> rawType = Types.getRawType(type);
 boolean platformType = Util.isPlatformType(rawType);
 for (Field field : rawType.getDeclaredFields()) {
  if (!includeField(platformType, field.getModifiers())) continue;
  // Look up a type adapter for this type.
  Type fieldType = resolve(type, rawType, field.getGenericType());
  Set<? extends Annotation> annotations = Util.jsonAnnotations(field);
  String fieldName = field.getName();
  JsonAdapter<Object> adapter = moshi.adapter(fieldType, annotations, fieldName);
  // Create the binding between field and JSON.
  field.setAccessible(true);
  // Store it using the field's name. If there was already a field with this name, fail!
  Json jsonAnnotation = field.getAnnotation(Json.class);
  String name = jsonAnnotation != null ? jsonAnnotation.name() : fieldName;
  FieldBinding<Object> fieldBinding = new FieldBinding<>(name, field, adapter);
  FieldBinding<?> replaced = fieldBindings.put(name, fieldBinding);
  if (replaced != null) {
   throw new IllegalArgumentException("Conflicting fields:\n"
     + "    " + replaced.field + "\n"
     + "    " + fieldBinding.field);
  }
 }
}

相关文章