Skip to content

Commit 3a5dc3d

Browse files
committed
Merge code for XStream JSON processing article
2 parents a6768e6 + ce2891e commit 3a5dc3d

5 files changed

Lines changed: 148 additions & 49 deletions

File tree

‎xstream/pom.xml‎

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>org.baeldung</groupId>
5-
<artifactId>xstream-introduction</artifactId>
6-
<version>0.0.1-SNAPSHOT</version>
7-
<name>xstream-introduction</name>
8-
<description>An Introduction To XStream</description>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.baeldung</groupId>
5+
<artifactId>xstream-introduction</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<name>xstream-introduction</name>
8+
<description>An Introduction To XStream</description>
99

10-
<dependencies>
11-
<dependency>
12-
<groupId>com.thoughtworks.xstream</groupId>
13-
<artifactId>xstream</artifactId>
14-
<version>1.4.5</version>
15-
</dependency>
10+
<dependencies>
11+
<dependency>
12+
<groupId>com.thoughtworks.xstream</groupId>
13+
<artifactId>xstream</artifactId>
14+
<version>1.4.5</version>
15+
</dependency>
1616

17-
<dependency>
18-
<groupId>junit</groupId>
19-
<artifactId>junit</artifactId>
20-
<version>4.12</version>
21-
</dependency>
22-
23-
<dependency>
24-
<groupId>log4j</groupId>
25-
<artifactId>log4j</artifactId>
26-
<version>1.2.17</version>
27-
</dependency>
28-
</dependencies>
17+
<dependency>
18+
<groupId>org.codehaus.jettison</groupId>
19+
<artifactId>jettison</artifactId>
20+
<version>1.3.7</version>
21+
</dependency>
2922

30-
<build>
31-
<plugins>
32-
<plugin>
33-
<groupId>org.apache.maven.plugins</groupId>
34-
<artifactId>maven-compiler-plugin</artifactId>
35-
<version>3.5.1</version>
36-
<configuration>
37-
<source>1.8</source>
38-
<target>1.8</target>
39-
</configuration>
40-
</plugin>
41-
</plugins>
42-
</build>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.12</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>log4j</groupId>
31+
<artifactId>log4j</artifactId>
32+
<version>1.2.17</version>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<version>3.5.1</version>
42+
<configuration>
43+
<source>1.8</source>
44+
<target>1.8</target>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
4349

4450
</project>
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package com.baeldung.initializer;
22

33
import com.thoughtworks.xstream.XStream;
4+
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
5+
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
46

57
public class SimpleXstreamInitializer {
68

7-
private XStream xtreamInstance;
8-
99
public XStream getXstreamInstance() {
10-
if (xtreamInstance == null) {
11-
synchronized (SimpleXstreamInitializer.class) {
12-
if (xtreamInstance == null) {
13-
xtreamInstance = new XStream();
14-
}
15-
}
16-
}
10+
XStream xtreamInstance = new XStream();
1711
return xtreamInstance;
1812
}
13+
14+
public XStream getXstreamJettisonMappedInstance() {
15+
XStream xstreamInstance = new XStream(new JettisonMappedXmlDriver());
16+
return xstreamInstance;
17+
}
18+
19+
public XStream getXstreamJsonHierarchicalInstance() {
20+
XStream xstreamInstance = new XStream(new JsonHierarchicalStreamDriver());
21+
return xstreamInstance;
22+
}
1923
}

‎xstream/src/main/resources/log4j.properties‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Root logger option
22
log4j.rootLogger=DEBUG, file
3-
43
# Redirect log messages to console
54
# log4j.appender.stdout=org.apache.log4j.ConsoleAppender
65
# log4j.appender.stdout.Target=System.out
76
# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
87
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
9-
108
# Redirect log messages to a log file, support file rolling.
119
log4j.appender.file=org.apache.log4j.RollingFileAppender
1210
log4j.appender.file.File=D:\\Test\\xstream-application.log
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.test;
2+
3+
import com.baeldung.initializer.SimpleXstreamInitializer;
4+
import com.baeldung.pojo.ContactDetails;
5+
import com.baeldung.pojo.Customer;
6+
import com.baeldung.utility.SimpleDataGeneration;
7+
import com.thoughtworks.xstream.XStream;
8+
import org.junit.Assert;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class XStreamJettisonTest {
13+
14+
private Customer customer = null;
15+
16+
private String dataJson = null;
17+
18+
private XStream xstream = null;
19+
20+
@Before
21+
public void dataSetup() {
22+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
23+
xstream = simpleXstreamInitializer.getXstreamJettisonMappedInstance();
24+
xstream.processAnnotations(Customer.class);
25+
}
26+
27+
@Test
28+
public void convertObjectToJson() {
29+
customer = SimpleDataGeneration.generateData();
30+
xstream.alias("customer", Customer.class);
31+
xstream.alias("contactDetails", ContactDetails.class);
32+
xstream.aliasField("fn", Customer.class, "firstName");
33+
dataJson = xstream.toXML(customer);
34+
System.out.println(dataJson);
35+
Assert.assertNotNull(dataJson);
36+
}
37+
38+
@Test
39+
public void convertJsonToObject() {
40+
customer = SimpleDataGeneration.generateData();
41+
dataJson = xstream.toXML(customer);
42+
customer = (Customer) xstream.fromXML(dataJson);
43+
System.out.println(customer);
44+
Assert.assertNotNull(customer);
45+
}
46+
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.test;
2+
3+
import com.baeldung.initializer.SimpleXstreamInitializer;
4+
import com.baeldung.pojo.ContactDetails;
5+
import com.baeldung.pojo.Customer;
6+
import com.baeldung.utility.SimpleDataGeneration;
7+
import com.thoughtworks.xstream.XStream;
8+
import org.junit.Assert;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class XStreamJsonHierarchicalTest {
13+
14+
private Customer customer = null;
15+
private String dataJson = null;
16+
private XStream xstream = null;
17+
18+
@Before
19+
public void dataSetup() {
20+
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
21+
xstream = simpleXstreamInitializer.getXstreamJsonHierarchicalInstance();
22+
xstream.processAnnotations(Customer.class);
23+
}
24+
25+
@Test
26+
public void convertObjectToJson() {
27+
customer = SimpleDataGeneration.generateData();
28+
xstream.alias("customer", Customer.class);
29+
xstream.alias("contactDetails", ContactDetails.class);
30+
xstream.aliasField("fn", Customer.class, "firstName");
31+
dataJson = xstream.toXML(customer);
32+
System.out.println(dataJson);
33+
Assert.assertNotNull(dataJson);
34+
}
35+
36+
@Test(expected = UnsupportedOperationException.class)
37+
public void convertJsonToObject() {
38+
customer = SimpleDataGeneration.generateData();
39+
dataJson = xstream.toXML(customer);
40+
customer = (Customer) xstream.fromXML(dataJson);
41+
Assert.assertNotNull(customer);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)