本文整理了Java中com.squareup.moshi.Json
类的一些代码示例,展示了Json
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Json
类的具体详情如下:
包路径:com.squareup.moshi.Json
类名称:Json
暂无
代码示例来源:origin: square/moshi
public final class Player {
public final String username;
public final @Json(name = "lucky number") int luckyNumber;
public Player(String username, int luckyNumber) {
this.username = username;
this.luckyNumber = luckyNumber;
}
@Override public String toString() {
return username + " gets lucky with " + luckyNumber;
}
}
代码示例来源: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
@Json(name = "not-started") NOT_STARTED,
@Json(name = "in-progress") IN_PROGRESS,
@Json(name = "rejected") REJECTED,
@Json(name = "completed") COMPLETED
代码示例来源: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/osstrich
static final class Select {
@Json(name = "response") Response response;
static final class Response {
@Json(name = "docs") List<Artifact> artifacts;
}
}
}
代码示例来源: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: CoinbaseWallet/toshi-headless-client
public class Balance {
@Json(name = "confirmed_balance")
private String confirmedBalanceAsHex;
@Json(name = "unconfirmed_balance")
private String unconfirmedBalanceAsHex;
public BigInteger getConfirmedBalance() {
return TypeConverter.StringHexToBigInteger(confirmedBalanceAsHex);
}
public BigInteger getUnconfirmedBalance() {
return TypeConverter.StringHexToBigInteger(unconfirmedBalanceAsHex);
}
}
代码示例来源: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: square/osstrich
static final class Response {
@Json(name = "docs") List<Artifact> artifacts;
}
}
代码示例来源: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: CodeDex/CodeDex
/**
* Representation of repository json object, sample: https://api.github.com/users/fabianterhorst/repos
*/
public class Repo {
private int id;
private String name;
@Json(name = "full_name")
private String fullName;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getFullName() {
return fullName;
}
}
代码示例来源: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: xing/xing-android-sdk
/** Reason for the profile visit. */
public static class Reason implements Serializable {
private static final long serialVersionUID = 1L;
@Json(name = "text")
private final String text;
public Reason(String text) {
this.text = text;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Reason reason = (Reason) o;
return text != null ? text.equals(reason.text) : reason.text == null;
}
@Override
public int hashCode() {
return text != null ? text.hashCode() : 0;
}
@Override
public String toString() {
return text;
}
public String text() {
return text;
}
}
代码示例来源: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: square/osstrich
private static final String JAVADOC_EXTENSION = "-javadoc.jar";
@Json(name = "g") String groupId;
@Json(name = "a") String artifactId;
String latestVersion;
@Json(name = "p") String packaging;
long timestamp;
@Json(name = "ec") Set<String> extensions;
代码示例来源: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: xing/xing-android-sdk
private static final long serialVersionUID = 1L;
@Json(name = "content")
private String content;
代码示例来源: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);
}
}
}
代码示例来源:origin: xing/xing-android-sdk
private static final long serialVersionUID = 1L;
@Json(name = "preview_content")
private String previewContent;
代码示例来源:origin: xing/xing-android-sdk
public class CompanyLinks implements Serializable {
private static final long serialVersionUID = 1L;
@Json(name = "xing")
private final String xing;
@Json(name = "thumbnail")
private final String thumbnail;
@Json(name = "logo")
private final String logo;
内容来源于网络,如有侵权,请联系作者删除!