下面列出了怎么用com.google.gson.annotations.SerializedName的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Get's the class' id property name
*
* @param clazz
* @return Id Property name
*/
@SuppressWarnings("rawtypes")
private static String getIdPropertyName(Class clazz) {
// Search for annotation called id, regardless case
for (Field field : clazz.getDeclaredFields()) {
SerializedName serializedName = field.getAnnotation(SerializedName.class);
if (serializedName != null && serializedName.value().equalsIgnoreCase("id")) {
return serializedName.value();
} else if (field.getName().equalsIgnoreCase("id")) {
return field.getName();
}
}
// Otherwise, return empty
return "";
}
/**
* Validates the class has an id property defined
*
* @param clazz
*/
private <E> void validateClass(Class<E> clazz) {
if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
throw new IllegalArgumentException("The class type used for creating a MobileServiceTable must be a concrete class");
}
int idPropertyCount = 0;
for (Field field : clazz.getDeclaredFields()) {
SerializedName serializedName = field.getAnnotation(SerializedName.class);
if (serializedName != null) {
if (serializedName.value().equalsIgnoreCase("id")) {
idPropertyCount++;
}
} else {
if (field.getName().equalsIgnoreCase("id")) {
idPropertyCount++;
}
}
}
if (idPropertyCount != 1) {
throw new IllegalArgumentException("The class representing the MobileServiceTable must have a single id property defined");
}
}
/**
* @return the system properties defined or annotated in the entity class
*
* @param clazz Target entity class
*/
public static <F> EnumSet<MobileServiceSystemProperty> getSystemProperties(Class<F> clazz) {
EnumSet<MobileServiceSystemProperty> result = EnumSet.noneOf(MobileServiceSystemProperty.class);
Class<?> idClazz = getIdPropertyClass(clazz);
if (idClazz != null && !isIntegerClass(idClazz)) {
// Search for system properties annotations, regardless case
for (Field field : clazz.getDeclaredFields()) {
SerializedName serializedName = field.getAnnotation(SerializedName.class);
if (serializedName != null) {
if (SystemPropertyNameToEnum.containsKey(serializedName.value())) {
result.add(SystemPropertyNameToEnum.get(serializedName.value()));
}
} else {
if (SystemPropertyNameToEnum.containsKey(field.getName())) {
result.add(SystemPropertyNameToEnum.get(field.getName()));
}
}
}
}
// Otherwise, return empty
return result;
}
private static String serializedName(Member member) {
if(member instanceof AnnotatedElement) {
SerializedName nameAnnot = ((AnnotatedElement) member).getAnnotation(SerializedName.class);
if(nameAnnot != null) return nameAnnot.value();
}
return member.getName();
}
@SerializedName("turn-on-reminder")
TURN_ON_REMINDER("turn-on-reminder"),
@SerializedName("turn-on-autopayment")
TURN_ON_AUTOPAYMENT("turn-on-autopayment"),
@SerializedName("repeat")
REPEAT("repeat"),
@SerializedName("add-to-favourites")
ADD_TO_FAVOURITES("add-to-favourites");
@SerializedName("daily")
DAILY,
@SerializedName("weekly")
WEEKLY,
@SerializedName("monthly")
MONTHLY;
@Override
public String toString() { return name().toLowerCase(); }
@SerializedName("_meta")
public abstract CivicApiMetadata meta();
@Nullable
@SerializedName("reference_build")
public abstract String referenceBuild();
@SerializedName("evidence_items")
protected abstract CivicEvidenceItemMetadata evidenceItems();
@Nullable
@SerializedName("image_srouce")
public abstract String getImageSrouce();
@SerializedName("url")
public abstract String url();
@Nullable
@SerializedName("representative_transcript2")
public abstract String representativeTranscript2();
@SerializedName("id")
public abstract int id();
@SerializedName("username")
public abstract String username();
@Nullable
@SerializedName("comments")
public abstract List<Comment> getComments();
@SerializedName("evidence_level")
public abstract Character level();
@SerializedName("country")
public abstract String country();
@SerializedName("size")
public abstract Integer size();
@Nullable
@SerializedName("a")
String[] array();
static String getFieldName(FieldNamingStrategy fieldNamingPolicy, Field f) {
SerializedName serializedName = f.getAnnotation(SerializedName.class);
return serializedName == null ? fieldNamingPolicy.translateName(f) : serializedName.value();
}
@NotNull
@SerializedName("ptum")
public abstract String primaryTumor();
@SerializedName("type")
public abstract int getType();
/** The label for the configured repository from which this revision originates. */
@SerializedName(
value = "repository_name",
alternate = {"repositoryName"}
)
public abstract String repositoryName();
@SerializedName("variant_types")
List<CivicVariantType> variantTypes();
@SerializedName("id")
public abstract int getId();
/** The unique ID assigned to this revision by the underlying revision control system. */
@SerializedName(
value = "rev_id",
alternate = {"revId"}
)
public abstract String revId();
@Nullable
@SerializedName("command_string") // TODO(cushon): remove pending rharter/auto-value-gson#18
public abstract String commandString();
@SerializedName("login")
public abstract String login();
@SerializedName("accepted_count")
public abstract int accepted();
@SerializedName("id")
public abstract int getId();