Java 强制将属性序列化为Jackson中的标识

Java 强制将属性序列化为Jackson中的标识,java,jackson,jsonidentityinfo,Java,Jackson,Jsonidentityinfo,我有一节课要上 (免责声明,我是用Kotlin编写的,但我将其翻译成Java,以便更多的人能够阅读) 当我从这个JSON反序列化一个Bar实例时: { "fooList": [ { "id": 0, "fooList": [1] }, { "id": 1, "fooList": [] } ] } 然后我将其序列化回来,我希望输出与输入相同,但实际上是这样的: { "fooList": [ {

我有一节课要上

(免责声明,我是用Kotlin编写的,但我将其翻译成Java,以便更多的人能够阅读)

当我从这个JSON反序列化一个
Bar
实例时:

{
  "fooList": [
    {
      "id": 0,
      "fooList": [1]
    }, 
    {
      "id": 1,
      "fooList": []
    }
  ]
}
然后我将其序列化回来,我希望输出与输入相同,但实际上是这样的:

{
  "fooList": [
    {
      "id": 0,
      "fooList": [
        {
          "id": 1,
          "fooList": []
        }
      ]
    },
    1
  ]
}

您可以创建一个从
Foo
类扩展而来的类,以使用注释
@JsonGetter
愚者提供可选的序列化,就像包装器一样

Foo
class:

public class Foo implements Serializable {
    private int id;
    private List<Foo> fooList;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Foo> getFooList() {
        return fooList;
    }

    public void setFooList(List<Foo> fooList) {
        this.fooList = fooList;
    }
}
public class Bar implements Serializable {
    public List<FooJsonSimplifiedSerializationWrapper> fooList;

    public List<FooJsonSimplifiedSerializationWrapper> getFooList() {
        return fooList;
    }

    public void setFooList(List<FooJsonSimplifiedSerializationWrapper> fooList) {
        this.fooList = fooList;
    }
}
fooojsonSimplifiedSerializationWrapper
是用于序列化的
Foo
包装器,它有一个从
Lst
转换为列表的方法,您必须在序列化之前的某个时间调用该方法:

public class FooJsonSimplifiedSerializationWrapper extends Foo {
    @JsonGetter("fooList")
    public List<Integer> serializeFooList() {
        return this.getFooList().stream().map(f -> f.getId()).collect(Collectors.toList());
    }

    public static List<FooJsonSimplifiedSerializationWrapper> convertFromFoo(List<Foo> fooList) {
        return fooList.stream().map(f -> {
            FooJsonSimplifiedSerializationWrapper fooSimplified = new FooJsonSimplifiedSerializationWrapper();
            BeanUtils.copyProperties(f, fooSimplified);

            return fooSimplified;

        }).collect(Collectors.toList());
    }
}
此代码将打印:

Foo序列化:{“id”:1,“傻瓜主义者”:[{“id”:2,“傻瓜主义者”:[]},{“id”:3,“傻瓜主义者”:[]}

Bar序列化:{“傻瓜”:[{“id”:1,“傻瓜”:[2,3]}]}


另一个解决方案可能涉及将
视图
@JsonView
注释一起使用,并自定义视图以使序列化适应您的需要,但我认为这是一个更麻烦的解决方案。

您的问题不是很清楚。您可以添加包含Foo对象列表的另一个类(如果存在)的代码吗。在输入数据的情况下,你可以添加你期望的json输出数据吗。在上面。
public class Bar implements Serializable {
    public List<FooJsonSimplifiedSerializationWrapper> fooList;

    public List<FooJsonSimplifiedSerializationWrapper> getFooList() {
        return fooList;
    }

    public void setFooList(List<FooJsonSimplifiedSerializationWrapper> fooList) {
        this.fooList = fooList;
    }
}
public class FooJsonSimplifiedSerializationWrapper extends Foo {
    @JsonGetter("fooList")
    public List<Integer> serializeFooList() {
        return this.getFooList().stream().map(f -> f.getId()).collect(Collectors.toList());
    }

    public static List<FooJsonSimplifiedSerializationWrapper> convertFromFoo(List<Foo> fooList) {
        return fooList.stream().map(f -> {
            FooJsonSimplifiedSerializationWrapper fooSimplified = new FooJsonSimplifiedSerializationWrapper();
            BeanUtils.copyProperties(f, fooSimplified);

            return fooSimplified;

        }).collect(Collectors.toList());
    }
}
public static void main(String[] args) throws IOException {
    Foo foo = new Foo();
    foo.setId(1);

    Foo fooChild = new Foo();
    fooChild.setId(2);
    fooChild.setFooList(new ArrayList<>());

    Foo fooChild2 = new Foo();
    fooChild2.setId(3);
    fooChild2.setFooList(new ArrayList<>());

    foo.setFooList(Arrays.asList(fooChild, fooChild2));

    Bar bar = new Bar();
    bar.setFooList(FooJsonSimplifiedSerializationWrapper.convertFromFoo(Arrays.asList(foo)));

    System.out.println(new ObjectMapper().writeValueAsString(foo));
    System.out.println(new ObjectMapper().writeValueAsString(bar));
}