【问题标题】:Jackson JSON serializer returns extra collection nameJackson JSON 序列化程序返回额外的集合名称
【发布时间】:2016-06-24 18:32:53
【问题描述】:

我有一个作为杰克逊列表返回的东西的列表。我想要的是这样的:

"things": { [
        {
          "id": "1234",

..学生名单

但目前,我得到了这个:

"things": {
      "HashSet": [
        {
          "id": "1234",

我正在使用 JsonSerializer>,这就是它添加 HashSet 字段的原因。我尝试在该字段上添加一个 json 属性,但由于它是一个局部变量,因此不允许这样做。

我目前正在使用 jackson 库并已调查:

Jackson annotation - How to rename element names?

How to rename root key in JSON serialization with Jackson

但他们似乎有一个完全不同的问题。有任何想法吗?谢谢!

编辑:

添加了类实现。请注意,我称其为所有者,其中包含事物。此外,我的 jpa 注释也在那里。谢谢。

@Entity @Table(name = "owner") 
public class Owner extends BaseEntity implements Persistence {

@Column 
private String name;

@Column 
private String description;

@Column 
private Integer capacity;

@Column
@JsonSerialize(using = ThingSerializer.class)
@JsonProperty("things")
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "owners")
private Set<Thing> tihngs = new HashSet<>();


public class ThingSerializer extends JsonSerializer<Set<Thing>> {

@Override public void serialize(Set<Thing> thingSet, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws
                                                                                                                           IOException,
                                                                                                                           JsonProcessingException {

    Set<Thing> things = new HashSet<>();

    thingSet.forEach(thing -> {

        Thing t = new Thing();

        t.setCreatedBy(thing.getCreatedBy());
        t.setCreationDate(thing.getCreationDate());
        t.setId(thing.getId());
        t.setDateModified(thing.getDateModified());
        t.setModifiedBy(thing.getModifiedBy());
        t.setStatus(thing.getStatus());

        things.add(s);
    });

    jsonGenerator.writeObject(things);

}

}

事物实体

@Entity
@Table(name = "thing")
public class Thing extends BaseEntity implements Persistence {

private static final long serialVersionUID = 721739537425505668L;

private String createdBy;
private Date creationDate;
.
.
.




@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "ThingOwner", joinColumns = @JoinColumn(name = "thing_id") , inverseJoinColumns = @JoinColumn(name = "owner_id") )
private Set<Owner> owners = new HashSet<>();

【问题讨论】:

  • 你好,你能补充一些细节,比如学生类,序列化代码吗?

标签: java json jackson


【解决方案1】:

你为什么不使用 ObjectMapper 来序列化和反序列化你的数据?

看看这个小测试,我相信它可以满足您的需求:

@Test
public void myTest() throws Exception{
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(
            mapper.getSerializationConfig().
            getDefaultVisibilityChecker().
            withFieldVisibility(JsonAutoDetect.Visibility.ANY).
            withGetterVisibility(JsonAutoDetect.Visibility.NONE).
            withSetterVisibility(JsonAutoDetect.Visibility.NONE).
            withCreatorVisibility(JsonAutoDetect.Visibility.NONE).
            withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

    mapper.setSerializationInclusion(Include.NON_NULL);

    TestObject value = new TestObject();
    value.a = "TestAvalue";
    value.set = new HashSet<>();
    value.set.add(new SetValueObject(1, 1));
    value.set.add(new SetValueObject(2, 2));
    String test = mapper.writeValueAsString(value);
    System.out.println(test);
}


public class TestObject{
    String a;
    Set<SetValueObject> set;
}

public class SetValueObject{

    public SetValueObject(int a, int b){
        this.a = a;
        this.b = b;
    }


    int a;
    int b;
}

输出:

{"a":TestAvalue","set":[{"a":1,"b":1},{"a":2,"b":2}]}

用 Jackson 2.6.1 测试

而且我已经修改了我的一个测试,所以我不确定您是否需要所有这些 ObjectMapper 配置 => 这只是为了让您了解使用 ObjectMapper 的另一种方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2019-06-27
    • 1970-01-01
    • 2014-11-17
    • 2017-01-02
    • 1970-01-01
    相关资源
    最近更新 更多