一种更清洁的方法可能是
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:你需要一个 customTypeAdapterFactory
和一个 custom 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<Entry>
没有使用自定义。
问题出在这里,在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 将输入Entry
(Class<?> raw
参数)识别为
interface Map.Entry<K, V> { ... }
所以
if (raw.isInterface())
yield true
,并返回一个空boundFields
LinkedHashMap
。
因此,这里
for (BoundField boundField : boundFields.values()) { ... }
没有执行循环,也没有提取和写入任何值
boundField.write(...)