fastjson java8 stream 转换后 toJsonString 输出错误

fkvaft9z  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(229)

输出结果

  • 使用fastjson显示的结果
{{"name":"zhangsan"}:[{"code":"edit"}],{"name":"root"}:[{"$ref":"$.User2\\(name\\=zhangsan\\)[0]"},{"code":"admin"}]}
  • 使用Jackson显示的结果
{"User2(name=zhangsan)":[{"code":"edit"}],"User2(name=root)":[{"code":"edit"},{"code":"admin"}]}

复现过程

版本信息

  • jdk8
  • fastjson 1.2.58
  • streamex 0.6.5

主要代码

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User2 {
    String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Role {
    private String code;
}
@Test
    public void mapOperations() throws JsonProcessingException {

        Map<Role, List<User2>> role2users = new HashMap<>();
        User2 root = new User2("root");
        role2users.put(new Role("admin"), Lists.newArrayList(root));
        role2users.put(new Role("edit"), Lists.newArrayList(root, new User2("zhangsan")));

        Map<User2, List<Role>> users2roles = transformMap(role2users);

        //Json对象转为String字符串
        System.out.println(objectMapper.writeValueAsString(users2roles));
        System.out.println(JSON.toJSONString(users2roles));
    }

    public Map<User2, List<Role>> transformMap(Map<Role, List<User2>> role2users) {
        Map<User2, List<Role>> users2roles = EntryStream.of(role2users)
                .flatMapValues(List::stream)
                .invert()
                .grouping();
        return users2roles;
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题