Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to use a custom mapper for serialization #1217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

howard-3
Copy link

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.

@benmccann
Copy link
Collaborator

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;
  }

@howard-3
Copy link
Author

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(...);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants