【问题标题】:Java - Serializing Iterable<Map.Entry<>> with GsonJava - 使用 Gson 序列化 Iterable<Map.Entry<>>
【发布时间】:2019-03-24 14:27:20
【问题描述】:

我尝试使用 Google GSON 库从 Map.Entry 类型序列化“Iterable”- 我得到了一个空的输出。

这里是代码示例:

static private Iterable<Map.Entry<String, Integer>> getIterable(){
    HashMap<String, Integer> map = new HashMap<>();
    map.put("1", 1);
    map.put("2", 2);

    Iterable<Map.Entry<String, Integer>> iterable = map.entrySet();
    return  iterable;

}

public static void main(String[] args) {

    Iterable<Map.Entry<String, Integer>> myIterable = getIterable();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.enableComplexMapKeySerialization().create();
    String  a= gson.toJson(myIterable);
    System.out.println(a);

}

这是输出:

[{},{}]

知道我做错了什么吗?

谢谢:)

java 版本:1.8
gson 版本:2.6.2

【问题讨论】:

  • 为什么不自己解析地图呢? gson.toJson(map);
  • 这只是一个小例子 - 但在现实世界中我使用返回 Iterable 的 API..

标签: java serialization gson entryset


【解决方案1】:

一种更简洁的方法可能是

final Iterable<Entry<String, Integer>> iterable = getIterable();
final Gson gson = new Gson();
final JsonArray jsonArray = new JsonArray();

for (final Entry<String, Integer> entry : iterable) {
    final JsonElement jsonElement = gson.toJsonTree(entry);
    jsonElement.getAsJsonObject().remove("hash");
    jsonArray.add(jsonElement);
}

或者我喜欢的Stream 版本

StreamSupport.stream(iterable.spliterator(), false)
             .map(gson::toJsonTree)
             .map(JsonElement::getAsJsonObject)
             .peek(obj -> obj.remove("hash"))
             .collect(of(
                     JsonArray::new,
                     (array, obj) -> array.add(obj),
                     (output, toMerge) -> {
                         output.addAll(toMerge);
                         return output;
                     }
             ));

输出:[{"key":"1","value":1},{"key":"2","value":2}]


TL;DR:您需要一个自定义的TypeAdapterFactory一个自定义的TypeAdapter

TypeAdapters上查看此方法

public static <TT> TypeAdapterFactory newFactory(
    final TypeToken<TT> type, final TypeAdapter<TT> typeAdapter) {
  return new TypeAdapterFactory() {
    @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
    @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
      return typeToken.equals(type) ? (TypeAdapter<T>) typeAdapter : null;
    }
  };
}

没有自定义TypeAdapterFactory

typeToken.equals(type)

返回false,甚至没有使用自定义TypeAdapter&lt;Entry&gt;


问题出在ReflectiveTypeAdapterFactory#write

@Override public void write(JsonWriter out, T value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }

  out.beginObject();
  try {
    for (BoundField boundField : boundFields.values()) {
      if (boundField.writeField(value)) {
        out.name(boundField.name);
        boundField.write(out, value);
      }
    }
  } catch (IllegalAccessException e) {
    throw new AssertionError(e);
  }
  out.endObject();
}

ReflectiveTypeAdapterFactory#getBoundFields

private Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type, Class<?> raw) {
  Map<String, BoundField> result = new LinkedHashMap<String, BoundField>();

  if (raw.isInterface()) {
    return result;
  }

Gson 将输入 EntryClass&lt;?&gt; raw 参数)识别为

interface Map.Entry<K, V> { ... }

因此

if (raw.isInterface())

产生true,并返回一个空的boundFields LinkedHashMap
因此,这里

for (BoundField boundField : boundFields.values()) { ... }

没有执行循环,也没有提取和写入值

boundField.write(...)

【讨论】:

  • @Eyalleshem 这没什么。最终,我建议您序列化 Map 本身,而不是其 entrySet。定制是以维护为代价的,我为此被烧了很多次;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 2023-04-04
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
相关资源
最近更新 更多