Example usage for com.fasterxml.jackson.core JsonParser getText

List of usage examples for com.fasterxml.jackson.core JsonParser getText

Introduction

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

Prototype

public abstract String getText() throws IOException, JsonParseException;

Source Link

Document

Method for accessing textual representation of the current token; if no current token (before first call to #nextToken , or after encountering end-of-input), returns null.

Usage

From source file:org.sead.repositories.reference.RefRepository.java

private JsonNode getAggregation(String id, File indexFile, CountingInputStream cis, boolean withChildren,
        Long oreFileSize) throws JsonParseException, JsonMappingException, IOException {
    log.debug("Getting Aggregation");

    long curPos = 0;

    // Always need to generate these
    ArrayList<String> entries = new ArrayList<String>();
    ArrayList<Long> offsets = new ArrayList<Long>();

    FileInputStream fis = new FileInputStream(indexFile);
    JsonFactory f = new MappingJsonFactory();
    JsonParser jp = f.createParser(fis);

    JsonToken current;/*from   w ww.java  2 s  .com*/
    log.debug("Reading Index file");
    current = jp.nextToken(); // Start object

    while ((current = jp.nextToken()) != null) {
        if (current.equals(JsonToken.FIELD_NAME)) {
            String fName = jp.getText();
            current = jp.nextToken(); // Get to start of
            // value
            long offset = jp.getLongValue();
            log.trace("Adding: " + fName + " : " + offset);
            entries.add(fName);
            offsets.add(offset);
        }
    }
    IOUtils.closeQuietly(fis);

    File descFile = getDescFile(id);
    InputStream is = new FileInputStream(descFile);
    ObjectNode resultNode = (ObjectNode) mapper.readTree(is);
    IOUtils.closeQuietly(is);

    log.trace(resultNode.toString());
    if ((resultNode.has("Has Part")) && withChildren) {

        resultNode = getChildren(resultNode, indexFile, cis, oreFileSize, curPos, entries, offsets);
    } else {
        resultNode.remove("aggregates");
    }
    log.debug("Aggregation retrieved");
    return resultNode;
}

From source file:org.sead.repositories.reference.RefRepository.java

private JsonNode getItem(String item, File indexFile, CountingInputStream cis, boolean withChildren,
        Long oreFileSize, long curOffset, ArrayList<String> entries, ArrayList<Long> offsets)
        throws JsonParseException, JsonMappingException, IOException {
    log.trace("Getting: " + item + " with starting offset: " + curOffset);

    long curPos = curOffset;

    if ((entries == null) || (offsets == null)) {
        entries = new ArrayList<String>();
        offsets = new ArrayList<Long>();

        FileInputStream fis = new FileInputStream(indexFile);
        JsonFactory f = new MappingJsonFactory();
        JsonParser jp = f.createParser(fis);

        JsonToken current;//from w w  w  .  j  a v a  2  s.  co  m
        log.trace("Reading Index file");
        current = jp.nextToken(); // Start object

        while ((current = jp.nextToken()) != null) {
            if (current.equals(JsonToken.FIELD_NAME)) {
                String fName = jp.getText();
                current = jp.nextToken(); // Get to start of
                // value
                long offset = jp.getLongValue();
                log.trace("Adding: " + fName + " : " + offset);
                entries.add(fName);
                offsets.add(offset);
            }
        }
        try {
            fis.close();
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

    }

    byte[] b = null;
    int bytesRead = 0;

    int index = entries.indexOf(item);
    if (index == -1) {
        log.warn(item + " not in index");
    }
    // getSizeEstimateFor(index)
    int estSize;
    if (index < offsets.size() - 1) {
        estSize = (int) (offsets.get(index + 1) - offsets.get(index));
    } else {
        estSize = (int) (oreFileSize - offsets.get(index));
    }
    curPos += skipTo(cis, curPos, offsets.get(index));
    log.trace("Current Pos updated to : " + curPos);
    b = new byte[estSize];
    bytesRead = cis.read(b);
    log.trace("Read " + bytesRead + " bytes");
    if (bytesRead == estSize) {
        log.trace("Read: " + new String(b));
        InputStream is = new ByteArrayInputStream(b);
        // mapper seems to be OK ignoring a last char such as a comma after
        // the object/tree
        ObjectNode resultNode = (ObjectNode) mapper.readTree(is);
        try {
            is.close();
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

        curPos += bytesRead;
        log.trace("curPos: " + curPos + " : count: " + cis.getByteCount());

        log.trace(resultNode.toString());
        if ((resultNode.has("Has Part")) && withChildren) {
            resultNode = getChildren(resultNode, indexFile, cis, oreFileSize, curPos, entries, offsets);
        } else {
            resultNode.remove("aggregates");
        }
        /*
         * if (args[2] != null) { long offset2 = Long.parseLong(args[2]);
         * sbc.position(offset2); b.clear(); sbc.read(b);
         * 
         * InputStream is2 = new ByteArrayInputStream(b.array());
         * 
         * JsonNode node2 = mapper.readTree(is2);
         * System.out.println(node2.toString()); is2.close(); }
         */
        return resultNode;
    } else {
        return null;
    }

}

From source file:org.sead.repositories.reference.RefRepository.java

protected static void generateIndex(InputStream ro, File descFile, File indexFile)
        throws JsonParseException, IOException {

    log.debug("Generating desc and index files");
    JsonFactory f = new MappingJsonFactory(); // reading
    JsonParser jp = f.createParser(ro);

    JsonGenerator generator = new JsonFactory().createGenerator(descFile, JsonEncoding.UTF8);

    JsonToken current;/* w ww.j a  v  a  2s.  c om*/

    current = jp.nextToken();

    report(jp, current);
    while ((current = jp.nextToken()) != null) {
        if (current.equals(JsonToken.FIELD_NAME)) {
            String fName = jp.getText();
            if (fName.equals("describes")) {
                log.trace("describes");
                while (((current = jp.nextToken()) != null)) {
                    if (jp.isExpectedStartObjectToken()) {
                        generator.setCodec(new ObjectMapper());
                        generator.useDefaultPrettyPrinter();

                        generator.writeStartObject();

                        while (((current = jp.nextToken()) != JsonToken.END_OBJECT)) {
                            if (current != JsonToken.FIELD_NAME) {
                                log.warn("Unexpected Token!");
                                report(jp, current);

                            } else {
                                report(jp, current);
                                String name = jp.getText();
                                current = jp.nextToken(); // Get to start of
                                // value
                                if (!name.equals("aggregates")) {
                                    log.trace("Writing: " + name);
                                    generator.writeFieldName(name);
                                    generator.writeTree(jp.readValueAsTree());
                                } else {
                                    report(jp, current);
                                    log.trace("Skipping?");
                                    if (current.isStructStart()) {
                                        indexChildren(indexFile, jp);
                                        // jp.skipChildren();
                                    } else {
                                        log.warn("Was Not Struct start!");
                                    }
                                    log.trace("Hit aggregates");

                                }
                            }
                        }

                        generator.writeEndObject();

                        generator.close();
                    }
                }
            }
        }
    }
}

From source file:org.sead.repositories.reference.RefRepository.java

private static void indexChildren(File index, JsonParser jp) throws IOException {

    JsonGenerator generator = new JsonFactory().createGenerator(index, JsonEncoding.UTF8);
    generator.useDefaultPrettyPrinter();

    generator.writeStartObject();//from w w w .j  av  a 2 s .  c o m

    JsonToken cur = jp.nextToken();
    while (cur.equals(JsonToken.START_OBJECT)) {
        long start = jp.getTokenLocation().getByteOffset();
        int depth = 1;
        while (depth > 0) {
            cur = jp.nextToken();
            if (cur.equals(JsonToken.START_OBJECT)) {
                depth++;
            } else if (cur.equals(JsonToken.END_OBJECT)) {
                depth--;
            } else if (cur.equals(JsonToken.FIELD_NAME) && depth == 1) {
                if (jp.getText().equals("@id")) {
                    cur = jp.nextToken();

                    String vName = jp.getText();
                    generator.writeNumberField(vName, start);
                } else {
                    report(jp, cur);
                }
            }
        }
        cur = jp.nextToken();
    }
    generator.writeEndObject();
    generator.close();

}

From source file:org.sead.repositories.reference.RefRepository.java

private static void report(JsonParser jp, JsonToken token) {
    boolean struct = token.isStructStart() || token.isStructEnd();
    try {//from  w w w.j a va  2 s  .c  o m
        String tag = struct ? token.asString() : jp.getText();
        log.trace("Tag: " + tag);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    long currentOffset = jp.getCurrentLocation().getByteOffset();
    long tokenOffset = jp.getTokenLocation().getByteOffset();
    log.trace("Cur: " + currentOffset + " tok: " + tokenOffset);
}

From source file:ren.hankai.cordwood.jackson.DateDeserializerTest.java

@Test
public void testDeserialize() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateDeserializer des = new DateDeserializer();
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"2018-09-01\"}");

    String val = null;
    do {//  www  . j av a 2 s .  c o  m
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    final Date expDate = DateUtils.parseDate(jp.getText(), "yyyy-MM-dd");
    Assert.assertTrue(DateUtils.isSameDay(date, expDate));
}

From source file:ren.hankai.cordwood.jackson.DateDeserializerTest.java

@Test
public void testDeserializeWithCustomFormat() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateDeserializer des = new DateDeserializer("yyyy|MM|dd");
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"2018|09|01\"}");

    String val = null;
    do {/*from  w w  w  . j  a  v  a  2  s  .co  m*/
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    final Date expDate = DateUtils.parseDate(jp.getText(), "yyyy|MM|dd");
    Assert.assertTrue(DateUtils.isSameDay(date, expDate));
}

From source file:ren.hankai.cordwood.jackson.DateTimeDeserializerTest.java

@Test
public void testDeserialize() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateTimeDeserializer des = new DateTimeDeserializer();
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"2018-09-01 13:23:12\"}");

    String val = null;
    do {/*from   w  w  w . jav a  2s. com*/
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    final Date expDate = DateUtils.parseDate(jp.getText(), "yyyy-MM-dd HH:mm:ss");
    Assert.assertTrue(DateUtils.truncatedEquals(date, expDate, Calendar.SECOND));
}

From source file:ren.hankai.cordwood.jackson.DateTimeDeserializerTest.java

@Test
public void testDeserializeWithCustomFormat() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateTimeDeserializer des = new DateTimeDeserializer("yyyy|MM|dd HH|mm|ss");
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"2018|09|01 13|23|12\"}");

    String val = null;
    do {//from  w  ww . j  ava  2  s. c  o m
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    final Date expDate = DateUtils.parseDate(jp.getText(), "yyyy|MM|dd HH|mm|ss");
    Assert.assertTrue(DateUtils.truncatedEquals(date, expDate, Calendar.SECOND));
}