Example usage for com.fasterxml.jackson.core JsonFactory configure

List of usage examples for com.fasterxml.jackson.core JsonFactory configure

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory configure.

Prototype

public final JsonFactory configure(JsonGenerator.Feature f, boolean state) 

Source Link

Document

Method for enabling or disabling specified generator feature (check JsonGenerator.Feature for list of features)

Usage

From source file:com.enremmeta.onenow.Utils.java

public static Config loadConfig(String fileName, Class<? extends Config> configClass)
        throws MalformedURLException, IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(Feature.ALLOW_COMMENTS, true);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    Config config;//from  w  w  w. j a v  a 2  s  .c  o m

    if (fileName.startsWith("http://") || fileName.startsWith("https://")) {
        URL url = new URL(fileName);
        config = mapper.readValue(url, configClass);
    } else {
        File file = new File(fileName);
        config = mapper.readValue(file, configClass);
    }
    return config;
}

From source file:com.tomtom.speedtools.json.JsonObjectMapperFactory.java

public static ObjectMapper createJsonObjectMapper() {

    // Create a Json factory with customer properties.
    final JsonFactory jsonFactory = new JsonFactory();

    // Json parsing features.
    jsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

    // Json generation features.
    jsonFactory.configure(Feature.QUOTE_FIELD_NAMES, true).configure(Feature.WRITE_NUMBERS_AS_STRINGS, false);

    // Create a custom object mapper from the newly created factory. This object mapper will be used by RestEasy.
    final ObjectMapper mapper = new ObjectMapper(jsonFactory);

    // Set generic mapper configuration.
    mapper.configure(MapperFeature.USE_ANNOTATIONS, true).configure(MapperFeature.AUTO_DETECT_GETTERS, false)
            .configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false)
            .configure(MapperFeature.AUTO_DETECT_SETTERS, false)
            .configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);

    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY).setSerializationInclusion(Include.NON_NULL)
            .disableDefaultTyping().disable(SerializationFeature.WRITE_NULL_MAP_VALUES);

    // Set deserialization configuration.
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
            .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);

    // Set serialization configuration.
    mapper.configure(SerializationFeature.INDENT_OUTPUT, false)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true)
            .configure(SerializationFeature.WRAP_ROOT_VALUE, false)
            .configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, false)
            .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true)
            .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, false);

    // The annotation inspectors and additional mappers should be set by the caller.
    return mapper;
}

From source file:org.jberet.support.io.NoMappingJsonFactoryObjectFactory.java

static void configureJsonFactoryFeatures(final JsonFactory jsonFactory, final String jsonFactoryFeatures) {
    final StringTokenizer st = new StringTokenizer(jsonFactoryFeatures, ",");
    while (st.hasMoreTokens()) {
        final String[] pair = parseSingleFeatureValue(st.nextToken().trim());
        final String key = pair[0];
        final String value = pair[1];
        final JsonFactory.Feature feature;
        try {//from w w w . j a v a  2 s . c  o m
            feature = JsonFactory.Feature.valueOf(key);
        } catch (final Exception e1) {
            throw SupportMessages.MESSAGES.unrecognizedReaderWriterProperty(key, value);
        }
        if ("true".equals(value)) {
            if (!feature.enabledByDefault()) {
                jsonFactory.configure(feature, true);
            }
        } else if ("false".equals(value)) {
            if (feature.enabledByDefault()) {
                jsonFactory.configure(feature, false);
            }
        } else {
            throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, value, key);
        }
    }
}

From source file:io.github.cdelmas.spike.restlet.infrastructure.JacksonCustomConverter.java

private ObjectMapper createMapper() {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.registerModule(new Jdk8Module());
    return mapper;
}

From source file:org.ojai.json.impl.JsonDocumentStream.java

public JsonDocumentStream(InputStream in, Map<FieldPath, Type> fieldPathTypeMap,
        Events.Delegate eventDelegate) {
    inputStream = in;/*from w  w  w.  j  a va  2s  . c  om*/
    readStarted = false;
    iteratorOpened = false;
    this.eventDelegate = eventDelegate;
    this.fieldPathTypeMap = fieldPathTypeMap;
    try {
        JsonFactory jFactory = new JsonFactory();
        /* setting explicitly AUTO_CLOSE_SOURCE = false to ensure that
         * jsonParser.close() do not close the underlying inputstream.
         * It has to be closed by the owner of the stream.
         */
        jFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
        jsonParser = jFactory.createParser(inputStream);
    } catch (IOException e) {
        throw new DecodingException(e);
    }
}

