SerializationAnnotationsTest.java
package com.fasterxml.jackson.databind.ser;
import java.io.*;
import java.util.*;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import static org.junit.jupiter.api.Assertions.*;
/**
* This unit test suite tests use of some basic Jackson annotations for
* bean serialization.
*/
public class SerializationAnnotationsTest
extends DatabindTestUtil
{
// Class for testing {@link JsonProperty} annotations with getters
final static class SizeClassGetter
{
@JsonProperty public int size() { return 3; }
@JsonProperty("length") public int foobar() { return -17; }
// note: need not be public since there's annotation
@JsonProperty protected int value() { return 0; }
// dummy method; not a getter signature
protected int getNotReally(int arg) { return 0; }
}
// And additional testing to cover [JACKSON-64]
final static class SizeClassGetter2
{
// Should still be considered property "x"
@JsonProperty protected int getX() { return 3; }
}
// and some support for testing [JACKSON-120]
final static class SizeClassGetter3
{
// Should be considered property "y" even tho non-public
@JsonSerialize protected int getY() { return 8; }
}
/**
* Class for testing {@link JsonSerializer} annotation
* for class itself.
*/
@JsonSerialize(using=BogusSerializer.class)
final static class ClassSerializer {
}
/**
* Class for testing an active {@link JsonSerialize#using} annotation
* for a method
*/
final static class ClassMethodSerializer {
private int _x;
public ClassMethodSerializer(int x) { _x = x; }
@JsonSerialize(using=StringSerializer.class)
public int getX() { return _x; }
}
/**
* Class for testing an inactive (one that will not have any effect)
* {@link JsonSerialize} annotation for a method
*/
final static class InactiveClassMethodSerializer {
private int _x;
public InactiveClassMethodSerializer(int x) { _x = x; }
// Basically, has no effect, hence gets serialized as number
@JsonSerialize(using=JsonSerializer.None.class)
public int getX() { return _x; }
}
/**
* Class for verifying that getter information is inherited
* as expected via normal class inheritance
*/
static class BaseBean {
public int getX() { return 1; }
@JsonProperty("y")
private int getY() { return 2; }
}
static class SubClassBean extends BaseBean {
public int getZ() { return 3; }
}
/*
/**********************************************************
/* Other helper classes
/**********************************************************
*/
public final static class BogusSerializer extends JsonSerializer<Object>
{
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
throws IOException
{
jgen.writeBoolean(true);
}
}
private final static class StringSerializer extends JsonSerializer<Object>
{
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
throws IOException
{
jgen.writeString("X"+value+"X");
}
}
/*
/**********************************************************
/* Main tests
/**********************************************************
*/
private final ObjectMapper MAPPER = newJsonMapper();
@Test
public void testSimpleGetter() throws Exception
{
Map<String,Object> result = writeAndMap(MAPPER, new SizeClassGetter());
assertEquals(3, result.size());
assertEquals(Integer.valueOf(3), result.get("size"));
assertEquals(Integer.valueOf(-17), result.get("length"));
assertEquals(Integer.valueOf(0), result.get("value"));
}
@Test
public void testSimpleGetter2() throws Exception
{
Map<String,Object> result = writeAndMap(MAPPER, new SizeClassGetter2());
assertEquals(1, result.size());
assertEquals(Integer.valueOf(3), result.get("x"));
}
// testing [JACKSON-120], implied getter
@Test
public void testSimpleGetter3() throws Exception
{
Map<String,Object> result = writeAndMap(MAPPER, new SizeClassGetter3());
assertEquals(1, result.size());
assertEquals(Integer.valueOf(8), result.get("y"));
}
/**
* Let's also verify that inherited super-class getters are used
* as expected
*/
@Test
public void testGetterInheritance() throws Exception
{
Map<String,Object> result = writeAndMap(MAPPER, new SubClassBean());
assertEquals(3, result.size());
assertEquals(Integer.valueOf(1), result.get("x"));
assertEquals(Integer.valueOf(2), result.get("y"));
assertEquals(Integer.valueOf(3), result.get("z"));
}
/**
* Unit test to verify that {@link JsonSerialize#using} annotation works
* when applied to a class
*/
@Test
public void testClassSerializer() throws Exception
{
StringWriter sw = new StringWriter();
MAPPER.writeValue(sw, new ClassSerializer());
assertEquals("true", sw.toString());
}
/**
* Unit test to verify that @JsonSerializer annotation works
* when applied to a Method
*/
@Test
public void testActiveMethodSerializer() throws Exception
{
StringWriter sw = new StringWriter();
MAPPER.writeValue(sw, new ClassMethodSerializer(13));
/* Here we will get wrapped as an object, since we have
* full object, just override a single property
*/
assertEquals("{\"x\":\"X13X\"}", sw.toString());
}
@Test
public void testInactiveMethodSerializer() throws Exception
{
String json = MAPPER.writeValueAsString(new InactiveClassMethodSerializer(8));
// Here we will get wrapped as an object, since we have
// full object, just override a single property
assertEquals("{\"x\":8}", json);
}
}