本文整理汇总了Java中com.github.wnameless.json.flattener.FlattenMode类的典型用法代码示例。如果您正苦于以下问题:Java FlattenMode类的具体用法?Java FlattenMode怎么用?Java FlattenMode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FlattenMode类属于com.github.wnameless.json.flattener包,在下文中一共展示了FlattenMode类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: putAsJson
import com.github.wnameless.json.flattener.FlattenMode; //导入依赖的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();
}
}
示例2: dotNotationToStandardJson
import com.github.wnameless.json.flattener.FlattenMode; //导入依赖的package包/类
/**
* 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;
}
}
示例3: testGetAndPut
import com.github.wnameless.json.flattener.FlattenMode; //导入依赖的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()));
}
}
示例4: withFlattenMode
import com.github.wnameless.json.flattener.FlattenMode; //导入依赖的package包/类
/**
* 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;
}