Skip to content

Commit 49b8975

Browse files
yavuztasYavuz Tas
andauthored
BAEL-5192 Code samples for the article (eugenp#11483)
* BAEL-5192 implement code samples * BAEL-5192 simplify some methods * BAEL-5192 remove duplicated test method Co-authored-by: Yavuz Tas <[email protected]>
1 parent 1477d48 commit 49b8975

6 files changed

Lines changed: 333 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.jackson.booleanAsInt;
2+
3+
public class Game {
4+
5+
private Long id;
6+
private String name;
7+
private Boolean paused;
8+
private Boolean over;
9+
10+
public Game() {
11+
}
12+
13+
public Game(Long id, String name) {
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
public Long getId() {
19+
return this.id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return this.name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public Boolean isPaused() {
35+
return paused;
36+
}
37+
38+
public void setPaused(Boolean paused) {
39+
this.paused = paused;
40+
}
41+
42+
public Boolean isOver() {
43+
return over;
44+
}
45+
46+
public void setOver(Boolean over) {
47+
this.over = over;
48+
}
49+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.jackson.booleanAsInt;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
5+
6+
public class GameAnnotatedByJsonFormat {
7+
8+
private Long id;
9+
private String name;
10+
11+
@JsonFormat(shape = Shape.NUMBER)
12+
private boolean paused;
13+
14+
@JsonFormat(shape = Shape.NUMBER)
15+
private boolean over;
16+
17+
public GameAnnotatedByJsonFormat() {
18+
}
19+
20+
public GameAnnotatedByJsonFormat(Long id, String name) {
21+
this.id = id;
22+
this.name = name;
23+
}
24+
25+
public Long getId() {
26+
return this.id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return this.name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public boolean isPaused() {
42+
return paused;
43+
}
44+
45+
public void setPaused(boolean paused) {
46+
this.paused = paused;
47+
}
48+
49+
public boolean isOver() {
50+
return over;
51+
}
52+
53+
public void setOver(boolean over) {
54+
this.over = over;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.jackson.booleanAsInt;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5+
6+
public class GameAnnotatedByJsonSerializeDeserialize {
7+
8+
private Long id;
9+
private String name;
10+
11+
@JsonSerialize(using = NumericBooleanSerializer.class)
12+
@JsonDeserialize(using = NumericBooleanDeserializer.class)
13+
private Boolean paused;
14+
15+
@JsonSerialize(using = NumericBooleanSerializer.class)
16+
@JsonDeserialize(using = NumericBooleanDeserializer.class)
17+
private Boolean over;
18+
19+
public GameAnnotatedByJsonSerializeDeserialize() {
20+
}
21+
22+
public GameAnnotatedByJsonSerializeDeserialize(Long id, String name) {
23+
this.id = id;
24+
this.name = name;
25+
}
26+
27+
public Long getId() {
28+
return this.id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return this.name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public Boolean isPaused() {
44+
return paused;
45+
}
46+
47+
public void setPaused(Boolean paused) {
48+
this.paused = paused;
49+
}
50+
51+
public Boolean isOver() {
52+
return over;
53+
}
54+
55+
public void setOver(Boolean over) {
56+
this.over = over;
57+
}
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.jackson.booleanAsInt;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.databind.DeserializationContext;
5+
import com.fasterxml.jackson.databind.JsonDeserializer;
6+
import java.io.IOException;
7+
8+
public class NumericBooleanDeserializer extends JsonDeserializer<Boolean> {
9+
10+
@Override
11+
public Boolean deserialize(JsonParser p, DeserializationContext ctxt)
12+
throws IOException {
13+
if ("1".equals(p.getText())) {
14+
return Boolean.TRUE;
15+
}
16+
if ("0".equals(p.getText())) {
17+
return Boolean.FALSE;
18+
}
19+
// for other than "1" or "0" throw exception by using Jackson internals
20+
throw ctxt.weirdStringException(p.getText(), Boolean.class, "only \"1\" or \"0\" recognized");
21+
}
22+
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.jackson.booleanAsInt;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.JsonSerializer;
5+
import com.fasterxml.jackson.databind.SerializerProvider;
6+
import java.io.IOException;
7+
8+
public class NumericBooleanSerializer extends JsonSerializer<Boolean> {
9+
10+
@Override
11+
public void serialize(Boolean value, JsonGenerator gen, SerializerProvider serializers)
12+
throws IOException {
13+
gen.writeString(value ? "1" : "0");
14+
}
15+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.baeldung.jackson.booleanAsInt;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.fasterxml.jackson.annotation.JsonFormat;
6+
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
9+
import com.fasterxml.jackson.databind.module.SimpleModule;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class BooleanAsIntegerUnitTest {
15+
16+
private ObjectMapper mapper;
17+
18+
@BeforeEach
19+
public void setup() {
20+
mapper = new ObjectMapper();
21+
}
22+
23+
@Test
24+
public void givenBoolean_serializedAsInteger() throws Exception {
25+
GameAnnotatedByJsonFormat
26+
game = new GameAnnotatedByJsonFormat(1L, "My Game");
27+
game.setPaused(true);
28+
game.setOver(false);
29+
String json = mapper.writeValueAsString(game);
30+
31+
assertThat(json)
32+
.isEqualTo("{\"id\":1,\"name\":\"My Game\",\"paused\":1,\"over\":0}");
33+
}
34+
35+
@Test
36+
public void givenInteger_deserializedAsBooleanByDefault() throws Exception {
37+
// Integer "1" and "0" values deserialized correctly out of the box.
38+
// No configuration or @JsonFormat annotation needed.
39+
String json = "{\"id\":1,\"name\":\"My Game\",\"paused\":1,\"over\":0}";
40+
Game game = mapper.readValue(json, Game.class);
41+
42+
assertThat(game.isPaused()).isEqualTo(true);
43+
assertThat(game.isOver()).isEqualTo(false);
44+
}
45+
46+
@Test
47+
public void givenBoolean_serializedAsIntegerGlobally() throws Exception {
48+
// global configuration override for the type Boolean
49+
mapper.configOverride(Boolean.class)
50+
.setFormat(JsonFormat.Value.forShape(Shape.NUMBER));
51+
52+
Game game = new Game(1L, "My Game");
53+
game.setPaused(true);
54+
game.setOver(false);
55+
String json = mapper.writeValueAsString(game);
56+
57+
assertThat(json)
58+
.isEqualTo("{\"id\":1,\"name\":\"My Game\",\"paused\":1,\"over\":0}");
59+
}
60+
61+
@Test
62+
public void givenBooleanWithCustomSerializer_serializedAsNumericString() throws Exception {
63+
GameAnnotatedByJsonSerializeDeserialize
64+
game = new GameAnnotatedByJsonSerializeDeserialize(1L, "My Game");
65+
game.setPaused(true);
66+
game.setOver(false);
67+
String json = mapper.writeValueAsString(game);
68+
69+
assertThat(json)
70+
.isEqualTo("{\"id\":1,\"name\":\"My Game\",\"paused\":\"1\",\"over\":\"0\"}");
71+
}
72+
73+
@Test
74+
public void givenNumericStringWithCustomDeserializer_deserializedAsBoolean() throws Exception {
75+
String json = "{\"id\":1,\"name\":\"My Game\",\"paused\":\"1\",\"over\":\"0\"}";
76+
GameAnnotatedByJsonSerializeDeserialize
77+
game = mapper.readValue(json, GameAnnotatedByJsonSerializeDeserialize.class);
78+
79+
assertThat(game.isPaused()).isEqualTo(true);
80+
assertThat(game.isOver()).isEqualTo(false);
81+
}
82+
83+
@Test
84+
public void givenBooleanWithCustomSerializer_serializedAsNumericStringGlobally() throws Exception {
85+
// setting serializers globally
86+
SimpleModule module = new SimpleModule();
87+
module.addSerializer(Boolean.class, new NumericBooleanSerializer());
88+
mapper.registerModule(module);
89+
90+
Game game = new Game(1L, "My Game");
91+
game.setPaused(true);
92+
game.setOver(false);
93+
String json = mapper.writeValueAsString(game);
94+
95+
assertThat(json)
96+
.isEqualTo("{\"id\":1,\"name\":\"My Game\",\"paused\":\"1\",\"over\":\"0\"}");
97+
}
98+
99+
@Test
100+
public void givenNumericStringWithCustomDeserializer_deserializedAsBooleanGlobally() throws Exception {
101+
// setting deserializers globally
102+
SimpleModule module = new SimpleModule();
103+
module.addDeserializer(Boolean.class, new NumericBooleanDeserializer());
104+
mapper.registerModule(module);
105+
106+
String json = "{\"id\":1,\"name\":\"My Game\",\"paused\":\"1\",\"over\":\"0\"}";
107+
Game game = mapper.readValue(json, Game.class);
108+
109+
assertThat(game.isPaused()).isEqualTo(true);
110+
assertThat(game.isOver()).isEqualTo(false);
111+
}
112+
113+
@Test
114+
public void givenInvalidStringWithCustomDeserializer_throwsInvalidFormatException() {
115+
// another number other than "1" or "0"
116+
String json = "{\"id\":1,\"name\":\"My Game\",\"paused\":\"5\"}";
117+
InvalidFormatException e = Assertions.assertThrows(
118+
InvalidFormatException.class, () -> mapper.readValue(json, GameAnnotatedByJsonSerializeDeserialize.class)
119+
);
120+
121+
assertThat(e.getValue()).isEqualTo("5");
122+
123+
// non-numeric string
124+
String json2 = "{\"id\":1,\"name\":\"My Game\",\"paused\":\"xxx\"}";
125+
InvalidFormatException e2 = Assertions.assertThrows(
126+
InvalidFormatException.class, () -> mapper.readValue(json2, GameAnnotatedByJsonSerializeDeserialize.class)
127+
);
128+
129+
assertThat(e2.getValue()).isEqualTo("xxx");
130+
}
131+
132+
}

0 commit comments

Comments
 (0)