HashMap toJson 为空 JSON

HashMap toJson is empty with JSON

以下输出:

private static class Target extends HashMap<String, String> {
}

public static void main(String[] args) {
    final Moshi build = new Moshi.Builder().build();
    final JsonAdapter<Target> adapter = build.adapter(Target.class);
    final Target value = new Target();
    value.put("foo", "bar");
    System.out.println(adapter.toJson(value));
}

{} - 我希望 {"foo":"bar"} - 我如何获得预期的输出?

创建 JsonAdapter 时使用 Map class 而不是 Target

private static class Target extends HashMap<String, String> {
}

public static void main(String[] args) {
    final Moshi build = new Moshi.Builder().build();
    final JsonAdapter<Map> adapter = build.adapter(Map.class);
    final Target value = new Target();
    value.put("foo", "bar");
    System.out.println(adapter.toJson(value));
}