Merge pull request #14

Merge remote-tracking branch 'remotes/messagepack/master' into MessagePack
This commit is contained in:
YuCheng Hu 2020-11-29 12:32:32 -05:00 committed by GitHub
commit 9aa572b63e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 992 additions and 0 deletions

40
message-pack/.travis.yml Normal file
View File

@ -0,0 +1,40 @@
language: java
install: true
jdk:
- openjdk8
os:
- linux
branches:
only:
- master
cache:
directories:
- $HOME/.gradle
before_install:
- chmod +x gradlew
before_script:
- git fetch
- git checkout -b gh-pages
- git merge origin/master
- git rm --cached --ignore-unmatch index.html
script:
- ./gradlew asciidoctor
deploy:
provider: s3
access_key_id: "$AWS_ACCESS_KEY"
secret_access_key: "$AWS_SECRET_KEY"
region: us-east-2
bucket: "com.ossez.docs"
skip_cleanup: true
local_dir: build/asciidoc/html5
upload-dir: messagepack-docs

8
message-pack/README.adoc Normal file
View File

@ -0,0 +1,8 @@
= MessagePack 文档
YuCheng Hu <yhu@ossez.com>
:doctype: book
:page-layout: docs
:page-description: MessagePack
:page-keywords: MessagePack
MessagePack 的相关文档,本文是根据官方的内容和我们项目中的具体实践进行编辑的。

94
message-pack/build.gradle Normal file
View File

@ -0,0 +1,94 @@
plugins {
id "org.asciidoctor.convert" version "2.3.0"
}
apply plugin: 'java'
version = '1.0.0'
allprojects {
repositories {
maven { url "https://maven.ossez.com/repository/internal" }
}
ext {
environmentProperty = project.hasProperty('environment') ? getProperty('environment') : 'hsql'
docResourcesVersion = '0.1.0'
}
apply plugin: 'idea'
}
configurations {
docs
}
dependencies {
docs "com.ossez.docresources:ossez-doc-resources:${docResourcesVersion}@zip"
}
task prepareAsciidocBuildZH(type: Sync) {
dependsOn configurations.docs
// copy doc resources
from {
configurations.docs.collect { zipTree(it) }
}
// and doc sources
from "src/docs/asciidoc/"
// to a build directory of your choice
into "$buildDir/asciidoc/assemble"
}
task('makePDFZH', type: org.asciidoctor.gradle.AsciidoctorTask){
dependsOn prepareAsciidocBuildZH
backends 'pdf'
sourceDir "$buildDir/asciidoc/assemble"
sources {
include 'index-single.adoc'
}
options doctype: 'book', eruby: 'erubis'
logDocuments = true
attributes 'icons': 'font',
'sectanchors': '',
'sectnums': '',
'toc': '',
'source-highlighter' : 'coderay',
revnumber: project.version
}
asciidoctor {
dependsOn makePDFZH
backends 'html5'
sourceDir "$buildDir/asciidoc/assemble"
resources {
from(sourceDir) {
include 'images/*', 'css/**', 'js/**'
}
}
options doctype: 'book', eruby: 'erubis'
logDocuments = true
attributes 'docinfo': 'shared',
// use provided stylesheet
stylesdir: "css/",
stylesheet: 'ossez.css',
'linkcss': true,
'icons': 'font',
'sectanchors': '',
// use provided highlighter
'source-highlighter=highlight.js',
'highlightjsdir=js/highlight',
'highlightjs-theme=atom-one-dark-reasonable',
'idprefix': '',
'idseparator': '-',
'spring-version': project.version,
'allow-uri-read': '',
revnumber: project.version
}

188
message-pack/gradlew vendored Normal file
View File

@ -0,0 +1,188 @@
#!/usr/bin/env sh
#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"

100
message-pack/gradlew.bat vendored Normal file
View File

