• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.objectid;
2 
3 import com.fasterxml.jackson.annotation.*;
4 
5 import com.fasterxml.jackson.core.type.TypeReference;
6 import com.fasterxml.jackson.databind.*;
7 import com.fasterxml.jackson.databind.json.JsonMapper;
8 import com.fasterxml.jackson.databind.testutil.NoCheckSubTypeValidator;
9 
10 import java.util.*;
11 
12 public class TestAbstractWithObjectId extends BaseMapTest
13 {
14     interface BaseInterface { }
15 
16     @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
17     static class BaseInterfaceImpl implements BaseInterface {
18 
19         @JsonProperty
20         private List<BaseInterfaceImpl> myInstances = new ArrayList<BaseInterfaceImpl>();
21 
addInstance(BaseInterfaceImpl instance)22         void addInstance(BaseInterfaceImpl instance) {
23             myInstances.add(instance);
24         }
25     }
26 
27     static class ListWrapper<T extends BaseInterface> {
28 
29         @JsonProperty
30         private List<T> myList = new ArrayList<T>();
31 
add(T t)32         void add(T t) {
33             myList.add(t);
34         }
35 
size()36         int size() {
37             return myList.size();
38         }
39     }
40 
testIssue877()41     public void testIssue877() throws Exception
42     {
43         // make two instances
44         BaseInterfaceImpl one = new BaseInterfaceImpl();
45         BaseInterfaceImpl two = new BaseInterfaceImpl();
46 
47         // add them to each other's list to show identify info being used
48         one.addInstance(two);
49         two.addInstance(one);
50 
51         // make a typed version of the list and add the 2 instances to it
52         ListWrapper<BaseInterfaceImpl> myList = new ListWrapper<BaseInterfaceImpl>();
53         myList.add(one);
54         myList.add(two);
55 
56         // make an object mapper that will add class info in so deserialisation works
57         ObjectMapper om = JsonMapper.builder()
58                 .activateDefaultTypingAsProperty(NoCheckSubTypeValidator.instance,
59                         ObjectMapper.DefaultTyping.NON_FINAL, "@class")
60                 .build();
61 
62         // write and print the JSON
63         String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(myList);
64         ListWrapper<BaseInterfaceImpl> result;
65 
66         result = om.readValue(json, new TypeReference<ListWrapper<BaseInterfaceImpl>>() { });
67 
68         assertNotNull(result);
69         // see what we get back
70         assertEquals(2, result.size());
71     }
72 }
73