Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hprose serialization support. #162

Merged
merged 5 commits into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions motan-extension/serialization-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.hprose</groupId>
<artifactId>hprose-java</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2009-2016 Weibo, Inc.
*
* 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.
*/

package com.weibo.api.motan.serialize;

import com.weibo.api.motan.codec.Serialization;
import com.weibo.api.motan.core.extension.SpiMeta;
import hprose.io.ByteBufferStream;
import hprose.io.HproseReader;
import hprose.io.HproseWriter;
import java.io.IOException;

/**
* hprose 序列化,不要求序列化的对象实现 java.io.Serializable 接口,
* 但序列化的字段需要是 public 的,或者定义有 public 的 setter 和 getter 方法。
*
* @author mabingyao
* @version 创建时间:2016-8-11
*
*/
@SpiMeta(name = "hprose")
public class HproseSerialization implements Serialization {

@Override
public byte[] serialize(Object data) throws IOException {
ByteBufferStream stream = null;
try {
stream = new ByteBufferStream();
HproseWriter writer = new HproseWriter(stream.getOutputStream());
writer.serialize(data);
byte[] result = stream.toArray();
return result;
}
finally {
if (stream != null) {
stream.close();
}
}
}

@SuppressWarnings("unchecked")
@Override
public <T> T deserialize(byte[] data, Class<T> clz) throws IOException {
return new HproseReader(data).unserialize(clz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2009-2016 Weibo, Inc.
#
# 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.
#

com.weibo.api.motan.serialize.HproseSerialization