1 package com.fasterxml.jackson.databind.struct; 2 3 import com.fasterxml.jackson.annotation.*; 4 import com.fasterxml.jackson.databind.BaseMapTest; 5 import com.fasterxml.jackson.databind.ObjectMapper; 6 7 public class TestPOJOAsArrayAdvanced extends BaseMapTest 8 { 9 @JsonFormat(shape=JsonFormat.Shape.ARRAY) 10 @JsonPropertyOrder(alphabetic=true) 11 static class CreatorAsArray 12 { 13 protected int x, y; 14 public int a, b; 15 16 @JsonCreator CreatorAsArray(@sonProperty"x") int x, @JsonProperty("y") int y)17 public CreatorAsArray(@JsonProperty("x") int x, @JsonProperty("y") int y) 18 { 19 this.x = x; 20 this.y = y; 21 } 22 getX()23 public int getX() { return x; } getY()24 public int getY() { return y; } 25 } 26 27 @JsonFormat(shape=JsonFormat.Shape.ARRAY) 28 @JsonPropertyOrder({"a","b","x","y"}) 29 static class CreatorAsArrayShuffled 30 { 31 protected int x, y; 32 public int a, b; 33 34 @JsonCreator CreatorAsArrayShuffled(@sonProperty"x") int x, @JsonProperty("y") int y)35 public CreatorAsArrayShuffled(@JsonProperty("x") int x, @JsonProperty("y") int y) 36 { 37 this.x = x; 38 this.y = y; 39 } 40 getX()41 public int getX() { return x; } getY()42 public int getY() { return y; } 43 } 44 45 static class ViewA { } 46 static class ViewB { } 47 48 @JsonFormat(shape=JsonFormat.Shape.ARRAY) 49 @JsonPropertyOrder(alphabetic=true) 50 static class AsArrayWithView 51 { 52 @JsonView(ViewA.class) 53 public int a; 54 @JsonView(ViewB.class) 55 public int b; 56 public int c; 57 } 58 59 @JsonFormat(shape=JsonFormat.Shape.ARRAY) 60 @JsonPropertyOrder(alphabetic=true) 61 static class AsArrayWithViewAndCreator 62 { 63 @JsonView(ViewA.class) 64 public int a; 65 @JsonView(ViewB.class) 66 public int b; 67 public int c; 68 69 @JsonCreator AsArrayWithViewAndCreator(@sonProperty"a") int a, @JsonProperty("b") int b, @JsonProperty("c") int c)70 public AsArrayWithViewAndCreator(@JsonProperty("a") int a, 71 @JsonProperty("b") int b, 72 @JsonProperty("c") int c) 73 { 74 this.a = a; 75 this.b = b; 76 this.c = c; 77 } 78 } 79 80 /* 81 /***************************************************** 82 /* Basic tests 83 /***************************************************** 84 */ 85 86 private final static ObjectMapper MAPPER = new ObjectMapper(); 87 testWithView()88 public void testWithView() throws Exception 89 { 90 // Ok, first, ensure that serializer will "black out" filtered properties 91 AsArrayWithView input = new AsArrayWithView(); 92 input.a = 1; 93 input.b = 2; 94 input.c = 3; 95 String json = MAPPER.writerWithView(ViewA.class).writeValueAsString(input); 96 assertEquals("[1,null,3]", json); 97 98 // and then that conversely deserializer does something similar 99 AsArrayWithView result = MAPPER.readerFor(AsArrayWithView.class).withView(ViewB.class) 100 .readValue("[1,2,3]"); 101 // should include 'c' (not view-able) and 'b' (include in ViewB) but not 'a' 102 assertEquals(3, result.c); 103 assertEquals(2, result.b); 104 assertEquals(0, result.a); 105 } 106 testWithViewAndCreator()107 public void testWithViewAndCreator() throws Exception 108 { 109 AsArrayWithViewAndCreator result = MAPPER.readerFor(AsArrayWithViewAndCreator.class) 110 .withView(ViewB.class) 111 .readValue("[1,2,3]"); 112 // should include 'c' (not view-able) and 'b' (include in ViewB) but not 'a' 113 assertEquals(3, result.c); 114 assertEquals(2, result.b); 115 assertEquals(0, result.a); 116 } 117 testWithCreatorsOrdered()118 public void testWithCreatorsOrdered() throws Exception 119 { 120 CreatorAsArray input = new CreatorAsArray(3, 4); 121 input.a = 1; 122 input.b = 2; 123 124 // note: Creator properties get sorted ahead of others, hence not [1,2,3,4] but: 125 String json = MAPPER.writeValueAsString(input); 126 assertEquals("[3,4,1,2]", json); 127 128 // and should get back in proper order, too 129 CreatorAsArray output = MAPPER.readValue(json, CreatorAsArray.class); 130 assertEquals(1, output.a); 131 assertEquals(2, output.b); 132 assertEquals(3, output.x); 133 assertEquals(4, output.y); 134 } 135 136 // Same as above, but ordering of properties different... testWithCreatorsShuffled()137 public void testWithCreatorsShuffled() throws Exception 138 { 139 CreatorAsArrayShuffled input = new CreatorAsArrayShuffled(3, 4); 140 input.a = 1; 141 input.b = 2; 142 143 // note: explicit ordering overrides implicit creators-first ordering: 144 String json = MAPPER.writeValueAsString(input); 145 assertEquals("[1,2,3,4]", json); 146 147 // and should get back in proper order, too 148 CreatorAsArrayShuffled output = MAPPER.readValue(json, CreatorAsArrayShuffled.class); 149 assertEquals(1, output.a); 150 assertEquals(2, output.b); 151 assertEquals(3, output.x); 152 assertEquals(4, output.y); 153 } 154 } 155