Example usage for com.fasterxml.jackson.core JsonParseException JsonParseException

List of usage examples for com.fasterxml.jackson.core JsonParseException JsonParseException

Introduction

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

Prototype

public JsonParseException(String msg, JsonLocation loc, Throwable root) 

Source Link

Usage

From source file:org.anhonesteffort.flock.registration.model.SubscriptionPlan.java

public static SubscriptionPlan buildFromSerialized(Integer planType, String serializedPlan)
        throws JsonParseException {
    try {//  w ww  .  j a v a  2  s  .c  o  m
        switch (planType) {
        case PLAN_TYPE_NONE:
            return PLAN_NONE;

        case PLAN_TYPE_STRIPE:
            return MapperUtil.getMapper().readValue(serializedPlan, StripePlan.class);

        case PLAN_TYPE_GOOGLE:
            return MapperUtil.getMapper().readValue(serializedPlan, GooglePlan.class);

        default:
            Log.e(SubscriptionPlan.class.getName(), "unknown plan type" + planType);
            return PLAN_NONE;
        }
    } catch (IOException e) {
        throw new JsonParseException("unable to build plan for type " + planType, null, e);
    }
}

From source file:com.msopentech.odatajclient.engine.metadata.edm.v4.annotation.DynExprConstructDeserializer.java

