Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing generic java object to JSON using Jackson

Tags:

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?

like image 919
oferbu Avatar asked Nov 07 '11 15:11

oferbu


People also ask

How do you serialize an object to JSON Jackson in Java?

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.

Does Jackson use Java serialization?

Note that Jackson does not use java.

What is Jackson object serialization?

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.


1 Answers

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).

like image 165
Dave Newton Avatar answered Oct 25 '22 14:10

Dave Newton