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

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

Introduction

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

Prototype

public JsonFactory() 

Source Link

Document

Default constructor used to create factory instances.

Usage

From source file:at.plechinger.spring.security.scribe.JsonUtil.java

public static Map<String, Object> parseMap(String input) throws IOException {
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(factory);
    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
    };/*from w w  w .j  av a 2  s.  c om*/
    return mapper.readValue(input, typeRef);
}

From source file:org.ng200.openolympus.controller.auth.AuthenticationResponder.java

public static void writeLoginStatusJson(Writer out, String authMessage, List<String> captchaErrorCodes)
        throws IOException, JsonGenerationException {
    final JsonFactory factory = new JsonFactory();
    final JsonGenerator generator = factory.createGenerator(out);
    generator.writeStartObject();// w ww .  j a v  a2  s .  com
    generator.writeStringField("auth", authMessage);
    if (captchaErrorCodes != null && !captchaErrorCodes.isEmpty()) {
        generator.writeArrayFieldStart("captchas");
        for (final String captchaErrorCode : captchaErrorCodes) {
            generator.writeString(captchaErrorCode);
        }
        generator.writeEndArray();
    } else {
        generator.writeNullField("captchas");
    }
    generator.writeEndObject();
    generator.close();
}

From source file:org.apache.flink.runtime.jobgraph.jsonplan.JsonPlanGenerator.java

public static String generatePlan(JobGraph jg) {
    try {//from   w w  w  .j av  a2  s  . co m
        final StringWriter writer = new StringWriter(1024);

        final JsonFactory factory = new JsonFactory();
        final JsonGenerator gen = factory.createGenerator(writer);

        // start of everything
        gen.writeStartObject();
        gen.writeStringField("jid", jg.getJobID().toString());
        gen.writeStringField("name", jg.getName());
        gen.writeArrayFieldStart("nodes");

        // info per vertex
        for (JobVertex vertex : jg.getVertices()) {

            String operator = vertex.getOperatorName() != null ? vertex.getOperatorName() : NOT_SET;

            String operatorDescr = vertex.getOperatorDescription() != null ? vertex.getOperatorDescription()
                    : NOT_SET;

            String optimizerProps = vertex.getResultOptimizerProperties() != null
                    ? vertex.getResultOptimizerProperties()
                    : EMPTY;

            String description = vertex.getOperatorPrettyName() != null ? vertex.getOperatorPrettyName()
                    : vertex.getName();

            // make sure the encoding is HTML pretty
            description = StringEscapeUtils.escapeHtml4(description);
            description = description.replace("\n", "<br/>");
            description = description.replace("\\", "&#92;");

            operatorDescr = StringEscapeUtils.escapeHtml4(operatorDescr);
            operatorDescr = operatorDescr.replace("\n", "<br/>");

            gen.writeStartObject();

            // write the core properties
            gen.writeStringField("id", vertex.getID().toString());
            gen.writeNumberField("parallelism", vertex.getParallelism());
            gen.writeStringField("operator", operator);
            gen.writeStringField("operator_strategy", operatorDescr);
            gen.writeStringField("description", description);

            if (!vertex.isInputVertex()) {
                // write the input edge properties
                gen.writeArrayFieldStart("inputs");

                List<JobEdge> inputs = vertex.getInputs();
                for (int inputNum = 0; inputNum < inputs.size(); inputNum++) {
                    JobEdge edge = inputs.get(inputNum);
                    if (edge.getSource() == null) {
                        continue;
                    }

                    JobVertex predecessor = edge.getSource().getProducer();

                    String shipStrategy = edge.getShipStrategyName();
                    String preProcessingOperation = edge.getPreProcessingOperationName();
                    String operatorLevelCaching = edge.getOperatorLevelCachingDescription();

                    gen.writeStartObject();
                    gen.writeNumberField("num", inputNum);
                    gen.writeStringField("id", predecessor.getID().toString());

                    if (shipStrategy != null) {
                        gen.writeStringField("ship_strategy", shipStrategy);
                    }
                    if (preProcessingOperation != null) {
                        gen.writeStringField("local_strategy", preProcessingOperation);
                    }
                    if (operatorLevelCaching != null) {
                        gen.writeStringField("caching", operatorLevelCaching);
                    }

                    gen.writeStringField("exchange", edge.getSource().getResultType().name().toLowerCase());

                    gen.writeEndObject();
                }

                gen.writeEndArray();
            }

            // write the optimizer properties
            gen.writeFieldName("optimizer_properties");
            gen.writeRawValue(optimizerProps);

            gen.writeEndObject();
        }

        // end of everything
        gen.writeEndArray();
        gen.writeEndObject();

        gen.close();

        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException("Failed to generate plan", e);
    }
}

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.talend.components.jira.datum.EntityParser.java

/**
 * Parses JSON and searches for total property
 * //from w ww. j a  v  a  2  s.c  o m
 * @param json JSON string
 * @return total property value, if it is exist or -1 otherwise
 */
static int getTotal(String json) {
    JsonFactory factory = new JsonFactory();
    try {
        JsonParser parser = factory.createParser(json);

        boolean totalFound = rewindToField(parser, "total");

        if (!totalFound) {
            return UNDEFINED;
        }

        // get total value
        String value = parser.getText();

        return Integer.parseInt(value);
    } catch (IOException e) {
        LOG.debug("Exception during JSON parsing. {}", e.getMessage());
    }
    return UNDEFINED;
}

From source file:invar.lib.data.DataParserJson.java

public DataParserJson() {
    this.factory = new JsonFactory();
}

From source file:net.geco.model.iojson.JacksonSerializer.java

public JacksonSerializer(Writer writer, boolean debug) throws IOException {
    this();/*w  ww .ja v  a 2 s  .  c o  m*/
    JsonFactory jsonFactory = new JsonFactory();
    gen = jsonFactory.createGenerator(writer);
    if (debug) {
        gen.useDefaultPrettyPrinter();
    } else {
        gen.configure(Feature.QUOTE_FIELD_NAMES, false);
    }
}

From source file:com.orange.ocara.model.loader.AbstractJsonParser.java

protected <T> T readValue(InputStream jsonStream, Class<T> valueType) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    com.fasterxml.jackson.core.JsonParser jp = jsonFactory.createParser(jsonStream);
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.readValue(jp, valueType);
}

From source file:com.virtlink.commons.configuration2.jackson.JsonConfiguration.java

/**
 * Initializes a new instance of the {@link JsonConfiguration} class.
 */
public JsonConfiguration() {
    super(new JsonFactory());
}

From source file:demo.OwaspConfig.java

@Bean
@Primary/*from ww w .j  av a 2 s . com*/
public ObjectMapper objectMapper() {
    JsonFactory factory = new JsonFactory();
    factory.setCharacterEscapes(new OwaspCharacterEscapes());
    return new ObjectMapper(factory);
}