You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by st...@apache.org on 2026/07/23 20:36:13 UTC
(johnzon) branch master updated: JOHNZON-436 JsonGenerator.write("foo", (String) null) creates wrong JSON
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push:
new 1f048a14 JOHNZON-436 JsonGenerator.write("foo", (String) null) creates wrong JSON
1f048a14 is described below
commit 1f048a14f1f42c1840823d87be6d922ebe53556e
Author: Mark Struberg <st...@apache.org>
AuthorDate: Thu Jul 23 22:35:32 2026 +0200
JOHNZON-436 JsonGenerator.write("foo", (String) null) creates wrong JSON
null value should not get wrapped in ""
---
.../org/apache/johnzon/core/JsonGeneratorImpl.java | 10 ++++++---
.../apache/johnzon/core/JsonGeneratorImplTest.java | 24 ++++++++++++++++++++++
2 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
index 34305258..dc61842e 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
@@ -660,9 +660,13 @@ class JsonGeneratorImpl implements JsonGenerator, JsonChars, Serializable {
if (peek == GeneratorState.START_ARRAY || peek == GeneratorState.IN_ARRAY) {
writeIndent();
}
- justWrite(QUOTE_CHAR);
- writeEscaped0(value);
- justWrite(QUOTE_CHAR);
+ if (value == null) {
+ writeValue(NULL);
+ } else {
+ justWrite(QUOTE_CHAR);
+ writeEscaped0(value);
+ justWrite(QUOTE_CHAR);
+ }
alignState();
}
diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonGeneratorImplTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonGeneratorImplTest.java
index 9ca0bb44..03d089cd 100644
--- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonGeneratorImplTest.java
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonGeneratorImplTest.java
@@ -73,6 +73,30 @@ public class JsonGeneratorImplTest {
assertEquals("{\"foo\":null}", writer.toString());
}
+ @Test
+ public void writeKeyWriteNullBigDecimalAttribute() {
+ final StringWriter writer = new StringWriter();
+ Json.createGenerator(writer)
+ .writeStartObject().write("foo", (BigDecimal) null).writeEnd().close();
+ assertEquals("{\"foo\":null}", writer.toString());
+ }
+
+ @Test
+ public void writeKeyWriteNullBigIntegerAttribute() {
+ final StringWriter writer = new StringWriter();
+ Json.createGenerator(writer)
+ .writeStartObject().write("foo", (BigInteger) null).writeEnd().close();
+ assertEquals("{\"foo\":null}", writer.toString());
+ }
+
+ @Test
+ public void writeKeyWriteNullStringAttribute() {
+ final StringWriter writer = new StringWriter();
+ Json.createGenerator(writer)
+ .writeStartObject().write("foo", (String) null).writeEnd().close();
+ assertEquals("{\"foo\":null}", writer.toString());
+ }
+
@Test
public void closeOnce() throws Throwable {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();