Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JOHNZON-401] fix serialization of nested polymorphic objects #118

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,6 @@ public void doWriteObject(Object object, JsonGenerator generator, boolean writeB
generator.writeStartObject();
}

if (classMapping.serializedPolymorphicProperties != null) {
for (Map.Entry<String, String> polymorphicProperty : classMapping.serializedPolymorphicProperties) {
generator.write(polymorphicProperty.getKey(), polymorphicProperty.getValue());
}
}

final boolean writeEnd = doWriteObjectBody(object, ignoredProperties, jsonPointer, generator);
if (writeEnd && writeBody) {
generator.writeEnd();
Expand Down Expand Up @@ -376,6 +370,12 @@ private boolean doWriteObjectBody(final Object object, final Collection<String>
return true;
}

if (classMapping.serializedPolymorphicProperties != null) {
for (Map.Entry<String, String> polymorphicProperty : classMapping.serializedPolymorphicProperties) {
generator.write(polymorphicProperty.getKey(), polymorphicProperty.getValue());
}
}

for (final Map.Entry<String, Mappings.Getter> getterEntry : classMapping.getters.entrySet()) {
final Mappings.Getter getter = getterEntry.getValue();
if (ignored != null && ignored.contains(getterEntry.getKey())) {
Expand Down
Loading