Search in sources :

Example 71 with DocumentContext

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())));
}
Also used : Arrays(java.util.Arrays) Collection(java.util.Collection) Option(com.jayway.jsonpath.Option) NotNull(javax.validation.constraints.NotNull) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) JsonContext(com.jayway.jsonpath.internal.JsonContext) FileStoreService(org.obiba.mica.file.FileStoreService) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) DocumentContext(com.jayway.jsonpath.DocumentContext) SchemaFormContentAware(org.obiba.mica.core.domain.SchemaFormContentAware) JSONArray(net.minidev.json.JSONArray) Service(org.springframework.stereotype.Service) Map(java.util.Map) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Configuration.defaultConfiguration(com.jayway.jsonpath.Configuration.defaultConfiguration) Assert(org.springframework.util.Assert) JsonContext(com.jayway.jsonpath.internal.JsonContext) JSONArray(net.minidev.json.JSONArray) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 72 with DocumentContext

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());
}
Also used : JSONArray(net.minidev.json.JSONArray) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 73 with DocumentContext

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;
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 74 with DocumentContext

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");
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 75 with DocumentContext

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();
}
Also used : BufferedInputStream(org.apache.nifi.stream.io.BufferedInputStream) BufferedInputStream(org.apache.nifi.stream.io.BufferedInputStream) InputStream(java.io.InputStream) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) DocumentContext(com.jayway.jsonpath.DocumentContext)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)146 Test (org.junit.Test)106 HashMap (java.util.HashMap)14 BaseTest (com.jayway.jsonpath.BaseTest)12 Map (java.util.Map)12 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)8 JsonPath (com.jayway.jsonpath.JsonPath)7 File (java.io.File)7 List (java.util.List)7 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)6 Query (org.graylog.plugins.views.search.Query)6 MessageList (org.graylog.plugins.views.search.searchtypes.MessageList)6 Time (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Time)6 Values (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Values)6 DateTime (org.joda.time.DateTime)6 Configuration (com.jayway.jsonpath.Configuration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 InvalidJsonException (com.jayway.jsonpath.InvalidJsonException)4