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:json.internal.BaseJValueObjectDeserializer.java

public JValue deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
    switch (jp.getCurrentToken()) {
    case START_ARRAY:
        return parseArray(jp, ctx);
    case START_OBJECT:
        return parseObject(jp, ctx);
    case VALUE_STRING:
        return jString(jp.getText());
    case VALUE_NUMBER_INT:
    case VALUE_NUMBER_FLOAT:
        return JNumber$.MODULE$.apply(_parseDouble(jp, ctx));
    case VALUE_TRUE:
        return JTrue$.MODULE$;
    case VALUE_FALSE:
        return JFalse$.MODULE$;
    case VALUE_NULL:
        return JNull$.MODULE$;
    case VALUE_EMBEDDED_OBJECT:
        throwException("JSON parser - unexpected embedded object");
        return null;
    case END_OBJECT:
        throwException("JSON parser - unexpected end of object");
        return null;
    case FIELD_NAME:
        throwException("JSON parser - unexpected field name");
        return null;
    case END_ARRAY:
        throwException("JSON parser - unexpected end of array");
        return null;
    case NOT_AVAILABLE:
        throwException("JSON parser - unexpected end of token");
        return null;
    default:// w w  w.  j  a va  2  s . c  om
        throwException("JSON parser - unexpected token " + jp.getCurrentToken());
        return null;
    }
}

From source file:com.sdl.odata.unmarshaller.json.core.JsonProcessor.java

/**
 * Process OData links.//from  w ww . ja va2s. c om
 *
 * @param jsonParser the parser
 * @throws IOException If unable to read input parser
 */
private void processLinks(JsonParser jsonParser) throws IOException {

    LOG.info("@odata.bind tag found - start parsing");

    final String fullLinkFieldName = jsonParser.getText();
    final String key = fullLinkFieldName.substring(0, fullLinkFieldName.indexOf(ODATA_BIND));
    JsonToken token = jsonParser.nextToken();
    if (token != JsonToken.START_ARRAY) {
        // Single link
        links.put(key, processLink(jsonParser));
    } else {
        // Array of links
        final List<String> linksList = new ArrayList<>();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            linksList.add(processLink(jsonParser));
        }
        this.links.put(key, linksList);

    }
}

From source file:de.taimos.dvalin.interconnect.model.DateTimeDeserializerWithTZ.java

@Override
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();/*from w w  w.j  av  a 2 s .  c  om*/
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new DateTime(jp.getLongValue(), DateTimeZone.forTimeZone(ctxt.getTimeZone()));
    }
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (str.length() == 0) { // [JACKSON-360]
            return null;
        }
        // catch serialized time zones
        if ((str.charAt(str.length() - 6) == '+') || (str.charAt(str.length() - 1) == 'Z')
                || (str.charAt(str.length() - 6) == '-')) {
            return new DateTime(str);
        }
        return new DateTime(str, DateTimeZone.forTimeZone(ctxt.getTimeZone()));
    }
    ctxt.handleUnexpectedToken(this.handledType(), jp);
    // never reached
    return null;
}

From source file:com.sdl.odata.renderer.json.writer.JsonPropertyWriterTest.java

private Map<String, Object> getMapFromJson(String json) throws IOException {
    Map<String, Object> map = new HashMap<>();
    JsonParser jsonParser = new JsonFactory().createParser(json);
    jsonParser.nextToken();//from w ww  .  j av a  2 s  .c o m
    while (jsonParser.nextToken() != null) {
        String key = jsonParser.getText();
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
            map.put(key, getJsonArray(jsonParser));
        } else {
            map.put(key, jsonParser.getText());
        }
    }
    return map;
}

From source file:com.inversoft.json.ZonedDateTimeDeserializer.java

@Override
public ZonedDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();/* ww  w. j  a  va2s . c o  m*/
    long value;
    if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) {
        value = jp.getLongValue();
    } else if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (str.length() == 0) {
            return null;
        }

        try {
            value = Long.parseLong(str);
        } catch (NumberFormatException e) {
            throw ctxt.mappingException(handledType());
        }
    } else {
        throw ctxt.mappingException(handledType());
    }

    return ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.UTC);
}

From source file:com.sdl.odata.renderer.json.writer.JsonPropertyWriterTest.java

private Map<String, String> getJsonObject(JsonParser jsonParser) throws IOException {
    Map<String, String> map = new HashMap<>();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        String key = jsonParser.getText();
        jsonParser.nextToken();/*from ww w .ja  va2 s  . co m*/
        map.put(key, jsonParser.getText());
    }
    return map;
}

