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:org.jsfr.json.JacksonParserTest.java

@Test
public void testLargeJsonJackson() throws Exception {
    final AtomicLong counter = new AtomicLong();
    ObjectMapper om = new ObjectMapper();
    JsonFactory f = new JsonFactory();
    JsonParser jp = f.createParser(read("allthethings.json"));
    long start = System.currentTimeMillis();
    jp.nextToken();//from  w ww  .j  a v  a 2s  .  c  o  m
    jp.nextToken();
    jp.nextToken();
    while (jp.nextToken() == JsonToken.FIELD_NAME) {
        if (jp.nextToken() == JsonToken.START_OBJECT) {
            TreeNode tree = om.readTree(jp);
            counter.incrementAndGet();
            LOGGER.trace("value: {}", tree);
        }
    }
    jp.close();
    LOGGER.info("Jackson processes {} value in {} millisecond", counter.get(),
            System.currentTimeMillis() - start);
}

From source file:gate.corpora.DataSiftFormat.java

@Override
public void unpackMarkup(gate.Document doc) throws DocumentFormatException {
    if ((doc == null) || (doc.getSourceUrl() == null && doc.getContent() == null)) {
        throw new DocumentFormatException("GATE document is null or no content found. Nothing to parse!");
    }//w w  w.  ja v a  2  s  . co m

    setNewLineProperty(doc);
    String jsonString = StringUtils.trimToEmpty(doc.getContent().toString());

    // TODO build the new content
    StringBuilder concatenation = new StringBuilder();

    try {
        ObjectMapper om = new ObjectMapper();

        JsonFactory factory = new JsonFactory(om);
        JsonParser parser = factory.createParser(jsonString);

        Map<DataSift, Long> offsets = new HashMap<DataSift, Long>();

        Iterator<DataSift> it = parser.readValuesAs(DataSift.class);
        while (it.hasNext()) {
            DataSift ds = it.next();
            offsets.put(ds, (long) concatenation.length());
            concatenation.append(ds.getInteraction().getContent()).append("\n\n");
        }

        // Set new document content
        DocumentContent newContent = new DocumentContentImpl(concatenation.toString());

        doc.edit(0L, doc.getContent().size(), newContent);

        AnnotationSet originalMarkups = doc.getAnnotations("Original markups");
        for (Map.Entry<DataSift, Long> item : offsets.entrySet()) {
            DataSift ds = item.getKey();
            Interaction interaction = ds.getInteraction();
            Long start = item.getValue();

            FeatureMap features = interaction.asFeatureMap();
            features.putAll(ds.getFurtherData());

            originalMarkups.add(start, start + interaction.getContent().length(), "Interaction", features);
        }

    } catch (InvalidOffsetException | IOException e) {
        throw new DocumentFormatException(e);
    }
}

From source file:com.world.watch.worldwatchcron.util.WWCron.java

public long extractLatestTimeStamp(String newsJson) {
    long maxTS = 0;
    JsonFactory factory = new JsonFactory();
    try {// w  w  w  .java 2  s. co  m
        JsonParser parser = factory.createParser(newsJson);
        while (!parser.isClosed()) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                break;
            }
            String fieldName = parser.getCurrentName();
            if (fieldName != null && fieldName.equals("date")) {
                parser.nextToken();
                long date = Long.parseLong(parser.getText());
                if (maxTS < date) {
                    maxTS = date;
                }
            }
        }
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        //logger.error("File not found ", e);
    }
    return maxTS;
}

From source file:com.netflix.hollow.jsonadapter.AbstractHollowJsonAdaptorTask.java

