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 ; } }
it get into infinite loop of:
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)
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() ;
How can I convert RPCRespond to JSON?
Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.
Note that Jackson does not use java.
Jackson is a solid and mature JSON serialization/deserialization library for Java. The ObjectMapper API provides a straightforward way to parse and generate JSON response objects with a lot of flexibility. This article discussed the main features that make the library so popular.
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) ; }
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With