title: Serializing generic java object to JSON using Jackson
body: <p>When i tried to convert the following class instance to JSON (using Jackson) </p>
  
  <pre><code>public class RPCRespond&lt;Result&gt; {
  
      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 ;
      }
  
  }
  </code></pre>
  
  <p>it get into infinite loop of: </p>
  
  <pre>
   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)
  </pre>
  
  <p>The initiation of the RPCRespond is done by</p>
  
  <pre><code>User u = new User() ;
  u.setFirstName("aaaa") ;
  RPCRespond&lt;User&gt; result = new RPCRespond&lt;User&gt;(u) ;
  
  result.setSuccess(true) ;
  
  return result.getAsJSON() ;
  </code></pre>
  
  <p>How can I convert RPCRespond to JSON? </p>