Example usage for com.fasterxml.jackson.databind ObjectMapper enable

List of usage examples for com.fasterxml.jackson.databind ObjectMapper enable

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper enable.

Prototype

public ObjectMapper enable(SerializationFeature f) 

Source Link

Document

Method for enabling specified DeserializationConfig feature.

Usage

From source file:org.agatom.springatom.boot.init.rest.SARepositoryRestMvcConfiguration.java

/** {@inheritDoc} */
@Override//from  w w w  .j  av  a2 s .co m
protected void configureJacksonObjectMapper(final ObjectMapper objectMapper) {
    super.configureJacksonObjectMapper(objectMapper);

    for (final Boolean key : this.enableDisableFeatures.keySet()) {
        final List<Object> objects = this.enableDisableFeatures.get(key);
        for (final Object obj : objects) {
            if (key) {
                if (ClassUtils.isAssignableValue(SerializationFeature.class, obj)) {
                    objectMapper.enable((SerializationFeature) obj);
                } else {
                    objectMapper.enable((DeserializationFeature) obj);
                }
            } else {
                if (ClassUtils.isAssignableValue(SerializationFeature.class, obj)) {
                    objectMapper.disable((SerializationFeature) obj);
                } else {
                    objectMapper.disable((DeserializationFeature) obj);
                }
            }
        }
    }
}

From source file:org.dswarm.graph.gdm.test.GDMResource4Test.java

@Test
public void readVersionedGDMRecordByURIFromDBThatWasWrittenAsGDM() throws IOException {

    LOG.debug("start read versioned GDM record by URI test for GDM resource at {} DB", dbType);

    final String dataModelURI = "http://data.slub-dresden.de/resources/3333";

    writeGDMToDBInternal(dataModelURI, "versioning/csv.gdm.v1.json");

    final String recordClassURI = "http://data.slub-dresden.de/resources/1/schema#RecordType";

    writeGDMToDBInternalWDeprecation(dataModelURI, "versioning/csv.gdm.v2.json", recordClassURI);

    final ObjectMapper objectMapper = Util.getJSONObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    final ObjectNode requestJson = objectMapper.createObjectNode();

    final String recordURI = "http://data.slub-dresden.de/datamodels/DataModel-574990f5-4785-4020-b86a-9765bb084f16/records/34aa79b1-4d70-4511-a36a-4a6311300c47";
    final String legacyRecordIdentifierAP = "http://data.slub-dresden.de/resources/1/schema#id";

    requestJson.put(DMPStatics.RECORD_URI_IDENTIFIER, recordURI);
    requestJson.put(DMPStatics.LEGACY_RECORD_IDENTIFIER_ATTRIBUTE_PATH, legacyRecordIdentifierAP);
    requestJson.put(DMPStatics.DATA_MODEL_URI_IDENTIFIER, dataModelURI);
    requestJson.put(DMPStatics.VERSION_IDENTIFIER, 1);

    final Resource actualResource = readGDMRecord(requestJson, 6);

    Assert.assertEquals(recordURI, actualResource.getUri());

    // should retrieve the latest version
    requestJson.remove(DMPStatics.VERSION_IDENTIFIER);

    final Resource actualResource2 = readGDMRecord(requestJson, 5);

    Assert.assertEquals(recordURI, actualResource2.getUri());

    LOG.debug("finished read versioned GDM record by URI test for GDM resource at {} DB", dbType);
}

From source file:org.dswarm.graph.gdm.test.GDMResource4Test.java

