Merge branch 'master' of git@github.com:msgpack/msgpack

This commit is contained in:
Muga Nishizawa 2010-11-09 09:28:38 +09:00
commit 56ad6915d0
20 changed files with 489 additions and 149 deletions

View file

@ -51,7 +51,7 @@ public class MessagePack {
return out.toByteArray();
}
public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException {
public static void pack(OutputStream out, Object obj, Template tmpl) throws IOException, MessageTypeException {
new Packer(out).pack(obj, tmpl);
}
@ -86,13 +86,9 @@ public class MessagePack {
}
}
public static MessagePackObject unpack(InputStream in) {
public static MessagePackObject unpack(InputStream in) throws IOException {
Unpacker pac = new Unpacker(in);
try {
return pac.unpackObject();
} catch (IOException e) {
throw new RuntimeException(e);
}
return pac.unpackObject();
}
public static Object unpack(InputStream in, Template tmpl) throws IOException, MessageTypeException {

View file

@ -21,6 +21,8 @@ import java.util.List;
import java.util.Set;
import java.util.Map;
import java.math.BigInteger;
import org.msgpack.template.ClassTemplate;
import org.msgpack.template.NullableTemplate;
public abstract class MessagePackObject implements Cloneable, MessagePackable {
static {
@ -140,5 +142,10 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable {
public Object convert(Template tmpl) throws MessageTypeException {
return tmpl.convert(this);
}
public <T> T convert(Class<T> klass) throws MessageTypeException {
// FIXME nullable?
return (T)convert(new NullableTemplate(new ClassTemplate(klass)));
}
}

View file

@ -23,19 +23,16 @@ public class Templates {
public static void load() { }
public static Template tNullable(Template elementTemplate) {
return new NullableTemplate(elementTemplate);
}
public static final Template TAny = AnyTemplate.getInstance();
public static Template tAny() {
return TAny;
}
public static Template tOptional(Template elementTemplate) {
return new OptionalTemplate(elementTemplate);
}
public static Template tOptional(Template elementTemplate, Object defaultObject) {
return new OptionalTemplate(elementTemplate, defaultObject);
}
public static Template tList(Template elementTemplate) {
return new ListTemplate(elementTemplate);

View file

@ -23,6 +23,8 @@ import java.io.IOException;
import java.util.Iterator;
import java.nio.ByteBuffer;
import java.math.BigInteger;
import org.msgpack.template.ClassTemplate;
import org.msgpack.template.NullableTemplate;
/**
* Unpacker enables you to deserialize objects from stream.
@ -581,8 +583,8 @@ public class Unpacker implements Iterable<MessagePackObject> {
}
final public <T> T unpack(Class<T> klass) throws IOException, MessageTypeException {
// FIXME optional?
return (T)unpack(Templates.tOptional(Templates.tClass(klass)));
// FIXME nullable?
return (T)unpack(new NullableTemplate(new ClassTemplate(klass)));
}
}

View file

@ -0,0 +1,28 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
//
// 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 org.msgpack.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MessagePackNullable {
}

View file

@ -0,0 +1,60 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
//
// 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 org.msgpack.template;
import java.nio.ByteBuffer;
import java.io.IOException;
import org.msgpack.*;
public class ByteBufferTemplate implements Template {
private ByteBufferTemplate() { }
public void pack(Packer pk, Object target) throws IOException {
byte[] bytes = byteBufferToByteArray((ByteBuffer)target);
pk.packByteArray(bytes);
}
private static byte[] byteBufferToByteArray(ByteBuffer b) {
if (b.hasArray() && b.position() == 0 && b.arrayOffset() == 0
&& b.remaining() == b.capacity()) {
return b.array();
} else {
int size = b.remaining();
byte[] bytes = new byte[size];
System.arraycopy(b.array(), b.arrayOffset() + b.position(), bytes, 0, size);
return bytes;
}
}
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
byte[] bytes = pac.unpackByteArray();
return ByteBuffer.wrap(bytes);
}
public Object convert(MessagePackObject from) throws MessageTypeException {
byte[] bytes = from.asByteArray();
return ByteBuffer.wrap(bytes);
}
static public ByteBufferTemplate getInstance() {
return instance;
}
static final ByteBufferTemplate instance = new ByteBufferTemplate();
}

View file

@ -0,0 +1,56 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
//
// 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 org.msgpack.template;
import java.io.IOException;
import org.msgpack.*;
public class NullableTemplate implements Template {
private Template elementTemplate;
public NullableTemplate(Template elementTemplate) {
this.elementTemplate = elementTemplate;
}
public Template getElementTemplate() {
return elementTemplate;
}
public void pack(Packer pk, Object target) throws IOException {
if(target == null) {
pk.packNil();
} else {
elementTemplate.pack(pk, target);
}
}
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
if(pac.tryUnpackNull()) {
return null;
}
return elementTemplate.unpack(pac);
}
public Object convert(MessagePackObject from) throws MessageTypeException {
if(from.isNil()) {
return null;
}
return elementTemplate.convert(from);
}
}

View file

@ -39,7 +39,9 @@ import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Unpacker;
import org.msgpack.annotation.MessagePackOptional;
import org.msgpack.annotation.MessagePackNullable;
import org.msgpack.template.OptionalTemplate;
import org.msgpack.template.NullableTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -253,7 +255,6 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
}
Template createTemplate(Field field) {
boolean isOptional = isAnnotated(field, MessagePackOptional.class);
Class<?> c = field.getType();
Template tmpl = null;
if (List.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c)) {
@ -261,12 +262,15 @@ class DynamicCodeGen extends DynamicCodeGenBase implements Constants {
} else {
tmpl = createTemplate(c);
}
if (isOptional) {
// for pack
if (isAnnotated(field, MessagePackOptional.class)) {
// @Optional types
return new OptionalTemplate(tmpl);
} else {
return tmpl;
}
if (!c.isPrimitive() && isAnnotated(field, MessagePackNullable.class)) {
// @Nullable reference types
return new NullableTemplate(tmpl);
}
return tmpl;
}
private boolean isAnnotated(Field field, Class<? extends Annotation> with) {