diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java index 87d83899..52351430 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTest.java @@ -41,6 +41,17 @@ public class JsonbPolymorphismTest { @Rule public JsonbRule jsonb = new JsonbRule(); + @Test + public void testNestedSerialization() { + Labrador labrador = new Labrador(); + labrador.dogAge = 3; + labrador.labradorName = "john"; + AnimalWrapper wrapper = new AnimalWrapper(); + wrapper.animal = labrador; + + assertEquals("{\"animal\":{\"@animal\":\"dog\",\"@dog\":\"labrador\",\"dogAge\":3,\"labradorName\":\"john\"}}", jsonb.toJson(wrapper)); + } + @Test public void testSerialization() { Labrador labrador = new Labrador(); @@ -100,6 +111,10 @@ public void testCreatorDeserialization() { } + public static class AnimalWrapper { + public Animal animal; + } + @JsonbTypeInfo(key = "@animal", value = @JsonbSubtype(alias = "dog", type = Dog.class)) public interface Animal { } 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 cf44c5c7..c06f38df 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 @@ -192,12 +192,6 @@ public void doWriteObject(Object object, JsonGenerator generator, boolean writeB generator.writeStartObject(); } - if (classMapping.serializedPolymorphicProperties != null) { - for (Map.Entry polymorphicProperty : classMapping.serializedPolymorphicProperties) { - generator.write(polymorphicProperty.getKey(), polymorphicProperty.getValue()); - } - } - final boolean writeEnd = doWriteObjectBody(object, ignoredProperties, jsonPointer, generator); if (writeEnd && writeBody) { generator.writeEnd(); @@ -376,6 +370,12 @@ private boolean doWriteObjectBody(final Object object, final Collection return true; } + if (classMapping.serializedPolymorphicProperties != null) { + for (Map.Entry polymorphicProperty : classMapping.serializedPolymorphicProperties) { + generator.write(polymorphicProperty.getKey(), polymorphicProperty.getValue()); + } + } + for (final Map.Entry getterEntry : classMapping.getters.entrySet()) { final Mappings.Getter getter = getterEntry.getValue(); if (ignored != null && ignored.contains(getterEntry.getKey())) {