Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Use ReflectionConverter for Optional classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dangmai committed Jan 14, 2023
1 parent f994a84 commit 94dc27f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/net/dangmai/serializer/Apex.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.converters.collections.TreeMapConverter;
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
import com.thoughtworks.xstream.core.ClassLoaderReference;
import com.thoughtworks.xstream.core.util.CompositeClassLoader;
import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
Expand All @@ -31,6 +32,21 @@ private static void setUpXStream(XStream xstream, int mode) {
xstream.setMode(mode);
xstream.registerConverter(new CustomCollectionConverter(xstream.getMapper()));
xstream.registerConverter(new CustomTreeMapConverter(xstream.getMapper()));

// Since XStream 1.4.20, XStream has a dedicated Optional converter.
// However, it doesn't play nice with Optional lists when serializing
// to JSON, so we use the old ReflectionConverter instead.
// The downside for this converter is that it only works if access to
// these libraries are opened manually, but we add those access
// anyway as part of the Gradle build.
xstream.registerConverter(
new ReflectionConverter(
xstream.getMapper(),
xstream.getReflectionProvider(),
Optional.class
),
XStream.PRIORITY_VERY_HIGH
);
}

public enum OutputFormat { XML, JSON }
Expand Down

0 comments on commit 94dc27f

Please sign in to comment.