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

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

Introduction

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

Prototype

public abstract JsonToken nextToken() throws IOException, JsonParseException;

Source Link

Document

Main iteration method, which will advance stream enough to determine type of the next token, if any.

Usage

From source file:com.mozilla.bagheera.validation.Validator.java

public boolean isValidJson(String json) {
    boolean isValid = false;
    JsonParser parser = null;
    try {//w  w w  .j a  va2s .c o  m
        parser = jsonFactory.createJsonParser(json);
        while (parser.nextToken() != null) {
            // noop
        }
        isValid = true;
    } catch (JsonParseException ex) {
        LOG.error("JSON parse error");
    } catch (IOException e) {
        LOG.error("JSON IO error");
    } finally {
        if (parser != null) {
            try {
                parser.close();
            } catch (IOException e) {
                LOG.error("Error closing JSON parser", e);
            }
        }
    }

    return isValid;
}

From source file:io.syndesis.core.json.StringTrimmingJsonDeserializerTest.java

private JsonParser parserWithString(final String value) throws JsonParseException, IOException {
    final JsonParser parser = new ObjectMapper().getFactory()
            .createParser(Optional.ofNullable(value).map(v -> "[\"" + v + "\"]").orElse("[null]"));
    parser.nextToken();// array
    parser.nextToken();// string

    return parser;
}

From source file:org.apache.ode.jacob.soup.jackson.ChannelRefDeserializer.java

@Override
public ChannelRef deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    Object target = null;/* w ww .  ja v a  2  s  .  c om*/

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        if (jp.getCurrentToken() == JsonToken.FIELD_NAME) {
            // if we're not already on the field, advance by one.
            jp.nextToken();
        }

        if ("target".equals(fieldname)) {
            target = jp.readValueAs(Object.class);
        }
    }

    if (target == null) {
        throw ctxt.mappingException(ChannelRef.class);
    }

    return new ChannelRef(target);
}

From source file:com.joliciel.jochre.search.webClient.SearchResults.java

public SearchResults(String json) {
    try {//from   w  w w  . jav a 2s.c  o  m
        scoreDocs = new ArrayList<SearchDocument>();
        Reader reader = new StringReader(json);
        JsonFactory jsonFactory = new JsonFactory(); // or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory 
        JsonParser jsonParser = jsonFactory.createJsonParser(reader);
        // Sanity check: verify that we got "Json Object":
        if (jsonParser.nextToken() != JsonToken.START_OBJECT)
            throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at "
                    + jsonParser.getCurrentLocation());
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {

            String baseName = jsonParser.getCurrentName();
            LOG.debug("Found baseName: " + baseName);
            if (jsonParser.nextToken() != JsonToken.START_OBJECT)
                throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken()
                        + " at " + jsonParser.getCurrentLocation());

            SearchDocument doc = new SearchDocument(baseName, jsonParser);
            scoreDocs.add(doc);

        } // next scoreDoc
    } catch (JsonParseException e) {
        LOG.error(e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOG.error(e);
        throw new RuntimeException(e);
    }
}

From source file:com.taveloper.http.test.pojo.parse.ActivityFeedParse.java

public ActivityFeed readJson(JsonParser in) throws JsonParseException, IOException {
    //        System.out.println("ActivityFeedParse.readJson");
    JsonToken curToken = in.nextToken();
    ActivityFeed object = new ActivityFeed();
    while (curToken == JsonToken.FIELD_NAME) {
        String curName = in.getText();
        JsonToken nextToken = in.nextToken();
        if ("items".equals(curName)) {
            ArrayList<Activity> arrayList = new ArrayList<Activity>();
            ActivityParse activityParse = new ActivityParse();
            switch (nextToken) {
            case START_ARRAY:
                while (in.nextToken() != JsonToken.END_ARRAY) {
                    arrayList.add(activityParse.readJson(in));
                }/*  w  w w  .  j ava 2s. c o  m*/
                break;
            case START_OBJECT:
                arrayList.add(activityParse.readJson(in));
                break;
            default:
                throw new IllegalArgumentException(
                        "unexpected JSON node type: " + nextToken + in.getCurrentName());
            }
            object.setActivities(arrayList);
        }
        curToken = in.nextToken();
    }
    return object;
}

