1 package com.fasterxml.jackson.databind.ser; 2 3 import java.io.IOException; 4 import java.util.*; 5 6 import com.fasterxml.jackson.annotation.JsonPropertyOrder; 7 import com.fasterxml.jackson.databind.*; 8 import com.fasterxml.jackson.databind.annotation.JsonSerialize; 9 10 public class TestJsonSerializeAs extends BaseMapTest 11 { 12 public interface Fooable { getFoo()13 public int getFoo(); 14 } 15 16 // force use of interface 17 @JsonSerialize(as=Fooable.class) 18 public static class FooImpl implements Fooable { 19 @Override getFoo()20 public int getFoo() { return 42; } getBar()21 public int getBar() { return 15; } 22 } 23 24 static class FooImplNoAnno implements Fooable { 25 @Override getFoo()26 public int getFoo() { return 42; } getBar()27 public int getBar() { return 15; } 28 } 29 30 public class Fooables { getFoos()31 public FooImpl[] getFoos() { 32 return new FooImpl[] { new FooImpl() }; 33 } 34 } 35 36 public class FooableWrapper { getFoo()37 public FooImpl getFoo() { 38 return new FooImpl(); 39 } 40 } 41 42 // for [databind#1023] 43 static class FooableWithFieldWrapper { 44 @JsonSerialize(as=Fooable.class) getFoo()45 public Fooable getFoo() { 46 return new FooImplNoAnno(); 47 } 48 } 49 50 interface Bean1178Base { getA()51 public int getA(); 52 } 53 54 @JsonPropertyOrder({"a","b"}) 55 static abstract class Bean1178Abstract implements Bean1178Base { 56 @Override getA()57 public int getA() { return 1; } 58 getB()59 public int getB() { return 2; } 60 } 61 62 static class Bean1178Impl extends Bean1178Abstract { getC()63 public int getC() { return 3; } 64 } 65 66 static class Bean1178Wrapper { 67 @JsonSerialize(contentAs=Bean1178Abstract.class) 68 public List<Bean1178Base> values; Bean1178Wrapper(int count)69 public Bean1178Wrapper(int count) { 70 values = new ArrayList<Bean1178Base>(); 71 for (int i = 0; i < count; ++i) { 72 values.add(new Bean1178Impl()); 73 } 74 } 75 } 76 77 static class Bean1178Holder { 78 @JsonSerialize(as=Bean1178Abstract.class) 79 public Bean1178Base value = new Bean1178Impl(); 80 } 81 82 /* 83 /********************************************************** 84 /* Test methods 85 /********************************************************** 86 */ 87 88 private final ObjectWriter WRITER = objectWriter(); 89 testSerializeAsInClass()90 public void testSerializeAsInClass() throws IOException { 91 assertEquals("{\"foo\":42}", WRITER.writeValueAsString(new FooImpl())); 92 } 93 testSerializeAsForArrayProp()94 public void testSerializeAsForArrayProp() throws IOException { 95 assertEquals("{\"foos\":[{\"foo\":42}]}", 96 WRITER.writeValueAsString(new Fooables())); 97 } 98 testSerializeAsForSimpleProp()99 public void testSerializeAsForSimpleProp() throws IOException { 100 assertEquals("{\"foo\":{\"foo\":42}}", 101 WRITER.writeValueAsString(new FooableWrapper())); 102 } 103 104 // for [databind#1023] testSerializeWithFieldAnno()105 public void testSerializeWithFieldAnno() throws IOException { 106 assertEquals("{\"foo\":{\"foo\":42}}", 107 WRITER.writeValueAsString(new FooableWithFieldWrapper())); 108 } 109 110 // for [databind#1178] testSpecializedContentAs()111 public void testSpecializedContentAs() throws IOException { 112 assertEquals(aposToQuotes("{'values':[{'a':1,'b':2}]}"), 113 WRITER.writeValueAsString(new Bean1178Wrapper(1))); 114 } 115 116 // for [databind#1231] (and continuation of [databind#1178]) testSpecializedAsIntermediate()117 public void testSpecializedAsIntermediate() throws IOException { 118 assertEquals(aposToQuotes("{'value':{'a':1,'b':2}}"), 119 WRITER.writeValueAsString(new Bean1178Holder())); 120 } 121 } 122