使用Jackson将通用java对象序列化为JSON [英] Serializing generic java object to JSON using Jackson

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

问题描述

当我尝试将以下类实例转换为JSON(使用Jackson)时

When i tried to convert the following class instance to JSON (using Jackson)

public class RPCRespond<Result> {

    private int code;
    private Result result;
    private boolean success;
    private String failureReason;

    public RPCRespond() {
        this.code = 0;
        this.success = true;
        this.result = null;
    }

    public RPCRespond(Result result) {
        this.code = 0;
        this.success = true;
        this.result = result;
    }

    public RPCRespond(int code, String failureReason) {
        this.code = code;
        this.success = false;
        this.failureReason = failureReason;
    }

    public RPCRespond(int code, String failureReason, Object... args) {
        this.code = code;
        this.success = false;
        this.failureReason = String.format(failureReason, args);
    }

    public Result getResult() {
        return result;
    }


    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getFailureReason() {
        return failureReason;
    }

    public void setFailureReason(String failureReason) {
        this.failureReason = failureReason;
    }

    public int getCode() {
        return code;
    }

    public boolean getSuccess() {
        return success;
    }

    @Transient
    public String getAsJSON() {

        String json = "";

        ObjectMapper mapper = new ObjectMapper();
        json = mapper.writeValueAsString(this) ; 

        return json ;
    }

}

它进入无限循环:


 at sun.reflect.GeneratedMethodAccessor48.invoke(Unknown Source)    at
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)     at
 org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483)
    at
 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418)
    at
 org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
    at
 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
    at
 org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
    at
 org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
    at
 org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2566)
    at
 org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:2088)

RPCRespond的启动由

The initiation of the RPCRespond is done by

User u = new User() ;
u.setFirstName("aaaa") ;
RPCRespond<User> result = new RPCRespond<User>(u) ;

result.setSuccess(true) ;

return result.getAsJSON() ;

如何将RPCRespond转换为JSON?

How can I convert RPCRespond to JSON?

推荐答案

默认情况下,Jackson会通过 get 方法序列化。一旦它达到 getAsJson 方法, poom ,无限循环。使用 @JsonIgnore 注释标记它。

By default Jackson will serialize via get methods. Once it hits the getAsJson method, poom, infinite loop. Mark it with the @JsonIgnore annotation.

@JsonIgnore
public String getAsJSON() {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(this) ; 
}

您还可以将Jackson配置为仅基于属性进行序列化,这可能会消除需要 @JsonIgnore ,但这可能会或可能不会满足您的需求。

You can also configure Jackson to serialize based on properties only, which may eliminate the need for the @JsonIgnore, but that may or may not meet your needs.

相信最新的Jackson允许选择使用Hibernate @Transient 注释,虽然我不确定如何配置它(最近的更改)。

I believe the latest Jackson allows the choice of using the Hibernate @Transient annotation, although I'm not sure how to configure it (recent change).

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

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