Skip to content

Commit

Permalink
Fix #308
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Sep 14, 2022
1 parent 61e0ead commit 36b30be
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
5 changes: 4 additions & 1 deletion xstream/src/java/com/thoughtworks/xstream/XStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -710,7 +712,7 @@ protected void setupSecurity() {
allowTypeHierarchy(Path.class);

final Set<Class<?>> types = new HashSet<>();
types.addAll(Arrays.<Class<?>>asList(BitSet.class, Charset.class, Class.class, Currency.class, Date.class,
types.addAll(Arrays.<Class<?>>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()) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 && type == AtomicBoolean.class;
}

@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);
}
}

0 comments on commit 36b30be

Please sign in to comment.