1 package com.fasterxml.jackson.databind.objectid; 2 3 import java.io.IOException; 4 import java.util.*; 5 6 import com.fasterxml.jackson.annotation.*; 7 import com.fasterxml.jackson.databind.*; 8 9 public class ObjectId687Test extends BaseMapTest 10 { 11 // for [databind#687] 12 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="label") 13 static class ReferredWithCreator { 14 public String label; 15 16 @JsonCreator ReferredWithCreator(@sonProperty"label")String label)17 ReferredWithCreator(@JsonProperty("label")String label) { 18 this.label = label; 19 } 20 } 21 22 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="label") 23 static class ReferringToObjWithCreator { 24 public String label = "test1"; 25 26 public List<ReferredWithCreator> refs = new ArrayList<ReferredWithCreator>(); addRef(ReferredWithCreator r)27 public void addRef(ReferredWithCreator r) { 28 refs.add(r); 29 } 30 } 31 32 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="label") 33 static class EnclosingForRefsWithCreator { 34 public String label = "enclosing1"; 35 public ReferredWithCreator baseRef; 36 public ReferringToObjWithCreator nextRef; 37 } 38 39 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="label") 40 static class ReferredWithNoCreator { 41 public String label = "label2"; 42 } 43 44 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="label") 45 static class ReferringToObjWithNoCreator { 46 public String label = "test2"; 47 public List<ReferredWithNoCreator> refs = new ArrayList<ReferredWithNoCreator>(); addRef(ReferredWithNoCreator r)48 public void addRef(ReferredWithNoCreator r) { 49 refs.add(r); 50 } 51 } 52 53 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="label") 54 static class EnclosingForRefWithNoCreator { 55 public String label = "enclosing2"; 56 public ReferredWithNoCreator baseRef; 57 public ReferringToObjWithNoCreator nextRef; 58 } 59 60 /* 61 /***************************************************** 62 /* Test methods 63 /***************************************************** 64 */ 65 66 private final ObjectMapper MAPPER = newJsonMapper(); 67 68 // for [databind#687] testSerializeDeserializeWithCreator()69 public void testSerializeDeserializeWithCreator() throws IOException { 70 ReferredWithCreator base = new ReferredWithCreator("label1"); 71 ReferringToObjWithCreator r = new ReferringToObjWithCreator(); 72 r.addRef(base); 73 EnclosingForRefsWithCreator e = new EnclosingForRefsWithCreator(); 74 e.baseRef = base; 75 e.nextRef = r; 76 77 String json = MAPPER.writeValueAsString(e); 78 79 EnclosingForRefsWithCreator result = MAPPER.readValue(json, 80 EnclosingForRefsWithCreator.class); 81 assertNotNull(result); 82 assertEquals(result.label, e.label); 83 84 // also, compare by re-serializing: 85 assertEquals(json, MAPPER.writeValueAsString(result)); 86 } 87 testSerializeDeserializeNoCreator()88 public void testSerializeDeserializeNoCreator() throws IOException { 89 ReferredWithNoCreator base = new ReferredWithNoCreator(); 90 ReferringToObjWithNoCreator r = new ReferringToObjWithNoCreator(); 91 r.addRef(base); 92 EnclosingForRefWithNoCreator e = new EnclosingForRefWithNoCreator(); 93 e.baseRef = base; 94 e.nextRef = r; 95 96 String json = MAPPER.writeValueAsString(e); 97 98 EnclosingForRefWithNoCreator result = MAPPER.readValue(json, 99 EnclosingForRefWithNoCreator.class); 100 assertNotNull(result); 101 assertEquals(result.label, e.label); 102 103 // also, compare by re-serializing: 104 assertEquals(json, MAPPER.writeValueAsString(result)); 105 } 106 } 107