@ -0,0 +1,100 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@ -0,0 +1 @@
rootProject.name = 'messagepack-docs'

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@ -0,0 +1,561 @@
= MessagePack Java 0.6.X QuickStart
YuCheng Hu <yhu@ossez.com>
:doctype: book
:toc: left
:toclevels: 4
:page-layout: docs
:page-description: Protocol Buffers
:page-keywords: Protocol Buffers
:imagesdir: images
:includedir: _includes
中文标题【MessagePack Java 0.6.X 快速开始指南】
WARNING: 0.6.x 版本的 MessagePack 已经过期被淘汰了。如果你现在开始使用 MessagePack 话,请不要使用这个版本。
我们再这里保留 0.6.x 版本的内容主要用于参考用途。
最新的 MessagePack 版本请参考: https://github.com/msgpack/msgpack-java 中的项目源代码。
TIP: MessagePack 中文文档请参考: http://docs.ossez.com/messagepack-docs/index.html
TIP: MessagePack 测试和示例源代码: https://github.com/cwiki-us-demo/serialize-deserialize-demo-java
这个指南提供了使用 msgpack-java 的快速指南。在开始的时候,我们将会介绍如何安装 msgpack-java然后将会运行如何使用 msgpack 来对对象序列化/反序列化serialize/deserizalize对象。
== 安装
你可以使用下面 2 种方法来安装 msgpack-java —— 从 maven 下载或者直接构建 jar 包。
=== 从 Maven2 仓库中进行安装
MessagePack 针对 Java 的使用已经发布到 Maven 的中央仓库中Maven Central Repository。你可以使用下面的参数来配置你项目的 pom.xml 文件。
[source,xml]
----
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
<version>${msgpack.version}</version>
</dependency>
----
你需要将 ${msgpack.version} 替换为当前的 MessagePack 版本,有关可以使用的具体版本你可以访问 http://repo1.maven.org/maven2/org/msgpack/msgpack/ 中的版本。
请注意,在 0.6.x 版本中最新的版本只更新到 0.6.12。
=== 从 git 仓库中进行安装
你可以从代码仓库中获得最新的代码。
[source]
----
$ git clone git@github.com:msgpack/msgpack-java.git
$ cd msgpack-java
$ mvn package
----
使用上面的代码进行编译后u你将会在 msgpack-java/target 目录中得到 msgpack.jar 包。
同时你也需要 https://code.google.com/p/json-simple/ 和 https://github.com/jboss-javassist/javassist 来让 msgpack.jar 可以在项目中使用。否则你将会收到 NoClassDefFoundError 错误。
== 如何使用
下面是如何使用的示例代码。
=== 使用一个消息打包message-packable
使用注解 '_@Message_' 来让你可以序列化你自己类中对象的 public 字段。
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Object.java 中下载到本地后进行编译测试。
[source,java]
----
package com.insight.demo.msgpack;
import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
/**
* MessagePack6Objects
*
* @author yhu
*/
public class MessagePack6Object {
final Logger logger = LoggerFactory.getLogger(MessagePack6Object.class);
/**
* MessageData Message Object
*/
@Message // Annotation
public static class MessageData {
// public fields are serialized.
public String uuid;
public String name;
public double version;
}
/**
* Test MessagePack6Objects
*/
@Test
public void testMessagePack6Objects() {
logger.debug("MessagePack6Objects for Objects");
String uuid = UUID.randomUUID().toString();
// INIT OBJ
MessageData src = new MessageData();
src.uuid = uuid;
src.name = "MessagePack6";
src.version = 0.6;
try {
MessagePack msgPack = new MessagePack();
// Serialization
logger.debug("------ Serialization ------");
byte[] bytes = msgPack.write(src);
logger.debug("Bytes Array Length: [{}]", bytes.length);
// Deserialization
logger.debug("------ Deserialization ------");
MessageData dst = msgPack.read(bytes, MessageData.class);
logger.debug("Check Object for UUID: [{}]", dst.uuid);
assertEquals(uuid, dst.uuid);
} catch (Exception ex) {
logger.error("MessagePack Serialization And Deserialization error", ex);
}
}
}
----
如果你希望按照顺序序列化多个对象的话,你可以使用 '_Packer_' 和 '_Unpacker_' 多个对象。
这是因为 '_MessagePack.write(Object)_' 和 '_read(byte[])_' 实际上每次都调用创建了 '_Packer_' 和 '_Unpacker_' 对象。
为了使用 '_Packer_' 和 '_Unpacker_' 对象,请调用 '_createPacker(OutputStream)_' 和 '_createUnpacker(InputStream)_'。
本代码可以 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Objects.java 中查看。
[source,java]
----
package com.insight.demo.msgpack;
import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.msgpack.packer.Packer;
import org.msgpack.unpacker.Unpacker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
/**
* MessagePack6Objects
*
* @author yhu
*/
public class MessagePack6Objects {
final Logger logger = LoggerFactory.getLogger(MessagePack6Objects.class);
/**
* MessageData Message Objects
*/
@Message // Annotation
public static class MessageData {
// public fields are serialized.
public String uuid;
public String name;
public double version;
}
/**
* Test MessagePack6Objects
*/
@Test
public void testMessagePack6Objects() {
logger.debug("MessagePack6Objects for Objects");
String uuid = UUID.randomUUID().toString();
// INIT OBJ
MessageData src1 = new MessageData();
src1.uuid = uuid;
src1.name = "MessagePack6-src1";
src1.version = 0.6;
MessageData src2 = new MessageData();
src2.uuid = uuid;
src2.name = "MessagePack6-src2";
src2.version = 10.6;
MessageData src3 = new MessageData();
src3.uuid = uuid;
src3.name = "MessagePack6-src3";
src3.version = 1.6;
try {
MessagePack msgPack = new MessagePack();
// Serialization
logger.debug("------ Serialization ------");
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = msgPack.createPacker(out);
packer.write(src1);
packer.write(src2);
packer.write(src3);
byte[] bytes = out.toByteArray();
logger.debug("Bytes Array Length: [{}]", bytes.length);
// Deserialization
logger.debug("------ Deserialization ------");
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
Unpacker unpacker = msgPack.createUnpacker(in);
MessageData dst1 = unpacker.read(MessageData.class);
MessageData dst2 = unpacker.read(MessageData.class);
MessageData dst3 = unpacker.read(MessageData.class);
logger.debug("Check Object for UUID: [{}]", dst1.uuid);
assertEquals(uuid, dst1.uuid);
} catch (Exception ex) {
logger.error("MessagePack Serialization And Deserialization error", ex);
}
}
}
----
=== 多种类型变量的序列化和反序列化serialization/deserialization
类 '_Packer/Unpacker_' 允许序列化和反序列化多种类型的变量,如后续程序所示。这个类启用序列化和反序列化多种类型的变量和序列化主要类型变量以及包装类,'_String_' 对象,
'_byte[]_' 对象,'_ByteBuffer 对象等的方法相似。
如上面提示的,你可以序列化和反序列化你自己的对象,前提是你自己的对象需要使用 '_@Message_' 注解。
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Types.java 中查看。
[source,java]
----
package com.insight.demo.msgpack;
import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.packer.Packer;
import org.msgpack.unpacker.Unpacker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
/**
* MessagePack6Types
*
* @author yhu
*/
public class MessagePack6Types {
final Logger logger = LoggerFactory.getLogger(MessagePack6Types.class);
/**
* Test MessagePack6Types
*/
@Test
public void testMessagePack6Types() {
logger.debug("testMessagePack6Types for Types");
MessagePack msgpack = new MessagePack();
try {
//
// Serialization
//
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = msgpack.createPacker(out);
// Serialize values of primitive types
packer.write(true); // boolean value
packer.write(10); // int value
packer.write(10.5); // double value
// Serialize objects of primitive wrapper types
packer.write(Boolean.TRUE);
packer.write(new Integer(10));
packer.write(new Double(10.5));
// Serialize various types of arrays
packer.write(new int[]{1, 2, 3, 4});
packer.write(new Double[]{10.5, 20.5});
packer.write(new String[]{"msg", "pack", "for", "java"});
packer.write(new byte[]{0x30, 0x31, 0x32}); // byte array
// Serialize various types of other reference values
packer.write("MessagePack"); // String object
packer.write(ByteBuffer.wrap(new byte[]{0x30, 0x31, 0x32})); // ByteBuffer object
packer.write(BigInteger.ONE); // BigInteger object
//
// Deserialization
//
byte[] bytes = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
Unpacker unpacker = msgpack.createUnpacker(in);
// to primitive values
boolean b = unpacker.readBoolean(); // boolean value
int i = unpacker.readInt(); // int value
double d = unpacker.readDouble(); // double value
// to primitive wrapper value
Boolean wb = unpacker.read(Boolean.class);
Integer wi = unpacker.read(Integer.class);
Double wd = unpacker.read(Double.class);
// to arrays
int[] ia = unpacker.read(int[].class);
Double[] da = unpacker.read(Double[].class);
String[] sa = unpacker.read(String[].class);
byte[] ba = unpacker.read(byte[].class);
// to String object, ByteBuffer object, BigInteger object, List object and Map object
String ws = unpacker.read(String.class);
ByteBuffer buf = unpacker.read(ByteBuffer.class);
BigInteger bi = unpacker.read(BigInteger.class);
} catch (Exception ex) {
logger.error("MessagePack Serialization And Deserialization error", ex);
}
}
}
----
方法 '_Packer#write()_' 允许序列化多种类型的数据。
类 '_Unpacker_' 针对反序列化二进制数据为主要变量,提供了一个反序列化方法。例如,你希望将二进制数据反序列化为 '_boolean_' (或者 '_int_') 数据类型,你可以使用 '_Unpacker_' 中的 '_readBoolean_' (或者 '_readInt_') 方法。
'_Unpacker_' 同时也为参考变量提供了一个读取的方法。这个方法允许为一个参考变量从二进制数据中进行反序列化。参考变量的定义为你将类型指定为一个参数。
例如,你希望反序列化二进制数据到 '_String_' (或者 '_byte[]_') 对象,你必须在调用 '_read(String.class)_' (或者 '_read(byte[].class)_') 方法的时候定义描述。
== List, Map 对象的序列化和反序列化serialization/deserialization
为了序列化原生的容器对象例如  '_List_' 和 '_Map_' 对象,你必须使用 '_Template_'。
'_Template_' 对象是 serializer 和 deserializer 的配对。例如,为了序列化一个 '_List_' 对象,在 '_List_' 对象中 '_Integer_' 对象为元素你可以使用下面的方法来创建一个模板对象Template object
[source,java]
----
Template listTmpl = Templates.tList(Templates.TInteger);
----
类 '_tList_' '_TInteger_' 是静态方法,字段为 '_Templates_'。
一个 '_List 和 '_Map_' 对象的用例如下显示:
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6Template.java 中查看。
[source,java]
----
package com.insight.demo.msgpack;
import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.packer.Packer;
import org.msgpack.template.Template;
import org.msgpack.unpacker.Unpacker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.msgpack.template.Templates.*;
/**
* MessagePack6Template
*
* @author yhu
*/
public class MessagePack6Template {
final Logger logger = LoggerFactory.getLogger(MessagePack6Template.class);
/**
* Test MessagePack6Template
*/
@Test
public void testMessagePack6Template() {
logger.debug("MessagePack6Template for Template");
MessagePack msgpack = new MessagePack();
try {
// Create templates for serializing/deserializing List and Map objects
Template<List<String>> listTmpl = tList(TString);
Template<Map<String, String>> mapTmpl = tMap(TString, TString);
//
// Serialization
//
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = msgpack.createPacker(out);
// Serialize List object
List<String> list = new ArrayList<String>();
list.add("msgpack");
list.add("for");
list.add("java");
packer.write(list); // List object
// Serialize Map object
Map<String, String> map = new HashMap<String, String>();
map.put("sadayuki", "furuhashi");
map.put("muga", "nishizawa");
packer.write(map); // Map object
//
// Deserialization
//
byte[] bytes = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
Unpacker unpacker = msgpack.createUnpacker(in);
// to List object
List<String> dstList = unpacker.read(listTmpl);
// to Map object
Map<String, String> dstMap = unpacker.read(mapTmpl);
} catch (Exception ex) {
logger.error("MessagePack Serialization And Deserialization error", ex);
}
}
}
----
=== 不使用注解annotations来序列化
如果你不能添加 @Message 到你的定义对象中但是你还是希望进行序列化。你可以使用 register 方法来在类中启用序列化对象。
如下的代码所示:
[source,java]
----
MessagePack msgpack = new MessagePack();
msgpack.register(MyMessage2.class);
----
例如如果 MyMessage2 类被包含到了外部的库中了。你没有办法比较容易的编辑源代码,添加 '_@Message_' 到源代码中。
register 方法能够允许为 MyMessage2 自动创建一个 serializer 和 deserializer 对。
你可以在执行方面后序列化对象 MyMessage2。
=== 可选字段
你可添加一个新的字段来保持可用性。在新字段中使用 '_@Optional_' 注解。
[source,java]
----
@Message
public static class MyMessage {
public String name;
public double version;
// new field
@Optional
public int flag = 0;
}
----
如果你尝试反序列化老版本数据的话,可选字段将会被忽略。
=== 动态类型
我们知道 Java 是一个静态类型的语言。通过输入 '_Value_' MessagePack能够实现动态的特性。 
'_Value_' 有方法来检查自己的类型('_isIntegerType()_', '_isArrayType()_', 等...),同时也转换为自己的类型 ('_asStringValue()_', '_convert(Template)_')。
本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6DynamicTyping.java 中查看。
[source,java]
----
package com.insight.demo.msgpack;
import org.junit.Test;
import org.msgpack.MessagePack;
import org.msgpack.type.Value;
import org.msgpack.unpacker.Converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import static org.msgpack.template.Templates.TString;
import static org.msgpack.template.Templates.tList;
/**
* MessagePack6Objects
*
* @author yhu
*/
public class MessagePack6DynamicTyping {
final Logger logger = LoggerFactory.getLogger(MessagePack6DynamicTyping.class);
/**
* Test MessagePack6Objects
*/
@Test
public void MessagePack6DynamicTyping() {
logger.debug("MessagePack6Objects for Objects");
// Create serialize objects.
List<String> src = new ArrayList<String>();
src.add("msgpack");
src.add("kumofs");
src.add("viver");
MessagePack msgpack = new MessagePack();
try {
// Serialize
byte[] raw = msgpack.write(src);
// Deserialize directly using a template
List<String> dst1 = msgpack.read(raw, tList(TString));
// Or, Deserialze to Value then convert type.
Value dynamic = msgpack.read(raw);
List<String> dst2 = new Converter(dynamic).read(tList(TString));
} catch (Exception ex) {
logger.error("MessagePack Serialization And Deserialization error", ex);
}
}
}
----