From 643f22752f5bf620a8031fed16aaf2704f594ebd Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 14 Sep 2022 12:22:35 -0700 Subject: [PATCH] Fix #308 --- .../com/thoughtworks/xstream/XStream.java | 5 +- .../extended/AtomicBooleanConverter.java | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 xstream/src/java/com/thoughtworks/xstream/converters/extended/AtomicBooleanConverter.java diff --git a/xstream/src/java/com/thoughtworks/xstream/XStream.java b/xstream/src/java/com/thoughtworks/xstream/XStream.java index 1045307aa..c388bedf9 100644 --- a/xstream/src/java/com/thoughtworks/xstream/XStream.java +++ b/xstream/src/java/com/thoughtworks/xstream/XStream.java @@ -101,6 +101,7 @@ import java.util.UUID; import java.util.Vector; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Pattern; import com.thoughtworks.xstream.converters.ConversionException; @@ -141,6 +142,7 @@ import com.thoughtworks.xstream.converters.enums.EnumConverter; import com.thoughtworks.xstream.converters.enums.EnumMapConverter; import com.thoughtworks.xstream.converters.enums.EnumSetConverter; +import com.thoughtworks.xstream.converters.extended.AtomicBooleanConverter; import com.thoughtworks.xstream.converters.extended.CharsetConverter; import com.thoughtworks.xstream.converters.extended.ColorConverter; import com.thoughtworks.xstream.converters.extended.CurrencyConverter; @@ -710,7 +712,7 @@ protected void setupSecurity() { allowTypeHierarchy(Path.class); final Set> types = new HashSet<>(); - types.addAll(Arrays.>asList(BitSet.class, Charset.class, Class.class, Currency.class, Date.class, + types.addAll(Arrays.>asList(AtomicBoolean.class, BitSet.class, Charset.class, Class.class, Currency.class, Date.class, DecimalFormatSymbols.class, File.class, Locale.class, Object.class, Pattern.class, StackTraceElement.class, String.class, StringBuffer.class, StringBuilder.class, URL.class, URI.class, UUID.class)); if (JVM.isSQLAvailable()) { @@ -996,6 +998,7 @@ protected void setupConverters() { registerConverter(new JavaMethodConverter(classLoaderReference), PRIORITY_NORMAL); registerConverter(new JavaFieldConverter(classLoaderReference), PRIORITY_NORMAL); registerConverter(new LambdaConverter(mapper, reflectionProvider, classLoaderReference), PRIORITY_NORMAL); + registerConverter(new AtomicBooleanConverter(), PRIORITY_NORMAL); if (JVM.isAWTAvailable()) { registerConverter(new FontConverter(mapper), PRIORITY_NORMAL); diff --git a/xstream/src/java/com/thoughtworks/xstream/converters/extended/AtomicBooleanConverter.java b/xstream/src/java/com/thoughtworks/xstream/converters/extended/AtomicBooleanConverter.java new file mode 100644 index 000000000..76cc2f077 --- /dev/null +++ b/xstream/src/java/com/thoughtworks/xstream/converters/extended/AtomicBooleanConverter.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2022 XStream Committers. + * All rights reserved. + * + * The software in this package is published under the terms of the BSD + * style license a copy of which has been included with this distribution in + * the LICENSE.txt file. + * + * Created on 9 September 2022 by Basil Crow. + */ +package com.thoughtworks.xstream.converters.extended; + +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.MarshallingContext; +import com.thoughtworks.xstream.converters.SingleValueConverterWrapper; +import com.thoughtworks.xstream.converters.UnmarshallingContext; +import com.thoughtworks.xstream.converters.basic.BooleanConverter; +import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; +import java.util.concurrent.atomic.AtomicBoolean; + +/** Converts an {@link AtomicBoolean}. */ +public class AtomicBooleanConverter implements Converter { + + @Override + public boolean canConvert(Class type) { + return type != null && AtomicBoolean.class.isAssignableFrom(type); + } + + @Override + public void marshal( + Object source, HierarchicalStreamWriter writer, MarshallingContext context) { + final AtomicBoolean atomicBoolean = (AtomicBoolean) source; + writer.startNode("value"); + context.convertAnother( + atomicBoolean.get(), new SingleValueConverterWrapper(BooleanConverter.BINARY)); + writer.endNode(); + } + + @Override + public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { + reader.moveDown(); + Object item = + context.convertAnother( + null, + Boolean.class, + new SingleValueConverterWrapper(BooleanConverter.BINARY)); + boolean value = item instanceof Boolean ? ((Boolean) item).booleanValue() : false; + reader.moveUp(); + return new AtomicBoolean(value); + } +}