com.github.wnameless.json.flattener.JsonFlattener Java Examples
The following examples show how to use
com.github.wnameless.json.flattener.JsonFlattener.
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 |
/** * 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: JsonReader.java From tablesaw with Apache License 2.0 | 5 votes |
private Table convertArrayOfObjects(JsonNode jsonObj, ReadOptions options) throws IOException { // flatten each object inside the array StringBuilder result = new StringBuilder("["); for (int i = 0; i < jsonObj.size(); i++) { JsonNode rowObj = jsonObj.get(i); String flattenedRow = JsonFlattener.flatten(mapper.writeValueAsString(rowObj)); if (i != 0) { result.append(","); } result.append(flattenedRow); } String flattenedJsonString = result.append("]").toString(); JsonNode flattenedJsonObj = mapper.readTree(flattenedJsonString); Set<String> colNames = new HashSet<>(); for (JsonNode row : flattenedJsonObj) { Iterator<String> fieldNames = row.fieldNames(); while (fieldNames.hasNext()) { colNames.add(fieldNames.next()); } } List<String> columnNames = new ArrayList<>(colNames); List<String[]> dataRows = new ArrayList<>(); for (JsonNode node : flattenedJsonObj) { String[] arr = new String[columnNames.size()]; for (int i = 0; i < columnNames.size(); i++) { if (node.has(columnNames.get(i))) { arr[i] = node.get(columnNames.get(i)).asText(); } else { arr[i] = null; } } dataRows.add(arr); } return TableBuildingUtils.build(columnNames, dataRows, options); }
Example #3
Source File: MongoDocumentFlattener.java From baleen with Apache License 2.0 | 5 votes |
/** * Flatten a Mongo Document into a Map of Strings of Key Value pairs * * @return a Map of key value pairs corresponding to the flattened document */ public Map<String, String> flatten() { Map<String, String> flattenedDocument = new HashMap<>(); Map<String, Object> flatJsonMap = JsonFlattener.flattenAsMap(document.toJson()); for (Entry<String, Object> entry : flatJsonMap.entrySet()) { flattenedDocument.put(entry.getKey(), entry.getValue().toString()); } return flattenedDocument; }
Example #4
Source File: JsonReader.java From tablesaw with Apache License 2.0 | 5 votes |
private Table convertArrayOfObjects(JsonNode jsonObj, ReadOptions options) throws IOException { // flatten each object inside the array StringBuilder result = new StringBuilder("["); for (int i = 0; i < jsonObj.size(); i++) { JsonNode rowObj = jsonObj.get(i); String flattenedRow = JsonFlattener.flatten(mapper.writeValueAsString(rowObj)); if (i != 0) { result.append(","); } result.append(flattenedRow); } String flattenedJsonString = result.append("]").toString(); JsonNode flattenedJsonObj = mapper.readTree(flattenedJsonString); Set<String> colNames = new HashSet<>(); for (JsonNode row : flattenedJsonObj) { Iterator<String> fieldNames = row.fieldNames(); while (fieldNames.hasNext()) { colNames.add(fieldNames.next()); } } List<String> columnNames = new ArrayList<>(colNames); List<String[]> dataRows = new ArrayList<>(); for (JsonNode node : flattenedJsonObj) { String[] arr = new String[columnNames.size()]; for (int i = 0; i < columnNames.size(); i++) { if (node.has(columnNames.get(i))) { arr[i] = node.get(columnNames.get(i)).asText(); } else { arr[i] = null; } } dataRows.add(arr); } return TableBuildingUtils.build(columnNames, dataRows, options); }
Example #5
Source File: EtcdJsonTest.java From etcd4j with Apache License 2.0 | 5 votes |
@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 #6
Source File: PropertyUtil.java From streams with Apache License 2.0 | 5 votes |
public static Map<String, Object> flattenToMap(ObjectMapper mapper, ObjectNode object) { Map<String, Object> flatObject; try { flatObject = mapper.readValue(JsonFlattener.flatten(mapper.writeValueAsString(object)), Map.class); } catch( Exception ex ) { return null; } return flatObject; }
Example #7
Source File: PropertyUtil.java From streams with Apache License 2.0 | 5 votes |
public static ObjectNode flattenToObjectNode(ObjectMapper mapper, ObjectNode object) { ObjectNode flatObjectNode; try { flatObjectNode = mapper.readValue(JsonFlattener.flatten(mapper.writeValueAsString(object)), ObjectNode.class); } catch( Exception ex ) { return null; } return flatObjectNode; }
Example #8
Source File: FlattenJson.java From nifi with Apache License 2.0 | 5 votes |
@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); } }