private AbstractElOrAttrConstruct getElOrAttrInstance(final String simpleClassName) throws JsonParseException {
    try {//www  .  j  a  v a 2 s.co m
        @SuppressWarnings("unchecked")
        Class<? extends AbstractElOrAttrConstruct> elOrAttrClass = (Class<? extends AbstractElOrAttrConstruct>) ClassUtils
                .getClass(getClass().getPackage().getName() + "." + simpleClassName);
        return elOrAttrClass.newInstance();
    } catch (Exception e) {
        throw new JsonParseException("Could not instantiate " + simpleClassName, JsonLocation.NA, e);
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.JSONPropertySerializer.java

/**
 * {@inheritDoc }// w ww  .j a va2s .c  o m
 */
@Override
public void serialize(final JSONProperty property, final JsonGenerator jgen, final SerializerProvider provider)
        throws IOException, JsonProcessingException {

    jgen.writeStartObject();

    if (property.getMetadata() != null) {
        jgen.writeStringField(ODataConstants.JSON_METADATA, property.getMetadata().toASCIIString());
    }

    final Element content = property.getContent();
    if (XMLUtils.hasOnlyTextChildNodes(content)) {
        jgen.writeStringField(ODataConstants.JSON_VALUE, content.getTextContent());
    } else {
        try {
            final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
            final Document document = builder.newDocument();
            final Element wrapper = document.createElement(ODataConstants.ELEM_PROPERTY);

            if (XMLUtils.hasElementsChildNode(content)) {
                wrapper.appendChild(document.renameNode(document.importNode(content, true), null,
                        ODataConstants.JSON_VALUE));

                DOMTreeUtils.writeSubtree(jgen, wrapper);
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                wrapper.appendChild(document.renameNode(document.importNode(content, true), null,
                        ODataConstants.JSON_VALUE));

                DOMTreeUtils.writeSubtree(jgen, wrapper, true);
            } else {
                DOMTreeUtils.writeSubtree(jgen, content);
            }
        } catch (Exception e) {
            throw new JsonParseException("Cannot serialize property", JsonLocation.NA, e);
        }
    }

    jgen.writeEndObject();
}

From source file:com.streamsets.datacollector.json.JsonObjectReaderImpl.java

@Override
public Object read() throws IOException {
    if (closed) {
        throw new IOException("The parser is closed");
    }/*www.ja  v  a  2s  . c o  m*/
    try {
        Object value = null;
        switch (mode) {
        case ARRAY_OBJECTS:
            value = readObjectFromArray();
            break;
        case MULTIPLE_OBJECTS:
            value = readObjectFromStream();
            break;
        }
        return value;
    } catch (RuntimeJsonMappingException ex) {
        throw new JsonParseException(ex.toString(), jsonParser.getTokenLocation(), ex);
    }
}

From source file:org.commonjava.maven.atlas.graph.jackson.ProjectRelationshipDeserializer.java

@Override
public T deserialize(final JsonParser jp, final DeserializationContext ctx)
        throws JsonProcessingException, IOException {
    Map<String, Object> ast = new HashMap<String, Object>();
    Map<String, JsonLocation> locations = new HashMap<String, JsonLocation>();

    JsonToken token = jp.getCurrentToken();
    String currentField = null;/*w  ww .  j  ava  2 s .  co  m*/
    List<String> currentArry = null;

    Logger logger = LoggerFactory.getLogger(getClass());
    do {
        //                logger.info( "Token: {}", token );
        switch (token) {
        case START_ARRAY: {
            //                        logger.info( "Starting array for field: {}", currentField );
            currentArry = new ArrayList<String>();
            break;
        }
        case END_ARRAY:
            //                        logger.info( "Ending array for field: {}", currentField );
            locations.put(currentField, jp.getCurrentLocation());
            ast.put(currentField, currentArry);
            currentArry = null;
            break;
        case FIELD_NAME:
            currentField = jp.getCurrentName();
            break;
        case VALUE_STRING:
            if (currentArry != null) {
                currentArry.add(jp.getText());
            } else {
                locations.put(currentField, jp.getCurrentLocation());
                ast.put(currentField, jp.getText());
            }
            break;
        case VALUE_NUMBER_INT:
            locations.put(currentField, jp.getCurrentLocation());
            ast.put(currentField, jp.getIntValue());
            break;
        case VALUE_NUMBER_FLOAT:
            locations.put(currentField, jp.getCurrentLocation());
            ast.put(currentField, jp.getFloatValue());
            break;
        case VALUE_TRUE:
            locations.put(currentField, jp.getCurrentLocation());
            ast.put(currentField, Boolean.TRUE);
            break;
        case VALUE_FALSE:
            locations.put(currentField, jp.getCurrentLocation());
            ast.put(currentField, Boolean.FALSE);
            break;
        }

        token = jp.nextToken();
    } while (token != JsonToken.END_OBJECT);

    StringBuilder sb = new StringBuilder();
    sb.append("AST is:");
    for (String field : ast.keySet()) {
        Object value = ast.get(field);
        sb.append("\n  ").append(field).append(" = ");
        if (value == null) {
            sb.append("null");
        } else {
            sb.append(value).append("  (type: ").append(value.getClass().getSimpleName()).append(")");
        }
    }

    logger.debug(sb.toString());

    final RelationshipType type = RelationshipType.getType((String) ast.get(RELATIONSHIP_TYPE));

    final String uri = (String) ast.get(POM_LOCATION_URI);
    URI pomLocation;
    if (uri == null) {
        pomLocation = RelationshipUtils.POM_ROOT_URI;
    } else {
        try {
            pomLocation = new URI(uri);
        } catch (final URISyntaxException e) {
            throw new JsonParseException("Invalid " + POM_LOCATION_URI + ": '" + uri + "': " + e.getMessage(),
                    locations.get(POM_LOCATION_URI), e);
        }
    }

    Collection<URI> sources = new HashSet<URI>();
    List<String> srcs = (List<String>) ast.get(SOURCE_URIS);
    if (srcs != null) {
        for (String u : srcs) {
            try {
                sources.add(new URI(u));
            } catch (URISyntaxException e) {
                throw new JsonParseException("Failed to parse source URI: " + u, locations.get(SOURCE_URIS));
            }
        }
    }

    String decl = (String) ast.get(DECLARING_REF);
    final ProjectVersionRef declaring = SimpleProjectVersionRef.parse(decl);

    String tgt = (String) ast.get(TARGET_REF);
    Integer index = (Integer) ast.get(INDEX);
    if (index == null) {
        index = 0;
    }

    // handle null implicitly by comparing to true.
    boolean managed = Boolean.TRUE.equals(ast.get(MANAGED));
    boolean inherited = Boolean.TRUE.equals(ast.get(INHERITED));
    boolean mixin = Boolean.TRUE.equals(ast.get(MIXIN));
    boolean optional = Boolean.TRUE.equals(ast.get(OPTIONAL));

    ProjectRelationship<?, ?> rel = null;
    switch (type) {
    case DEPENDENCY: {
        final ArtifactRef target = SimpleArtifactRef.parse(tgt);

        String scp = (String) ast.get(SCOPE);
        final DependencyScope scope;
        if (scp == null) {
            scope = DependencyScope.compile;
        } else {
            scope = DependencyScope.getScope(scp);
        }

        rel = new SimpleDependencyRelationship(sources, pomLocation, declaring, target, scope, index, managed,
                inherited, optional);
        break;
    }
    case EXTENSION: {
        final ProjectVersionRef target = SimpleProjectVersionRef.parse(tgt);

        rel = new SimpleExtensionRelationship(sources, pomLocation, declaring, target, index, inherited);
        break;
    }
    case PARENT: {
        final ProjectVersionRef target = SimpleProjectVersionRef.parse(tgt);

        rel = new SimpleParentRelationship(sources, declaring, target);
        break;
    }
    case PLUGIN: {
        final ProjectVersionRef target = SimpleProjectVersionRef.parse(tgt);

        Boolean report = (Boolean) ast.get(REPORTING);
        rel = new SimplePluginRelationship(sources, pomLocation, declaring, target, index, managed,
                Boolean.TRUE.equals(report), inherited);
        break;
    }
    case PLUGIN_DEP: {
        String plug = (String) ast.get(PLUGIN_REF);
        if (plug == null) {
            throw new JsonParseException(
                    "No plugin reference (field: " + PLUGIN_REF + ") found in plugin-dependency relationship!",
                    jp.getCurrentLocation());
        }

        final ProjectRef plugin = SimpleProjectRef.parse(plug);
        final ArtifactRef target = SimpleArtifactRef.parse(tgt);

        rel = new SimplePluginDependencyRelationship(sources, pomLocation, declaring, plugin, target, index,
                managed, inherited);
        break;
    }
    case BOM: {
        final ProjectVersionRef target = SimpleProjectVersionRef.parse(tgt);

        rel = new SimpleBomRelationship(sources, pomLocation, declaring, target, index, inherited, mixin);
        break;
    }
    }

    return (T) rel;
}

From source file:com.msopentech.odatajclient.engine.data.json.JSONPropertyDeserializer.java

@Override
protected JSONProperty doDeserializeV3(final JsonParser parser, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    final JSONProperty property = new JSONProperty();

    if (tree.hasNonNull(ODataConstants.JSON_METADATA)) {
        property.setMetadata(URI.create(tree.get(ODataConstants.JSON_METADATA).textValue()));
        tree.remove(ODataConstants.JSON_METADATA);
    }/*from  w ww . j ava  2  s.co m*/

    try {
        final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = builder.newDocument();

        Element content = document.createElement(ODataConstants.ELEM_PROPERTY);

        if (property.getMetadata() != null) {
            final String metadataURI = property.getMetadata().toASCIIString();
            final int dashIdx = metadataURI.lastIndexOf('#');
            if (dashIdx != -1) {
                content.setAttribute(ODataConstants.ATTR_M_TYPE, metadataURI.substring(dashIdx + 1));
            }
        }

        JsonNode subtree = null;
        if (tree.has(ODataConstants.JSON_VALUE)) {
            if (tree.has(ODataConstants.JSON_TYPE)
                    && StringUtils.isBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {

                content.setAttribute(ODataConstants.ATTR_M_TYPE, tree.get(ODataConstants.JSON_TYPE).asText());
            }

            final JsonNode value = tree.get(ODataConstants.JSON_VALUE);
            if (value.isValueNode()) {
                content.appendChild(document.createTextNode(value.asText()));
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                subtree = tree.objectNode();
                ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE, tree.get(ODataConstants.JSON_VALUE));
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE + "@" + ODataConstants.JSON_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
            } else {
                subtree = tree.get(ODataConstants.JSON_VALUE);
            }
        } else {
            subtree = tree;
        }

        if (subtree != null) {
            DOMTreeUtilsV3.buildSubtree(content, subtree);
        }

        final List<Node> children = XMLUtils.getChildNodes(content, Node.ELEMENT_NODE);
        if (children.size() == 1) {
            final Element value = (Element) children.iterator().next();
            if (ODataConstants.JSON_VALUE.equals(XMLUtils.getSimpleName(value))) {
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    value.setAttribute(ODataConstants.ATTR_M_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
                content = value;
            }
        }

        property.setContent(content);
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build property", parser.getCurrentLocation(), e);
    }

    return property;
}

From source file:com.msopentech.odatajclient.engine.data.impl.JSONPropertyDeserializer.java

@Override
protected JSONProperty doDeserialize(final JsonParser parser, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);

    final JSONProperty property = new JSONProperty();

    if (tree.hasNonNull(ODataConstants.JSON_METADATA)) {
        property.setMetadata(URI.create(tree.get(ODataConstants.JSON_METADATA).textValue()));
        tree.remove(ODataConstants.JSON_METADATA);
    }//w  ww  .j  av a2  s. co m

    try {
        final DocumentBuilder builder = XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = builder.newDocument();

        Element content = document.createElement(ODataConstants.ELEM_PROPERTY);

        if (property.getMetadata() != null) {
            final String metadataURI = property.getMetadata().toASCIIString();
            final int dashIdx = metadataURI.lastIndexOf('#');
            if (dashIdx != -1) {
                content.setAttribute(ODataConstants.ATTR_M_TYPE, metadataURI.substring(dashIdx + 1));
            }
        }

        JsonNode subtree = null;
        if (tree.has(ODataConstants.JSON_VALUE)) {
            if (tree.has(ODataConstants.JSON_TYPE)
                    && StringUtils.isBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {

                content.setAttribute(ODataConstants.ATTR_M_TYPE, tree.get(ODataConstants.JSON_TYPE).asText());
            }

            final JsonNode value = tree.get(ODataConstants.JSON_VALUE);
            if (value.isValueNode()) {
                content.appendChild(document.createTextNode(value.asText()));
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                subtree = tree.objectNode();
                ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE, tree.get(ODataConstants.JSON_VALUE));
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE + "@" + ODataConstants.JSON_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
            } else {
                subtree = tree.get(ODataConstants.JSON_VALUE);
            }
        } else {
            subtree = tree;
        }

        if (subtree != null) {
            JSONDOMTreeUtils.buildSubtree(client, content, subtree);
        }

        final List<Node> children = XMLUtils.getChildNodes(content, Node.ELEMENT_NODE);
        if (children.size() == 1) {
            final Element value = (Element) children.iterator().next();
            if (ODataConstants.JSON_VALUE.equals(XMLUtils.getSimpleName(value))) {
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    value.setAttribute(ODataConstants.ATTR_M_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
                content = value;
            }
        }

        property.setContent(content);
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build property", parser.getCurrentLocation(), e);
    }

    return property;
}

From source file:com.streamsets.pipeline.lib.json.StreamingJsonParser.java

public Object read() throws IOException {
    if (closed) {
        throw new IOException("The parser is closed");
    }/*from www . j ava 2 s .  c  o m*/
    try {
        Object value = null;
        switch (mode) {
        case ARRAY_OBJECTS:
            value = readObjectFromArray();
            break;
        case MULTIPLE_OBJECTS:
            value = readObjectFromStream();
            break;
        }
        return value;
    } catch (RuntimeJsonMappingException ex) {
        throw new JsonParseException(ex.toString(), jsonParser.getTokenLocation(), ex);
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.JSONPropertyDeserializer.java

@Override
protected JSONProperty doDeserializeV4(JsonParser parser, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser);
    final JSONProperty property = new JSONProperty();

    try {//  w  w  w . j  a v  a2 s.c o  m
        final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
        final Document document = builder.newDocument();

        Element content = document.createElement(ODataConstants.ELEM_PROPERTY);

        JsonNode subtree = null;
        if (tree.has(ODataConstants.JSON_VALUE)) {
            if (tree.has(v4AnnotationPrefix + ODataConstants.JSON_TYPE)
                    && StringUtils.isBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {

                content.setAttribute(ODataConstants.ATTR_M_TYPE,
                        tree.get(v4AnnotationPrefix + ODataConstants.JSON_TYPE).asText());
            }

            final JsonNode value = tree.get(ODataConstants.JSON_VALUE);
            if (value.isValueNode()) {
                content.appendChild(document.createTextNode(value.asText()));
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                subtree = tree.objectNode();
                ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE, tree.get(ODataConstants.JSON_VALUE));
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    ((ObjectNode) subtree).put(ODataConstants.JSON_VALUE + "@" + ODataConstants.JSON_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
            } else {
                subtree = tree.get(ODataConstants.JSON_VALUE);
            }
        } else {
            subtree = tree;
        }

        if (subtree != null) {
            DOMTreeUtilsV4.buildSubtree(content, subtree);
        }

        final List<Node> children = XMLUtils.getChildNodes(content, Node.ELEMENT_NODE);
        if (children.size() == 1) {
            final Element value = (Element) children.iterator().next();
            if (ODataConstants.JSON_VALUE.equals(XMLUtils.getSimpleName(value))) {
                if (StringUtils.isNotBlank(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                    value.setAttribute(ODataConstants.ATTR_M_TYPE,
                            content.getAttribute(ODataConstants.ATTR_M_TYPE));
                }
                content = value;
            }
        }

        property.setContent(content);
    } catch (ParserConfigurationException e) {
        throw new JsonParseException("Cannot build property", parser.getCurrentLocation(), e);
    }

    return property;
}