mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
KotlinJsonAdapter (#281)
* Add kotlin-module with support for Kotlin data classes * Naming and style changes to KotlinJsonAdapter. Biggest changes: * Attempt to support regular classes and data classes * Avoid parameter hashing when indexing is sufficient for constructor parameters
This commit is contained in:
@@ -105,19 +105,6 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if {@code rawType} is built in. We don't reflect on private fields of platform
|
||||
* types because they're unspecified and likely to be different on Java vs. Android.
|
||||
*/
|
||||
private boolean isPlatformType(Class<?> rawType) {
|
||||
String name = rawType.getName();
|
||||
return name.startsWith("android.")
|
||||
|| name.startsWith("java.")
|
||||
|| name.startsWith("javax.")
|
||||
|| name.startsWith("kotlin.")
|
||||
|| name.startsWith("scala.");
|
||||
}
|
||||
|
||||
/** Returns true if fields with {@code modifiers} are included in the emitted JSON. */
|
||||
private boolean includeField(boolean platformType, int modifiers) {
|
||||
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) return false;
|
||||
@@ -125,6 +112,19 @@ final class ClassJsonAdapter<T> extends JsonAdapter<T> {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns true if {@code rawType} is built in. We don't reflect on private fields of platform
|
||||
* types because they're unspecified and likely to be different on Java vs. Android.
|
||||
*/
|
||||
static boolean isPlatformType(Class<?> rawType) {
|
||||
String name = rawType.getName();
|
||||
return name.startsWith("android.")
|
||||
|| name.startsWith("java.")
|
||||
|| name.startsWith("javax.")
|
||||
|| name.startsWith("kotlin.")
|
||||
|| name.startsWith("scala.");
|
||||
}
|
||||
|
||||
private final ClassFactory<T> classFactory;
|
||||
private final FieldBinding<?>[] fieldsArray;
|
||||
private final JsonReader.Options options;
|
||||
|
@@ -19,12 +19,23 @@ import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/** Customizes how a field is encoded as JSON. */
|
||||
@Target({FIELD, METHOD})
|
||||
/**
|
||||
* Customizes how a field is encoded as JSON.
|
||||
*
|
||||
* <p>Although this annotation doesn't declare a {@link Target}, it is only honored in the following
|
||||
* elements:
|
||||
*
|
||||
* <ul>
|
||||
* <li><strong>Java class fields</strong>
|
||||
* <li><strong>Kotlin properties</strong> for use with {@code moshi-kotlin}. This includes both
|
||||
* properties declared in the constructor and properties declared as members.
|
||||
* </ul>
|
||||
*
|
||||
* <p>Users of the <a href="https://github.com/rharter/auto-value-moshi">AutoValue: Moshi
|
||||
* Extension</a> may also use this annotation on abstract getters.
|
||||
*/
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
public @interface Json {
|
||||
|
@@ -533,15 +533,6 @@ public final class MoshiTest {
|
||||
}
|
||||
|
||||
@Test public void addNullFails() throws Exception {
|
||||
JsonAdapter jsonAdapter = new JsonAdapter() {
|
||||
@Override public Object fromJson(JsonReader reader) throws IOException {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@Override public void toJson(JsonWriter writer, Object value) throws IOException {
|
||||
throw new AssertionError();
|
||||
}
|
||||
};
|
||||
Type type = Object.class;
|
||||
Class<? extends Annotation> annotation = Annotation.class;
|
||||
Moshi.Builder builder = new Moshi.Builder();
|
||||
|
Reference in New Issue
Block a user