This is an automated email from the ASF dual-hosted git repository.
struberg pushed a commit to branch johnzon-1.2.x
in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/johnzon-1.2.x by this push:
new c77759f0 JOHNZON-421 JsonbSerializer crashes when using primitives
c77759f0 is described below
commit c77759f025f0aaf447a652903a56e5688028d660
Author: Mark Struberg <[email protected]>
AuthorDate: Tue Feb 10 11:25:56 2026 +0100
JOHNZON-421 JsonbSerializer crashes when using primitives
If an object is serialised into a primitive, then we did crash with an
Exception
JsonGenerationException: write(param) is only valid in arrays.
---
.../jsonb/serializer/SerialiseAsPrimitiveTest.java | 129 +++++++++++++++++++++
.../johnzon/mapper/MappingGeneratorImpl.java | 9 +-
2 files changed, 134 insertions(+), 4 deletions(-)
diff --git
a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/serializer/SerialiseAsPrimitiveTest.java
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/serializer/SerialiseAsPrimitiveTest.java
new file mode 100644
index 00000000..8c2a1a63
--- /dev/null
+++
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/serializer/SerialiseAsPrimitiveTest.java
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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 org.apache.johnzon.jsonb.serializer;
+
+import java.lang.reflect.Type;
+
+import org.junit.Test;
+
+import jakarta.json.bind.Jsonb;
+import jakarta.json.bind.JsonbBuilder;
+import jakarta.json.bind.annotation.JsonbTypeDeserializer;
+import jakarta.json.bind.annotation.JsonbTypeSerializer;
+import jakarta.json.bind.serializer.DeserializationContext;
+import jakarta.json.bind.serializer.JsonbDeserializer;
+import jakarta.json.bind.serializer.JsonbSerializer;
+import jakarta.json.bind.serializer.SerializationContext;
+import jakarta.json.stream.JsonGenerator;
+import jakarta.json.stream.JsonParser;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * This test checks a JsonbSerialize/JsonbDeserialize roundtrip when using a
primitive as placeholder
+ */
+public class SerialiseAsPrimitiveTest {
+
+
+ public static class TestConstant {
+ public final static TestConstant VAL_1 = new TestConstant("A");
+ public final static TestConstant VAL_2 = new TestConstant("B");
+ public final static TestConstant VAL_3 = new TestConstant("C");
+
+ private final String code;
+
+ public TestConstant(String code) {
+ this.code = code;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public static TestConstant getByCode(String code) {
+ switch (code) {
+ case "A": return VAL_1;
+ case "B": return VAL_2;
+ case "C": return VAL_3;
+ default: return null;
+ }
+ }
+ }
+
+ public static class ConstantUsage {
+ private int i;
+
+ @JsonbTypeSerializer(ConstantJsonbSerialiser.class)
+ @JsonbTypeDeserializer(ConstantJsonbDeserializer.class)
+ private TestConstant testConstant;
+
+ public int getI() {
+ return i;
+ }
+
+ public void setI(int i) {
+ this.i = i;
+ }
+
+ public TestConstant getTestConstant() {
+ return testConstant;
+ }
+
+ public void setTestConstant(TestConstant testEnum) {
+ this.testConstant = testEnum;
+ }
+ }
+
+ public static class ConstantJsonbSerialiser implements
JsonbSerializer<TestConstant> {
+ @Override
+ public void serialize(TestConstant val, JsonGenerator generator,
SerializationContext ctx) {
+ if (val == null) {
+ ctx.serialize(null, generator);
+ } else {
+ ctx.serialize(val.getCode(), generator);
+ }
+ }
+ }
+
+ public static class ConstantJsonbDeserializer implements
JsonbDeserializer<TestConstant> {
+
+ @Override
+ public TestConstant deserialize(JsonParser parser, DeserializationContext
ctx, Type rtType) {
+ if (rtType instanceof TestConstant) {
+ final String key = parser.getString();
+ if (key != null) {
+ return TestConstant.getByCode(key);
+ }
+ }
+
+ return null;
+ }
+}
+
+
+
+ @Test
+ public void testEnumJsonb() {
+ ConstantUsage enumVerwender = new ConstantUsage();
+ enumVerwender.setI(1);
+ enumVerwender.setTestConstant(TestConstant.VAL_2);
+
+ Jsonb jsonb = JsonbBuilder.create();
+ final String json = jsonb.toJson(enumVerwender);
+ assertTrue(json.contains("\"testConstant\":\"B\""));
+ }
+
+}
diff --git
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
index 224bdd40..f7347997 100644
---
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
+++
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
@@ -131,7 +131,7 @@ public class MappingGeneratorImpl implements
MappingGenerator {
return;
}
- if (writePrimitives(object)) {
+ if (writePrimitives(object, generator)) {
return;
}
@@ -231,7 +231,8 @@ public class MappingGeneratorImpl implements
MappingGenerator {
/**
* @return {@code true} if it was a primitive, {@code false} if the value
did not get handled
*/
- private boolean writePrimitives(final Object value) {
+ private boolean writePrimitives(final Object value,
+ final JsonGenerator generator) {
boolean handled = false;
if (value == null) {
return true; // fake a write
@@ -550,7 +551,7 @@ public class MappingGeneratorImpl implements
MappingGenerator {
String valJsonPointer = jsonPointers == null ? null :
jsonPointers.get(o);
if (valJsonPointer != null) {
// write JsonPointer instead of the original object
- writePrimitives(valJsonPointer);
+ writePrimitives(valJsonPointer, generator);
} else {
ObjectConverter.Writer objectConverterToUse = objectConverter;
if (o != null && objectConverterToUse == null) {
@@ -693,7 +694,7 @@ public class MappingGeneratorImpl implements
MappingGenerator {
private void writeItem(final Object o, final Collection<String>
ignoredProperties, JsonPointerTracker jsonPointer) {
if (o == null) {
generator.writeNull();
- } else if (!writePrimitives(o)) {
+ } else if (!writePrimitives(o, generator)) {
if (Collection.class.isInstance(o)) {
doWriteIterable(Collection.class.cast(o), ignoredProperties,
jsonPointer);
} else if (o.getClass().isArray()) {