Merge code for XStream JSON processing article

This commit is contained in:
David Morley 2016-04-11 05:27:20 -05:00
commit 3a5dc3d746
5 changed files with 148 additions and 49 deletions

View File

@ -1,44 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId> <groupId>org.baeldung</groupId>
<artifactId>xstream-introduction</artifactId> <artifactId>xstream-introduction</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>xstream-introduction</name> <name>xstream-introduction</name>
<description>An Introduction To XStream</description> <description>An Introduction To XStream</description>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.thoughtworks.xstream</groupId> <groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId> <artifactId>xstream</artifactId>
<version>1.4.5</version> <version>1.4.5</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.codehaus.jettison</groupId>
<artifactId>junit</artifactId> <artifactId>jettison</artifactId>
<version>4.12</version> <version>1.3.7</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>junit</groupId>
<artifactId>log4j</artifactId> <artifactId>junit</artifactId>
<version>1.2.17</version> <version>4.12</version>
</dependency> </dependency>
</dependencies>
<build> <dependency>
<plugins> <groupId>log4j</groupId>
<plugin> <artifactId>log4j</artifactId>
<groupId>org.apache.maven.plugins</groupId> <version>1.2.17</version>
<artifactId>maven-compiler-plugin</artifactId> </dependency>
<version>3.5.1</version> </dependencies>
<configuration>
<source>1.8</source> <build>
<target>1.8</target> <plugins>
</configuration> <plugin>
</plugin> <groupId>org.apache.maven.plugins</groupId>
</plugins> <artifactId>maven-compiler-plugin</artifactId>
</build> <version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -1,19 +1,23 @@
package com.baeldung.initializer; package com.baeldung.initializer;
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
public class SimpleXstreamInitializer { public class SimpleXstreamInitializer {
private XStream xtreamInstance;
public XStream getXstreamInstance() { public XStream getXstreamInstance() {
if (xtreamInstance == null) { XStream xtreamInstance = new XStream();
synchronized (SimpleXstreamInitializer.class) {
if (xtreamInstance == null) {
xtreamInstance = new XStream();
}
}
}
return xtreamInstance; return xtreamInstance;
} }
public XStream getXstreamJettisonMappedInstance() {
XStream xstreamInstance = new XStream(new JettisonMappedXmlDriver());
return xstreamInstance;
}
public XStream getXstreamJsonHierarchicalInstance() {
XStream xstreamInstance = new XStream(new JsonHierarchicalStreamDriver());
return xstreamInstance;
}
} }

View File

@ -1,12 +1,10 @@
# Root logger option # Root logger option
log4j.rootLogger=DEBUG, file log4j.rootLogger=DEBUG, file
# Redirect log messages to console # Redirect log messages to console
# log4j.appender.stdout=org.apache.log4j.ConsoleAppender # log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# log4j.appender.stdout.Target=System.out # log4j.appender.stdout.Target=System.out
# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling. # Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\Test\\xstream-application.log log4j.appender.file.File=D:\\Test\\xstream-application.log

View File

@ -0,0 +1,47 @@
package com.baeldung.test;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.baeldung.pojo.ContactDetails;
import com.baeldung.pojo.Customer;
import com.baeldung.utility.SimpleDataGeneration;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class XStreamJettisonTest {
private Customer customer = null;
private String dataJson = null;
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamJettisonMappedInstance();
xstream.processAnnotations(Customer.class);
}
@Test
public void convertObjectToJson() {
customer = SimpleDataGeneration.generateData();
xstream.alias("customer", Customer.class);
xstream.alias("contactDetails", ContactDetails.class);
xstream.aliasField("fn", Customer.class, "firstName");
dataJson = xstream.toXML(customer);
System.out.println(dataJson);
Assert.assertNotNull(dataJson);
}
@Test
public void convertJsonToObject() {
customer = SimpleDataGeneration.generateData();
dataJson = xstream.toXML(customer);
customer = (Customer) xstream.fromXML(dataJson);
System.out.println(customer);
Assert.assertNotNull(customer);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.test;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.baeldung.pojo.ContactDetails;
import com.baeldung.pojo.Customer;
import com.baeldung.utility.SimpleDataGeneration;
import com.thoughtworks.xstream.XStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class XStreamJsonHierarchicalTest {
private Customer customer = null;
private String dataJson = null;
private XStream xstream = null;
@Before
public void dataSetup() {
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamJsonHierarchicalInstance();
xstream.processAnnotations(Customer.class);
}
@Test
public void convertObjectToJson() {
customer = SimpleDataGeneration.generateData();
xstream.alias("customer", Customer.class);
xstream.alias("contactDetails", ContactDetails.class);
xstream.aliasField("fn", Customer.class, "firstName");
dataJson = xstream.toXML(customer);
System.out.println(dataJson);
Assert.assertNotNull(dataJson);
}
@Test(expected = UnsupportedOperationException.class)
public void convertJsonToObject() {
customer = SimpleDataGeneration.generateData();
dataJson = xstream.toXML(customer);
customer = (Customer) xstream.fromXML(dataJson);
Assert.assertNotNull(customer);
}
}