forked from elastic/elasticsearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullSafeJsonGeneratorTest.java
More file actions
140 lines (124 loc) · 5.45 KB
/
Copy pathNullSafeJsonGeneratorTest.java
File metadata and controls
140 lines (124 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.clients.json;
import co.elastic.clients.testkit.ModelTestCase;
import co.elastic.clients.util.ApiTypeHelper;
import jakarta.json.JsonValue;
import jakarta.json.stream.JsonGenerator;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.Map;
public class NullSafeJsonGeneratorTest extends ModelTestCase {
@Test
public void testNullReferencesAreWrittenAsJsonNull() {
StringWriter writer = new StringWriter();
try (JsonGenerator generator = new NullSafeJsonGenerator(mapper.jsonProvider().createGenerator(writer))) {
generator.writeStartObject();
// writeKey + write(value) overloads with null reference values
generator.writeKey("string_field");
generator.write((String) null);
generator.writeKey("decimal_field");
generator.write((BigDecimal) null);
generator.writeKey("integer_field");
generator.write((BigInteger) null);
generator.writeKey("json_value_field");
generator.write((JsonValue) null);
// name+value overloads with null reference values — DelegatingJsonGenerator splits these into
// writeKey(name) + write(value), so they reach the overrides above via virtual dispatch.
generator.write("named_string", (String) null);
generator.write("named_decimal", (BigDecimal) null);
generator.write("named_integer", (BigInteger) null);
generator.write("named_json_value", (JsonValue) null);
generator.writeEnd();
}
assertEquals(
"{" +
"\"string_field\":null," +
"\"decimal_field\":null," +
"\"integer_field\":null," +
"\"json_value_field\":null," +
"\"named_string\":null," +
"\"named_decimal\":null," +
"\"named_integer\":null," +
"\"named_json_value\":null" +
"}",
writer.toString()
);
}
@Test
public void testNonNullValuesPassThrough() {
StringWriter writer = new StringWriter();
try (JsonGenerator generator = new NullSafeJsonGenerator(mapper.jsonProvider().createGenerator(writer))) {
generator.writeStartObject();
generator.write("string_field", "hello");
generator.write("int_field", 42);
generator.write("long_field", 10000000000L);
generator.write("double_field", 1.5);
generator.write("bool_field", true);
generator.write("decimal_field", new BigDecimal("3.14"));
generator.write("integer_field", new BigInteger("12345"));
generator.writeEnd();
}
assertEquals(
"{" +
"\"string_field\":\"hello\"," +
"\"int_field\":42," +
"\"long_field\":10000000000," +
"\"double_field\":1.5," +
"\"bool_field\":true," +
"\"decimal_field\":3.14," +
"\"integer_field\":12345" +
"}",
writer.toString()
);
}
@Test
public void testToJsonStringWithMapWhileWorkaroundActive() {
// SimpleJsonpMapper only serializes types registered in the Java client and doesn't support arbitrary Maps.
Assumptions.assumeFalse(jsonImpl == JsonImpl.Simple, "SimpleJsonpMapper does not support arbitrary Map serialization");
Map<String, Object> map = new LinkedHashMap<>();
map.put("a", 1);
map.put("b", "two");
try (ApiTypeHelper.DisabledChecksHandle h =
ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck(true)) {
assertEquals("{\"a\":1,\"b\":\"two\"}", JsonpUtils.toJsonString(map, mapper));
}
}
@Test
public void testToJsonStringWithPojoWhileWorkaroundActive() {
Assumptions.assumeFalse(jsonImpl == JsonImpl.Simple, "SimpleJsonpMapper does not support POJO serialization");
SomePojo pojo = new SomePojo();
pojo.name = "test";
pojo.value = 42;
try (ApiTypeHelper.DisabledChecksHandle h =
ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck(true)) {
String json = JsonpUtils.toJsonString(pojo, mapper);
assertTrue(json.contains("\"name\":\"test\""), "Expected name field, got: " + json);
assertTrue(json.contains("\"value\":42"), "Expected value field, got: " + json);
}
}
public static class SomePojo {
public String name;
public int value;
}
}