@Test public void testCreatingJSONObject() throws Exception { String[] recipe = new String[] { "parse-as-json event", "columns-replace s/event_ "write-as-json-object coordinates latitude,longitude", "keep coordinates", "write-as-json-map location", "keep location" }; List<Row> rows = Arrays.asList( new Row("event", EVENT) ); rows = TestingRig.execute(recipe, rows); Assert.assertEquals(1, rows.size()); Assert.assertEquals("{\"coordinates\":{\"latitude\":-122.43345423,\"longitude\":37.234424223}}", rows.get(0).getValue("location")); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { JsonObject object = new JsonObject(); for (String col : columns) { Object value = row.getValue(col); if (value instanceof Integer) { object.addProperty(col, (Integer) value); } else if (value instanceof Long) { object.addProperty(col, (Long) value); } else if (value instanceof Number) { object.addProperty(col, (Number) value); } else if (value instanceof Float) { object.addProperty(col, (Float) value); } else if (value instanceof Double) { object.addProperty(col, (Double) value); } else if (value instanceof Short) { object.addProperty(col, (Short) value); } else if (value instanceof Character) { object.addProperty(col, Character.toString((Character) value)); } else if (value instanceof String) { object.addProperty(col, (String) value); } else if (value instanceof JsonElement) { object.add(col, (JsonElement) value); } else if (value instanceof JsonNull) { object.add(col, (JsonNull) value); } } row.addOrSet(column, object); } return rows; }
WriteAsJsonObject implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { JsonObject object = new JsonObject(); for (String col : columns) { Object value = row.getValue(col); if (value instanceof Integer) { object.addProperty(col, (Integer) value); } else if (value instanceof Long) { object.addProperty(col, (Long) value); } else if (value instanceof Number) { object.addProperty(col, (Number) value); } else if (value instanceof Float) { object.addProperty(col, (Float) value); } else if (value instanceof Double) { object.addProperty(col, (Double) value); } else if (value instanceof Short) { object.addProperty(col, (Short) value); } else if (value instanceof Character) { object.addProperty(col, Character.toString((Character) value)); } else if (value instanceof String) { object.addProperty(col, (String) value); } else if (value instanceof JsonElement) { object.add(col, (JsonElement) value); } else if (value instanceof JsonNull) { object.add(col, (JsonNull) value); } } row.addOrSet(column, object); } return rows; } }
WriteAsJsonObject implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { JsonObject object = new JsonObject(); for (String col : columns) { Object value = row.getValue(col); if (value instanceof Integer) { object.addProperty(col, (Integer) value); } else if (value instanceof Long) { object.addProperty(col, (Long) value); } else if (value instanceof Number) { object.addProperty(col, (Number) value); } else if (value instanceof Float) { object.addProperty(col, (Float) value); } else if (value instanceof Double) { object.addProperty(col, (Double) value); } else if (value instanceof Short) { object.addProperty(col, (Short) value); } else if (value instanceof Character) { object.addProperty(col, Character.toString((Character) value)); } else if (value instanceof String) { object.addProperty(col, (String) value); } else if (value instanceof JsonElement) { object.add(col, (JsonElement) value); } else if (value instanceof JsonNull) { object.add(col, (JsonNull) value); } } row.addOrSet(column, object); } return rows; }  }
WriteAsJsonObject implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { JsonObject object = new JsonObject(); for (String col : columns) { Object value = row.getValue(col); if (value instanceof Integer) { object.addProperty(col, (Integer) value); } else if (value instanceof Long) { object.addProperty(col, (Long) value); } else if (value instanceof Number) { object.addProperty(col, (Number) value); } else if (value instanceof Float) { object.addProperty(col, (Float) value); } else if (value instanceof Double) { object.addProperty(col, (Double) value); } else if (value instanceof Short) { object.addProperty(col, (Short) value); } else if (value instanceof Character) { object.addProperty(col, Character.toString((Character) value)); } else if (value instanceof String) { object.addProperty(col, (String) value); } else if (value instanceof JsonElement) { object.add(col, (JsonElement) value); } else if (value instanceof JsonNull) { object.add(col, (JsonNull) value); } } row.addOrSet(column, object); } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); }
WriteAsJsonObject implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { JsonObject object = new JsonObject(); for (String col : columns) { Object value = row.getValue(col); if (value instanceof Integer) { object.addProperty(col, (Integer) value); } else if (value instanceof Long) { object.addProperty(col, (Long) value); } else if (value instanceof Number) { object.addProperty(col, (Number) value); } else if (value instanceof Float) { object.addProperty(col, (Float) value); } else if (value instanceof Double) { object.addProperty(col, (Double) value); } else if (value instanceof Short) { object.addProperty(col, (Short) value); } else if (value instanceof Character) { object.addProperty(col, Character.toString((Character) value)); } else if (value instanceof String) { object.addProperty(col, (String) value); } else if (value instanceof JsonElement) { object.add(col, (JsonElement) value); } else if (value instanceof JsonNull) { object.add(col, (JsonNull) value); } } row.addOrSet(column, object); } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); static final String NAME; }