1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Java1.4でのJsonシリアライズ・デシリアライズ

Posted at

概要

今の時代Java1.4を使用しているは少ないかもしれせんが、
Java1.4で作られた業務アプリにてJsonのシリアライズ、デシリアライズをする必要があり、調査した結果を記載します。

結論

ObjectMapper(Jackson)はJava1.4では使えないので以下を使用すれば可能です。

  • xstream-1.2.2
  • jettison-1.0
  • stax-api-1.0.1

ライブラリの追加

Mavenを使用している場合は以下の依存関係追加してください。
jarが欲しいかたは以下URLから取得してください。

<!-- 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>

シリアライズ

ServletにてリクエストのJsonをPersonクラスにシリアライズするサンプルです。

// 省略
BufferedReader r = new bufferedReader(request.getReader())
string rewuest = r.readLine()
XStream x = new XStream(new JettisonMappedXmlDriver())
Person s = (Person)xstream.fromXML(x)

デシリアライズ

Personクラスをデシリアライズしてレスポンスを返すサンプルです。

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

参考サイト

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?