Skip to content

Instantly share code, notes, and snippets.

@brianv0
Created May 22, 2014 17:41
Show Gist options
  • Save brianv0/5ac674f36afd04610a17 to your computer and use it in GitHub Desktop.
Save brianv0/5ac674f36afd04610a17 to your computer and use it in GitHub Desktop.
package jackson.test;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
/**
* @author brianv0
*/
public class JsonValueTestCase extends TestCase {
// The following is required for the testDecimalMetadata test case. That case fails.
@JsonTypeName(value = "decimalValue")
public static class DecimalValue {
private java.math.BigDecimal value;
public DecimalValue(){ this.value = java.math.BigDecimal.valueOf( 1234.4321 ); }
@JsonValue
public java.math.BigDecimal getValue(){ return value; }
}
@JsonPropertyOrder({"key","value"})
public static class DecimalEntry {
public DecimalEntry(){}
public String getKey(){ return "num"; }
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXTERNAL_PROPERTY)
public DecimalValue getValue(){
return new DecimalValue();
}
}
public static class DecimalMetadata {
@JsonProperty("metadata")
public List<DecimalEntry> getMetadata() {
return new ArrayList<DecimalEntry>() { {add(new DecimalEntry());} };
}
}
// The following succeeds. It's included for comparison
@JsonTypeName(value = "doubleValue")
public static class DoubleValue {
private Double value;
public DoubleValue(){ this.value = 1234.4321; }
@JsonValue
public Double getValue(){ return value; }
}
@JsonPropertyOrder({"key","value"})
public static class DoubleEntry {
public DoubleEntry(){}
public String getKey(){ return "num"; }
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXTERNAL_PROPERTY)
public DoubleValue getValue(){ return new DoubleValue(); }
}
public static class DoubleMetadata {
@JsonProperty("metadata")
public List<DoubleEntry> getMetadata() {
return new ArrayList<DoubleEntry>() { {add(new DoubleEntry());} };
}
}
public void testDoubleMetadata() throws IOException{
DoubleMetadata doub = new DoubleMetadata();
String expected = "{\"metadata\":[{\"key\":\"num\",\"value\":1234.4321,\"@type\":\"doubleValue\"}]}";
AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector( primary );
StringWriter writer = new StringWriter();
mapper.writeValue( writer, doub);
String json = writer.toString();
assertEquals("Serialized json not equivalent", expected, json);
}
public void testDecimalMetadata() throws IOException{
DecimalMetadata dec = new DecimalMetadata();
String expected = "{\"metadata\":[{\"key\":\"num\",\"value\":1234.4321,\"@type\":\"decimalValue\"}]}";
AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector( primary );
StringWriter writer = new StringWriter();
mapper.writeValue( writer, dec);
String json = writer.toString();
assertEquals("Serialized json not equivalent", expected, json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment