use of com.jayway.jsonpath.DocumentContext in project mica2 by obiba.
the class SchemaFormContentFileService method getPathFilesMap.
private Map<String, JSONArray> getPathFilesMap(DocumentContext context, Object json) {
DocumentContext reader = new JsonContext(defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES)).parse(json);
JSONArray paths = null;
try {
paths = context.read("$..obibaFiles");
} catch (PathNotFoundException e) {
return null;
}
return paths.stream().collect(Collectors.toMap(Object::toString, p -> (JSONArray) reader.read(p.toString())));
}
use of com.jayway.jsonpath.DocumentContext in project mica2 by obiba.
the class SchemaFormContentFileService method save.
public void save(@NotNull SchemaFormContentAware newEntity, SchemaFormContentAware oldEntity, String entityPath) {
Assert.notNull(newEntity, "New content cannot be null");
Object json = defaultConfiguration().jsonProvider().parse(newEntity.getContent());
DocumentContext newContext = JsonPath.using(defaultConfiguration().addOptions(Option.AS_PATH_LIST)).parse(json);
Map<String, JSONArray> newPaths = getPathFilesMap(newContext, json);
// content does not have any file field
if (newPaths == null)
return;
if (oldEntity != null) {
Object oldJson = defaultConfiguration().jsonProvider().parse(oldEntity.getContent());
DocumentContext oldContext = JsonPath.using(defaultConfiguration().addOptions(Option.AS_PATH_LIST)).parse(oldJson);
Map<String, JSONArray> oldPaths = getPathFilesMap(oldContext, oldJson);
if (oldPaths != null) {
saveAndDelete(oldPaths, newPaths, entityPath);
} else {
// schema and definition now have files
newPaths.values().forEach(v -> saveFiles(v, entityPath));
}
} else {
newPaths.values().forEach(v -> saveFiles(v, entityPath));
}
cleanup(newPaths, newContext);
newEntity.setContent(newContext.jsonString());
}
use of com.jayway.jsonpath.DocumentContext in project mica2 by obiba.
the class MicaConfigResource method getGlobalTranslations.
private DocumentContext getGlobalTranslations(String locale, boolean _default) throws IOException {
String userProfileTranslations = getUserProfileTranslations(locale);
String micaTranslations = micaConfigService.getTranslations(locale, _default);
DocumentContext globalTranslations = JsonPath.parse(micaTranslations);
globalTranslations.put("$", "userProfile", JsonPath.parse(userProfileTranslations).read("$"));
return globalTranslations;
}
use of com.jayway.jsonpath.DocumentContext in project spring-boot-admin by codecentric.
the class InfoTest method test_json_serialize.
@Test
public void test_json_serialize() throws Exception {
Info info = Info.from(Collections.singletonMap("foo", "bar"));
String json = objectMapper.writeValueAsString(info);
DocumentContext doc = JsonPath.parse(json);
assertThat(doc.read("$.foo", String.class)).isEqualTo("bar");
}
use of com.jayway.jsonpath.DocumentContext in project nifi by apache.
the class AbstractJsonPathProcessor method validateAndEstablishJsonContext.
static DocumentContext validateAndEstablishJsonContext(ProcessSession processSession, FlowFile flowFile) {
// Parse the document once into an associated context to support multiple path evaluations if specified
final AtomicReference<DocumentContext> contextHolder = new AtomicReference<>(null);
processSession.read(flowFile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(in)) {
DocumentContext ctx = JsonPath.using(STRICT_PROVIDER_CONFIGURATION).parse(bufferedInputStream);
contextHolder.set(ctx);
}
}
});
return contextHolder.get();
}
Aggregations