From source file:com.netflix.spectator.tdigest.Json.java

private TDigestMeasurement decode(JsonParser parser) throws IOException {
    expect(parser, JsonToken.START_OBJECT);
    require("name".equals(parser.nextFieldName()), "expected name");
    Id id = registry.createId(parser.nextTextValue());
    while (parser.nextToken() == JsonToken.FIELD_NAME) {
        id = id.withTag(parser.getText(), parser.nextTextValue());
    }/*from w w  w .j  a  va2 s  .  co  m*/
    long t = parser.nextLongValue(-1L);
    expect(parser, JsonToken.VALUE_EMBEDDED_OBJECT);
    TDigest v = AVLTreeDigest.fromBytes(ByteBuffer.wrap(parser.getBinaryValue()));
    expect(parser, JsonToken.END_ARRAY);
    return new TDigestMeasurement(id, t, v);
}

From source file:com.sdl.odata.unmarshaller.json.core.JsonProcessor.java

/**
 * This method fills odataMap that contains special tags "odata" with key:value When the key token is over, you need
 * to execute nextToken() in order to get value.
 *
 * @param jsonParser the parser/*from   w  ww .  j  a  v  a2 s.  c  o m*/
 * @throws IOException If unable to read input parser
 */
private void processSpecialTags(JsonParser jsonParser) throws IOException {
    LOG.info("@odata tags found - start parsing");
    String key = jsonParser.getCurrentName();
    jsonParser.nextToken();
    String value = jsonParser.getText();
    odataValues.put(key, value);
}

From source file:com.tomtom.speedtools.xmladapters.LocalDateAdapterTest.java

@Test
@SuppressWarnings({ "OverlyBroadThrowsClause", "SuppressionAnnotation" })
public void testJsonDeserializer() throws IOException {
    LOG.info("testJsonDeserializer");

    final JsonLocalDateDeserializer localDateDeserializer = new JsonLocalDateDeserializer();

    final JsonParser jp = Mockito.mock(JsonParser.class);
    final DeserializationContext ctxt = Mockito.mock(DeserializationContext.class);

    // Valid date.
    final LocalDate localDate = new LocalDate(1234567890123L, DateTimeZone.UTC);
    Mockito.when(jp.getText()).thenReturn("2009-02-13");
    Assert.assertEquals(localDate, localDateDeserializer.deserialize(jp, ctxt));

    // Null./*from  ww  w .j  a  v a2 s  . c  om*/
    Mockito.reset(jp);
    Assert.assertNull(localDateDeserializer.deserialize(jp, ctxt));

    try {
        // Month and day switched.
        Mockito.when(jp.getText()).thenReturn("2009-13-02");
        localDateDeserializer.deserialize(jp, ctxt);
        Assert.fail();
    } catch (final IllegalFieldValueException ignored) {
        //ignored.
    }

    try {
        // Month zero.
        Mockito.when(jp.getText()).thenReturn("2009-00-02");
        localDateDeserializer.deserialize(jp, ctxt);
        Assert.fail();
    } catch (final IllegalFieldValueException ignored) {
        //ignored.
    }

    try {
        // Nonexistent day in February.
        Mockito.when(jp.getText()).thenReturn("2009-02-31");
        localDateDeserializer.deserialize(jp, ctxt);
        Assert.fail();
    } catch (final IllegalFieldValueException ignored) {
        //ignored.
    }
}

From source file:org.o3project.ocnrm.model.bind.OduBindingData.java

@Override
public void bind(String name, String resource) throws JsonParseException, JsonMappingException, IOException {
    JsonFactory factory = new JsonFactory();
    JsonParser jp = factory.createParser(resource.toString());
    jp.nextToken();/*  www  . j av  a 2  s  .  c  om*/
    OduMapping terminationPoint = new OduMapping();
    terminationPoint.setName(name);
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        jp.nextToken();
        if ("dpid".equals(fieldname)) {
            terminationPoint.setDpid(jp.getText());
        } else if ("port".equals(fieldname)) {
            terminationPoint.setPort(jp.getText());
        } else if ("odutype".equals(fieldname)) {
            terminationPoint.setOdutype(jp.getText());
        } else if ("ts".equals(fieldname)) {
            String ts = jp.getText();
            terminationPoint.setTs(ts);
        } else if ("tpn".equals(fieldname)) {
            terminationPoint.setTpn(jp.getText());
        } else {
            throw new IllegalStateException("Unrecognized field '" + fieldname + "'!");
        }
        bindMap.put(terminationPoint.getName(), terminationPoint);
    }
    jp.close();
}