@Test
public void readVersionedGDMRecordByIDFromDBThatWasWrittenAsGDM() throws IOException {

    LOG.debug("start read versioned GDM record by ID test for GDM resource at {} DB", dbType);

    final String dataModelURI = "http://data.slub-dresden.de/resources/2222";

    writeGDMToDBInternal(dataModelURI, "versioning/csv.gdm.v1.json");

    final String recordClassURI = "http://data.slub-dresden.de/resources/1/schema#RecordType";

    writeGDMToDBInternalWDeprecation(dataModelURI, "versioning/csv.gdm.v2.json", recordClassURI);

    final ObjectMapper objectMapper = Util.getJSONObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    final ObjectNode requestJson = objectMapper.createObjectNode();

    final String recordID = "7890";
    final String recordURI = "http://data.slub-dresden.de/datamodels/DataModel-574990f5-4785-4020-b86a-9765bb084f16/records/34aa79b1-4d70-4511-a36a-4a6311300c47";
    final String legacyRecordIdentifierAP = "http://data.slub-dresden.de/resources/1/schema#id";

    requestJson.put(DMPStatics.RECORD_ID_IDENTIFIER, recordID);
    requestJson.put(DMPStatics.LEGACY_RECORD_IDENTIFIER_ATTRIBUTE_PATH, legacyRecordIdentifierAP);
    requestJson.put(DMPStatics.DATA_MODEL_URI_IDENTIFIER, dataModelURI);
    requestJson.put(DMPStatics.VERSION_IDENTIFIER, 1);

    final Resource actualResource = readGDMRecord(requestJson, 6);

    Assert.assertEquals(recordURI, actualResource.getUri());

    // should retrieve the latest version
    requestJson.remove(DMPStatics.VERSION_IDENTIFIER);

    final Resource actualResource2 = readGDMRecord(requestJson, 5);

    Assert.assertEquals(recordURI, actualResource2.getUri());

    LOG.debug("finished read versioned GDM record by ID test for GDM resource at {} DB", dbType);
}

From source file:org.dswarm.graph.gdm.test.GDMResource4Test.java

@Test
public void searchGDMRecordFromDBThatWasWrittenAsGDM3() throws IOException {

    LOG.debug("start search GDM records test 3 for GDM resource at {} DB", dbType);

    final String dataModelURI = "http://data.slub-dresden.de/resources/6666";

    writeGDMToDBInternal(dataModelURI, "versioning/csv.gdm.v1.json");

    final String recordClassURI = "http://data.slub-dresden.de/resources/1/schema#RecordType";

    writeGDMToDBInternalWDeprecation(dataModelURI, "versioning/csv.gdm.v2.json", recordClassURI);

    final ObjectMapper objectMapper = Util.getJSONObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    final ObjectNode requestJson = objectMapper.createObjectNode();

    final String recordURI = "http://data.slub-dresden.de/datamodels/DataModel-574990f5-4785-4020-b86a-9765bb084f16/records/34aa79b1-4d70-4511-a36a-4a6311300c47";
    final String keyAP = "http://data.slub-dresden.de/resources/1/schema#name";
    final String searchValue = "gluck";

    requestJson.put(DMPStatics.KEY_ATTRIBUTE_PATH_IDENTIFIER, keyAP);
    requestJson.put(DMPStatics.SEARCH_VALUE_IDENTIFIER, searchValue);
    requestJson.put(DMPStatics.DATA_MODEL_URI_IDENTIFIER, dataModelURI);
    requestJson.put(DMPStatics.VERSION_IDENTIFIER, 1);

    final Model actualModel = searchGDMRecords(requestJson, 6);

    Assert.assertEquals(recordURI, actualModel.getResources().iterator().next().getUri());

    // should retrieve the latest version
    requestJson.remove(DMPStatics.VERSION_IDENTIFIER);

    final Model actualModel2 = searchGDMRecords(requestJson, 5);

    Assert.assertEquals(recordURI, actualModel2.getResources().iterator().next().getUri());

    LOG.debug("finished search GDM records test 3 for GDM resource at {} DB", dbType);
}

From source file:org.dswarm.graph.gdm.test.GDMResource4Test.java

@Ignore
@Test/*from w w  w  .  j ava2 s  .  com*/
public void searchGDMRecordFromDBThatWasWrittenAsGDM4() throws IOException {

    LOG.debug("start search GDM records test 4 for GDM resource at {} DB", dbType);

    final String dataModelURI = "http://data.slub-dresden.de/resources/7777";

    writeGDMToDBInternal(dataModelURI, "versioning/csv.gdm.v1.json");

    final String recordClassURI = "http://data.slub-dresden.de/resources/1/schema#RecordType";

    writeGDMToDBInternalWDeprecation(dataModelURI, "versioning/csv.gdm.v2.json", recordClassURI);

    final ObjectMapper objectMapper = Util.getJSONObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    final ObjectNode requestJson = objectMapper.createObjectNode();

    final String recordURI = "http://data.slub-dresden.de/datamodels/DataModel-574990f5-4785-4020-b86a-9765bb084f16/records/7a7990dd-2e4a-4757-85f2-7de444a0e502";
    final String keyAP = "http://data.slub-dresden.de/resources/1/schema#name";
    final String searchValue = "brumm";

    requestJson.put(DMPStatics.KEY_ATTRIBUTE_PATH_IDENTIFIER, keyAP);
    requestJson.put(DMPStatics.SEARCH_VALUE_IDENTIFIER, searchValue);
    requestJson.put(DMPStatics.DATA_MODEL_URI_IDENTIFIER, dataModelURI);
    requestJson.put(DMPStatics.VERSION_IDENTIFIER, 1);

    final Model actualModel = searchGDMRecords(requestJson, 6);

    Assert.assertEquals(recordURI, actualModel.getResources().iterator().next().getUri());

    // should retrieve the latest version
    requestJson.remove(DMPStatics.VERSION_IDENTIFIER);

    final Model actualModel2 = searchGDMRecords(requestJson, 0);

    Assert.assertNotNull(actualModel2);
    Assert.assertEquals(0, actualModel2.size());

    LOG.debug("finished search GDM records test 4 for GDM resource at {} DB", dbType);
}

From source file:org.dswarm.graph.gdm.test.GDMResource4Test.java

@Test
public void readGDMModelFromDBThatWasWrittenAsGDM() throws IOException {

    LOG.debug("start read GDM model test for GDM resource at {} DB", dbType);

    writeGDMToDBInternal(DATA_MODEL_URI, MABXML_RESOURCE_GSON);

    final ObjectMapper objectMapper = Util.getJSONObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    final ObjectNode requestJson = objectMapper.createObjectNode();

    requestJson.put(DMPStatics.RECORD_CLASS_URI_IDENTIFIER,
            "http://www.ddb.de/professionell/mabxml/mabxml-1.xsd#datensatzType");
    requestJson.put(DMPStatics.DATA_MODEL_URI_IDENTIFIER, DATA_MODEL_URI);

    final String requestJsonString = objectMapper.writeValueAsString(requestJson);

    // POST the request
    final ClientResponse response = target().path("/get").type(MediaType.APPLICATION_JSON_TYPE)
            .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, requestJsonString);

    Assert.assertEquals("expected 200", 200, response.getStatus());

    final InputStream actualResult = response.getEntity(InputStream.class);
    final BufferedInputStream bis = new BufferedInputStream(actualResult, 1024);
    final ModelParser modelParser = new ModelParser(bis);
    final org.dswarm.graph.json.Model model = new org.dswarm.graph.json.Model();

    final Observable<Void> parseObservable = modelParser.parse().map(resource1 -> {

        model.addResource(resource1);//  w w w.  j  a v  a  2 s  .c  o m

        return null;
    });

    final Iterator<Void> iterator = parseObservable.toBlocking().getIterator();

    Assert.assertTrue(iterator.hasNext());

    while (iterator.hasNext()) {

        iterator.next();
    }

    bis.close();
    actualResult.close();

    LOG.debug("read '{}' statements", model.size());

    // 191 - 39 rdf:type statements = 152
    Assert.assertEquals("the number of statements should be 191", 191, model.size());

    LOG.debug("finished read GDM model test for GDM resource at {} DB", dbType);
}

From source file:com.serendio.lingo3g.ProcessingResult.java

/**
 * Serializes this processing result as JSON to the provided <code>writer</code>.
 * Documents, clusters and other attributes can be included or skipped in the output
 * as requested./*from ww  w.  j a  va 2 s .  c om*/
 * 
 * @param writer the writer to serialize this processing result to. The writer will
 *            <strong>not</strong> be closed.
 * @param callback JavaScript function name in which to wrap the JSON response or
 *            <code>null</code>.
 * @param indent if <code>true</code>, the output JSON will be pretty-printed
 * @param saveDocuments if <code>false</code>, documents will not be serialized.
 * @param saveClusters if <code>false</code>, clusters will not be serialized
 * @param saveOtherAttributes if <code>false</code>, other attributes will not be
 *            serialized
 * @throws IOException in case of any problems with serialization
 */
public void serializeJson(Writer writer, String callback, boolean indent, boolean saveDocuments,
        boolean saveClusters, boolean saveOtherAttributes) throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.getFactory().disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    if (StringUtils.isNotBlank(callback)) {
        writer.write(callback + "(");
    }
    final Map<String, Object> attrs = prepareAttributesForSerialization(saveDocuments, saveClusters,
            saveOtherAttributes);

    mapper.writeValue(writer, attrs);
    if (StringUtils.isNotBlank(callback)) {
        writer.write(");");
    }
}

From source file:ijfx.service.ui.ImageJInfoService.java

@AngularMethod(sync = true, description = "Return the list of all widgets", inputDescription = "No input")
public JSObject getModuleList() {
    try {// ww  w .j a v  a 2s . c  om
        ObjectMapper mapper = new ObjectMapper();
        logger.fine("Getting module list");
        SimpleModule simpleModule = new SimpleModule("ModuleSerializer");
        // simpleModule.addSerializer(ModuleItem<?>.class,new ModuleItemSerializer());
        simpleModule.addSerializer(ModuleInfo.class, new ModuleSerializer());
        simpleModule.addSerializer(ModuleItem.class, new ModuleItemSerializer());
        mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.registerModule(simpleModule);

        ArrayNode arrayNode = mapper.createArrayNode();

        moduleService.getModules().forEach(module -> {

            //System.out.println("Adding "+module.getName());
            arrayNode.add(mapper.convertValue(module, JsonNode.class));

        });

        //String json = mapper.writeValueAsString(moduleService.getModules());
        logger.fine("JSON done !");

        return JSONUtils.parseToJSON(appService.getCurrentWebEngine(), arrayNode.toString());
        //return JSONUtils.parseToJSON(appService.getCurrentWebEngine(), json);

    } catch (Exception ex) {
        logger.log(Level.SEVERE, null, ex);
    }
    logger.warning("Returning null");
    return null;
}

From source file:org.apache.taverna.robundle.manifest.Manifest.java

/**
 * Write as an RO Bundle JSON-LD manifest
 * //from w  ww  .j av a 2s.co m
 * @return The path of the written manifest (e.g. ".ro/manifest.json")
 * @throws IOException
 */
public Path writeAsJsonLD() throws IOException {
    Path jsonld = bundle.getFileSystem().getPath(RO, MANIFEST_JSON);
    createDirectories(jsonld.getParent());
    // Files.createFile(jsonld);
    if (!getManifest().contains(jsonld))
        getManifest().add(0, jsonld);
    ObjectMapper om = new ObjectMapper();
    om.addMixInAnnotations(Path.class, PathMixin.class);
    om.addMixInAnnotations(FileTime.class, FileTimeMixin.class);
    om.enable(INDENT_OUTPUT);
    om.disable(WRITE_EMPTY_JSON_ARRAYS);
    om.disable(FAIL_ON_EMPTY_BEANS);
    om.disable(WRITE_NULL_MAP_VALUES);

    om.setSerializationInclusion(Include.NON_NULL);
    try (Writer w = newBufferedWriter(jsonld, Charset.forName("UTF-8"), WRITE, TRUNCATE_EXISTING, CREATE)) {
        om.writeValue(w, this);
    }
    return jsonld;
}

From source file:com.proofpoint.json.JsonCodecFactory.java

private ObjectMapper createObjectMapper() {
    ObjectMapper objectMapper = null;

    // Work around for http://code.google.com/p/google-guice/issues/detail?id=627
    RuntimeException lastException = null;
    for (int i = 0; objectMapper == null && i < 10; i++) {
        try {//from ww  w .j a va2  s  .  c  o  m
            objectMapper = objectMapperProvider.get();
        } catch (RuntimeException e) {
            lastException = e;
        }
    }
    if (objectMapper == null) {
        throw lastException;
    }

    if (prettyPrint) {
        objectMapper.enable(INDENT_OUTPUT);
    } else {
        objectMapper.disable(INDENT_OUTPUT);
    }
    return objectMapper;
}