Skip to content

Commit 6b30c84

Browse files
authored
Fix booleans in JacksonJsonpGenerator and add tests (#327) (#328)
1 parent 61dba33 commit 6b30c84

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

java-client/src/main/java/co/elastic/clients/json/jackson/JacksonJsonpGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public JsonGenerator write(String name, double value) {
182182
public JsonGenerator write(String name, boolean value) {
183183
try {
184184
generator.writeFieldName(name);
185-
generator.writeBooleanField(name, value);
185+
generator.writeBoolean(value);
186186
} catch (IOException e) {
187187
throw JacksonUtils.convertException(e);
188188
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.json.jackson;
21+
22+
import jakarta.json.Json;
23+
import jakarta.json.JsonValue;
24+
import jakarta.json.stream.JsonGenerator;
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.io.StringWriter;
29+
30+
public class JacksonJsonpGeneratorTest extends Assertions {
31+
32+
@Test
33+
public void testWrite(){
34+
StringWriter sw = new StringWriter();
35+
JsonGenerator generator = new JacksonJsonpMapper().jsonProvider().createGenerator(sw);
36+
37+
generator.writeStartObject();
38+
39+
// Boolean
40+
generator.write("bool1", true);
41+
generator.writeKey("bool2");
42+
generator.write(false);
43+
44+
// String
45+
generator.write("str1", "foo");
46+
generator.writeKey("str2");
47+
generator.write("bar");
48+
49+
// Integer
50+
generator.write("int1", 42);
51+
generator.writeKey("int2");
52+
generator.write(1337);
53+
54+
// Long
55+
generator.write("long1", 123456789012345L);
56+
generator.writeKey("long2");
57+
generator.write(123456789012345L);
58+
59+
generator.write("double1", 0.001);
60+
generator.writeKey("double2");
61+
generator.write(12345.6789);
62+
63+
// JsonValue
64+
JsonValue jsonValue = Json.createObjectBuilder()
65+
.add("bool", true)
66+
.add("str", "foo")
67+
.add("int", 42)
68+
.add("long", 123456789012345L)
69+
.add("double", 12345.6789)
70+
.build();
71+
72+
generator.write("value", jsonValue);
73+
74+
generator.close();
75+
76+
assertEquals("{" +
77+
"\"bool1\":true," +
78+
"\"bool2\":false," +
79+
"\"str1\":\"foo\"," +
80+
"\"str2\":\"bar\"," +
81+
"\"int1\":42," +
82+
"\"int2\":1337," +
83+
"\"long1\":123456789012345," +
84+
"\"long2\":123456789012345," +
85+
"\"double1\":0.001," +
86+
"\"double2\":12345.6789," +
87+
"\"value\":{\"bool\":true,\"str\":\"foo\",\"int\":42,\"long\":123456789012345,\"double\":12345.6789}" +
88+
"}", sw.toString());
89+
}
90+
}

0 commit comments

Comments
 (0)