mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge branch 'kotucz-android_parametrized_type_workaround'
* kotucz-android_parametrized_type_workaround: Workaround for Android ParameterizedType implementation bug
This commit is contained in:
@@ -228,7 +228,7 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory {
|
|||||||
List<AdapterMethod> adapterMethods, Type type, Set<? extends Annotation> annotations) {
|
List<AdapterMethod> adapterMethods, Type type, Set<? extends Annotation> annotations) {
|
||||||
for (int i = 0, size = adapterMethods.size(); i < size; i++) {
|
for (int i = 0, size = adapterMethods.size(); i < size; i++) {
|
||||||
AdapterMethod adapterMethod = adapterMethods.get(i);
|
AdapterMethod adapterMethod = adapterMethods.get(i);
|
||||||
if (adapterMethod.type.equals(type) && adapterMethod.annotations.equals(annotations)) {
|
if (Types.equals(adapterMethod.type, type) && adapterMethod.annotations.equals(annotations)) {
|
||||||
return adapterMethod;
|
return adapterMethod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,8 @@ package com.squareup.moshi;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -357,6 +359,50 @@ public final class AdapterMethodsTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unfortunately in some versions of Android the implementations of {@link ParameterizedType}
|
||||||
|
* doesn't implement equals and hashCode. Confirm that we work around that.
|
||||||
|
*/
|
||||||
|
@Test public void parameterizedTypeEqualsNotUsed() throws Exception {
|
||||||
|
Moshi moshi = new Moshi.Builder()
|
||||||
|
.add(new ListOfStringJsonAdapter())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// This class doesn't implement equals() and hashCode() as it should.
|
||||||
|
ParameterizedType listOfStringType = new ParameterizedType() {
|
||||||
|
@Override public Type[] getActualTypeArguments() {
|
||||||
|
return new Type[] { String.class };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public Type getRawType() {
|
||||||
|
return List.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public Type getOwnerType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
JsonAdapter<List<String>> jsonAdapter = moshi.adapter(listOfStringType);
|
||||||
|
assertThat(jsonAdapter.toJson(Arrays.asList("a", "b", "c"))).isEqualTo("\"a|b|c\"");
|
||||||
|
assertThat(jsonAdapter.fromJson("\"a|b|c\"")).isEqualTo(Arrays.asList("a", "b", "c"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ListOfStringJsonAdapter {
|
||||||
|
@ToJson String listOfStringToJson(List<String> list) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
if (i > 0) result.append('|');
|
||||||
|
result.append(list.get(i));
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@FromJson List<String> listOfStringFromJson(String string) {
|
||||||
|
return Arrays.asList(string.split("\\|"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class Point {
|
static class Point {
|
||||||
final int x;
|
final int x;
|
||||||
final int y;
|
final int y;
|
||||||
|
Reference in New Issue
Block a user