0

I have a simple class for giving out count and db cursor information.

public class Cursor
{
    public String cursor = null;
    public int count = -1;
}

I do not wish to send count and cursor (while serialization) if they are null or -1 which are set by default.

Researching, I figured out that I should be using a view but I cannot figure out how to use it and the view implementation to avoid the default values.

Deserialization is not required for this class.

Thanks.

cloudpre
  • 1,001
  • 2
  • 15
  • 28

2 Answers2

0

If you use Jackson you can give

@JsonInclude(Include.NON_NULL) 
public class Cursor
{
     public String cursor = null;
     public Integer count = null;
}

I have changed the int to Integer when you give -1 there a value for that field.
You can check this post also.

Community
  • 1
  • 1
basiljames
  • 4,777
  • 4
  • 24
  • 41
  • Thanks. But jsonInclude works on 2.x but I am using 1.7. I will post another answer for 2.x- users. – cloudpre Sep 26 '12 at 03:39
0

If you are using 2.x, the above answer is the best. Use JSONInclude annotation.

However if you are using lesser versions, I do not believe there is any annotations for that. We have to customize the ObjectMapper.

public ObjectMapperProvider()
    {
    mapper = new ObjectMapper();

    mapper.configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES,
        false);
    mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES,
        false);

    }

In Web.xml

  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
         <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>com.your.package; org.codehaus.jackson.jaxrs</param-value>
            </init-param>
         <load-on-startup>1</load-on-startup>
      </servlet>

com.your.package should contain your new custom objectmapper class.

cloudpre
  • 1,001
  • 2
  • 15
  • 28