Json serialization / deserialization in Java 1.4

Overview

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.

Conclusion

ObjectMapper (Jackson) cannot be used in Java 1.4, so it can be done by using the following.

Add library

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>

Serialize

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)

Deserialize

This is a sample that deserializes the Person class and returns a response.

response.getWriter().xstream.toXML(Person)

Reference site

https://x-stream.github.io/json-tutorial.html

Recommended Posts

Json serialization / deserialization in Java 1.4
Read JSON in Java
Java --Serialization and Deserialization
POST JSON in Java
POST Json in Java ~ HttpURLConnection ~
Java serialization
Changes in Java 11
Rock-paper-scissors in Java
Try using JSON format API in Java
Java serialization notes
Working with huge JSON in Java Lambda
Pi in Java
FizzBuzz in Java
Regarding the transient modifier and serialization in Java
Code to escape a JSON string in Java
[java] sort in list
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Parse Json in Scala
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
Java Object Serialization why & when
Try using RocksDB in Java
Avoid Yubaba's error in Java
[Java] JSON communication with jackson
JSON in Java and Jackson Part 1 Return JSON from the server
Get EXIF information in Java
Save Java PDF in Excel
How to use JSON data in WebSocket communication (Java, JavaScript)
Edit ini in Java: ini4j
Java history in this world
Try calling JavaScript in Java
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint
Implement two-step verification in Java
Refactoring: Make Blackjack in Java
Topic Analysis (LDA) in Java
NEologd preprocessing in Java neologdn-java