protected void processFile(Reader r, int maxSample) throws Exception {
    JsonArrayChunker chunker = new JsonArrayChunker(r, executor);
    chunker.initialize();/*from  w  w w.  j  ava2  s. c  o  m*/

    int counter = 0;

    Reader jsonObj = chunker.nextChunk();
    while (jsonObj != null && counter < maxSample) {

        final Reader currentObject = jsonObj;

        executor.execute(new Runnable() {
            public void run() {
                try {
                    JsonFactory factory = new JsonFactory();
                    JsonParser parser = factory.createParser(currentObject);
                    processRecord(parser);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        while (executor.getQueue().size() > maxWorkQueue) {
            Thread.sleep(5);
        }

        counter++;

        jsonObj.close();
        jsonObj = chunker.nextChunk();
    }

    executor.awaitSuccessfulCompletion();
}

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

/**
 * Parses the example document located at
 * "src/test/resources/extendedProcesses_example.json" and deserializes its
 * content into an instance of {@link Processes_Tamis}
 *///from  ww  w  .j  a v  a 2  s  . c  om
@Test
public void test() {
    try {

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

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

        this.jsonParser = jsonFactory.createParser(input);

        Processes_Tamis processes_short = processesDeserializer.deserialize(jsonParser, null);

        /*
         * Assert that values of the intantiated Processes_Tamis object
         * match the expected parameters from the example document
         */
        Assert.assertNotNull(processes_short);

        List<ProcessDescription_forProcessList> processDescriptions = processes_short.getProcesses();

        Assert.assertEquals(2, processDescriptions.size());

        ProcessDescription_forProcessList firstProcessDescription = processDescriptions.get(0);
        ProcessDescription_forProcessList secondProcessDescription = processDescriptions.get(1);

        /*
         * assertions for first process description
         */
        // in the example document there was no "abstract" element, thus,
        // description should be equal to label
        Assert.assertEquals(firstProcessDescription.getLabel(), firstProcessDescription.getDescription());
        Assert.assertEquals("org.n52.tamis.algorithm.interpolation", firstProcessDescription.getId());
        Assert.assertEquals("TAMIS Interpolation Process", firstProcessDescription.getLabel());

        /*
         * assertions for second process description
         */
        Assert.assertEquals(secondProcessDescription.getLabel(), secondProcessDescription.getDescription());
        Assert.assertEquals("org.n52.tamis.algorithm.soakageregressionmodel", secondProcessDescription.getId());
        Assert.assertEquals("TAMIS Soakage Regression Model", secondProcessDescription.getLabel());

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

From source file:org.o3project.ocnrm.odenos.linklayerizer.LinkLayerizerBoundarySet.java

public Map<String, LinklayerizerBoundary> changeJSONBoundariesToBoundaries(String jsonBoundary, String seqNo) {

    logger.info(seqNo + "\t" + "changeJSONBoundariestoBoundaries Start");

    linklayerizerBoundaryMap = new HashMap<String, LinklayerizerBoundary>();
    try {// w  ww  . j  a  va  2 s.c o  m
        JsonFactory factory = new JsonFactory();
        JsonParser jp = factory.createParser(jsonBoundary);
        jp.nextToken();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String mapKey = jp.getCurrentName();
            LinklayerizerBoundary llb = new LinklayerizerBoundary();
            jp.nextToken();
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String fieldname = jp.getCurrentName();
                jp.nextToken();
                if ("boundary_id".equals(fieldname)) {
                    llb.setBoundary_id(jp.getText());
                } else if ("lower_nw".equals(fieldname)) {
                    llb.setLower_nw(jp.getText());
                } else if ("lower_nw_node".equals(fieldname)) {
                    llb.setLower_nw_node(jp.getText());
                } else if ("lower_nw_port".equals(fieldname)) {
                    llb.setLower_nw_port(jp.getText());
                } else if ("upper_nw".equals(fieldname)) {
                    llb.setUpper_nw(jp.getText());
                } else if ("upper_nw_node".equals(fieldname)) {
                    llb.setUpper_nw_node(jp.getText());
                } else if ("upper_nw_port".equals(fieldname)) {
                    llb.setUpper_nw_port(jp.getText());
                } else if ("type".equals(fieldname)) {
                    continue;
                } else {
                    throw new IllegalStateException(seqNo + "\t" + "Unrecognized field '" + fieldname + "'!");
                }
            }
            linklayerizerBoundaryMap.put(mapKey, llb);
        }
        jp.close();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    logger.info(seqNo + "\t" + "changeJSONBoundariestoBoundaries End");
    return linklayerizerBoundaryMap;
}

From source file:com.trenurbanoapp.webapi.WebApiRestClient.java

public AssetsResponse assets() {
    HttpClient httpClient = getHttpClient();
    HttpGet httpGet = new HttpGet(urlbase + "/asset");
    getRequestCallback().doWithRequest(httpGet);
    try {/*w w w  .  j av a  2  s.c o  m*/
        return httpClient.execute(httpGet, response -> {
            int status = response.getStatusLine().getStatusCode();
            if (!(status >= 200 && status < 300)) {
                return new AssetsResponse(status, response.getStatusLine().getReasonPhrase());
            }

            List<Asset> assets = new ArrayList<>();
            final JsonFactory jsonFactory = getObjectMapper().getFactory();
            final JsonParser parser = jsonFactory.createParser(response.getEntity().getContent());
            JsonToken token;
            while ((token = parser.nextToken()) != null) {
                switch (token) {
                case START_OBJECT:
                    JsonNode node = parser.readValueAsTree();
                    Asset asset = createAsset(node);
                    assets.add(asset);
                    break;
                }
            }
            return new AssetsResponse(assets);
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * Parses the example document located at
 * "src/test/resources/SosTimeseriesInformation_example.json" and
 * deserializes its content into an instance of
 * {@link SosTimeseriesInformation}/*from   ww w.jav  a2  s  .c  om*/
 */
@Test
public void test() {
    try {

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

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

        this.jsonParser = jsonFactory.createParser(input);

        SosTimeseriesInformation sosTimeseriesInformation = sosTimeseriesInformationDeserializer
                .deserialize(jsonParser, null);

        /*
         * Assert that values of the intantiated SosTimeseriesInformation
         * object match the expected parameters from the example document
         */
        Assert.assertNotNull(sosTimeseriesInformation);

        /*
         * feature of interest
         */
        Assert.assertEquals("sta_a4bff7499d440d9bb67ad73e048c5a47",
                sosTimeseriesInformation.getFeatureOfInterest());

        /*
         * SOS URL
         */
        Assert.assertEquals("http://pegelonline.wsv.de/webservices/gis/gdi-sos",
                sosTimeseriesInformation.getSosUrl());

        /*
         * offering id
         */
        Assert.assertEquals("off_c599e9b089385d08aa00b7c9c4c6f3ef", sosTimeseriesInformation.getOfferingId());

        /*
         * procedure
         */
        Assert.assertEquals("pro_e878d48e49ebc2a156e2810470bae9a2", sosTimeseriesInformation.getProcedure());

        /*
         * observedProperty
         */
        Assert.assertEquals("phe_f60bcfb1f6ac3d37210b5d757a1bf48e",
                sosTimeseriesInformation.getObservedProperty());

        /*
         * temporal filter cannot be set via JsonDeserializer since the
         * document does not contain this information.! Thus, cannot be set!
         */

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

From source file:com.mnxfst.stream.listener.webtrends.WebtrendsTokenRequest.java

public String execute() throws Exception {

    final String assertion = buildAssertion();
    final Map<String, String> requestParams = new HashMap<String, String>() {
        private static final long serialVersionUID = 1919397363313726934L;

        {//from   www. ja v  a 2  s  . c  o  m
            put(REQUEST_PARAM_CLIENT_ID, clientId);
            put(REQUEST_PARAM_CLIENT_ASSERTION, java.net.URLEncoder.encode(assertion, "UTF-8"));
        }
    };

    final String result = httpPost(requestParams);

    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonParser jp = factory.createParser(result);
    JsonNode obj = mapper.readTree(jp);

    JsonNode error = obj.findValue("Error");
    if (error != null)
        throw new Exception(obj.findValue("Description").asText());

    return obj.findValue("access_token").asText();
}

From source file:com.trenurbanoapp.webapi.WebApiRestClient.java

public AssetsPositionResponse assetsPosition() {

    HttpClient httpClient = getHttpClient();
    HttpGet httpGet = new HttpGet(urlbase + "/asset/position");
    getRequestCallback().doWithRequest(httpGet);
    try {//from   w w w . j a  va 2 s.  c  om
        return httpClient.execute(httpGet, response -> {
            int status = response.getStatusLine().getStatusCode();
            if (!(status >= 200 && status < 300)) {
                return new AssetsPositionResponse(status, response.getStatusLine().getReasonPhrase());
            }

            List<AssetPosition> assets = new ArrayList<>();
            final JsonFactory jsonFactory = getObjectMapper().getFactory();
            final JsonParser parser = jsonFactory.createParser(response.getEntity().getContent());
            JsonToken token;
            while ((token = parser.nextToken()) != null) {
                switch (token) {
                case START_OBJECT:
                    JsonNode node = parser.readValueAsTree();
                    AssetPosition position = createAssetPosition(node);
                    assets.add(position);
                    if (assetPositionCallback != null) {
                        assetPositionCallback.execute(position);
                    }
                    break;
                }
            }
            return new AssetsPositionResponse(assets);
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}