1 package com.fasterxml.jackson.databind.ser; 2 3 import com.fasterxml.jackson.annotation.*; 4 5 import com.fasterxml.jackson.databind.*; 6 7 /** 8 * Unit tests for verifying that constraints on ordering of serialized 9 * properties are held. 10 */ 11 public class SerializationOrderTest 12 extends BaseMapTest 13 { 14 static class BeanWithCreator 15 { 16 public int a; 17 public int b; 18 public int c; 19 BeanWithCreator(@sonProperty"c") int c, @JsonProperty("a") int a)20 @JsonCreator public BeanWithCreator(@JsonProperty("c") int c, @JsonProperty("a") int a) { 21 this.a = a; 22 this.c = c; 23 } 24 } 25 26 @JsonPropertyOrder({"c", "a", "b"}) 27 static class BeanWithOrder 28 { 29 public int d, b, a, c; 30 BeanWithOrder(int a, int b, int c, int d)31 public BeanWithOrder(int a, int b, int c, int d) { 32 this.a = a; 33 this.b = b; 34 this.c = c; 35 this.d = d; 36 } 37 } 38 39 @JsonPropertyOrder(value={"d"}, alphabetic=true) 40 static class SubBeanWithOrder extends BeanWithOrder 41 { SubBeanWithOrder(int a, int b, int c, int d)42 public SubBeanWithOrder(int a, int b, int c, int d) { 43 super(a, b, c, d); 44 } 45 } 46 47 @JsonPropertyOrder({"b", "a", 48 // note: including non-existant properties is fine (has no effect, but not an error) 49 "foobar", 50 "c" 51 }) 52 static class OrderMixIn { } 53 54 @JsonPropertyOrder(value={"a","b","x","z"}) 55 static class BeanFor268 { 56 @JsonProperty("a") public String xA = "a"; 57 @JsonProperty("z") public String aZ = "z"; xB()58 @JsonProperty("b") public String xB() { return "b"; } aX()59 @JsonProperty("x") public String aX() { return "x"; } 60 } 61 62 static class BeanFor459 { 63 public int d = 4; 64 public int c = 3; 65 public int b = 2; 66 public int a = 1; 67 } 68 69 // For [databind#311] 70 @JsonPropertyOrder(alphabetic = true) 71 static class BeanForGH311 { 72 private final int a; 73 private final int b; 74 75 @JsonCreator BeanForGH311(@sonProperty"b") int b, @JsonProperty("a") int a)76 public BeanForGH311(@JsonProperty("b") int b, @JsonProperty("a") int a) { //b and a are out of order, although alphabetic = true 77 this.a = a; 78 this.b = b; 79 } 80 getA()81 public int getA() { return a; } getB()82 public int getB() { return b; } 83 } 84 85 // We'll expect ordering of "FUBAR" 86 @JsonPropertyOrder({ "f" }) 87 static class OrderingByIndexBean { 88 public int r; 89 public int a; 90 91 @JsonProperty(index = 1) 92 public int b; 93 94 @JsonProperty(index = 0) 95 public int u; 96 97 public int f; 98 } 99 100 /* 101 /********************************************* 102 /* Unit tests 103 /********************************************* 104 */ 105 106 private final ObjectMapper MAPPER = newJsonMapper(); 107 108 private final ObjectMapper ALPHA_MAPPER = jsonMapperBuilder() 109 .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) 110 .build(); 111 testImplicitOrderByCreator()112 public void testImplicitOrderByCreator() throws Exception { 113 assertEquals("{\"c\":1,\"a\":2,\"b\":0}", 114 MAPPER.writeValueAsString(new BeanWithCreator(1, 2))); 115 } 116 testExplicitOrder()117 public void testExplicitOrder() throws Exception { 118 assertEquals("{\"c\":3,\"a\":1,\"b\":2,\"d\":4}", 119 MAPPER.writeValueAsString(new BeanWithOrder(1, 2, 3, 4))); 120 } 121 testAlphabeticOrder()122 public void testAlphabeticOrder() throws Exception { 123 assertEquals("{\"d\":4,\"a\":1,\"b\":2,\"c\":3}", 124 MAPPER.writeValueAsString(new SubBeanWithOrder(1, 2, 3, 4))); 125 } 126 testOrderWithMixins()127 public void testOrderWithMixins() throws Exception 128 { 129 ObjectMapper m = jsonMapperBuilder() 130 .addMixIn(BeanWithOrder.class, OrderMixIn.class) 131 .build(); 132 assertEquals("{\"b\":2,\"a\":1,\"c\":3,\"d\":4}", 133 serializeAsString(m, new BeanWithOrder(1, 2, 3, 4))); 134 } 135 testOrderWrt268()136 public void testOrderWrt268() throws Exception 137 { 138 assertEquals("{\"a\":\"a\",\"b\":\"b\",\"x\":\"x\",\"z\":\"z\"}", 139 MAPPER.writeValueAsString(new BeanFor268())); 140 } 141 testOrderWithFeature()142 public void testOrderWithFeature() throws Exception 143 { 144 assertEquals("{\"a\":1,\"b\":2,\"c\":3,\"d\":4}", 145 ALPHA_MAPPER.writeValueAsString(new BeanFor459())); 146 } 147 148 // [databind#311] testAlphaAndCreatorOrdering()149 public void testAlphaAndCreatorOrdering() throws Exception 150 { 151 String json = ALPHA_MAPPER.writeValueAsString(new BeanForGH311(2, 1)); 152 assertEquals("{\"a\":1,\"b\":2}", json); 153 } 154 155 // [databind#2555] testOrderByIndexEtc()156 public void testOrderByIndexEtc() throws Exception 157 { 158 // since "default" order can actually vary with later JDKs, only verify 159 // case of alphabetic-as-default 160 assertEquals(aposToQuotes("{'f':0,'u':0,'b':0,'a':0,'r':0}"), 161 ALPHA_MAPPER.writeValueAsString(new OrderingByIndexBean())); 162 } 163 } 164