Skip to content

Commit

Permalink
Support java.util.Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ejba committed May 8, 2022
1 parent eb21c8d commit bba1cb3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
4 changes: 3 additions & 1 deletion xstream/src/java/com/thoughtworks/xstream/XStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
Expand Down Expand Up @@ -712,7 +713,7 @@ protected void setupSecurity() {
final Set<Class<?>> types = new HashSet<>();
types.addAll(Arrays.<Class<?>>asList(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));
String.class, StringBuffer.class, StringBuilder.class, URL.class, URI.class, UUID.class, Optional.class));
if (JVM.isSQLAvailable()) {
types.add(JVM.loadClassForName("java.sql.Timestamp"));
types.add(JVM.loadClassForName("java.sql.Time"));
Expand Down Expand Up @@ -801,6 +802,7 @@ protected void setupAliases() {
alias("bit-set", BitSet.class);
alias("trace", StackTraceElement.class);
alias("currency", Currency.class);
alias("optional", Optional.class);

alias("map", Map.class);
alias("entry", Map.Entry.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 8. May 2022.
*/
package com.thoughtworks.xstream.converters.extended;

import java.util.Optional;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.core.util.HierarchicalStreams;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;


/**
* Converts an {@link Optional}.
*/
public class OptionalConverter implements Converter {

private final Mapper mapper;

public OptionalConverter(final Mapper mapper) {
this.mapper = mapper;
}

public boolean canConvert(Class<?> type){
return type != null && type == Optional.class;
}

public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
final Optional<?> optional = (Optional<?>)source;

if (optional.isPresent()){
Object item = optional.get();
context.convertAnother(item);
} else {
final String name = mapper.serializedClass(null);
writer.startNode(name, Mapper.Null.class);
writer.endNode();
}
}

public Optional<?> unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
final Class<?> type = HierarchicalStreams.readClassType(reader, mapper);
Object item = context.convertAnother(null, type);
return Optional.ofNullable(item);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thoughtworks.xstream.converters.extended;

import java.util.Optional;

import com.thoughtworks.acceptance.AbstractAcceptanceTest;

public class OptionalConverterTest extends AbstractAcceptanceTest {

public void testEmpty() {
String expected = "<optional/>";

assertBothWays(Optional.empty(), expected);
}

public void testWithStringValue() {
String expected =
"<optional>\n" +
" <value class=\"string\">hi</value>\n" +
"</optional>";


assertBothWays(Optional.of("hi"), expected);
}

public void testWithIntValue() {
String expected =
"<optional>\n" +
" <value class=\"int\">1</value>\n" +
"</optional>";


assertBothWays(Optional.of(1), expected);
}
}

0 comments on commit bba1cb3

Please sign in to comment.