From source file:javaslang.jackson.datatype.deserialize.MapDeserializer.java

@Override
public Map<?, ?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    final java.util.List<Tuple2<Object, Object>> result = new java.util.ArrayList<>();
    while (p.nextToken() != JsonToken.END_OBJECT) {
        String name = p.getCurrentName();
        Object key = keyDeserializer.deserializeKey(name, ctxt);
        p.nextToken();/*from  ww  w  .j  a v  a 2  s  . c om*/
        result.add(Tuple.of(key, valueDeserializer.deserialize(p, ctxt)));
    }
    if (TreeMap.class.isAssignableFrom(handledType())) {
        return TreeMap.ofEntries(keyComparator, result);
    }
    if (LinkedHashMap.class.isAssignableFrom(handledType())) {
        return LinkedHashMap.ofEntries(result);
    }
    // default deserialization [...] -> Map
    return HashMap.ofEntries(result);
}

From source file:com.github.jonpeterson.jackson.module.versioning.VersionedModelDeserializer.java

@Override
public T deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode jsonNode = parser.readValueAsTree();

    if (!(jsonNode instanceof ObjectNode))
        throw context.mappingException("value must be a JSON object");

    ObjectNode modelData = (ObjectNode) jsonNode;

    JsonNode modelVersionNode = modelData.remove(jsonVersionedModel.propertyName());

    String modelVersion = null;//  w w  w  .j  a  va  2  s .  com
    if (modelVersionNode != null)
        modelVersion = modelVersionNode.asText();

    if (modelVersion == null)
        modelVersion = jsonVersionedModel.defaultDeserializeToVersion();

    if (modelVersion.isEmpty())
        throw context.mappingException("'" + jsonVersionedModel.propertyName()
                + "' property was null and defaultDeserializeToVersion was not set");

    // convert the model if converter specified and model needs converting
    if (converter != null && (jsonVersionedModel.alwaysConvert()
            || !modelVersion.equals(jsonVersionedModel.currentVersion())))
        modelData = converter.convert(modelData, modelVersion, jsonVersionedModel.currentVersion(),
                context.getNodeFactory());

    // set the serializeToVersionProperty value to the source model version if the defaultToSource property is true
    if (serializeToVersionAnnotation != null && serializeToVersionAnnotation.defaultToSource())
        modelData.put(serializeToVersionProperty.getName(), modelVersion);

    JsonParser postInterceptionParser = new TreeTraversingParser(modelData, parser.getCodec());
    postInterceptionParser.nextToken();
    return delegate.deserialize(postInterceptionParser, context);
}

From source file:net.troja.eve.crest.CrestDataProcessor.java

private String processNext(final JsonParser jsonParser) throws IOException {
    String next = null;/*  w w w .  j a  va2 s.  c  o  m*/
    jsonParser.nextToken();
    if (JsonPaths.HREF.equals(jsonParser.getCurrentName())) {
        jsonParser.nextToken();
        next = jsonParser.getText();
        jsonParser.nextToken();
    }
    return next;
}

From source file:ch.rasc.wampspring.message.EventMessage.java

public EventMessage(JsonParser jp, WampSession wampSession) throws IOException {
    super(WampMessageType.EVENT);

    if (jp.nextToken() != JsonToken.VALUE_STRING) {
        throw new IOException();
    }/*from  w w w .  j a  va  2 s.  co  m*/
    setTopicURI(replacePrefix(jp.getValueAsString(), wampSession));

    jp.nextToken();
    this.event = jp.readValueAs(Object.class);
}