å°ãã·ãªã¢ã©ã¤ãºé¢ä¿ã®ã©ã¤ãã©ãªãç®ã«ããæ©ä¼ãããã¾ãã¦ãããããã°ããããã¾ã¨ãè¨äºãã£ããªãã¨ãããã¨ãæãåºãã¾ããã
MessagePackãKryoãProtocol Buffersãªã©ã®ã·ãªã¢ã©ã¤ã¶ã¼ã®ããã©ã¼ãã³ã¹æ¯è¼
http://blog.katty.in/4567
æ°ã«ã¯ãªã£ã¦ãããã®ã®ãå®éã«ãããã®ã©ã¤ãã©ãªã使ã£ã¦ã³ã¼ããæ¸ãããã¨ã¯ãªãã£ãã®ã§ï¼Protocol Buffersã¯é¤ãï¼ãããæ©ä¼ã ãªã¨æã試ãã¦ã¿ã¾ããã
ä»åã¯ã以ä¸ã«ã¤ãã¦æ¸ãã¦ããã¾ãã
æå¾ã®æ¹ã«ããªãå人çãªè¶£åãå ¥ã£ã¦ãã¾ãããæ°ã«ããªãæ¹åã§â¦ãJavaæ¨æºãå ¥ã£ã¦ããã®ã¯ãã¨ããããã¨ãã£ãæãã§ã
ãããããç°¡åã«ã·ãªã¢ã©ã¤ã¶ã¼ãã¨ã«ã·ãªã¢ã©ã¤ãºå¯¾è±¡ã®ã¯ã©ã¹ã¨ãã·ãªã¢ã©ã¤ãºï¼ãã·ãªã¢ã©ã¤ãºã®ã³ã¼ãããã¹ãã³ã¼ãã¨ãã¦ï¼JUnitï¼AssertJï¼æ¸ãã¦ããã¾ãã
åºæ¬çã«ã¯ãåã·ãªã¢ã©ã¤ã¶ã¼ã®ããã¥ã¡ã³ãããã³ã¼ããèµ·ããã¦ãã¾ãããã¡ãã£ã¨ä¸å®ãªãã®ã¯å è¿°ã®åç §ã¨ã³ããªã«è¨åããã¦ãã以ä¸ã®å 容ã¨ãè¦ã¦ãã¾ãã
thrift-protobuf-compare
https://github.com/eishay/jvm-serializers/wiki
https://github.com/eishay/jvm-serializers/tree/master/tpc/src/serializers
ã§ã¯ãæ¸ãã¦ããã¾ãã
Javaæ¨æº
ã¾ãã¯ãJavaã«æ¨æºæè¼ããã¦ãããã®ãããæ®éã«ãObjectInputStreamï¼ObjectOutputStreamã使ããã¤ã§ããã
ã·ãªã¢ã©ã¤ãºå¯¾è±¡ã®ã¯ã©ã¹ã
src/main/java/example/StandardBook.java
package example; import java.io.Serializable; import java.util.List; public class StandardBook implements Serializable { private static final long serialVersionUID = 1L; public String isbn; public String title; public int price; public List<String> tags; }
ã¯ã©ã¹ãã®ãã®ãããã£ã¼ã«ãã¨ãã¦å®ç¾©ãããã¡ã³ãã¼ãSerializableã§ããã°OKã§ãã
ã·ãªã¢ã©ã¤ãºï¼ãã·ãªã¢ã©ã¤ãºã確èªãããã¹ãã³ã¼ãã
src/test/java/org/littlewings/serialization/JavaStandardTest.java
package org.littlewings.serialization; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Arrays; import example.StandardBook; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; public class JavaStandardTest { @Test public void testJavaStandardSerialization() throws IOException, ClassNotFoundException { StandardBook src = new StandardBook(); src.isbn = "978-4774169316"; src.title = "Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "; src.price = 1980; src.tags = new ArrayList<>(Arrays.asList("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // ã·ãªã¢ã©ã¤ãº try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(src); } byte[] binary = baos.toByteArray(); // ã·ãªã¢ã©ã¤ãºå¾ã®ãµã¤ãº assertThat(binary) .hasSize(325); // ãã·ãªã¢ã©ã¤ãº try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(binary))) { StandardBook dest = (StandardBook) ois.readObject(); assertThat(dest.isbn) .isEqualTo("978-4774169316"); assertThat(dest.title) .isEqualTo("Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "); assertThat(dest.price) .isEqualTo(1980); assertThat(dest.tags) .containsSequence("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!"); } } }
ObjectOutputStreamã®å種writeã¡ã½ããã§ã·ãªã¢ã©ã¤ãºãObjectInputStreamã®å種readã¡ã½ããã§ãã·ãªã¢ã©ã¤ãºã§ãã
使ãæ¹ã¨ãã¦ã¯ãåç´ã ã¨æãã¾ãã
ãã ããåç §ã¨ã³ããªã®ãã³ããã¼ã¯çµæã«ãããã¾ããããã·ãªã¢ã©ã¤ãºå¾ã®ãµã¤ãºå¤§ãããé度é ãã§ãã
Kryo
åç §ã¨ã³ããªã®ãã³ããã¼ã¯ã§ããããªãæ°ã«ãªãçµæãå©ãåºãã¦ããã·ãªã¢ã©ã¤ã¶ã¼ã
Kryo
https://github.com/EsotericSoftware/kryo
å¾ã§è¨è¼ããMessagePackãProtocol Buffersã¨ã¯ç°ãªããJavaåãã®ã·ãªã¢ã©ã¤ã¶ã¼ã«ãªãã¾ãã
å©ç¨ããããã«ã¯ãã©ã¤ãã©ãªã®ä¾åé¢ä¿ãæ¸ãå¿ è¦ãããã¾ãã®ã§ãããããå ã¯å¿ è¦ã«å¿ãã¦Mavenä¾åé¢ä¿ãæ¸ãã¦ããã¾ãã
Mavenä¾åé¢ä¿ã
<dependency> <groupId>com.esotericsoftware</groupId> <artifactId>kryo</artifactId> <version>3.0.1</version> </dependency>
ç¾å¨ã¯ã3ç³»ãææ°ã®ããã§ãã
ã·ãªã¢ã©ã¤ãºå¯¾è±¡ã®ã¯ã©ã¹ã
src/main/java/example/KryoBook.java
package example; import java.util.List; public class KryoBook { public String isbn; public String title; public int price; public List<String> tags; }
ä»åã¯ãã®ã¯ã©ã¹ãKryoã®ã·ãªã¢ã©ã¤ã¶ã¼ã«æ¾ãè¾¼ã¿ã¾ããããããã«ã¤ãã¦ã¯Serializableãå®è£ ãã¦ããå¿ è¦ã¯ãªãã¿ããã§ãã
ãã£ã¼ã«ããªã©ã¯ãä»åã®ä¾ã ã¨ããã©ã«ãã®ãã®ã使ããããã¨ããæããªã®ã§ãããã
Default serializers
https://github.com/EsotericSoftware/kryo#default-serializers
ã·ãªã¢ã©ã¤ãºï¼ãã·ãªã¢ã©ã¤ãºã確èªãããã¹ãã³ã¼ãã
src/test/java/org/littlewings/serialization/KryoTest.java
package org.littlewings.serialization; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Arrays; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; import example.KryoBook; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; public class KryoTest { @Test public void testKryoSerialization() { KryoBook src = new KryoBook(); src.isbn = "978-4774169316"; src.title = "Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "; src.price = 1980; src.tags = new ArrayList<>(Arrays.asList("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!")); Kryo kryo = new Kryo(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // ã·ãªã¢ã©ã¤ãº try (Output output = new Output(baos)) { kryo.writeObject(output, src); } byte[] binary = baos.toByteArray(); // ã·ãªã¢ã©ã¤ãºå¾ã®ãµã¤ãº assertThat(binary) .hasSize(169); // ãã·ãªã¢ã©ã¤ãº try (Input input = new Input(new ByteArrayInputStream(binary))) { KryoBook dest = kryo.readObject(input, KryoBook.class); assertThat(dest.isbn) .isEqualTo("978-4774169316"); assertThat(dest.title) .isEqualTo("Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "); assertThat(dest.price) .isEqualTo(1980); assertThat(dest.tags) .containsSequence("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!"); } } }
使ãæ¹èªä½ã¯ããã£ããåç´ã§ãã
æä¾ããã¦ããã·ãªã¢ã©ã¤ã¶ã¼ã足ããªãã£ãããèªåã§ä½ãããã®è¾ºãããå¼ã£å¼µã£ã¦ãããããã®ã§ããããã
A project that provides kryo (v2) serializers for some jdk types and some external libs like e.g. joda time.
https://github.com/magro/kryo-serializers
説æãkryo (v2)ã¨ãªã£ã¦ãã¾ãããMavenä¾åé¢ä¿ãè¦ã¦ã¿ãã¨ä¸å¿3ç³»ã«ã¯ãªã£ã¦ãããã§ãâ¦ã
MessagePack
ãã¤ããªã·ãªã¢ã©ã¤ã¼ã¼ã·ã§ã³ãã©ã¼ããããMessagePackã®Javaåãå®è£ ã
MessagePack for Java
https://github.com/msgpack/msgpack-java
ããã§ã¯ãv7ï¼0.7ï¼ç³»ãæ±ãã¾ãã
v7ã§ã¯ãcoreã¨jackson-dataformat-msgpackã«åããã¦ãã¦ãæåã«è¦ããµã³ãã«ãcoreã®ã¿ã§æ¸ããã¦ãããã®ã§ããã
Packerï¼Unpackerã使ã£ããã®ã§ãããã¯é¢åããã ãªãã¨æã£ã¦ã¡ãã£ã¨è©¦ããå¾ã«jackson-dataformat-msgpackã®å 容ãã¡ããã¨è¦ã¦ããâ¦ã£ã¦æãã«ãªã£ã¦ã¾ããâ¦ãJacksonã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦ä½¿ãããã§ãããã
ã§ã¯ã使ã£ã¦ã¿ã¾ãã
Mavenä¾åé¢ä¿ã
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>jackson-dataformat-msgpack</artifactId>
<version>0.7.0-p8</version>
</dependency>
ã·ãªã¢ã©ã¤ãºå¯¾è±¡ã®ã¯ã©ã¹ã
src/main/java/example/MsgPackBook.java
package example; import java.util.List; public class MsgPackBook { public String isbn; public String title; public int price; public List<String> tags; }
ã·ãªã¢ã©ã¤ãºï¼ãã·ãªã¢ã©ã¤ãºã確èªãããã¹ãã³ã¼ãã
src/test/java/org/littlewings/serialization/MsgPackTest.java
package org.littlewings.serialization; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import com.fasterxml.jackson.databind.ObjectMapper; import example.MsgPackBook; import org.junit.Test; import org.msgpack.jackson.dataformat.MessagePackFactory; import static org.assertj.core.api.Assertions.assertThat; public class MsgPackTest { @Test public void testMsgPackSerialization() throws IOException { MsgPackBook src = new MsgPackBook(); src.isbn = "978-4774169316"; src.title = "Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "; src.price = 1980; src.tags = new ArrayList<>(Arrays.asList("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!")); ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); // ã·ãªã¢ã©ã¤ãº byte[] binary = objectMapper.writeValueAsBytes(src); // ã·ãªã¢ã©ã¤ãºå¾ã®ãµã¤ãº assertThat(binary) .hasSize(167); // ãã·ãªã¢ã©ã¤ãº MsgPackBook dest = objectMapper.readValue(binary, MsgPackBook.class); assertThat(dest.isbn) .isEqualTo("978-4774169316"); assertThat(dest.title) .isEqualTo("Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "); assertThat(dest.price) .isEqualTo(1980); assertThat(dest.tags) .containsSequence("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!"); } }
使ãæ¹ã¯ãJacksonã®ObjectMapperã使ãã°ããã ããªã®ã§ãå²ã¨ç°¡åã§ãããªããJSONã«ã¯ãªããªãã®ã§ObjectMapper#writeValueAsStringã¨ãããã¨ä¾å¤ãé£ãã§ãã¾ãã
ãªããPackerï¼Unpackerã使ã£ã¦æåã§ã·ãªã¢ã©ã¤ãºï¼ãã·ãªã¢ã©ã¤ãºããã¨ããããªæãã«ãªãã¾ãããJacksonã¨çµ±åãã¦ããæ¹ããè¦ãã¨ãLow LevelãªAPIã«ãªãã¾ããã
ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (MessagePacker packer = MessagePack.newDefaultPacker(baos)) { packer .packString(src.isbn) .packString(src.title) .packInt(src.price) .packArrayHeader(4) .packString(src.tags.get(0)) .packString(src.tags.get(1)) .packString(src.tags.get(2)) .packString(src.tags.get(3)); } byte[] binary = baos.toByteArray(); assertThat(binary) .hasSize(144); try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(binary)) { MsgPackBook dest = new MsgPackBook(); dest.isbn = unpacker.unpackString(); dest.title = unpacker.unpackString(); dest.price = unpacker.unpackInt(); int size = unpacker.unpackArrayHeader(); dest.tags = new ArrayList<>(); IntStream .range(0, size) .forEach(i -> { try { dest.tags.add(unpacker.unpackString()); } catch (IOException e) { throw new UncheckedIOException(e); } }); assertThat(dest.isbn) .isEqualTo("978-4774169316"); assertThat(dest.title) .isEqualTo("Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "); assertThat(dest.price) .isEqualTo(1980); assertThat(dest.tags) .containsSequence("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!"); }
Protocol Buffers
Googleã®ã·ãªã¢ã©ã¤ãºãã¼ã¿ãã©ã¼ããããProtocol Buffersã®Javaåãå®è£ ã
Protocol Buffers
https://developers.google.com/protocol-buffers/
Developer Guide
https://developers.google.com/protocol-buffers/docs/overview
Protocol Buffer Basics: Java
https://developers.google.com/protocol-buffers/docs/javatutorial
Java Generated Code
https://developers.google.com/protocol-buffers/docs/reference/java-generated
ä»åç´¹ä»ãã¦ããã·ãªã¢ã©ã¤ã¶ã¼ã§ãå¯ä¸ãã¼ã¿æ§é ã®å®ç¾©ãã¡ã¤ã«ï¼IDLï¼ãå¿ è¦ãªãã®ã§ãã
Mavenä¾åé¢ä¿ã
<dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.6.1</version> </dependency>
IDLã
src/main/resources/book.proto
// $ protoc --java_out=src/main/java src/main/resources/book.proto package example; // option java_package = "example"; option java_outer_classname = "ProtocolBuffersBook"; message BookData { required string isbn = 1; required string title = 2; required int32 price = 3; repeated string tags = 4; }
ã³ãã³ãããæ¸ããªãã¦ãä»åã¯çµæãåãé¨åã¯ã³ã¡ã³ãã¢ã¦ãã§æ¸ãã¦ãã¾ãã
ã§ãã³ã¡ã³ãã«ãæ¸ãã¦ãã¾ããããã®IDLããããããã³ãã³ãã§Javaã¯ã©ã¹ãçæãã¾ãã
$ protoc --java_out=src/main/java src/main/resources/book.proto
ã§ãããã£ãã¯ã©ã¹â¦ã¯é·ãã®ã§çç¥â¦ã
ä¸é¨æãåºãã¨ããã®ãããªçµæã«ãªã£ã¦ãã¾ãã
src/main/java/example/ProtocolBuffersBook.java
// Generated by the protocol buffer compiler. DO NOT EDIT! // source: src/main/resources/book.proto package example; public final class ProtocolBuffersBook { private ProtocolBuffersBook() {} public static void registerAllExtensions( com.google.protobuf.ExtensionRegistry registry) { } public static final class BookData extends com.google.protobuf.GeneratedMessage implements BookDataOrBuilder { // ãçç¥ã } public static final class BookData extends com.google.protobuf.GeneratedMessage implements BookDataOrBuilder { // ãçç¥ã } // ãçç¥ã
ã·ãªã¢ã©ã¤ãºï¼ãã·ãªã¢ã©ã¤ãºã確èªãããã¹ãã³ã¼ãã
src/test/java/org/littlewings/serialization/ProtocolBuffersTest.java
package org.littlewings.serialization; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Arrays; import example.ProtocolBuffersBook; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; public class ProtocolBuffersTest { @Test public void testProtocolBuffers() throws IOException { ProtocolBuffersBook.BookData.Builder bookBuilder = ProtocolBuffersBook.BookData.newBuilder(); bookBuilder.setIsbn("978-4774169316"); bookBuilder.setTitle("Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "); bookBuilder.setPrice(1980); bookBuilder.addAllTags(Arrays.asList("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!")); ProtocolBuffersBook.BookData src = bookBuilder.build(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // ã·ãªã¢ã©ã¤ãº src.writeTo(baos); byte[] binary = baos.toByteArray(); // ã·ãªã¢ã©ã¤ãºå¾ã®ãµã¤ãº assertThat(binary) .hasSize(148); // ãã·ãªã¢ã©ã¤ãº ProtocolBuffersBook.BookData dest = ProtocolBuffersBook.BookData.parseFrom(new ByteArrayInputStream(binary)); assertThat(dest.getIsbn()) .isEqualTo("978-4774169316"); assertThat(dest.getTitle()) .isEqualTo("Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "); assertThat(dest.getPrice()) .isEqualTo(1980); assertThat(dest.getTagsList()) .containsSequence("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!"); } }
Builderã使ã£ã¦çµã¿ä¸ãããã¨ãèªåçæãããã³ã¼ãã使ãã®ã§é°å²æ°ãã¡ãã£ã¨ä»ã¨ã¯éãã¾ããã
JBoss Marshalling
ã¡ãã£ã¨å人çãªèå³ã§ãä»å試ãã¦ã¿ãã·ãªã¢ã©ã¤ã¶ã¼ã§ãã
JBoss Marshalling
http://jbossmarshalling.jboss.org/
ä½ç½®ä»ãçã«ã¯Javaæ¨æºã®ã·ãªã¢ã©ã¤ã¶ã¼ãæ¹åãããã®ãã¨ããæãã¿ããã§ãã
ãã ãã¡ãã£ã¨å°ã£ããã¨ã¨ãã¦ã¯ããã¥ã¡ã³ããã»ã¨ãã©ãªãã以ä¸ã®ããã¥ã¡ã³ãã¨Infinispanã®ã½ã¼ã¹ã³ã¼ããããã¦æåã«ç´¹ä»ãããã³ããã¼ã¯ã®ã³ã¼ããè¦ãªããæ¸ãã¦ã¿ã¾ããã
Marshalling API quick start
https://docs.jboss.org/author/display/JBMAR/Marshalling+API+quick+start
ãã¨ãJBoss Marshallingã«ã¯Riverã¨Serialããããããªã®ã§ããã両è ã®ä½¿ãåãã¯ããããã£ã¦ãã¾ãããRiverã®æ¹ãéãï¼ãã¨ãã使ããã¦ãããï¼
Mavenä¾åé¢ä¿ã
<dependency> <groupId>org.jboss.marshalling</groupId> <artifactId>jboss-marshalling-river</artifactId> <version>1.4.10.Final</version> </dependency>
ã·ãªã¢ã©ã¤ãºå¯¾è±¡ã®ã¯ã©ã¹ã
src/main/java/example/MarshallingBook.java
package example; import java.io.Serializable; import java.util.List; public class MarshallingBook implements Serializable { private static final long serialVersionUID = 1L; public String isbn; public String title; public int price; public List<String> tags; }
JBoss Marshallingã®å ´åã¯ã対象ãSerializableã§ããå¿ è¦ãããã¾ãã
ã·ãªã¢ã©ã¤ãºï¼ãã·ãªã¢ã©ã¤ãºã確èªãããã¹ãã³ã¼ãã
src/test/java/org/littlewings/serialization/JBossMarshallingRiverTest.java
package org.littlewings.serialization; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import example.MarshallingBook; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.Marshalling; import org.jboss.marshalling.MarshallingConfiguration; import org.jboss.marshalling.Unmarshaller; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; public class JBossMarshallingRiverTest { @Test public void testJBossMarshallingRiver() throws IOException, ClassNotFoundException { MarshallingBook src = new MarshallingBook(); src.isbn = "978-4774169316"; src.title = "Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "; src.price = 1980; src.tags = new ArrayList<>(Arrays.asList("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!")); MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("river"); MarshallingConfiguration configuration = new MarshallingConfiguration(); configuration.setVersion(3); Marshaller marshaller = marshallerFactory.createMarshaller(configuration); Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // ã·ãªã¢ã©ã¤ãº marshaller.start(Marshalling.createByteOutput(baos)); marshaller.writeObject(src); marshaller.finish(); byte[] binary = baos.toByteArray(); // ã·ãªã¢ã©ã¤ãºå¾ã®ãµã¤ãº assertThat(binary) .hasSize(237); // ãã·ãªã¢ã©ã¤ãº unmarshaller.start(Marshalling.createByteInput(new ByteArrayInputStream(binary))); MarshallingBook dest = unmarshaller.readObject(MarshallingBook.class); unmarshaller.finish(); assertThat(dest.isbn) .isEqualTo("978-4774169316"); assertThat(dest.title) .isEqualTo("Javaã¨ã³ã¸ãã¢é¤æèªæ¬ [ç¾å ´ã§å½¹ç«ã¤ææ°ç¥èãæºè¼!] "); assertThat(dest.price) .isEqualTo(1980); assertThat(dest.tags) .containsSequence("ããªãã¨", "Java", "ä»ãã", "ãã¦ã³ãã¼ã!"); } }
ä¾ãã»ã¨ãã©ãªãã®ã§ãããç»å ´äººç©ãããããã°ä½¿ãã®ã¯ããé£ãããªãããâ¦ï¼
ãªããä»å使ã£ãã®ã¯Riverã§ãããSerialã«åãæ¿ããã«ã¯Mavenä¾åé¢ä¿ã«ä»¥ä¸ãå ãã¦
<dependency> <groupId>org.jboss.marshalling</groupId> <artifactId>jboss-marshalling-serial</artifactId> <version>1.4.10.Final</version> </dependency>
MarshallerFactoryã®åå¾é¨åãããå¤ãã¾ãã
MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
ããã¦ãMarshallingConfigurationã®ãã¼ã¸ã§ã³æå®ãå¤ãããã5ããæå®ãã¾ãã
// ãã¼ã¸ã§ã³æå®ã¯ãªãã¦ããããæå®ããå ´åã¯ã5ãã®ã¿ // configuration.setVersion(5);
ä¸å¿ããªãã¨ã使ãã¾ããã
çµããã«
ã¨ããããããã ã使ã£ã¦ã¿ã¾ãããç°¡åã«ãã³ããã¼ã¯ãåã£ããããã®ã§ããããã¡ãã¨ããå¤ãããªãå°è±¡ã«ãªãã¾ããã
MessagePackãKryoãProtocol Buffersãªã©ã®ã·ãªã¢ã©ã¤ã¶ã¼ã®ããã©ã¼ãã³ã¹æ¯è¼
http://blog.katty.in/4567
Kryoãããªãéãã£ãã§ããã¨ã¯ãããMessagePackãããå£ããªãæãã ã£ãã®ã§ã好ã¿ã¨ãç¨éã§é¸ã¶æãããªãã¨ãæ®å¿µãªãããä»åã¯Protocol Buffersã ãé度ãè¦ãªãã£ãã®ã§ããããã¶ãé度ã§ããå°ããã¨ã¯ãªãã®ã§ãããï¼ä»¥ä¸åç §ï¼ã
Protocol Buffersã¯é
ã
http://frsyuki.hatenablog.com/entry/20081116/p1
ãã¨ãJavaæ¨æºã®ã·ãªã¢ã©ã¤ã¶ã¼ã®é ãã¨ã·ãªã¢ã©ã¤ãºå¾ã®ãã¤ããªãµã¤ãºã®å¤§ããã¯ã¡ãã£ã¨ç®ç«ã¤æãã§ããããä»ã«ã·ãªã¢ã©ã¤ã¶ã¼ãç»å ´ããã®ãããªãã»ã©ãªãã¨æãã¾ããã
ä»åãæ¯è¼çåç´ãªAPIããã£ã¨èããã ããªã®ã§ãã©ãã®ã·ãªã¢ã©ã¤ã¶ã¼ãããå°ãLow LevelãªAPIãªã©ãã£ããããã®ã§ããã®ãããã¯å ¨ç¶è©¦ãã¦ãã¾ããããããããå¿ è¦ã«å¿ãã¦è¿½ãæãã§ããã