如何将ObjectId序列化为JSON? [英] How to serialize ObjectId to JSON?

查看:308
本文介绍了如何将ObjectId序列化为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Product 类的ObjectId序列化为JSON。我得到了以下JSON:

I want to serialize ObjectId of my Product class to JSON. I got the following JSON:

[{"name":"Play for Java: Covers Play 2","type":"Book","company":"Manning Publications","price":30.0,"imagePath":"public/images/play-for-java.png","rating":4.5,"category":"Computer","author":"Nicolas Leroux","publicationDate":1396224000000,"numPage":320,"_id":539da7a6370882f10d5c2777}]

你可以注意到_ id没有正确序列化,它应该是539da7a6370882f10d5c2777(带双引号)而不只是 539da7a6370882f10d5c2777

You can notice that the "_id" didn't be properly serialized, it should be "539da7a6370882f10d5c2777" (with double quotes) and not just 539da7a6370882f10d5c2777.

因此,我试图实现我自己的 ObjectIdSerializer ,如下所示:

Therefore, I have tried to implement my own ObjectIdSerializer as following:

import java.io.IOException;

import org.bson.types.ObjectId;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class ObjectIdSerializer extends JsonSerializer<ObjectId> {
    @Override
    public void serialize(ObjectId value, JsonGenerator jsonGen,SerializerProvider provider) throws IOException,
            JsonProcessingException {
        jsonGen.writeString(value.toString());
    }
}

它给了我不同的错误: java.lang.String不能强制转换为org.bson.types.ObjectId(通过引用链:models.Book [_ id])

It gave me the different error: java.lang.String cannot be cast to org.bson.types.ObjectId (through reference chain: models.Book["_id"])

以下是我的产品类和预订类:

Product.java

@JsonTypeInfo(use= JsonTypeInfo.Id.CLASS,property="_class")
public class Product {
    @ObjectId @Id
    @JsonSerialize(using = ObjectIdSerializer.class)
    protected String id;
    @JsonProperty("name")
    protected String name;
    @JsonProperty("type")
    protected String type;
    @JsonProperty("description")
    protected String description;
    @JsonProperty("company")
    protected String company;
    @JsonProperty("price")
    protected float price;
    @JsonProperty("imagePath")
    protected String imagePath;
    @JsonProperty("imageName")
    protected String imageName;
    @JsonProperty("rating")
    protected float rating;

    public Product() {
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    // Getters and setters
   ...    

Book.java

public class Book extends Product{

    @JsonProperty("category")
    private String category;
    @JsonProperty("author")
    private String author;
    @JsonProperty("publicationDate")
    private Date publicationDate;
    @JsonProperty("numPage")
    private int numPage;

    public Book() {
    }
    // Getters and setters   
   ...

你能帮我解决一下如何正确地将 ObjectId 序列化为JSON?

Can you help me figure it out how can I properly serialize the ObjectId to JSON?

推荐答案

看起来杰克逊已经被定制为以特殊方式序列化字符串id字段。这可能是与 org.bson 库集成的一部分。

It looks like Jackson has been customized to serialize the string id field in a special way. That is probably a part of the integration with org.bson library.

问题是你的反序列化器是由 ObjectId 类型而不是 String <参数化的/ code>或plain Object 。尝试按如下方式更改它,并从字段声明中删除 @ObjectId 注释。下面是一个示例:

The problem is that your deserializer is parametrized by the ObjectId type instead of String or plain Object. Try to change it as follows and also remove the @ObjectId annotation from the field declaration. Here is an example:

public class ObjectIdSerializer extends JsonSerializer<Object> {
    @Override
    public void serialize(Object value, JsonGenerator jsonGen,SerializerProvider provider) throws IOException {
        jsonGen.writeString(value.toString());
    }
}

您也可以考虑采用 Jackson-Jongo提供程序类修复所有类的对象id序列化。

You may also consider adopting the Jackson-Jongo provider class to fix the object id serialization for all the classes.

这篇关于如何将ObjectId序列化为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