1 package com.fasterxml.jackson.databind.objectid; 2 3 import java.util.concurrent.atomic.AtomicReference; 4 5 import com.fasterxml.jackson.annotation.JsonIdentityInfo; 6 import com.fasterxml.jackson.annotation.ObjectIdGenerators; 7 8 import com.fasterxml.jackson.databind.*; 9 10 public class ReferentialWithObjectIdTest extends BaseMapTest 11 { 12 public static class EmployeeList { 13 public AtomicReference<Employee> first; 14 } 15 16 @JsonIdentityInfo(property="id", generator=ObjectIdGenerators.PropertyGenerator.class) 17 public static class Employee { 18 public int id; 19 public String name; 20 public AtomicReference<Employee> next; 21 next(Employee n)22 public Employee next(Employee n) { 23 next = new AtomicReference<Employee>(n); 24 return this; 25 } 26 } 27 28 /* 29 /********************************************************** 30 /* Test methods 31 /********************************************************** 32 */ 33 34 private final ObjectMapper MAPPER = new ObjectMapper(); 35 testAtomicWithObjectId()36 public void testAtomicWithObjectId() throws Exception 37 { 38 Employee first = new Employee(); 39 first.id = 1; 40 first.name = "Alice"; 41 42 Employee second = new Employee(); 43 second.id = 2; 44 second.name = "Bob"; 45 46 first.next(second); 47 second.next(first); 48 49 EmployeeList input = new EmployeeList(); 50 input.first = new AtomicReference<Employee>(first); 51 52 String json = MAPPER.writeValueAsString(input); 53 54 // and back 55 56 EmployeeList result = MAPPER.readValue(json, EmployeeList.class); 57 Employee firstB = result.first.get(); 58 assertNotNull(firstB); 59 assertEquals("Alice", firstB.name); 60 Employee secondB = firstB.next.get(); 61 assertNotNull(secondB); 62 assertEquals("Bob", secondB.name); 63 assertNotNull(secondB.next.get()); 64 assertSame(firstB, secondB.next.get()); 65 } 66 } 67