@Test public void testWriteToJson() throws Exception { String[] directives = new String[] { "write-as-json-map test", "keep test" }; JSONObject o = new JSONObject(); o.put("a", 1); o.put("b", "2"); String url = "http: List<Row> rows = Arrays.asList( new Row().add("int", 1).add("string", "this is string"), new Row("url", url) .add("o", o) .add("i1", 1) .add("i2", (double) 1.8f) ); rows = TestingRig.execute(directives, rows); Assert.assertTrue(rows.size() == 2); Type stringStringMapType = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> map = GSON.fromJson((String) rows.get(0).getValue("test"), stringStringMapType); Assert.assertEquals(ImmutableMap.of("string", "this is string", "int", "1"), map); JsonObject jsonObject = new JsonParser().parse((String) rows.get(1).getValue("test")).getAsJsonObject(); Assert.assertEquals(1, jsonObject.get("i1").getAsInt()); Assert.assertEquals(1.8f, jsonObject.get("i2").getAsFloat(), 0.001); Assert.assertEquals(url, jsonObject.get("url").getAsString()); Assert.assertEquals(GSON.toJson(o), jsonObject.get("o").toString()); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { Map<String, Object> toJson = new HashMap<>(); for (Pair<String, Object> entry : row.getFields()) { toJson.put(entry.getFirst(), entry.getSecond()); } row.addOrSet(column, gson.toJson(toJson)); } return rows; }
WriteAsJsonMap implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { Map<String, Object> toJson = new HashMap<>(); for (Pair<String, Object> entry : row.getFields()) { toJson.put(entry.getFirst(), entry.getSecond()); } row.addOrSet(column, gson.toJson(toJson)); } return rows; } }
WriteAsJsonMap implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { Map<String, Object> toJson = new HashMap<>(); for (Pair<String, Object> entry : row.getFields()) { toJson.put(entry.getFirst(), entry.getSecond()); } row.addOrSet(column, gson.toJson(toJson)); } return rows; }  }
WriteAsJsonMap implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { Map<String, Object> toJson = new HashMap<>(); for (Pair<String, Object> entry : row.getFields()) { toJson.put(entry.getFirst(), entry.getSecond()); } row.addOrSet(column, gson.toJson(toJson)); } 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(); }
WriteAsJsonMap implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { Map<String, Object> toJson = new HashMap<>(); for (Pair<String, Object> entry : row.getFields()) { toJson.put(entry.getFirst(), entry.getSecond()); } row.addOrSet(column, gson.toJson(toJson)); } 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; }