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

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

Introduction

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

Prototype

public abstract JsonLocation getCurrentLocation();

Source Link

Document

Method that returns location of the last processed character; usually for error reporting purposes.

Usage

From source file:ca.ualberta.physics.cssdp.util.JSONMnemonicDeserializer.java

@Override
public Mnemonic deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    try {//from w  w w. ja va2s.  c  o  m
        return Mnemonic.of(jp.getText());
    } catch (IllegalArgumentException e) {
        String msg = "Could not deserialize json representation of Mnemonic " + jp.getText()
                + " into a Mnemonic object because " + e.getMessage();
        logger.error(msg, e);
        throw new JsonParseException(msg, jp.getCurrentLocation());
    }
}

From source file:org.emfjson.jackson.databind.deser.EObjectDeserializer.java

private void handleUnknownProperty(JsonParser jp, Resource resource, DeserializationContext ctxt)
        throws IOException {
    if (resource != null && ctxt.getConfig().hasDeserializationFeatures(FAIL_ON_UNKNOWN_PROPERTIES.getMask())) {
        resource.getErrors()//from w  w w .ja v a2 s  . c o m
                .add(new JSONException("Unknown feature " + jp.getCurrentName(), jp.getCurrentLocation()));
    }
    // we didn't find a feature so consume
    // the field and move on
    jp.nextToken();
    jp.skipChildren();
}

From source file:com.google.openrtb.json.AbstractOpenRtbJsonReader.java

/**
 * Read any extensions that may exist in a message.
 *
 * @param msg Builder of a message that may contain extensions
 * @param par The JSON parser, positioned at the "ext" field
 * @param <EB> Type of message builder being constructed
 * @throws IOException any parsing error
 *//*from w  w w  . j ava  2 s .co m*/
protected final <EB extends ExtendableBuilder<?, EB>> void readExtensions(EB msg, JsonParser par)
        throws IOException {
    @SuppressWarnings("unchecked")
    Set<OpenRtbJsonExtReader<EB>> extReaders = factory.getReaders((Class<EB>) msg.getClass());
    if (extReaders.isEmpty()) {
        par.skipChildren();
        return;
    }

    startObject(par);
    JsonToken tokLast = par.getCurrentToken();
    JsonLocation locLast = par.getCurrentLocation();

    while (true) {
        boolean extRead = false;
        for (OpenRtbJsonExtReader<EB> extReader : extReaders) {
            if (extReader.filter(par)) {
                extReader.read(msg, par);
                JsonToken tokNew = par.getCurrentToken();
                JsonLocation locNew = par.getCurrentLocation();
                boolean advanced = tokNew != tokLast || !locNew.equals(locLast);
                extRead |= advanced;

                if (!endObject(par)) {
                    return;
                } else if (advanced && par.getCurrentToken() != JsonToken.FIELD_NAME) {
                    tokLast = par.nextToken();
                    locLast = par.getCurrentLocation();
                } else {
                    tokLast = tokNew;
                    locLast = locNew;
                }
            }
        }

        if (!endObject(par)) {
            // Can't rely on this exit condition inside the for loop because no readers may filter.
            return;
        }

        if (!extRead) {
            // No field was consumed by any reader, so we need to skip the field to make progress.
            if (logger.isDebugEnabled()) {
                logger.debug("Extension field not consumed by any reader, skipping: {} @{}:{}",
                        par.getCurrentName(), locLast.getLineNr(), locLast.getCharOffset());
            }
            par.nextToken();
            par.skipChildren();
            tokLast = par.nextToken();
            locLast = par.getCurrentLocation();
        }
        // Else loop, try all readers again
    }
}

From source file:com.ryan.ryanreader.jsonwrap.JsonBufferedObject.java

@Override
protected void buildBuffered(final JsonParser jp) throws IOException {

    JsonToken jt;/*w w w  . ja v a  2  s .c o  m*/

    while ((jt = jp.nextToken()) != JsonToken.END_OBJECT) {

        if (jt != JsonToken.FIELD_NAME)
            throw new JsonParseException("Expecting field name, got " + jt.name(), jp.getCurrentLocation());

        final String fieldName = jp.getCurrentName();
        final JsonValue value = new JsonValue(jp);

        synchronized (this) {
            properties.put(fieldName, value);
            notifyAll();
        }

        value.buildInThisThread();
    }
}

From source file:com.joliciel.jochre.search.highlight.Snippet.java

private void read(JsonParser jsonParser) {
    try {//from  w w  w .j  a  va2  s  . co m
        if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT)
            throw new RuntimeException("Expected START_OBJECT, but was " + jsonParser.getCurrentToken() + " at "
                    + jsonParser.getCurrentLocation());
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jsonParser.getCurrentName();

            if (fieldName.equals("docId")) {
                this.docId = jsonParser.nextIntValue(0);
            } else if (fieldName.equals("field")) {
                this.field = jsonParser.nextTextValue();
            } else if (fieldName.equals("start")) {
                this.startOffset = jsonParser.nextIntValue(0);
            } else if (fieldName.equals("end")) {
                this.endOffset = jsonParser.nextIntValue(0);
            } else if (fieldName.equals("score")) {
                jsonParser.nextValue();
                this.score = jsonParser.getDoubleValue();
                this.scoreCalculated = true;
            } else if (fieldName.equals("terms")) {
                if (jsonParser.nextToken() != JsonToken.START_ARRAY)
                    throw new RuntimeException("Expected START_ARRAY, but was " + jsonParser.getCurrentToken()
                            + " at " + jsonParser.getCurrentLocation());
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT)
                        throw new RuntimeException("Expected START_OBJECT, but was "
                                + jsonParser.getCurrentToken() + " at " + jsonParser.getCurrentLocation());
                    int termDocId = docId;
                    String termField = field;
                    int termStart = 0;
                    int termEnd = 0;
                    int pageIndex = 0;
                    int imageIndex = 0;
                    double weight = 0.0;
                    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                        String termFieldName = jsonParser.getCurrentName();
                        if (termFieldName.equals("docId")) {
                            termDocId = jsonParser.nextIntValue(0);
                        } else if (termFieldName.equals("field")) {
                            termField = jsonParser.nextTextValue();
                        } else if (termFieldName.equals("start")) {
                            termStart = jsonParser.nextIntValue(0);
                        } else if (termFieldName.equals("end")) {
                            termEnd = jsonParser.nextIntValue(0);
                        } else if (termFieldName.equals("pageIndex")) {
                            pageIndex = jsonParser.nextIntValue(0);
                        } else if (termFieldName.equals("imageIndex")) {
                            imageIndex = jsonParser.nextIntValue(0);
                        } else if (termFieldName.equals("weight")) {
                            jsonParser.nextValue();
                            weight = jsonParser.getDoubleValue();
                        } else {
                            throw new RuntimeException("Unexpected term field name: " + termFieldName + " at "
                                    + jsonParser.getCurrentLocation());
                        }
                    }
                    HighlightTerm highlightTerm = new HighlightTerm(termDocId, termField, termStart, termEnd,
                            imageIndex, pageIndex);
                    highlightTerm.setWeight(weight);
                    this.highlightTerms.add(highlightTerm);
                }
            } else {
                throw new RuntimeException(
                        "Unexpected field name: " + fieldName + " at " + jsonParser.getCurrentLocation());
            }
        }
    } catch (JsonParseException e) {
        LOG.error(e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOG.error(e);
        throw new RuntimeException(e);
    }
}

From source file:org.dswarm.graph.json.deserializer.ModelDeserializer.java

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

    final ObjectCodec oc = jp.getCodec();

    if (oc == null) {

        return null;
    }//  w  ww .  ja va  2s .  co m

    final JsonNode node = oc.readTree(jp);

    if (node == null) {

        return null;
    }

    if (!ArrayNode.class.isInstance(node)) {

        throw new JsonParseException("expected a JSON array full of resource objects of the model",
                jp.getCurrentLocation());
    }

    if (node.size() <= 0) {

        return null;
    }

    final Model model = new Model();

    for (final JsonNode resourceNode : node) {

        final Resource resource = resourceNode.traverse(oc).readValueAs(Resource.class);
        model.addResource(resource);
    }

    return model;
}