From source file:org.restlet.ext.jackson.JacksonRepresentation.java

/**
 * Creates a Jackson object mapper based on a media type. It supports JSON,
 * JSON Smile, XML, YAML and CSV./*from www  . ja  v  a2 s.c  o  m*/
 * 
 * @return The Jackson object mapper.
 */
protected ObjectMapper createObjectMapper() {
    ObjectMapper result = null;

    if (MediaType.APPLICATION_JSON.isCompatible(getMediaType())) {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(jsonFactory);
    } else if (MediaType.APPLICATION_JSON_SMILE.isCompatible(getMediaType())) {
        SmileFactory smileFactory = new SmileFactory();
        smileFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(smileFactory);
        // [ifndef android]
    } else if (MediaType.APPLICATION_XML.isCompatible(getMediaType())
            || MediaType.TEXT_XML.isCompatible(getMediaType())) {
        javax.xml.stream.XMLInputFactory xif = XmlFactoryProvider.newInputFactory();
        xif.setProperty(javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
                isExpandingEntityRefs());
        xif.setProperty(javax.xml.stream.XMLInputFactory.SUPPORT_DTD, isExpandingEntityRefs());
        xif.setProperty(javax.xml.stream.XMLInputFactory.IS_VALIDATING, isValidatingDtd());
        javax.xml.stream.XMLOutputFactory xof = XmlFactoryProvider.newOutputFactory();
        XmlFactory xmlFactory = new XmlFactory(xif, xof);
        xmlFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new XmlMapper(xmlFactory);
        // [enddef]
    } else if (MediaType.APPLICATION_YAML.isCompatible(getMediaType())
            || MediaType.TEXT_YAML.isCompatible(getMediaType())) {
        YAMLFactory yamlFactory = new YAMLFactory();
        yamlFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(yamlFactory);
    } else if (MediaType.TEXT_CSV.isCompatible(getMediaType())) {
        CsvFactory csvFactory = new CsvFactory();
        csvFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new CsvMapper(csvFactory);
    } else {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
        result = new ObjectMapper(jsonFactory);
    }

    return result;
}

From source file:org.apache.drill.exec.store.parquet.metadata.Metadata.java

/**
 * Serialize parquet metadata to json and write to a file.
 *
 * @param parquetTableMetadata parquet table metadata
 * @param p file path//from  www.j  av  a 2  s .c om
 */
private void writeFile(ParquetTableMetadata_v3 parquetTableMetadata, Path p, FileSystem fs) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
    jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    SimpleModule module = new SimpleModule();
    module.addSerializer(ColumnMetadata_v3.class, new ColumnMetadata_v3.Serializer());
    mapper.registerModule(module);
    FSDataOutputStream os = fs.create(p);
    mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadata);
    os.flush();
    os.close();
}

From source file:org.apache.drill.exec.store.parquet.metadata.Metadata.java

private void writeFile(ParquetTableMetadataDirs parquetTableMetadataDirs, Path p, FileSystem fs)
        throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
    jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    SimpleModule module = new SimpleModule();
    mapper.registerModule(module);// w  ww  .  ja  v  a 2 s  .  c om
    FSDataOutputStream os = fs.create(p);
    mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadataDirs);
    os.flush();
    os.close();
}

From source file:org.apache.drill.exec.store.parquet.Metadata.java

/**
 * Serialize parquet metadata to json and write to a file
 *
 * @param parquetTableMetadata/*w  ww. ja v  a2 s.co  m*/
 * @param p
 * @throws IOException
 */
private void writeFile(ParquetTableMetadata_v3 parquetTableMetadata, Path p) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
    jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    SimpleModule module = new SimpleModule();
    module.addSerializer(ColumnMetadata_v3.class, new ColumnMetadata_v3.Serializer());
    mapper.registerModule(module);
    FSDataOutputStream os = fs.create(p);
    mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadata);
    os.flush();
    os.close();
}

From source file:org.apache.drill.exec.store.parquet.Metadata.java

private void writeFile(ParquetTableMetadataDirs parquetTableMetadataDirs, Path p) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
    jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    SimpleModule module = new SimpleModule();
    mapper.registerModule(module);/*from  www  . j a  v  a 2  s. co m*/
    FSDataOutputStream os = fs.create(p);
    mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadataDirs);
    os.flush();
    os.close();
}