Skip to content

Commit c716c4c

Browse files
committed
added new interfaces for deserializing data to objects with templates
1 parent 617ba7d commit c716c4c

File tree

1 file changed

+112
-25
lines changed

1 file changed

+112
-25
lines changed

src/main/java/org/msgpack/MessagePack.java

Lines changed: 112 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,13 @@ public BufferUnpacker createBufferUnpacker(ByteBuffer buffer) {
487487
* @return output byte array
488488
* @throws IOException
489489
*/
490-
public byte[] write(Object v) throws IOException {
490+
public <T> byte[] write(T v) throws IOException {
491491
BufferPacker pk = createBufferPacker();
492492
if (v == null) {
493493
pk.writeNil();
494494
} else {
495-
Template tmpl = registry.lookup(v.getClass());
495+
@SuppressWarnings("unchecked")
496+
Template<T> tmpl = registry.lookup(v.getClass());
496497
tmpl.write(pk, v);
497498
}
498499
return pk.toByteArray();
@@ -524,12 +525,13 @@ public <T> byte[] write(T v, Template<T> template) throws IOException {
524525
* serialized object
525526
* @throws IOException
526527
*/
527-
public void write(OutputStream out, Object v) throws IOException {
528+
public <T> void write(OutputStream out, T v) throws IOException {
528529
Packer pk = createPacker(out);
529530
if (v == null) {
530531
pk.writeNil();
531532
} else {
532-
Template tmpl = registry.lookup(v.getClass());
533+
@SuppressWarnings("unchecked")
534+
Template<T> tmpl = registry.lookup(v.getClass());
533535
tmpl.write(pk, v);
534536
}
535537
}
@@ -630,31 +632,56 @@ public Value read(InputStream in) throws IOException {
630632
* @param bytes
631633
* input byte array
632634
* @param v
633-
* deserialized object
634635
* @return
635636
* @throws IOException
636637
*/
637638
public <T> T read(byte[] bytes, T v) throws IOException {
638-
// TODO #MN if T is null?
639-
Template tmpl = registry.lookup(v.getClass());
640-
BufferUnpacker u = createBufferUnpacker(bytes);
641-
return (T) tmpl.read(u, v);
639+
@SuppressWarnings("unchecked")
640+
Template<T> tmpl = registry.lookup(v.getClass());
641+
return read(bytes, v, tmpl);
642+
}
643+
644+
/**
645+
* Deserializes byte array to object according to template.
646+
*
647+
* @since 0.6.0
648+
* @param bytes input byte array
649+
* @param tmpl template
650+
* @return
651+
* @throws IOException
652+
*/
653+
public <T> T read(byte[] bytes, Template<T> tmpl) throws IOException {
654+
return read(bytes, null, tmpl);
642655
}
643656

644657
/**
645658
* Deserializes byte array to object of specified class.
646659
*
647660
* @since 0.6.0
648-
* @param b
649-
* input {@link java.nio.ByteBuffer} object
661+
* @param bytes input byte array
650662
* @param c
651663
* @return
652664
* @throws IOException
653665
*/
654-
public <T> T read(byte[] b, Class<T> c) throws IOException {
666+
public <T> T read(byte[] bytes, Class<T> c) throws IOException {
667+
@SuppressWarnings("unchecked")
655668
Template<T> tmpl = registry.lookup(c);
656-
BufferUnpacker u = createBufferUnpacker(b);
657-
return tmpl.read(u, null);
669+
return read(bytes, null, tmpl);
670+
}
671+
672+
/**
673+
* Deserializes byte array to object according to specified template.
674+
*
675+
* @since 0.6.0
676+
* @param bytes input byte array
677+
* @param v
678+
* @param tmpl template
679+
* @return
680+
* @throws IOException
681+
*/
682+
public <T> T read(byte[] bytes, T v, Template<T> tmpl) throws IOException {
683+
BufferUnpacker u = createBufferUnpacker(bytes);
684+
return (T) tmpl.read(u, v);
658685
}
659686

660687
/**
@@ -668,10 +695,22 @@ public <T> T read(byte[] b, Class<T> c) throws IOException {
668695
* @throws IOException
669696
*/
670697
public <T> T read(ByteBuffer b, T v) throws IOException {
671-
// TODO #MN if T is null?
698+
@SuppressWarnings("unchecked")
672699
Template<T> tmpl = registry.lookup(v.getClass());
673-
BufferUnpacker u = createBufferUnpacker(b);
674-
return tmpl.read(u, v);
700+
return read(b, v, tmpl);
701+
}
702+
703+
/**
704+
* Deserializes buffer to object according to template.
705+
*
706+
* @since 0.6.0
707+
* @param b input buffer object
708+
* @param tmpl
709+
* @return
710+
* @throws IOException
711+
*/
712+
public <T> T read(ByteBuffer b, Template<T> tmpl) throws IOException {
713+
return read(b, null, tmpl);
675714
}
676715

677716
/**
@@ -681,11 +720,27 @@ public <T> T read(ByteBuffer b, T v) throws IOException {
681720
* @param b
682721
* @param c
683722
* @return
723+
* @throws IOException
684724
*/
685-
public <T> T read(ByteBuffer b, Class<T> c) {
725+
public <T> T read(ByteBuffer b, Class<T> c) throws IOException {
726+
@SuppressWarnings("unchecked")
686727
Template<T> tmpl = registry.lookup(c);
728+
return read(b, null, tmpl);
729+
}
730+
731+
/**
732+
* Deserializes buffer to object according to template.
733+
*
734+
* @since 0.6.0
735+
* @param b input buffer object
736+
* @param v
737+
* @param tmpl
738+
* @return
739+
* @throws IOException
740+
*/
741+
public <T> T read(ByteBuffer b, T v, Template<T> tmpl) throws IOException {
687742
BufferUnpacker u = createBufferUnpacker(b);
688-
return null;
743+
return tmpl.read(u, v);
689744
}
690745

691746
/**
@@ -699,8 +754,22 @@ public <T> T read(ByteBuffer b, Class<T> c) {
699754
* @throws IOException
700755
*/
701756
public <T> T read(InputStream in, T v) throws IOException {
757+
@SuppressWarnings("unchecked")
702758
Template<T> tmpl = registry.lookup(v.getClass());
703-
return tmpl.read(createUnpacker(in), v);
759+
return read(in, v, tmpl);
760+
}
761+
762+
/**
763+
* Deserializes input stream to object according to template.
764+
*
765+
* @since 0.6.0
766+
* @param in input stream
767+
* @param tmpl
768+
* @return
769+
* @throws IOException
770+
*/
771+
public <T> T read(InputStream in, Template<T> tmpl) throws IOException {
772+
return read(in, null, tmpl);
704773
}
705774

706775
/**
@@ -713,8 +782,24 @@ public <T> T read(InputStream in, T v) throws IOException {
713782
* @throws IOException
714783
*/
715784
public <T> T read(InputStream in, Class<T> c) throws IOException {
785+
@SuppressWarnings("unchecked")
716786
Template<T> tmpl = registry.lookup(c);
717-
return tmpl.read(createUnpacker(in), null);
787+
return read(in, null, tmpl);
788+
}
789+
790+
/**
791+
* Deserializes input stream to object according to template
792+
*
793+
* @since 0.6.0
794+
* @param in input stream
795+
* @param v
796+
* @param tmpl
797+
* @return
798+
* @throws IOException
799+
*/
800+
public <T> T read(InputStream in, T v, Template<T> tmpl) throws IOException {
801+
Unpacker u = createUnpacker(in);
802+
return tmpl.read(u, v);
718803
}
719804

720805
/**
@@ -727,7 +812,7 @@ public <T> T read(InputStream in, Class<T> c) throws IOException {
727812
* @throws IOException
728813
*/
729814
public <T> T convert(Value v, T to) throws IOException {
730-
// TODO #MN if T is null?
815+
@SuppressWarnings("unchecked")
731816
Template<T> tmpl = registry.lookup(to.getClass());
732817
return tmpl.read(new Converter(this, v), to);
733818
}
@@ -742,6 +827,7 @@ public <T> T convert(Value v, T to) throws IOException {
742827
* @throws IOException
743828
*/
744829
public <T> T convert(Value v, Class<T> c) throws IOException {
830+
@SuppressWarnings("unchecked")
745831
Template<T> tmpl = registry.lookup(c);
746832
return tmpl.read(new Converter(this, v), null);
747833
}
@@ -759,6 +845,7 @@ public <T> Value unconvert(T v) throws IOException {
759845
if (v == null) {
760846
pk.writeNil();
761847
} else {
848+
@SuppressWarnings("unchecked")
762849
Template<T> tmpl = registry.lookup(v.getClass());
763850
tmpl.write(pk, v);
764851
}
@@ -820,6 +907,7 @@ public void unregister() {
820907
* @param type
821908
* @return
822909
*/
910+
@SuppressWarnings("unchecked")
823911
public <T> Template<T> lookup(Class<T> type) {
824912
return registry.lookup(type);
825913
}
@@ -835,7 +923,7 @@ public <T> Template<T> lookup(Class<T> type) {
835923
* @throws IOException
836924
*/
837925
@Deprecated
838-
public static byte[] pack(Object v) throws IOException { // TODO IOException
926+
public static byte[] pack(Object v) throws IOException {
839927
return globalMessagePack.write(v);
840928
}
841929

@@ -862,8 +950,7 @@ public static void pack(OutputStream out, Object v) throws IOException {
862950
* @throws IOException
863951
*/
864952
@Deprecated
865-
public static <T> byte[] pack(T v, Template<T> template) throws IOException { // TODO
866-
// IOException
953+
public static <T> byte[] pack(T v, Template<T> template) throws IOException {
867954
return globalMessagePack.write(v, template);
868955
}
869956

0 commit comments

Comments
 (0)