mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-05-29 15:34:08 +02:00
java: uses TemplateRepostry as default
This commit is contained in:
parent
a3cd13b399
commit
461b147897
@ -18,6 +18,7 @@
|
||||
package org.msgpack;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public class CustomMessage {
|
||||
public static void registerPacker(Class<?> target, MessagePacker packer) {
|
||||
@ -33,6 +34,7 @@ public class CustomMessage {
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, Template tmpl) {
|
||||
TemplateRegistry.register(target, tmpl);
|
||||
CustomPacker.register(target, tmpl);
|
||||
CustomConverter.register(target, tmpl);
|
||||
CustomUnpacker.register(target, tmpl);
|
||||
|
@ -21,10 +21,11 @@ import java.io.OutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import org.msgpack.buffer.VectoredByteBuffer;
|
||||
import org.msgpack.util.codegen.DynamicTemplate;
|
||||
import org.msgpack.util.codegen.DynamicOrdinalEnumTemplate;
|
||||
import org.msgpack.util.codegen.FieldList;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
import org.msgpack.template.TemplateBuilder;
|
||||
|
||||
public class MessagePack {
|
||||
public static byte[] pack(Object obj) {
|
||||
@ -148,6 +149,8 @@ public class MessagePack {
|
||||
}
|
||||
|
||||
public static void register(Class<?> target) { // auto-detect
|
||||
TemplateRegistry.register(target);
|
||||
|
||||
Template tmpl;
|
||||
if(target.isEnum()) {
|
||||
tmpl = DynamicOrdinalEnumTemplate.create(target);
|
||||
@ -166,6 +169,7 @@ public class MessagePack {
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, FieldList opts) {
|
||||
TemplateRegistry.register(target); // FIXME FieldList
|
||||
Template tmpl = DynamicTemplate.create(target, opts);
|
||||
CustomPacker.register(target, tmpl);
|
||||
CustomConverter.register(target, tmpl);
|
||||
@ -173,6 +177,7 @@ public class MessagePack {
|
||||
}
|
||||
|
||||
public static void register(Class<?> target, Template tmpl) {
|
||||
TemplateRegistry.register(target, tmpl);
|
||||
CustomPacker.register(target, tmpl);
|
||||
CustomConverter.register(target, tmpl);
|
||||
CustomUnpacker.register(target, tmpl);
|
||||
|
@ -21,14 +21,9 @@ 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;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
public abstract class MessagePackObject implements Cloneable, MessagePackable {
|
||||
static {
|
||||
Templates.load();
|
||||
}
|
||||
|
||||
public boolean isNil() {
|
||||
return false;
|
||||
}
|
||||
@ -156,8 +151,10 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable {
|
||||
}
|
||||
|
||||
public <T> T convert(Class<T> klass, T to) throws MessageTypeException {
|
||||
// FIXME nullable?
|
||||
return (T)convert(new NullableTemplate(new ClassTemplate(klass)), to);
|
||||
if(isNil()) {
|
||||
return null;
|
||||
}
|
||||
return (T)convert(TemplateRegistry.lookup(klass), to);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,11 +25,7 @@ import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.msgpack.annotation.MessagePackDelegate;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.annotation.MessagePackOrdinalEnum;
|
||||
import org.msgpack.util.codegen.DynamicTemplate;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
/**
|
||||
* Packer enables you to serialize objects into OutputStream.
|
||||
@ -48,10 +44,6 @@ import org.msgpack.util.codegen.DynamicTemplate;
|
||||
* You can serialize objects that implements {@link MessagePackable} interface.
|
||||
*/
|
||||
public class Packer {
|
||||
static {
|
||||
Templates.load();
|
||||
}
|
||||
|
||||
public static void load() { }
|
||||
|
||||
protected byte[] castBytes = new byte[9];
|
||||
@ -525,7 +517,8 @@ public class Packer {
|
||||
}
|
||||
|
||||
public Packer pack(Object o) throws IOException {
|
||||
Templates.TAny.pack(this, o);
|
||||
if(o == null) { return packNil(); }
|
||||
TemplateRegistry.lookup(o.getClass()).pack(this, o);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -20,8 +20,6 @@ package org.msgpack;
|
||||
import org.msgpack.template.*;
|
||||
|
||||
public class Templates {
|
||||
public static void load() { }
|
||||
|
||||
public static Template tNullable(Template elementTemplate) {
|
||||
return new NullableTemplate(elementTemplate);
|
||||
}
|
||||
@ -46,7 +44,11 @@ public class Templates {
|
||||
}
|
||||
|
||||
public static Template tClass(Class target) {
|
||||
return new ClassTemplate(target);
|
||||
Template tmpl = TemplateRegistry.lookup(target);
|
||||
if(tmpl == null) {
|
||||
// FIXME
|
||||
}
|
||||
return tmpl;
|
||||
}
|
||||
|
||||
public static final Template TByte = ByteTemplate.getInstance();
|
||||
|
@ -23,8 +23,7 @@ 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;
|
||||
import org.msgpack.template.TemplateRegistry;
|
||||
|
||||
/**
|
||||
* Unpacker enables you to deserialize objects from stream.
|
||||
@ -105,10 +104,6 @@ import org.msgpack.template.NullableTemplate;
|
||||
* </pre>
|
||||
*/
|
||||
public class Unpacker implements Iterable<MessagePackObject> {
|
||||
static {
|
||||
Templates.load();
|
||||
}
|
||||
|
||||
// buffer:
|
||||
// +---------------------------------------------+
|
||||
// | [object] | [obje| unparsed ... | unused ...|
|
||||
@ -578,6 +573,19 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
// return unpackObject();
|
||||
//}
|
||||
|
||||
final public <T> T unpack(T to) throws IOException, MessageTypeException {
|
||||
return unpack((Class<T>)to.getClass(), to);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass) throws IOException, MessageTypeException {
|
||||
return unpack(klass, null);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass, T to) throws IOException, MessageTypeException {
|
||||
if(tryUnpackNull()) { return null; }
|
||||
return (T)TemplateRegistry.lookup(klass).unpack(this, to);
|
||||
}
|
||||
|
||||
final public Object unpack(Template tmpl) throws IOException, MessageTypeException {
|
||||
return unpack(tmpl, null);
|
||||
}
|
||||
@ -585,18 +593,5 @@ public class Unpacker implements Iterable<MessagePackObject> {
|
||||
final public <T> T unpack(Template tmpl, T to) throws IOException, MessageTypeException {
|
||||
return (T)tmpl.unpack(this, to);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass) throws IOException, MessageTypeException {
|
||||
return unpack(klass, null);
|
||||
}
|
||||
|
||||
final public <T> T unpack(T to) throws IOException, MessageTypeException {
|
||||
return unpack((Class<T>)to.getClass(), to);
|
||||
}
|
||||
|
||||
final public <T> T unpack(Class<T> klass, T to) throws IOException, MessageTypeException {
|
||||
// FIXME nullable?
|
||||
return (T)unpack(new NullableTemplate(new ClassTemplate(klass)), to);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user