使用Moshi JsonAdapter.Factory反序列化时丢失字段值

时间:2016-04-06 17:10:11

标签: java json moshi

我不知道我哪里出错了。

我的简单模型如下:

final class Data {
  final int num;

  Data(int num) {
    this.num = num;
  }

  static final class Wrapper {
    final List<Data> data;
    final long meta;

    Wrapper(List<Data> data, long meta) {
      this.data = data;
      this.meta = meta;
    }
  }
}

和我的工厂从Data

中提取Wrapper
final class ObjectAsListJsonAdapterFactory implements JsonAdapter.Factory {
  @Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
    if (!List.class.isAssignableFrom(Types.getRawType(type))) {
      return null;
    }
    JsonAdapter<List<Object>> listDelegate = moshi.nextAdapter(this, type, annotations);
    Type innerType = Types.collectionElementType(type, List.class);
    JsonAdapter<Object> objectDelegate = moshi.adapter(innerType, annotations);
    return new ListJsonAdapter<>(listDelegate, objectDelegate);
  }

  static final class ListJsonAdapter<T> extends JsonAdapter<List<T>> {
    private final JsonAdapter<List<T>> listDelegate;
    private final JsonAdapter<T> objectDelegate;

    ListJsonAdapter(JsonAdapter<List<T>> listDelegate, JsonAdapter<T> objectDelegate) {
      this.listDelegate = listDelegate;
      this.objectDelegate = objectDelegate;
    }

    @Override public List<T> fromJson(JsonReader jsonReader) throws IOException {
      if (jsonReader.peek() == JsonReader.Token.BEGIN_OBJECT) {
        return Collections.singletonList(objectDelegate.fromJson(jsonReader));
      } else {
        return listDelegate.fromJson(jsonReader);
      }
    }

    @Override public void toJson(JsonWriter jsonWriter, List<T> list) throws IOException {
      listDelegate.toJson(jsonWriter, list);
    }
  }
}

但是当我跑步时:

String json = "{\n"
    + "  \"data\": [\n"
    + "    {\n"
    + "      \"num\": 5\n"
    + "    }\n"
    + "  ],\n"
    + "  \"meta\": 21\n"
    + "}";
Moshi moshi = new Moshi.Builder().add(new ObjectAsListJsonAdapterFactory()).build();
ParameterizedType type = Types.newParameterizedType(List.class, Data.class);
JsonAdapter<List<Data>> adapter = moshi.adapter(type);
List<Data> expected = adapter.fromJson(json);

expected包含一个Datanum字段的值为0

我错过了什么?

1 个答案:

答案 0 :(得分:1)

该字段具有默认值,因为这不是该JSON所需的JsonAdapter.Factory。该工厂正在尝试制作像单个数组项目的对象。 这是你想要的解开:

static final class DataUnwrapperAdapterFactory implements JsonAdapter.Factory {
  @Override
  public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
    if (!List.class.isAssignableFrom(Types.getRawType(type))) {
      return null;
    }
    if (!Data.class.isAssignableFrom(Types.getRawType(Types.collectionElementType(type, List.class)))) {
      return null;
    }
    JsonAdapter<List<Data>> listDelegate = moshi.nextAdapter(this, type, annotations);
    JsonAdapter<Data.Wrapper> wrapperDelegate = moshi.adapter(Data.Wrapper.class, annotations);
    return new DataUnwrapperAdapter(listDelegate, wrapperDelegate);
  }

  static final class DataUnwrapperAdapter extends JsonAdapter<List<Data>> {
    private final JsonAdapter<List<Data>> listDelegate;
    private final JsonAdapter<Data.Wrapper> wrapperDelegate;

    DataUnwrapperAdapter(JsonAdapter<List<Data>> listDelegate,
        JsonAdapter<Data.Wrapper> wrapperDelegate) {
      this.listDelegate = listDelegate;
      this.wrapperDelegate = wrapperDelegate;
    }

    @Override public List<Data> fromJson(JsonReader jsonReader) throws IOException {
      if (jsonReader.peek() == JsonReader.Token.BEGIN_OBJECT) {
        Data.Wrapper wrapper = wrapperDelegate.fromJson(jsonReader);
        return wrapper.data;
      } else {
        return listDelegate.fromJson(jsonReader);
      }
    }

    @Override public void toJson(JsonWriter jsonWriter, List<Data> list) throws IOException {
      listDelegate.toJson(jsonWriter, list);
    }
  }
}

现在,您可以执行以下操作:

String json = "{\n"
    + "  \"other_garbage\": \"hiya\",\n"
    + "  \"data\": [\n"
    + "    {\n"
    + "      \"num\": 5\n"
    + "    }\n"
    + "  ],\n"
    + "  \"meta\": 21\n"
    + "}";
Moshi moshi = new Moshi.Builder().add(new DataUnwrapperAdapterFactory()).build();
ParameterizedType type = Types.newParameterizedType(List.class, Data.class);
JsonAdapter<List<Data>> adapter = moshi.adapter(type);
List<Data> expected = adapter.fromJson(json);

非常感谢Jake和Jesse(一如既往)获得此解决方案。