There may be few people using Java 1.4 nowadays, It is necessary to serialize and deserialize Json with a business application made with Java 1.4, and the result of the investigation is described.
ObjectMapper (Jackson) cannot be used in Java 1.4, so it can be done by using the following.
If you are using Maven, add the following dependencies. If you want a jar, please get it from the URL below.
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/stax/stax-api -->
<dependency>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
<version>1.0.1</version>
</dependency>
This is a sample to serialize the request Json to Person class in Servlet.
//abridgement
BufferedReader r = new bufferedReader(request.getReader())
string rewuest = r.readLine()
XStream x = new XStream(new JettisonMappedXmlDriver())
Person s = (Person)xstream.fromXML(x)
This is a sample that deserializes the Person class and returns a response.
response.getWriter().xstream.toXML(Person)
https://x-stream.github.io/json-tutorial.html
Recommended Posts