From source file:reactor.js.core.json.JSObjectDeserializer.java

@SuppressWarnings("unchecked")
@Override/* w  w  w .  jav  a2 s. c o  m*/
public JSObject deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    Object obj = jp.readValueAs(Object.class);

    Object parent = ctxt.getAttribute("parent");
    ctxt.setAttribute("parent", obj);

    if (Map.class.isInstance(obj)) {
        return new JavaScriptObject((Map) obj, null, null, parent);
    } else if (List.class.isInstance(obj)) {
        return new JavaScriptArray((List) obj);
    } else {
        throw new JsonMappingException("Cannot convert value to a valid JSON object", jp.getCurrentLocation());
    }
}

From source file:ninja.leaping.configurate.json.JSONConfigurationLoader.java

private void parseArray(JsonParser parser, ConfigurationNode node) throws IOException {
    JsonToken token;//  w w  w. j  av a2 s  .  c  om
    while ((token = parser.nextToken()) != null) {
        switch (token) {
        case END_ARRAY:
            return;
        default:
            parseValue(parser, node.getAppendedChild());
        }
    }
    throw new JsonParseException("Reached end of stream with unclosed array!", parser.getCurrentLocation());

}

From source file:ninja.leaping.configurate.json.JSONConfigurationLoader.java

private void parseObject(JsonParser parser, ConfigurationNode node) throws IOException {
    JsonToken token;//from www .ja  v  a 2s  .  c o m
    while ((token = parser.nextToken()) != null) {
        switch (token) {
        case END_OBJECT:
            return;
        default:
            parseValue(parser, node.getChild(parser.getCurrentName()));
        }
    }
    throw new JsonParseException("Reached end of stream with unclosed array!", parser.getCurrentLocation());
}

From source file:com.ntsync.shared.RawContact.java

private static RawOrganizationData readOrg(String rowId, JsonParser jp) throws IOException {
    String orgname = null;// w w w  .java 2 s.  c  o m
    OrganizationType orgtype = null;
    String orgLabel = null;
    String department = null;
    String jobTitle = null;
    String title = null;
    boolean isSuperPrimary = false;
    boolean isPrimary = false;

    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String namefield = jp.getCurrentName();
        // move to value
        if (jp.nextToken() == null) {
            throw new JsonParseException("Invalid JSON-Structure. End of Object missing.",
                    jp.getCurrentLocation());
        }
        if (ContactConstants.DATA.equals(namefield)) {
            orgname = jp.getValueAsString();
        } else if (ContactConstants.TYPE.equals(namefield)) {
            orgtype = OrganizationType.fromVal(jp.getValueAsInt());
        } else if (ContactConstants.PRIMARY.equals(namefield)) {
            isPrimary = jp.getValueAsBoolean();
        } else if (ContactConstants.SUPERPRIMARY.equals(namefield)) {
            isSuperPrimary = jp.getValueAsBoolean();
        } else if (ContactConstants.LABEL.equals(namefield)) {
            orgLabel = jp.getValueAsString();
        } else if (ContactConstants.ORGANIZATION_DEPARTMENT.equals(namefield)) {
            department = jp.getValueAsString();
        } else if (ContactConstants.ORGANIZATION_TITLE.equals(namefield)) {
            title = jp.getValueAsString();
        } else if (ContactConstants.ORGANIZATION_JOB.equals(namefield)) {
            jobTitle = jp.getValueAsString();
        } else {
            LOG.error("Unrecognized Organization-field for row with Id:" + rowId + " Fieldname:" + namefield);
        }
    }

    if (orgtype == null) {
        orgtype = OrganizationType.TYPE_OTHER;
    }

    return new RawOrganizationData(orgname, orgtype, orgLabel, isPrimary, isSuperPrimary, title, department,
            jobTitle);
}