com.github.wnameless.json.flattener.FlattenMode Java Examples

The following examples show how to use com.github.wnameless.json.flattener.FlattenMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EtcdUtil.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Puts the content of the Json recursively from the specified <i>path</i>
 * @param path root path (i.e. /path1/path2)
 * @param data JsonNode
 * @param etcdClient EtcdClient
 * @throws IOException
 * @throws EtcdAuthenticationException
 * @throws TimeoutException
 * @throws EtcdException
 */
public static void putAsJson(String path, JsonNode data, EtcdClient etcdClient)
        throws IOException, EtcdAuthenticationException, TimeoutException, EtcdException {

  Map<String, Object> flattened = new JsonFlattener(EtcdUtil.jsonToString(data))
          .withFlattenMode(FlattenMode.MONGODB)
          .withSeparator('/')
          .flattenAsMap();

  // clean previous data and replace it with new json structure
  try {
    etcdClient.delete(path).recursive().send().get();
  } catch (EtcdException e) {
    // interrupt always except when the previous key didn't exist
    if (EtcdErrorCode.KeyNotFound != e.errorCode) {
      throw e;
    }
  }

  // iterate over every flattened key and build the structure in etcd
  for (Map.Entry<String, Object> entry : flattened.entrySet()) {
    etcdClient.put(path + "/" + entry.getKey(), String.valueOf(entry.getValue())).send().get();
  }
}
 
Example #2
Source File: EtcdUtil.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Transforms etcd format (in dot notation) to a standard Json (with arrays and primitive types)
 * @param etcdJson from etcd
 * @return standardized Json
 * @throws IOException
 */
private static JsonNode dotNotationToStandardJson(JsonNode etcdJson) throws IOException {
  if (!etcdJson.isValueNode()) {
    String unflattened = new JsonUnflattener(jsonToString(flattenJson(etcdJson, "")))
            .withFlattenMode(FlattenMode.MONGODB)
            .withKeyTransformer(new KeyTransformer() {
              @Override
              public String transform(String s) {
                return s.replaceAll("__DOT__", "\\.");
              }
            })
            .unflatten();
    return mapper.readTree(unflattened);
  } else {
    return etcdJson;
  }
}
 
Example #3
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAndPut() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    EtcdNettyConfig config = new EtcdNettyConfig();
    config.setMaxFrameSize(1024 * 1024); // Desired max size

    this.etcd.close();

    EtcdNettyClient nettyClient = new EtcdNettyClient(config, CLUSTER.endpoints());
    this.etcd = new EtcdClient(nettyClient);

    File testJson = new File("src/test/resources/test_data.json");
    JsonNode original = mapper.readTree(testJson);

    JsonNode fromEtcd = EtcdUtil.getAsJson("/etcd4j_test", this.etcd );

    // flatten both and compare
    Map<String, Object> rootFlat = new JsonFlattener(EtcdUtil.jsonToString(original))
            .withFlattenMode(FlattenMode.MONGODB)
            .withSeparator('/')
            .flattenAsMap();

    Map<String, Object> testFlat = new JsonFlattener(EtcdUtil.jsonToString(fromEtcd))
            .withFlattenMode(FlattenMode.MONGODB)
            .withSeparator('/')
            .flattenAsMap();

    assertEquals(rootFlat.size(), testFlat.size());
    for (Map.Entry<String, Object> entry : rootFlat.entrySet()) {
        assertNotNull(testFlat.get(entry.getKey()));
    }
}
 
Example #4
Source File: FlattenJson.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final String mode = context.getProperty(FLATTEN_MODE).getValue();
    final FlattenMode flattenMode = getFlattenMode(mode);

    String separator = context.getProperty(SEPARATOR).evaluateAttributeExpressions(flowFile).getValue();


    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        session.exportTo(flowFile, bos);
        bos.close();

        String raw = new String(bos.toByteArray());
        final String flattened = new JsonFlattener(raw)
                .withFlattenMode(flattenMode)
                .withSeparator(separator.charAt(0))
                .withStringEscapePolicy(() -> StringEscapeUtils.ESCAPE_JSON)
                .flatten();

        flowFile = session.write(flowFile, os -> os.write(flattened.getBytes()));

        session.transfer(flowFile, REL_SUCCESS);
    } catch (Exception ex) {
        session.transfer(flowFile, REL_FAILURE);
    }
}
 
Example #5
Source File: FlattenJson.java    From nifi with Apache License 2.0 5 votes vote down vote up
private FlattenMode getFlattenMode(String mode) {
    if (FLATTEN_MODE_NORMAL.getValue().equals(mode)) {
        return FlattenMode.NORMAL;
    } else if (FLATTEN_MODE_DOT_NOTATION.getValue().equals(mode)) {
        return FlattenMode.MONGODB;
    } else {
        return FlattenMode.KEEP_ARRAYS;
    }
}
 
Example #6
Source File: JsonUnflattener.java    From json-flattener with Apache License 2.0 2 votes vote down vote up
/**
 * A fluent setter to setup a mode of the {@link JsonUnflattener}.
 * 
 * @param flattenMode
 *          a {@link FlattenMode}
 * @return this {@link JsonUnflattener}
 */
public JsonUnflattener withFlattenMode(FlattenMode flattenMode) {
  this.flattenMode = notNull(flattenMode);
  return this;
}