Java Code Examples for org.msgpack.core.MessagePacker#packNil()
The following examples show how to use
org.msgpack.core.MessagePacker#packNil() .
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 | 4 votes |
private void packAny(Object obj, MessagePacker packer) throws IOException { if (obj == null) { packer.packNil(); return; } if (BElement.class.isInstance(obj)) { BElement element = (BElement) obj; if (element.isValue()) { packValue(element.asValue(), packer); return; } if (element.isArray()) { packArray(element.asArray(), packer); return; } if (element.isObject()) { packMap(element.asObject(), packer); return; } if (element.isReference()) { packAny(element.asReference().getReference(), packer); return; } throw new BeanSerializationException("Cannot serialize belement which instance of: " + element.getClass()); } Class<?> type; if (Collection.class.isInstance(obj) || (type = obj.getClass()).isArray()) { packArray(obj, packer); return; } if (Map.class.isInstance(obj)) { packMap(obj, packer); return; } if (PrimitiveUtils.isPrimitive(type)) { packValue(obj, packer); return; } packPojo(obj, packer); }
Example 2
Source File: MsgpackSerializer.java From gridgo with MIT License | 4 votes |
private void packValue(Object obj, MessagePacker packer) throws IOException { var value = BValue.of(obj); var type = value.getType(); switch (type) { case BOOLEAN: packer.packBoolean(value.getBoolean()); return; case BYTE: packer.packByte(value.getByte()); return; case CHAR: case SHORT: packer.packShort(value.getShort()); return; case DOUBLE: packer.packDouble(value.getDouble()); return; case FLOAT: packer.packFloat(value.getFloat()); return; case INTEGER: packer.packInt(value.getInteger()); return; case LONG: packer.packLong(value.getLong()); return; case NULL: packer.packNil(); return; case RAW: byte[] bytes = value.getRaw(); packer.packBinaryHeader(bytes.length); packer.addPayload(bytes); return; case STRING: packer.packString(value.getString()); return; case GENERIC_NUMBER: packer.packDouble(value.getDouble()); return; default: throw new BeanSerializationException("Cannot writeValue object type: " + type); } }
Example 3
Source File: MessagePackGenerator.java From jackson-dataformat-msgpack with Apache License 2.0 | 4 votes |
private void packValue(Object v) throws IOException { MessagePacker messagePacker = getMessagePacker(); if (v == null) { messagePacker.packNil(); } else if (v instanceof Integer) { messagePacker.packInt((Integer) v); } else if (v instanceof ByteBuffer) { ByteBuffer bb = (ByteBuffer) v; messagePacker.packBinaryHeader(bb.limit()); messagePacker.writePayload(bb); } else if (v instanceof String) { messagePacker.packString((String) v); } else if (v instanceof Float) { messagePacker.packFloat((Float) v); } else if (v instanceof Long) { messagePacker.packLong((Long) v); } else if (v instanceof StackItemForObject) { packObject((StackItemForObject) v); } else if (v instanceof StackItemForArray) { packArray((StackItemForArray) v); } else if (v instanceof Double) { messagePacker.packDouble((Double) v); } else if (v instanceof BigInteger) { messagePacker.packBigInteger((BigInteger) v); } else if (v instanceof BigDecimal) { // TODO throw new NotImplementedException(); } else if (v instanceof Boolean) { messagePacker.packBoolean((Boolean) v); } else { throw new IllegalArgumentException(v.toString()); } }
Example 4
Source File: MessagePackParserTest.java From jackson-dataformat-msgpack with Apache License 2.0 | 4 votes |
@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++)); }