Skip to content

Instantly share code, notes, and snippets.

@nzroller
Created June 16, 2015 19:51
Show Gist options
  • Save nzroller/5c500b1f1515786c3e58 to your computer and use it in GitHub Desktop.
Save nzroller/5c500b1f1515786c3e58 to your computer and use it in GitHub Desktop.
Include.NON_EMPTY default and afterburner serialization test
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
public class PrimitiveTest {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public static class NonEmptyIntWrapper {
private int value;
public NonEmptyIntWrapper(int v) {
value = v;
}
public int getValue() {
return value;
}
}
// [Issue#39]
@Test
public void testEmptyExclusion() throws Exception {
ObjectMapper mapper = mapperWithModule();
String json;
json = mapper.writeValueAsString(new NonEmptyIntWrapper(3));
assertEquals("{\"value\":3}", json);
json = mapper.writeValueAsString(new NonEmptyIntWrapper(0));
assertEquals("{}", json);
mapper = new ObjectMapper();
json = mapper.writeValueAsString(new NonEmptyIntWrapper(3));
assertEquals("{\"value\":3}", json);
json = mapper.writeValueAsString(new NonEmptyIntWrapper(0));
assertEquals("{}", json);
}
private ObjectMapper mapperWithModule() {
return new ObjectMapper().registerModule(new AfterburnerModule());
}
}
@nzroller
Copy link
Author

Returns org.junit.ComparisonFailure: expected:<{[]}> but was:<{["value":0]}> for the non-afterburner version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment