Skip to content

Commit 5bd907a

Browse files
committed
Add two JsonGetter examples
1 parent e02f782 commit 5bd907a

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.jacksonannotation.serialization.jsongetter;
2+
3+
4+
import com.baeldung.jacksonannotation.domain.Item;
5+
import com.baeldung.jacksonannotation.domain.Person;
6+
import com.fasterxml.jackson.annotation.JsonGetter;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Source code github.com/eugenp/tutorials
13+
*
14+
* @author Alex Theedom www.baeldung.com
15+
* @version 1.0
16+
*/
17+
public class Author1 extends Person {
18+
19+
List<Item> items = new ArrayList<>();
20+
21+
public Author1(String firstName, String lastName) {
22+
super(firstName, lastName);
23+
}
24+
25+
@JsonGetter
26+
public List<Item> getItems() {
27+
return items;
28+
}
29+
30+
public void setItems(List<Item> items) {
31+
this.items = items;
32+
}
33+
}

video-tutorials/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsongetter/Author.java renamed to video-tutorials/jackson-annotations/src/main/java/com/baeldung/jacksonannotation/serialization/jsongetter/Author2.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* @author Alex Theedom www.baeldung.com
1515
* @version 1.0
1616
*/
17-
public class Author extends Person {
17+
public class Author2 extends Person {
1818

1919
List<Item> items = new ArrayList<>();
2020

21-
public Author(String firstName, String lastName) {
21+
public Author2(String firstName, String lastName) {
2222
super(firstName, lastName);
2323
}
2424

@@ -27,6 +27,7 @@ public List<Item> getItems() {
2727
return items;
2828
}
2929

30+
3031
public void setItems(List<Item> items) {
3132
this.items = items;
3233
}

‎video-tutorials/jackson-annotations/src/test/java/com/baeldung/jacksonannotation/serialization/jsongetter/JsonGetterTest.java‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,33 @@
1616
public class JsonGetterTest {
1717

1818
@Test
19-
public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException {
19+
public void whenSerializingUsingJsonGetter_andNoPropertyNameSet_thenCorrect() throws JsonProcessingException {
2020

2121
// arrange
22-
Author author = new Author("Alex", "Theedom");
22+
Author1 author = new Author1("Alex", "Theedom");
23+
24+
// act
25+
String result = new ObjectMapper().writeValueAsString(author);
26+
27+
// assert
28+
assertThat(from(result).getList("items")).isNotNull();
29+
30+
/*
31+
{
32+
"firstName": "Alex",
33+
"lastName": "Theedom",
34+
"items": []
35+
}
36+
*/
37+
38+
}
39+
40+
41+
@Test
42+
public void whenSerializingUsingJsonGetter_andPropertyNameSet_thenCorrect() throws JsonProcessingException {
43+
44+
// arrange
45+
Author2 author = new Author2("Alex", "Theedom");
2346

2447
// act
2548
String result = new ObjectMapper().writeValueAsString(author);

0 commit comments

Comments
 (0)