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:
Jesse Wilson
2017-04-18 23:51:37 -04:00
committed by GitHub
parent 8c18caf574
commit 81bbe870f1
7 changed files with 778 additions and 27 deletions

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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();