|
| 1 | +package com.fasterxml.jackson.databind.views; |
| 2 | + |
| 3 | +import static com.fasterxml.jackson.databind.BaseMapTest.newJsonMapper; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import com.fasterxml.jackson.annotation.*; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +public class ViewsWithCreatorTest |
| 11 | +{ |
| 12 | + |
| 13 | + public static class View { } |
| 14 | + public static class View1 extends View { } |
| 15 | + public static class View2 extends View { } |
| 16 | + |
| 17 | + @JsonAutoDetect( |
| 18 | + fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY, |
| 19 | + isGetterVisibility = JsonAutoDetect.Visibility.NONE, |
| 20 | + getterVisibility = JsonAutoDetect.Visibility.NONE) |
| 21 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 22 | + static class ObjWithCreator { |
| 23 | + |
| 24 | + public String a; |
| 25 | + |
| 26 | + @JsonView(View1.class) |
| 27 | + public String b; |
| 28 | + |
| 29 | + @JsonView(View2.class) |
| 30 | + public String c; |
| 31 | + |
| 32 | + public ObjWithCreator() { } |
| 33 | + |
| 34 | + @JsonCreator |
| 35 | + public ObjWithCreator(@JsonProperty("a") String a, @JsonProperty("b") String b, @JsonProperty("c") String c) { |
| 36 | + this.a = a; |
| 37 | + this.b = b; |
| 38 | + this.c = c; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public String toString() { |
| 43 | + return String.format("%s-%s-%s", a, b, c); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY, |
| 48 | + isGetterVisibility = JsonAutoDetect.Visibility.NONE, |
| 49 | + getterVisibility = JsonAutoDetect.Visibility.NONE) |
| 50 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 51 | + static class ObjWithoutCreator { |
| 52 | + |
| 53 | + public String a; |
| 54 | + |
| 55 | + @JsonView(View1.class) |
| 56 | + public String b; |
| 57 | + |
| 58 | + @JsonView(View2.class) |
| 59 | + public String c; |
| 60 | + |
| 61 | + public ObjWithoutCreator() { } |
| 62 | + |
| 63 | + public ObjWithoutCreator(String a, String b, String c) { |
| 64 | + this.a = a; |
| 65 | + this.b = b; |
| 66 | + this.c = c; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String toString() { |
| 71 | + return String.format("%s-%s-%s", a, b, c); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testWithJsonCreator() throws Exception { |
| 77 | + ObjectMapper mapper = newJsonMapper(); |
| 78 | + ObjectReader reader = mapper.readerFor(ObjWithCreator.class).withView(View1.class); |
| 79 | + |
| 80 | + // serialize first, |
| 81 | + String JSON = mapper.writeValueAsString(new ObjWithCreator("a", "b", "c")); |
| 82 | + // then back |
| 83 | + assertEquals("a-b-null", reader.readValue(JSON).toString()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testWithoutJsonCreator() throws Exception { |
| 88 | + ObjectMapper mapper = newJsonMapper(); |
| 89 | + ObjectReader reader = mapper.readerFor(ObjWithoutCreator.class).withView(View1.class); |
| 90 | + |
| 91 | + // serialize first, |
| 92 | + String JSON = mapper.writeValueAsString(new ObjWithoutCreator("a", "b", "c")); |
| 93 | + // then back |
| 94 | + assertEquals("a-b-null", reader.readValue(JSON).toString()); |
| 95 | + } |
| 96 | +} |
0 commit comments