本文整理汇总了Java中com.github.wnameless.json.flattener.JsonFlattener类的典型用法代码示例。如果您正苦于以下问题:Java JsonFlattener类的具体用法?Java JsonFlattener怎么用?Java JsonFlattener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonFlattener类属于com.github.wnameless.json.flattener包,在下文中一共展示了JsonFlattener类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: process
import com.github.wnameless.json.flattener.JsonFlattener; //导入依赖的package包/类
@Override
public String process(JsonNode jsonNode) throws JsonProcessingException {
final StringBuilder builder = new StringBuilder();
if(null != jsonNode){
final String jsonText = MAPPER.writeValueAsString(jsonNode);
final Map<String, Object> flatMap = JsonFlattener.flattenAsMap(jsonText);
final List<String> header = createHeader(flatMap);
header.forEach(h -> {
builder.append(h);
builder.append("\t");
});
builder.replace(builder.lastIndexOf("\t"), builder.lastIndexOf("\t") + 1, "\n");
for (final Iterator<String> iterator = flatMap.keySet().iterator(); iterator.hasNext(); ) {
for(int i = 0; i < header.size(); i++){
builder.append(flatMap.get(iterator.next()));
builder.append("\t");
}
builder.replace(builder.lastIndexOf("\t"), builder.lastIndexOf("\t") + 1, "\n");
}
}
return builder.toString();
}
示例2: putAsJson
import com.github.wnameless.json.flattener.JsonFlattener; //导入依赖的package包/类
/**
* 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
*/
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();
}
}
示例3: testGetAndPut
import com.github.wnameless.json.flattener.JsonFlattener; //导入依赖的package包/类
@Test
public void testGetAndPut() throws Exception {
ObjectMapper mapper = new ObjectMapper();
EtcdNettyConfig config = new EtcdNettyConfig();
EtcdNettyClient nettyClient = new EtcdNettyClient(config, URI.create("http://localhost:4001"));
config.setMaxFrameSize(1024 * 1024); // Desired max size
EtcdClient client = new EtcdClient(nettyClient);
File testJson = new File("src/test/resources/test_data.json");
JsonNode original = mapper.readTree(testJson);
JsonNode fromEtcd = EtcdUtil.getAsJson("/etcd4j_test", client);
// 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()));
}
}