use of org.msgpack.value.Value in project LogHub by fbacchella.
the class TestMsgpack method testmapsimple.
@Test
public void testmapsimple() throws IOException, DecodeException {
Decoder d = new Msgpack();
Map<Value, Value> destination = new HashMap<>();
destination.put(ValueFactory.newString("a"), ValueFactory.newString("0"));
destination.put(ValueFactory.newString("b"), ValueFactory.newInteger(1));
destination.put(ValueFactory.newString("c"), ValueFactory.newBoolean(false));
Value[] subdestination = new Value[4];
subdestination[0] = ValueFactory.newString("0");
subdestination[1] = ValueFactory.newInteger(1);
subdestination[2] = ValueFactory.newFloat(2.0);
subdestination[3] = ValueFactory.newNil();
destination.put(ValueFactory.newString("d"), ValueFactory.newArray(subdestination));
Value v = ValueFactory.newMap(destination);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = MessagePack.newDefaultPacker(out);
packer.packValue(v);
packer.close();
byte[] packed = out.toByteArray();
Map<String, Object> e = d.decode(ConnectionContext.EMPTY, packed);
testContent(e);
}
use of org.msgpack.value.Value in project embulk by embulk.
the class JsonParserPlugin method run.
@Override
public void run(TaskSource taskSource, Schema schema, FileInput input, PageOutput output) {
PluginTask task = taskSource.loadTask(PluginTask.class);
final boolean stopOnInvalidRecord = task.getStopOnInvalidRecord();
// record column
final Column column = schema.getColumn(0);
try (PageBuilder pageBuilder = newPageBuilder(schema, output);
FileInputInputStream in = new FileInputInputStream(input)) {
while (in.nextFile()) {
boolean evenOneJsonParsed = false;
try (JsonParser.Stream stream = newJsonStream(in, task)) {
Value value;
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()));
}
pageBuilder.setJson(column, value);
pageBuilder.addRecord();
evenOneJsonParsed = true;
} catch (JsonRecordValidateException e) {
if (stopOnInvalidRecord) {
throw new DataException(String.format("Invalid record: %s", value.toJson()), e);
}
log.warn(String.format("Skipped record (%s): %s", e.getMessage(), value.toJson()));
}
}
} catch (IOException | JsonParseException e) {
if (Exec.isPreview() && evenOneJsonParsed) {
// ignore in preview if at least one JSON is already parsed.
break;
}
throw new DataException(e);
}
}
pageBuilder.finish();
}
}
use of org.msgpack.value.Value in project embulk by embulk.
the class TestJsonParser method testOrdinaryInteger.
@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());
}
use of org.msgpack.value.Value in project embulk by embulk.
the class TestJsonParser method testOrdinaryFloat.
@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());
}
use of org.msgpack.value.Value in project embulk by embulk.
the class TestJsonParserPlugin method useUnEscapeInvalidEscapeString.
@Test
public void useUnEscapeInvalidEscapeString() throws Exception {
ConfigSource config = this.config.deepCopy().set("invalid_string_escapes", "UNESCAPE");
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("a")));
}
Aggregations