1 package com.fasterxml.jackson.databind.deser; 2 3 import com.fasterxml.jackson.annotation.JsonProperty; 4 import com.fasterxml.jackson.databind.*; 5 6 public class TestInnerClass extends BaseMapTest 7 { 8 static class Dog 9 { 10 public String name; 11 public Brain brain; 12 Dog()13 public Dog() { } Dog(String n, boolean thinking)14 public Dog(String n, boolean thinking) { 15 name = n; 16 brain = new Brain(); 17 brain.isThinking = thinking; 18 } 19 20 // note: non-static 21 public class Brain { 22 @JsonProperty("brainiac") 23 public boolean isThinking; 24 parentName()25 public String parentName() { return name; } 26 } 27 } 28 29 /* 30 /********************************************************** 31 /* Tests 32 /********************************************************** 33 */ 34 testSimpleNonStaticInner()35 public void testSimpleNonStaticInner() throws Exception 36 { 37 // Let's actually verify by first serializing, then deserializing back 38 ObjectMapper mapper = new ObjectMapper(); 39 Dog input = new Dog("Smurf", true); 40 String json = mapper.writeValueAsString(input); 41 Dog output = mapper.readValue(json, Dog.class); 42 assertEquals("Smurf", output.name); 43 assertNotNull(output.brain); 44 assertTrue(output.brain.isThinking); 45 // and verify correct binding... 46 assertEquals("Smurf", output.brain.parentName()); 47 output.name = "Foo"; 48 assertEquals("Foo", output.brain.parentName()); 49 50 // also, null handling 51 input.brain = null; 52 53 output = mapper.readValue(mapper.writeValueAsString(input), Dog.class); 54 assertNull(output.brain); 55 } 56 } 57