Skip to content

Commit 42bcb20

Browse files
committed
updated MessagePack document
1 parent 64cc860 commit 42bcb20

1 file changed

Lines changed: 65 additions & 17 deletions

File tree

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

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* serialization.
5858
* </p>
5959
*
60-
* <h3>Install</h3>
60+
* <h3>How to Install</h3>
6161
*
6262
* <p>
6363
* The official Maven2 repository for MessagePack for Java is located here. <a
@@ -201,9 +201,8 @@
201201
* values of various types as follows. They enable serializing/deserializing
202202
* values of various types like values of primitive types, values of primitive
203203
* wrapper classes, <code>String</code> objects, <code>byte[]</code> objects,
204-
* <code>ByteBuffer</code> objects, <code>List</code> objects, </code>Map</code>
205-
* objects and so on. As mentioned above, they also enable
206-
* serializing/deserizing objects of your own classes annotated by
204+
* <code>ByteBuffer</code> objects and so on. As mentioned above, they also
205+
* enable serializing/deserizing objects of your own classes annotated by
207206
* <code>@Message</code>.
208207
* </p>
209208
*
@@ -239,15 +238,6 @@
239238
* packer.write(&quot;MesagePack&quot;); // String object
240239
* packer.write(java.nio.ByteBufer.wrap(new byte[] { 0x30, 0x31, 0x32 })); // ByteBuffer object
241240
* packer.write(java.math.BigInteger.ONE); // BigInteger object
242-
* java.util.List&lt;String&gt; list = new java.util.ArrayList&lt;String&gt;();
243-
* list.add(&quot;msgpack&quot;);
244-
* list.add(&quot;for&quot;);
245-
* list.add(&quot;java&quot;);
246-
* packer.write(list); // List object
247-
* java.util.Map&lt;String, String&gt; map = new java.util.HashMap&lt;String, String&gt;();
248-
* map.put(&quot;sadayuki&quot;, &quot;furuhashi&quot;);
249-
* map.put(&quot;muga&quot;, &quot;nishizawa&quot;);
250-
* packer.write(map); // Map object
251241
*
252242
* //
253243
* // deserialization
@@ -277,10 +267,6 @@
277267
* String ws = unpacker.read(String.class);
278268
* java.nio.ByteBuffer buf = unpacker.read(java.nio.ByteBuffer.class);
279269
* java.math.BigInteger bi = unpacker.read(java.math.BigInteger.class);
280-
* java.util.List&lt;String&gt; dstList = new java.util.ArrayList&lt;String&gt;();
281-
* dstList = unpacker.read(dstList);
282-
* java.util.Map&lt;String, String&gt; dstMap = new java.util.HashMap&lt;String, String&gt;();
283-
* dstMap = unpacker.read(dstMap);
284270
* }
285271
* }
286272
* </pre>
@@ -307,6 +293,68 @@
307293
* <code>read(byte[].class)</code>) method.
308294
* </p>
309295
*
296+
* <h3><code>List</code>, <code>Map</code> objects Serialization/Deserialization
297+
* </h3>
298+
*
299+
* <p>
300+
* You can serialize <code>List</code> and <code>Map</code> objects with
301+
* {@link org.msgpack.template.Template} objects, which are pairs of
302+
* serializer/deserializer. The type of elements in <code>List</code> object
303+
* needs to be specified to <code>Template</code> object for effective
304+
* serialization/deserialization. Types of keys and values in <code>Map</code>
305+
* object also need to be specified to <code>Template</code> object.
306+
* </p>
307+
*
308+
* <pre>
309+
* import static org.msgpack.template.Templates.tList;
310+
* import static org.msgpack.template.Templates.tMap;
311+
* import static org.msgpack.template.Templates.TString;
312+
*
313+
* public class Main4 {
314+
* public static void main(String[] args) {
315+
* MessagePack msgpack = new MessagePack();
316+
*
317+
* // create templates for serializing/deserializing List and Map objects
318+
* org.msgpack.template.Template listTmpl = tList(TString);
319+
* org.msgpack.template.Template mapTmpl = tMap(TString, TString);
320+
*
321+
* //
322+
* // serialization
323+
* //
324+
*
325+
* ByteArrayOutputStream out = new ByteArrayOutputStream();
326+
* Packer packer = msgpack.createPacker(out);
327+
*
328+
* // serialize List object
329+
* java.util.List&lt;String&gt; list = new java.util.ArrayList&lt;String&gt;();
330+
* list.add(&quot;msgpack&quot;);
331+
* list.add(&quot;for&quot;);
332+
* list.add(&quot;java&quot;);
333+
* packer.write(list, listTmpl); // List object
334+
*
335+
* // serialize Map object
336+
* java.util.Map&lt;String, String&gt; map = new java.util.HashMap&lt;String, String&gt;();
337+
* map.put(&quot;sadayuki&quot;, &quot;furuhashi&quot;);
338+
* map.put(&quot;muga&quot;, &quot;nishizawa&quot;);
339+
* packer.write(map, mapTmpl); // Map object
340+
*
341+
* //
342+
* // deserialization
343+
* //
344+
*
345+
* byte[] bytes = out.toByteArray();
346+
* ByteArrayInputStream in = new ByteArrayInputStream(bytes);
347+
* Unpacker unpacker = msgpack.createUnpacker(in);
348+
*
349+
* // to List object
350+
* dstList = unpacker.read(dstList, listTmpl);
351+
*
352+
* // to Map object
353+
* dstMap = unpacker.read(dstMap, mapTmpl);
354+
* }
355+
* }
356+
* </pre>
357+
*
310358
* <h3>Without Annotations</h3>
311359
*
312360
* <p>

0 commit comments

Comments
 (0)