-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
xstream/src/java/com/thoughtworks/xstream/converters/extended/OptionalConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
xstream/src/test/com/thoughtworks/xstream/converters/extended/OptionalConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |