-
Notifications
You must be signed in to change notification settings - Fork 17
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
1 parent
d6fff81
commit f24bc6e
Showing
2 changed files
with
152 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>fr.an.tests</groupId> | ||
<artifactId>test-xstream</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>2.7.2</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.12</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.thoughtworks.xstream</groupId> | ||
<artifactId>xstream</artifactId> | ||
<version>1.4.20</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> | ||
|
111 changes: 111 additions & 0 deletions
111
test-xstream/src/main/java/fr/an/tests/xstream/XStreamApp.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,111 @@ | ||
package fr.an.tests.xstream; | ||
|
||
import com.thoughtworks.xstream.XStream; | ||
import com.thoughtworks.xstream.converters.Converter; | ||
import com.thoughtworks.xstream.converters.MarshallingContext; | ||
import com.thoughtworks.xstream.converters.UnmarshallingContext; | ||
import com.thoughtworks.xstream.io.HierarchicalStreamReader; | ||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; | ||
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; | ||
|
||
public class XStreamApp { | ||
|
||
public static void main(String[] args) { | ||
IgnorableCtx ctx = new IgnorableCtx(); | ||
A a = new A(); | ||
a.ctx = ctx; | ||
a.ctx2 = ctx; | ||
// a.ctx3ExplicitOmmitAnnotation = ctx; | ||
|
||
{ | ||
System.out.println("XStream ... default:"); | ||
XStream xstream1 = new XStream(); | ||
addDefaultAlias(xstream1); | ||
String xml1 = xstream1.toXML(a); | ||
System.out.println(xml1); | ||
System.out.println(); | ||
} | ||
|
||
{ | ||
System.out.println("XStream ... override ignore fields in IgnorableCtx"); | ||
XStream xstream = new XStream(); | ||
addDefaultAlias(xstream); | ||
xstream.registerConverter(new IgnorableCtxXStreamConverter()); | ||
String xml = xstream.toXML(a); | ||
System.out.println(xml); | ||
System.out.println(); | ||
} | ||
|
||
{ | ||
System.out.println("XStream.omitField(A.class, 'ctx')"); | ||
XStream xstream3 = new XStream(); | ||
addDefaultAlias(xstream3); | ||
xstream3.omitField(A.class, "ctx"); | ||
xstream3.omitField(A.class, "ctx2"); | ||
String xml3 = xstream3.toXML(a); | ||
System.out.println(xml3); | ||
System.out.println(); | ||
} | ||
|
||
|
||
{ | ||
System.out.println("XStream ... JSON"); | ||
XStream xstreamJson = new XStream(new JsonHierarchicalStreamDriver()); | ||
addDefaultAlias(xstreamJson); | ||
xstreamJson.omitField(A.class, "ctx"); | ||
xstreamJson.omitField(A.class, "ctx2"); | ||
String json = xstreamJson.toXML(a); | ||
System.out.println(json); | ||
System.out.println(); | ||
} | ||
|
||
} | ||
|
||
public static class IgnorableCtxXStreamConverter implements Converter { | ||
|
||
public boolean canConvert(Class type) { | ||
return IgnorableCtx.class.isAssignableFrom(type); | ||
} | ||
|
||
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { | ||
System.out.println("override XStream marshal for " + source + " => do nothing.."); | ||
} | ||
|
||
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { | ||
System.out.println("override XStream unmarshal for .. => new .."); | ||
return new IgnorableCtx(); | ||
} | ||
|
||
} | ||
|
||
public static void addDefaultAlias(XStream xstream) { | ||
xstream.alias("A", A.class); | ||
} | ||
|
||
// public static class AXStreamConverter extends Converter { | ||
// | ||
// public boolean canConvert(Class type) { | ||
// return IgnorableCtx.class.isAssignableFrom(type); | ||
// } | ||
// | ||
// } | ||
|
||
public static class IgnorableCtx { | ||
private int internalDetails = 1; // to be ignored.. | ||
private IgnorableCtxImpl impl = new IgnorableCtxImpl(); // to be ignored | ||
} | ||
|
||
public static class IgnorableCtxImpl { | ||
private int internalDetails2 = 2; // to be ignored.. | ||
} | ||
|
||
public static class A { | ||
private int okToSerialize; | ||
IgnorableCtx ctx; | ||
IgnorableCtx ctx2; | ||
|
||
// @XStreamOmitField // does not work?? | ||
// IgnorableCtx ctx3ExplicitOmmitAnnotation; | ||
} | ||
|
||
} |