@Test public void testString() throws Exception { final JsonParser parser = new JsonParser(); final Value msgpackValue = parser.parse("\"foobar\""); assertFalse(msgpackValue.getValueType().isNumberType()); assertTrue(msgpackValue.getValueType().isStringType()); assertEquals("foobar", msgpackValue.asStringValue().toString()); }
@Test public void testOrdinaryFloat() throws Exception { final JsonParser parser = new JsonParser(); final Value msgpackValue = parser.parse("12345.12"); assertTrue(msgpackValue.getValueType().isNumberType()); assertTrue(msgpackValue.getValueType().isFloatType()); assertFalse(msgpackValue.getValueType().isIntegerType()); assertFalse(msgpackValue.getValueType().isStringType()); assertEquals(12345.12, msgpackValue.asFloatValue().toDouble(), 0.000000001); // Not sure this |toString| is to be tested... assertEquals("12345.12", msgpackValue.asFloatValue().toString()); }
@Test public void useSkipInvalidEscapeString() throws Exception { ConfigSource config = this.config.deepCopy().set("invalid_string_escapes", "SKIP"); transaction(config, fileInput( "{\"\\a\":\"b\"}\\" )); List<Object[]> records = Pages.toObjects(plugin.newSchema(), output.pages); assertEquals(1, records.size()); Object[] record = records.get(0); Map<Value, Value> map = ((Value) record[0]).asMapValue().map(); assertEquals(newString("b"), map.get(newString(""))); }
private void appendString(StringBuilder sb, Value value) { if (value.isRawValue()) { sb.append(value.toJson()); } else { sb.append(value.toString()); } }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof Value)) { return false; } Value v = (Value) o; if (!v.isFloatValue()) { return false; } return value == v.asFloatValue().toFloat(); }
while ((value = stream.next()) != null) { try { if (!value.isMapValue()) { throw new JsonRecordValidateException( String.format("A Json record must not represent map value but it's %s", value.getValueType().name())); } catch (JsonRecordValidateException e) { if (stopOnInvalidRecord) { throw new DataException(String.format("Invalid record in %s: %s", fileName, value.toJson()), e); logger.warn(String.format("Skipped record in %s (%s): %s", fileName, e.getMessage(), value.toJson()));
@Test public void ingestWithAck() throws IOException { FluentdIngester.Config config = new FluentdIngester.Config(); config.setAckResponseMode(true); Ingester ingester = new FluentdIngester(config, fluentdSender); ingester.ingest(TAG, ByteBuffer.wrap(DATA)); ArgumentCaptor<byte[]> ackTokenArgumentCaptor = ArgumentCaptor.forClass(byte[].class); verify(fluentdSender, times(1)) .sendWithAck(byteBuffersArgumentCaptor.capture(), ackTokenArgumentCaptor.capture()); List<ByteBuffer> byteBuffers = byteBuffersArgumentCaptor.getAllValues().get(0); byte[] ingested = getIngestedData(byteBuffers); MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(ingested); ImmutableArrayValue arrayValue = unpacker.unpackValue().asArrayValue(); assertEquals(3, arrayValue.size()); assertEquals(TAG, arrayValue.get(0).asStringValue().asString()); assertArrayEquals(DATA, arrayValue.get(1).asRawValue().asByteArray()); Map<Value, Value> options = arrayValue.get(2).asMapValue().map(); assertEquals(2, options.size()); assertEquals(DATA.length, options.get(ValueFactory.newString("size")).asIntegerValue().asInt()); String ackToken = options.get(ValueFactory.newString("chunk")).asRawValue().asString(); UUID uuidFromAckToken = UUID.fromString(ackToken); List<byte[]> ackTokenArgumentCaptorAllValues = ackTokenArgumentCaptor.getAllValues(); assertEquals(1, ackTokenArgumentCaptorAllValues.size()); assertEquals(uuidFromAckToken, UUID.fromString(new String(ackTokenArgumentCaptorAllValues.get(0), CHARSET))); }
private Object value(Value value) { switch (value.getValueType()) { case NIL: return null; case BOOLEAN: return value.asBooleanValue().getBoolean(); case INTEGER: return value.asIntegerValue().toLong(); case FLOAT: return value.asFloatValue().toFloat(); case STRING: return value.asStringValue().toString(); case BINARY: case ARRAY: case MAP: case EXTENSION: default: throw new UnsupportedOperationException("Unsupported column type: " + value.getValueType()); } } }
if( value == null || value.isNilValue() ) if( EnumType.class.isAssignableFrom( valueTypeClass ) ) return (T) Enum.valueOf( (Class) valueType.primaryType(), value.asStringValue().asString() ); return (T) deserializeCollection( module, (CollectionType) valueType, value.asArrayValue() ); return (T) deserializeMap( module, (MapType) valueType, value.asMapValue() ); value.asMapValue() );
String key = kv.getKey().asStringValue().toString(); Value val = kv.getValue(); if (key.equals("comment")) { assertEquals(1.23, val.asFloatValue().toFloat(), 0.000001); nameEventsLength.addAndGet(val.asRawValue().asString().length()); ageEventsSum.addAndGet(val.asIntegerValue().asInt());
private static void addCsvValue(Writer out, Value value) throws IOException { if (value.isStringValue()) { addCsvText(out, value.asStringValue().asString()); } else if (value.isNilValue()) { // write nothing } else { addCsvText(out, value.toJson()); } }
byte[] packedBytes = secondValue.asRawValue().asByteArray(); MessageUnpacker eventsUnpacker = MessagePack.newDefaultUnpacker(packedBytes); while (eventsUnpacker.hasNext()) { assertEquals(2, arrayValue.size()); Value timestampValue = arrayValue.get(0); MapValue mapValue = arrayValue.get(1).asMapValue(); long timestampMillis; if (timestampValue.isIntegerValue()) { timestampMillis = timestampValue.asIntegerValue().asLong() * 1000; else if (timestampValue.isExtensionValue()) { ExtensionValue extensionValue = timestampValue.asExtensionValue(); if (extensionValue.getType() != 0) { throw new IllegalArgumentException("Unexpected extension type: " + extensionValue.getType()); Map<Value, Value> map = rootValue.get(2).asMapValue().map(); assertEquals(map.get(KEY_OPTION_SIZE).asIntegerValue().asLong(), packedBytes.length); RawValue ackResponseToken = chunk.asRawValue(); ack(acceptSocket, ackResponseToken.asBinaryValue().asByteArray());
String newMapJsonPath(String rootPath, Value elementPathValue) { String elementPath = elementPathValue.asStringValue().asString(); String newPath = new StringBuilder(rootPath).append("['").append(elementPath).append("']").toString(); return newPath; }
public Value visit(String rootPath, Value value) { if (! shouldVisit(rootPath)) { return value; } if (value == null) { return null; } else if (value.isArrayValue()) { return visitArray(rootPath, value.asArrayValue()); } else if (value.isMapValue()) { return visitMap(rootPath, value.asMapValue()); } else { return value; } } }
private void assertRecord0(byte[] formatted, long expectedTime) throws IOException { try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(formatted)) { Map<Value, Value> map = unpacker.unpackValue().asMapValue().map(); assertEquals(4, map.size()); assertEquals(expectedTime, map.get(KEY_TIME).asNumberValue().toLong()); assertEquals("first", map.get(KEY_NAME).asStringValue().asString()); assertEquals(42, map.get(KEY_AGE).asNumberValue().toInt()); assertEquals("hello@world.com", map.get(KEY_EMAIL).asStringValue().asString()); } }
private Object doGuessDeserialize( ModuleDescriptor module, ValueType valueType, Value value ) throws IOException, ClassNotFoundException switch( value.getValueType() ) MapValue mapValue = value.asMapValue(); Optional<String> typeInfo = mapValue .entrySet().stream() .filter( entry -> entry.getKey().isStringValue() ) .map( entry -> + " (" + value.getValueType() + ")" );
@Test public void testOrdinaryInteger() throws Exception { final JsonParser parser = new JsonParser(); final Value msgpackValue = parser.parse("12345"); assertTrue(msgpackValue.getValueType().isNumberType()); assertTrue(msgpackValue.getValueType().isIntegerType()); assertFalse(msgpackValue.getValueType().isFloatType()); assertFalse(msgpackValue.getValueType().isStringType()); assertEquals(12345, msgpackValue.asIntegerValue().asInt()); }
private boolean isTruthy(Value firstCol) { // Anything that is not NULL and not FALSE or 0 is considered truthy. switch (firstCol.getValueType()) { case NIL: return false; case BOOLEAN: return firstCol.asBooleanValue().getBoolean(); case INTEGER: return firstCol.asIntegerValue().asLong() != 0; default: return true; } } }
@Override public Short deserialize( Value value, BiFunction<Value, ValueType, Object> deserialize ) { return value.asIntegerValue().asShort(); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Value)) { return false; } Value v = (Value) o; if (!v.isStringValue()) { return false; } if (v instanceof ImmutableStringValueImpl) { ImmutableStringValueImpl bv = (ImmutableStringValueImpl) v; return Arrays.equals(data, bv.data); } else { return Arrays.equals(data, v.asStringValue().asByteArray()); } }