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

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

Introduction

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

Prototype

public JsonParser createParser(String content) throws IOException, JsonParseException 

Source Link

Document

Method for constructing parser for parsing contents of given String.

Usage

From source file:com.github.jcustenborder.kafka.connect.spooldir.JsonSchemaGenerator.java

@Override
protected Map<String, Schema.Type> determineFieldTypes(InputStream inputStream) throws IOException {
    Map<String, Schema.Type> typeMap = new LinkedHashMap<>();

    JsonFactory factory = new JsonFactory();
    try (JsonParser parser = factory.createParser(inputStream)) {
        Iterator<JsonNode> iterator = ObjectMapperFactory.INSTANCE.readValues(parser, JsonNode.class);
        while (iterator.hasNext()) {
            JsonNode node = iterator.next();
            if (node.isObject()) {
                Iterator<String> fieldNames = node.fieldNames();
                while (fieldNames.hasNext()) {
                    typeMap.put(fieldNames.next(), Schema.Type.STRING);
                }/*  ww  w . jav  a 2 s .  c  o  m*/
                break;
            }
        }
    }

    return typeMap;
}

From source file:monasca.log.api.app.validation.LogApplicationTypeValidationTest.java

private String getMessage(String json) throws JsonParseException, IOException {
    JsonFactory factory = new JsonFactory();
    JsonParser jp = factory.createParser(json);
    jp.nextToken();// w ww . j  a  v  a  2s  . c om
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        jp.nextToken();
        if ("message".equals(fieldname)) {

            return jp.getText();
        }
    }
    jp.close();
    return null;
}

From source file:com.cedarsoft.serialization.jackson.IgnoringSerializerTest.java

@Theory
public void testIt(@Nonnull String json) throws Exception {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    JsonParser parser = jsonFactory.createParser(new ByteArrayInputStream(json.getBytes()));

    Void result = serializer.deserialize(parser, Version.valueOf(1, 0, 0)); //we use the override stuff to avoid version/type check
    assertThat(result).isNull();/*  w w  w .  j  a  va 2  s . co  m*/

    JsonToken nextToken = parser.nextToken();
    assertThat(nextToken).isNull();
}

From source file:com.anrisoftware.simplerest.oanda.rest.NamedListParseResponse.java

private <T> JsonParser createParser(ObjectMapper mapper, HttpEntity entity)
        throws IOException, JsonParseException, JsonMappingException {
    JsonFactory factory = mapper.getFactory();
    JsonParser parser = factory.createParser(entity.getContent());
    return parser;
}

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:org.wrml.runtime.format.application.json.JsonModelParserFactory.java

@Override
public ModelParser createModelParser(final InputStream in) throws IOException, ModelParserException {

    final JsonFactory jsonFactory = new JsonFactory();
    final JsonParser jsonParser;

    try {//from ww w.  j  a  va2s.  com
        jsonParser = jsonFactory.createParser(in);
    } catch (final JsonParseException e) {
        throw new ModelParserException(
                "An serious JSON related problem has occurred while attempting to parse a Model.", e, null);
    } catch (final IOException e) {
        throw new ModelParserException(
                "An serious I/O related problem has occurred while attempting to parse a Model.", e, null);
    }

    final JsonModelParser parser = new JsonModelParser(jsonParser);
    return parser;
}

From source file:ai.susi.geo.GeoJsonReader.java

public GeoJsonReader(final InputStream is, final int concurrency) throws JsonParseException, IOException {
    this.concurrency = concurrency;
    this.featureQueue = new ArrayBlockingQueue<Feature>(Runtime.getRuntime().availableProcessors() * 2 + 1);
    JsonFactory factory = new JsonFactory();
    this.parser = factory.createParser(is);
}

From source file:org.n52.tamis.core.test.json.deserialize.CapabilitiesDeserializer_Test.java

/**
 * Parses the example document located at
 * "src/test/resources/extendedCapabilities_example.json" and deserializes
 * its content into an instance of {@link Capabilities_Tamis}
 *//*from   www  .  java 2 s  .  c  o  m*/
@Test
public void test() {
    try {

        input = this.getClass().getResourceAsStream(EXTENDED_CAPABILITIES_EXAMPLE_JSON);

        ObjectMapper objectMapper = new ObjectMapper();
        JsonFactory jsonFactory = objectMapper.getFactory();

        this.jsonParser = jsonFactory.createParser(input);

        Capabilities_Tamis capabilities_short = capabilitiesDeserializer.deserialize(jsonParser, null);

        /*
         * Assert that values of the intantiated Capabilities_Tamis object
         * match the expected parameters from the example document
         */
        Assert.assertNotNull(capabilities_short);
        Assert.assertEquals("52North WPS 4.0.0-SNAPSHOT", capabilities_short.getLabel());
        Assert.assertEquals("contact@52north.org", capabilities_short.getContact());
        Assert.assertEquals(String.valueOf(SERVICE_ID_CONSTANT), capabilities_short.getId().toString());
        Assert.assertEquals("WPS", capabilities_short.getType());

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:GetAppConfig.java

private void getConf() {
    String line;//  w ww  . ja va2 s .c  o m
    String json = "";
    try {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(this.jsonfile), "UTF-8"));
        while ((line = reader.readLine()) != null) {
            json += line;
        }
        reader.close();
    } catch (Exception e) {
        System.out.println("Error: readconf(): " + e.getMessage());
        return;
    }

    JsonFactory factory = new JsonFactory();
    try {
        JsonParser parser = factory.createParser(json);
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String name = parser.getCurrentName();
            if (name == null)
                continue;
            parser.nextToken();
            if (name.equals("userid")) {
                this.setUserID(parser.getText());
            } else if (name.equals("passwd")) {
                this.setPassword(parser.getText());
            } else if (name.equals("deviceid")) {
                this.setDeviceID(parser.getText());
            } else if (name.equals("pkgdir")) {
                this.setPackageDir(parser.getText());
            } else {
                parser.skipChildren();
            }
        } // while
    } catch (Exception e) {
        System.out.println("Error: parseconf(): " + e.getMessage());
    }
}

From source file:hr.ws4is.JsonDecoder.java

/**
 * Does actual conversion from JSON string to Java class instance
 * @param type/*from www  .  j  ava  2 s . c om*/
 * @param json
 * @throws IOException
 */
private void parse(final Class<T> type, final String json) throws IOException {
    final JsonFactory factory = new JsonFactory();
    final JsonParser jp = factory.createParser(json);
    final JsonNode jn = OBJECT_MAPPER.readTree(jp);

    if (jn.isArray()) {
        final TypeFactory tf = TypeFactory.defaultInstance();
        final JavaType jt = tf.constructCollectionType(ArrayList.class, type);
        objectList = OBJECT_MAPPER.readValues(jp, jt);
    } else {
        object = OBJECT_MAPPER.treeToValue(jn, type);
    }
}