mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-20 21:39:53 +01:00
java: adds MessagePack.register methods
This commit is contained in:
parent
0a345cb12b
commit
7ac4ad3e38
@ -21,6 +21,10 @@ import java.io.OutputStream;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
//import org.msgpack.util.codegen.DynamicTemplate; // FIXME
|
||||||
|
import org.msgpack.util.codegen.DynamicPacker;
|
||||||
|
import org.msgpack.util.codegen.DynamicConverter;
|
||||||
|
import org.msgpack.util.codegen.DynamicUnpacker;
|
||||||
|
|
||||||
public class MessagePack {
|
public class MessagePack {
|
||||||
public static byte[] pack(Object obj) {
|
public static byte[] pack(Object obj) {
|
||||||
@ -110,14 +114,43 @@ public class MessagePack {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//public static void register(Class<?> target); // TODO: auto-detect
|
public static void register(Class<?> target) { // auto-detect
|
||||||
|
// FIXME
|
||||||
|
//Template tmpl;
|
||||||
|
//if(List.isAssignableFrom(target)) {
|
||||||
|
//} else if(Set.isAssignableFrom(target)) {
|
||||||
|
//} else if(Map.isAssignableFrom(target)) {
|
||||||
|
//} else if(Collection.isAssignableFrom(target)) {
|
||||||
|
//} else if(BigInteger.isAssignableFrom(target)) {
|
||||||
|
//} else {
|
||||||
|
//}
|
||||||
|
|
||||||
//public static void register(Class<?> target, Template tmpl); // TODO
|
// FIXME
|
||||||
|
//Template tmpl = DynamicTemplate.create(target);
|
||||||
|
//register(target, tmpl);
|
||||||
|
|
||||||
//public static void registerPacker(Class<?> target, MessagePacker packer); // TODO
|
// FIXME
|
||||||
|
CustomPacker.register(target, DynamicPacker.create(target));
|
||||||
|
CustomConverter.register(target, DynamicConverter.create(target));
|
||||||
|
CustomUnpacker.register(target, DynamicUnpacker.create(target));
|
||||||
|
}
|
||||||
|
|
||||||
//public static void registerConverter(Class<?> target, MessageConverter converter); // TODO
|
public static void register(Class<?> target, Template tmpl) {
|
||||||
|
CustomPacker.register(target, tmpl);
|
||||||
|
CustomConverter.register(target, tmpl);
|
||||||
|
CustomUnpacker.register(target, tmpl);
|
||||||
|
}
|
||||||
|
|
||||||
//public static void registerUnpacker(Class<?> target, MessageUnpacker unpacker); // TODO
|
public static void registerPacker(Class<?> target, MessagePacker packer) {
|
||||||
|
CustomPacker.register(target, packer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void registerConverter(Class<?> target, MessageConverter converter) {
|
||||||
|
CustomConverter.register(target, converter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void registerUnpacker(Class<?> target, MessageUnpacker unpacker) {
|
||||||
|
CustomUnpacker.register(target, unpacker);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,23 +12,51 @@ import org.junit.Test;
|
|||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
public class TestMessagePackStaticMethods extends TestCase {
|
public class TestMessagePackStaticMethods extends TestCase {
|
||||||
|
public static class CustomClass {
|
||||||
|
public boolean bool;
|
||||||
|
public String str;
|
||||||
|
public List<Integer> list;
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj == this) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(obj instanceof CustomClass)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
CustomClass o = (CustomClass)obj;
|
||||||
|
return bool == o.bool && str.equals(o.str) && list.equals(o.list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "CustomClass<bool:"+bool+" str:"+str+" list:"+list+">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
MessagePack.register(CustomClass.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPackToByteArray() throws Exception {
|
public void testPackToByteArray() throws Exception {
|
||||||
byte[] a = MessagePack.pack("msgpack");
|
byte[] a = MessagePack.pack("msgpack");
|
||||||
byte[] b = MessagePack.pack((Object)1);
|
byte[] b = MessagePack.pack((Object)1);
|
||||||
byte[] c = MessagePack.pack((Object)null);
|
byte[] c = MessagePack.pack((Object)null);
|
||||||
byte[] d = MessagePack.pack(createStringList());
|
byte[] d = MessagePack.pack(createStringList());
|
||||||
|
byte[] e = MessagePack.pack(createCustomClass());
|
||||||
|
|
||||||
{
|
{
|
||||||
MessagePackObject aobj = MessagePack.unpack(a);
|
MessagePackObject aobj = MessagePack.unpack(a);
|
||||||
MessagePackObject bobj = MessagePack.unpack(b);
|
MessagePackObject bobj = MessagePack.unpack(b);
|
||||||
MessagePackObject cobj = MessagePack.unpack(c);
|
MessagePackObject cobj = MessagePack.unpack(c);
|
||||||
MessagePackObject dobj = MessagePack.unpack(d);
|
MessagePackObject dobj = MessagePack.unpack(d);
|
||||||
|
MessagePackObject eobj = MessagePack.unpack(e);
|
||||||
|
|
||||||
assertEquals(aobj, RawType.create("msgpack"));
|
assertEquals(aobj, RawType.create("msgpack"));
|
||||||
assertEquals(bobj, IntegerType.create(1));
|
assertEquals(bobj, IntegerType.create(1));
|
||||||
assertEquals(cobj, NilType.create());
|
assertEquals(cobj, NilType.create());
|
||||||
assertEquals(dobj, createStringList_dynamic());
|
assertEquals(dobj, createStringList_dynamic());
|
||||||
|
assertEquals(eobj, createCustomClass_dynamic());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +70,8 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
MessagePack.pack(cout, (Object)null);
|
MessagePack.pack(cout, (Object)null);
|
||||||
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
||||||
MessagePack.pack(dout, createStringList());
|
MessagePack.pack(dout, createStringList());
|
||||||
|
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(eout, createCustomClass());
|
||||||
|
|
||||||
{
|
{
|
||||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||||
@ -52,11 +82,14 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
MessagePackObject cobj = MessagePack.unpack(cin);
|
MessagePackObject cobj = MessagePack.unpack(cin);
|
||||||
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
||||||
MessagePackObject dobj = MessagePack.unpack(din);
|
MessagePackObject dobj = MessagePack.unpack(din);
|
||||||
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
|
MessagePackObject eobj = MessagePack.unpack(ein);
|
||||||
|
|
||||||
assertEquals(aobj, RawType.create("msgpack"));
|
assertEquals(aobj, RawType.create("msgpack"));
|
||||||
assertEquals(bobj, IntegerType.create(1));
|
assertEquals(bobj, IntegerType.create(1));
|
||||||
assertEquals(cobj, NilType.create());
|
assertEquals(cobj, NilType.create());
|
||||||
assertEquals(dobj, createStringList_dynamic());
|
assertEquals(dobj, createStringList_dynamic());
|
||||||
|
assertEquals(eobj, createCustomClass_dynamic());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +99,7 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
byte[] b = MessagePack.pack((Object)1, TInteger);
|
byte[] b = MessagePack.pack((Object)1, TInteger);
|
||||||
byte[] c = MessagePack.pack((Object)null, TAny);
|
byte[] c = MessagePack.pack((Object)null, TAny);
|
||||||
byte[] d = MessagePack.pack(createStringList(), tList(TString));
|
byte[] d = MessagePack.pack(createStringList(), tList(TString));
|
||||||
|
byte[] e = MessagePack.pack(createCustomClass(), tClass(CustomClass.class));
|
||||||
|
|
||||||
{
|
{
|
||||||
Object aobj = MessagePack.unpack(a, TString);
|
Object aobj = MessagePack.unpack(a, TString);
|
||||||
@ -73,20 +107,26 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
Object cobj_any = MessagePack.unpack(c, TAny);
|
Object cobj_any = MessagePack.unpack(c, TAny);
|
||||||
Object cobj_obj = MessagePack.unpack(c, tOptional(TAny));
|
Object cobj_obj = MessagePack.unpack(c, tOptional(TAny));
|
||||||
Object dobj = MessagePack.unpack(d, tList(TString));
|
Object dobj = MessagePack.unpack(d, tList(TString));
|
||||||
|
Object eobj = MessagePack.unpack(e, tClass(CustomClass.class));
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
assertEquals(aobj, "msgpack");
|
||||||
assertEquals(bobj, 1);
|
assertEquals(bobj, 1);
|
||||||
assertEquals(cobj_any, NilType.create());
|
assertEquals(cobj_any, NilType.create());
|
||||||
assertEquals(cobj_obj, null);
|
assertEquals(cobj_obj, null);
|
||||||
assertEquals(dobj, createStringList());
|
assertEquals(dobj, createStringList());
|
||||||
|
assertEquals(eobj, createCustomClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
String aobj = MessagePack.unpack(a, String.class);
|
String aobj = MessagePack.unpack(a, String.class);
|
||||||
Integer bobj = MessagePack.unpack(b, Integer.class);
|
Integer bobj = MessagePack.unpack(b, Integer.class);
|
||||||
Object cobj = MessagePack.unpack(c, Object.class);
|
Object cobj = MessagePack.unpack(c, Object.class);
|
||||||
|
CustomClass eobj = MessagePack.unpack(e, CustomClass.class);
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
assertEquals(aobj, "msgpack");
|
||||||
assertEquals(bobj, (Integer)1);
|
assertEquals(bobj, (Integer)1);
|
||||||
assertEquals(cobj, null);
|
assertEquals(cobj, null);
|
||||||
|
assertEquals(eobj, createCustomClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +140,8 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
MessagePack.pack(cout, (Object)null);
|
MessagePack.pack(cout, (Object)null);
|
||||||
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
||||||
MessagePack.pack(dout, createStringList());
|
MessagePack.pack(dout, createStringList());
|
||||||
|
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(eout, createCustomClass());
|
||||||
|
|
||||||
{
|
{
|
||||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||||
@ -112,12 +154,15 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
Object cobj_obj = MessagePack.unpack(cin_obj, tOptional(TAny));
|
Object cobj_obj = MessagePack.unpack(cin_obj, tOptional(TAny));
|
||||||
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
||||||
Object dobj = MessagePack.unpack(din, tList(TString));
|
Object dobj = MessagePack.unpack(din, tList(TString));
|
||||||
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
|
Object eobj = MessagePack.unpack(ein, tClass(CustomClass.class));
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
assertEquals(aobj, "msgpack");
|
||||||
assertEquals(bobj, 1);
|
assertEquals(bobj, 1);
|
||||||
assertEquals(cobj_any, NilType.create());
|
assertEquals(cobj_any, NilType.create());
|
||||||
assertEquals(cobj_obj, null);
|
assertEquals(cobj_obj, null);
|
||||||
assertEquals(dobj, createStringList());
|
assertEquals(dobj, createStringList());
|
||||||
|
assertEquals(eobj, createCustomClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -127,13 +172,17 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
Integer bobj = MessagePack.unpack(bin, Integer.class);
|
Integer bobj = MessagePack.unpack(bin, Integer.class);
|
||||||
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
||||||
Object cobj = MessagePack.unpack(cin, Object.class);
|
Object cobj = MessagePack.unpack(cin, Object.class);
|
||||||
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
|
Object eobj = MessagePack.unpack(ein, CustomClass.class);
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
assertEquals(aobj, "msgpack");
|
||||||
assertEquals(bobj, (Integer)1);
|
assertEquals(bobj, (Integer)1);
|
||||||
assertEquals(cobj, null);
|
assertEquals(cobj, null);
|
||||||
|
assertEquals(eobj, createCustomClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private List<String> createStringList() {
|
private List<String> createStringList() {
|
||||||
List<String> list = new ArrayList<String>();
|
List<String> list = new ArrayList<String>();
|
||||||
list.add("frsyuki");
|
list.add("frsyuki");
|
||||||
@ -149,5 +198,29 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
array[2] = RawType.create("gem-compile");
|
array[2] = RawType.create("gem-compile");
|
||||||
return ArrayType.create(array);
|
return ArrayType.create(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private CustomClass createCustomClass() {
|
||||||
|
CustomClass obj = new CustomClass();
|
||||||
|
obj.bool = true;
|
||||||
|
obj.str = "viver";
|
||||||
|
obj.list = new ArrayList<Integer>();
|
||||||
|
obj.list.add(1);
|
||||||
|
obj.list.add(2);
|
||||||
|
obj.list.add(3);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MessagePackObject createCustomClass_dynamic() {
|
||||||
|
MessagePackObject[] obj = new MessagePackObject[3];
|
||||||
|
obj[0] = BooleanType.create(true);
|
||||||
|
obj[1] = RawType.create("viver");
|
||||||
|
MessagePackObject[] list = new MessagePackObject[3];
|
||||||
|
list[0] = IntegerType.create(1);
|
||||||
|
list[1] = IntegerType.create(2);
|
||||||
|
list[2] = IntegerType.create(3);
|
||||||
|
obj[2] = ArrayType.create(list);
|
||||||
|
return ArrayType.create(obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user