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