tablesaw icon indicating copy to clipboard operation
tablesaw copied to clipboard

Added ability to use a custom mapper for serialization

Open howard-3 opened this issue 2 years ago • 3 comments

Thanks for contributing.

Description

Moved the object mapper instance to the json writer options class and allow it to be configurable.

Testing

Did you add a unit test? yes.

howard-3 avatar Jun 11 '23 01:06 howard-3

It's not clear to me that this really make anything easier? You can already convert a table to JSON without anything being built into TableSaw. E.g. here's some code I use to convert a table to JSON to be consumed by Chart.js:

  public static JsonNode toChartJsJson(int startIdx, String ticker, InstantColumn dates, DoubleColumn... columns) {
    ObjectNode data = jackson.createObjectNode();

    long[] timestamps = dates.asEpochMillisArray();

    ArrayNode datasets = jackson.createArrayNode();
    for (int i = 0; i < columns.length; i++) {
      ObjectNode dataset = jackson.createObjectNode();
      dataset.put("label", ticker != null && i == 0 ? ticker : columns[i].name());
      ArrayNode dataArray = jackson.createArrayNode();
      dataset.set("data", dataArray);
      for (int j = startIdx; j < dates.size(); j++) {
        ObjectNode dataPoint = jackson.createObjectNode();
        dataPoint.put("x", timestamps[j]);
        dataPoint.put("y", columns[i].get(j));
        dataArray.add(dataPoint);
      }
      dataset.put("backgroundColor", COLORS[i]);
      dataset.put("borderColor", COLORS[i]);
      dataset.put("pointRadius", 0);
      dataset.put("fill", false);
      dataset.put("lineTension", 0);
      dataset.put("borderWidth", 1);
      datasets.add(dataset);
    }

    data.set("datasets", datasets);
    return data;
  }

benmccann avatar Jun 11 '23 20:06 benmccann

So we use JsonWriter class to convert table into Json format. The code is simple 1-liner:

new JsonWriter().write(table, new Destination(stringWriter));

However JsonWriter contains a statically created object mapper with default date formats. Date formats tend to be different around the world. US prefers MM/dd/yyyy EU prefers dd/MM/yyyy some Asian countries prefer yyyy/MM/dd

To facilitate this we created our own custom JsonWriter, the code is similar what you have, however the only real change is the date format.

If we allow the use of a custom object mapper in the JsonWriteOptions, we don't need to implement our own writer class and reuse the JsonWriter class like

var asian = new JsonWriteOptions.Builder(new Destination(baos)).mapper(YYYY_MM_DD).build();
var eu = new JsonWriteOptions.Builder(new Destination(baos)).mapper(DD_MM_YYYY).build();
var us = ...
new JsonWriter().write(table, eu).write(...);

howard-3 avatar Jun 11 '23 21:06 howard-3