1 package com.fasterxml.jackson.databind.struct; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.annotation.*; 6 7 import com.fasterxml.jackson.databind.*; 8 9 public class PojoAsArray646Test extends BaseMapTest 10 { 11 @JsonFormat(shape = JsonFormat.Shape.ARRAY) 12 @JsonPropertyOrder(alphabetic = true) 13 static class Outer { 14 protected Map<String, TheItem> attributes; 15 Outer()16 public Outer() { 17 attributes = new HashMap<String, TheItem>(); 18 } 19 20 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_ARRAY) getAttributes()21 public Map<String, TheItem> getAttributes() { 22 return attributes; 23 } 24 } 25 26 @JsonFormat(shape = JsonFormat.Shape.ARRAY) 27 @JsonPropertyOrder(alphabetic = true) 28 static class TheItem { 29 30 @JsonFormat(shape = JsonFormat.Shape.ARRAY) 31 @JsonPropertyOrder(alphabetic = true) 32 public static class NestedItem { 33 public String nestedStrValue; 34 35 @JsonCreator NestedItem(@sonProperty"nestedStrValue") String nestedStrValue)36 public NestedItem(@JsonProperty("nestedStrValue") String nestedStrValue) { 37 this.nestedStrValue = nestedStrValue; 38 } 39 } 40 41 private String strValue; 42 private boolean boolValue; 43 private List<NestedItem> nestedItems; 44 45 @JsonCreator TheItem(@sonProperty"strValue") String strValue, @JsonProperty("boolValue") boolean boolValue, @JsonProperty("nestedItems") List<NestedItem> nestedItems)46 public TheItem(@JsonProperty("strValue") String strValue, @JsonProperty("boolValue") boolean boolValue, @JsonProperty("nestedItems") List<NestedItem> nestedItems) { 47 this.strValue = strValue; 48 this.boolValue = boolValue; 49 this.nestedItems = nestedItems; 50 } 51 getStrValue()52 public String getStrValue() { 53 return strValue; 54 } 55 setStrValue(String strValue)56 public void setStrValue(String strValue) { 57 this.strValue = strValue; 58 } 59 isBoolValue()60 public boolean isBoolValue() { 61 return boolValue; 62 } 63 setBoolValue(boolean boolValue)64 public void setBoolValue(boolean boolValue) { 65 this.boolValue = boolValue; 66 } 67 getNestedItems()68 public List<NestedItem> getNestedItems() { 69 return nestedItems; 70 } 71 setNestedItems(List<NestedItem> nestedItems)72 public void setNestedItems(List<NestedItem> nestedItems) { 73 this.nestedItems = nestedItems; 74 } 75 } 76 77 /* 78 /********************************************************** 79 /* Tests 80 /********************************************************** 81 */ 82 83 private final ObjectMapper MAPPER = new ObjectMapper(); 84 testWithCustomTypeId()85 public void testWithCustomTypeId() throws Exception { 86 87 List<TheItem.NestedItem> nestedList = new ArrayList<TheItem.NestedItem>(); 88 nestedList.add(new TheItem.NestedItem("foo1")); 89 nestedList.add(new TheItem.NestedItem("foo2")); 90 TheItem item = new TheItem("first", false, nestedList); 91 Outer outer = new Outer(); 92 outer.getAttributes().put("entry1", item); 93 94 String json = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(outer); 95 96 Outer result = MAPPER.readValue(json, Outer.class); 97 assertNotNull(result); 98 assertNotNull(result.attributes); 99 assertEquals(1, result.attributes.size()); 100 } 101 } 102