Java Code Examples for org.msgpack.core.MessagePacker#packArrayHeader()

The following examples show how to use org.msgpack.core.MessagePacker#packArrayHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MsgpackSerializer.java    From gridgo with MIT License 6 votes vote down vote up
private void packArray(Object obj, MessagePacker packer) throws IOException {
    if (BArray.class.isInstance(obj)) {
        BArray array = (BArray) obj;
        packer.packArrayHeader(array.size());
        for (var entry : array) {
            packAny(entry, packer);
        }
        return;
    }

    packer.packArrayHeader(ArrayUtils.length(obj));
    ArrayUtils.foreach(obj, ele -> {
        try {
            packAny(ele, packer);
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
    });
}
 
Example 2
Source File: MessagePackGenerator.java    From jackson-dataformat-msgpack with Apache License 2.0 5 votes vote down vote up
private void packArray(StackItemForArray stackItem) throws IOException {
    List<Object> values = stackItem.getValues();

    MessagePacker messagePacker = getMessagePacker();
    messagePacker.packArrayHeader(values.size());

    for (int i = 0; i < values.size(); i++) {
        Object v = values.get(i);
        packValue(v);
    }
}
 
Example 3
Source File: MsgpackGenerator.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(MessagePacker packer) throws IOException
{
    int size = list.size();
    packer.packArrayHeader(size);
    for (int i = 0; i != size; i++)
    {
        list.get(i).writeTo(packer);
    }
}
 
Example 4
Source File: MessagePackParserTest.java    From jackson-dataformat-msgpack with Apache License 2.0 4 votes vote down vote up
@Test
public void testParserShouldReadArray() throws IOException {
    MessagePacker packer = new MessagePacker(new OutputStreamBufferOutput(out));
    packer.packArrayHeader(10);
    // #1
    packer.packArrayHeader(3);
    {
        packer.packLong(Long.MAX_VALUE);
        packer.packNil();
        packer.packString("FOO BAR");
    }
    // #2
    packer.packString("str");
    // #3
    packer.packInt(Integer.MAX_VALUE);
    // #4
    packer.packLong(Long.MIN_VALUE);
    // #5
    packer.packFloat(Float.MAX_VALUE);
    // #6
    packer.packDouble(Double.MIN_VALUE);
    // #7
    BigInteger bi = new BigInteger(Long.toString(Long.MAX_VALUE));
    bi = bi.add(BigInteger.ONE);
    packer.packBigInteger(bi);
    // #8
    byte[] bytes = new byte[]{(byte) 0xFF, (byte) 0xFE, 0x01, 0x00};
    packer.packBinaryHeader(bytes.length);
    packer.writePayload(bytes);
    // #9
    packer.packMapHeader(2);
    {
        packer.packString("child_map_name");
        packer.packString("komamitsu");
        packer.packString("child_map_age");
        packer.packInt(42);
    }
    // #10
    packer.packBoolean(true);

    packer.flush();

    bytes = out.toByteArray();

    TypeReference<List<Object>> typeReference = new TypeReference<List<Object>>(){};
    List<Object> array = objectMapper.readValue(bytes, typeReference);
    assertEquals(10, array.size());
    int i = 0;
    // #1
    List<Object> childArray = (List<Object>) array.get(i++);
    {
        int j = 0;
        assertEquals(Long.MAX_VALUE, childArray.get(j++));
        assertEquals(null, childArray.get(j++));
        assertEquals("FOO BAR", childArray.get(j++));
    }
    // #2
    assertEquals("str", array.get(i++));
    // #3
    assertEquals(Integer.MAX_VALUE, array.get(i++));
    // #4
    assertEquals(Long.MIN_VALUE, array.get(i++));
    // #5
    assertEquals(Float.MAX_VALUE, (Double)array.get(i++), 0.001f);
    // #6
    assertEquals(Double.MIN_VALUE, (Double)array.get(i++), 0.001f);
    // #7
    assertEquals(bi, array.get(i++));
    // #8
    byte[] bs = (byte[]) array.get(i++);
    assertEquals(4, bs.length);
    assertEquals((byte)0xFF, bs[0]);
    assertEquals((byte)0xFE, bs[1]);
    assertEquals((byte)0x01, bs[2]);
    assertEquals((byte)0x00, bs[3]);
    // #9
    Map<String, Object> childMap = (Map<String, Object>) array.get(i++);
    {
        assertEquals(2, childMap.keySet().size());
        for (Map.Entry<String, Object> entry : childMap.entrySet()) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (k.equals("child_map_name")) {
                assertEquals("komamitsu", v);
            }
            else if (k.equals("child_map_age")) {
                assertEquals(42, v);
            }
        }
    }
    // #10
    assertEquals(true, array.get(i++));
}