Skip to content

Commit

Permalink
Fix AtomicReference marshalling. Closes x-stream#326
Browse files Browse the repository at this point in the history
  • Loading branch information
ablekhman committed Mar 28, 2023
1 parent d1ce7d0 commit edc6db5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,29 @@ public boolean canConvert(final Class type) {
}

public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
final AtomicReference ref = (AtomicReference)source;
writer.startNode(mapper.serializedMember(AtomicReference.class, "value"));

final Object object = ref.get();
final String name = mapper.serializedClass(object!= null ? object.getClass() : null);
writer.addAttribute(mapper.aliasForSystemAttribute("class"), name);
context.convertAnother(ref.get());
writer.endNode();
final AtomicReference ref = (AtomicReference)source;
if (ref.get() != null) {
writer.startNode(mapper.serializedMember(AtomicReference.class, "value"));

final Object object = ref.get();
final String name = mapper.serializedClass(object != null ? object.getClass() : null);
writer.addAttribute(mapper.aliasForSystemAttribute("class"), name);
context.convertAnother(ref.get());
writer.endNode();
}
}

public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
reader.moveDown();
if (reader.hasMoreChildren()) {
reader.moveDown();

final Class type = HierarchicalStreams.readClassType(reader, mapper);
final Object value = context.convertAnother(context, type);
reader.moveUp();
return new AtomicReference(value);
final Class type = HierarchicalStreams.readClassType(reader, mapper);
final Object value = context.convertAnother(context, type);
reader.moveUp();
return new AtomicReference(value);
} else {
return new AtomicReference();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public void testAtomicReferenceWithOldFormat() {
+ "</java.util.concurrent.atomic.AtomicReference>")).get());
}

public void testEmptyAtomicReference() {
final AtomicReference atomicRef = new AtomicReference();
assertBothWays(atomicRef, "<atomic-reference/>");
}

public void testAtomicReferenceWithAlias() {
xstream.aliasField("junit", AtomicReference.class, "value");
final AtomicReference atomicRef = new AtomicReference("test");
Expand Down

0 comments on commit edc6db5

Please sign in to comment.