Skip to content
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
11 changes: 10 additions & 1 deletion src/main/java/org/msgpack/MessagePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import org.msgpack.template.Template;
import org.msgpack.template.TemplateRegistry;
Expand Down Expand Up @@ -67,6 +68,9 @@ public MessagePack() {
public MessagePack(MessagePack msgpack) {
registry = new TemplateRegistry(msgpack.registry);
}
protected MessagePack(TemplateRegistry registry) {
this.registry = registry;
}

/**
*
Expand Down Expand Up @@ -185,7 +189,9 @@ public <T> byte[] write(T v) throws IOException {
BufferPacker pk = createBufferPacker();
if (v == null) {
pk.writeNil();
} else {
} else if(v instanceof Value){
return write((Value)v);
}else {
@SuppressWarnings("unchecked")
Template<T> tmpl = registry.lookup(v.getClass());
tmpl.write(pk, v);
Expand Down Expand Up @@ -606,6 +612,9 @@ public <T> Template<T> lookup(Class<T> type) {
return registry.lookup(type);
}

public Template<?> lookup(Type type) {
return registry.lookup(type);
}
private static final MessagePack globalMessagePack = new MessagePack();

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/msgpack/packer/AbstractPacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ public Packer write(String o) throws IOException {
public Packer write(Object o) throws IOException {
if(o == null) {
writeNil();
} else if(o instanceof Value){
write((Value)o);
} else {
Template tmpl = msgpack.lookup(o.getClass());
tmpl.write(this, o);
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/msgpack/template/TemplateRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class TemplateRegistry {
*/
private TemplateRegistry() {
parent = null;
chain = new TemplateBuilderChain(this);
chain = createTemplateBuilderChain();
genericCache = new HashMap<Type, GenericTemplate>();
cache = new HashMap<Type, Template<Type>>();
registerTemplates();
Expand All @@ -86,11 +86,16 @@ public TemplateRegistry(TemplateRegistry registry) {
} else {
parent = new TemplateRegistry();
}
chain = new TemplateBuilderChain(this);
chain = createTemplateBuilderChain();
cache = new HashMap<Type, Template<Type>>();
genericCache = parent.genericCache;
}

protected TemplateBuilderChain createTemplateBuilderChain(){
return new TemplateBuilderChain(this);
}


public void setClassLoader(final ClassLoader cl) {
chain = new TemplateBuilderChain(this, cl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public TemplateBuilderChain(final TemplateRegistry registry, final ClassLoader c
reset(registry, cl);
}

private void reset(final TemplateRegistry registry, final ClassLoader cl) {
protected void reset(final TemplateRegistry registry, final ClassLoader cl) {
if (registry == null) {
throw new NullPointerException("registry is null");
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/msgpack/TestMessagePack01.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -17,6 +18,7 @@
import static org.msgpack.template.Templates.tList;
import static org.msgpack.template.Templates.tMap;
import org.msgpack.type.Value;
import org.msgpack.type.ValueFactory;


public class TestMessagePack01 {
Expand Down Expand Up @@ -701,4 +703,23 @@ public <K, V> void testMap(Map<K, V> v, Class<K> keyElementClass, Class<V> value
}
}
}

/*:*
* test pack org.msgpack.type.Value, but compiler recognize it as java.lang.Object
*/
@Test
public void testValuePassedAsObject() throws IOException {

MessagePack msgpack = new MessagePack();
String text = "This class is Value but...";
Object value = ValueFactory.createRawValue("This class is Value but...");

byte[] strValue = msgpack.write(value);
for(byte b : strValue){
System.out.print(String.format("%02x ", b));
}
// should be raw type
assertEquals(0xa0 + text.length(),0xff & strValue[0]);
}

}
31 changes: 31 additions & 0 deletions src/test/java/org/msgpack/TestPackConvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;
import org.junit.Test;
import org.msgpack.packer.Packer;
import org.msgpack.type.ValueFactory;
import org.msgpack.unpacker.BufferUnpacker;
import org.msgpack.unpacker.Converter;
import org.msgpack.type.Value;
Expand Down Expand Up @@ -303,4 +306,32 @@ public <K, V> void testMap(Map<K, V> v, Class<K> keyElementClass, Class<V> value
assertEquals(e.getValue(), value);
}
}
@Test
public void testPackValue() throws IOException {
MessagePack msgpack = new MessagePack();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = msgpack.createPacker(out);
String text = "This is Value";
Value value = ValueFactory.createRawValue(text);
packer.write(value);
byte[] bytes = out.toByteArray();
Assert.assertEquals(text.length() + 1,bytes.length);
Assert.assertEquals(0xa0 + text.length(), 0xff & bytes[0]);
}


@Test
public void testPackValuePassedAsObject() throws IOException{
MessagePack msgpack = new MessagePack();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = msgpack.createPacker(out);
String text = "This is Value";
Object value = ValueFactory.createRawValue(text);
packer.write(value); // passed as object
byte[] bytes = out.toByteArray();
Assert.assertEquals(text.length() + 1,bytes.length);
Assert.assertEquals(0xa0 + text.length(), 0xff & bytes[0]);

}

}