Search in sources :

Example 41 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project dcos-commons by mesosphere.

the class DefaultServiceSpecTest method invalidTaskName.

@Test
public void invalidTaskName() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("invalid-task-name.yml").getFile());
    try {
        DefaultServiceSpec.newGenerator(file, SCHEDULER_CONFIG).build();
        Assert.fail("Expected exception");
    } catch (JsonMappingException e) {
        Assert.assertTrue(e.getCause().toString(), e.getCause() instanceof JsonParseException);
        JsonParseException cause = (JsonParseException) e.getCause();
        Assert.assertTrue(cause.getMessage(), cause.getMessage().contains("Duplicate field 'meta-data-task'"));
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) File(java.io.File) Test(org.junit.Test)

Example 42 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project service-proxy by membrane.

the class JSONBody method write.

@Override
public void write(XMLStreamWriter out) throws XMLStreamException {
    out.writeAttribute("type", "json");
    try {
        final JsonFactory jsonFactory = new JsonFactory();
        final JsonParser jp = jsonFactory.createParser(new InputStreamReader(msg.getBodyAsStreamDecoded(), msg.getCharset()));
        final List<String> stack = new ArrayList<String>();
        String name = "root";
        OUTER: while (jp.nextToken() != null) {
            switch(jp.getCurrentToken()) {
                case START_OBJECT:
                    if (name != null) {
                        stack.add(name);
                        out.writeStartElement(name);
                        out.writeAttribute("type", "o");
                        name = null;
                    }
                    break;
                case END_OBJECT:
                    out.writeEndElement();
                    name = stack.remove(stack.size() - 1);
                    if (stack.isEmpty())
                        break OUTER;
                    break;
                case FIELD_NAME:
                    name = jp.getCurrentName();
                    break;
                case START_ARRAY:
                    if (name != null) {
                        stack.add(name);
                        out.writeStartElement(name);
                        out.writeAttribute("type", "a");
                    }
                    name = "item";
                    break;
                case END_ARRAY:
                    out.writeEndElement();
                    name = stack.remove(stack.size() - 1);
                    if (stack.isEmpty())
                        break OUTER;
                    break;
                case VALUE_TRUE:
                case VALUE_FALSE:
                    out.writeStartElement(name);
                    out.writeAttribute("type", "b");
                    out.writeCharacters(Boolean.toString(jp.getBooleanValue()));
                    out.writeEndElement();
                    break;
                case VALUE_NULL:
                    out.writeStartElement(name);
                    out.writeAttribute("type", "n");
                    out.writeAttribute("isNull", "true");
                    out.writeEndElement();
                    break;
                case VALUE_STRING:
                case VALUE_NUMBER_INT:
                case VALUE_NUMBER_FLOAT:
                    out.writeStartElement(name);
                    out.writeAttribute("type", jp.getCurrentToken() == JsonToken.VALUE_STRING ? "s" : jp.getCurrentToken() == JsonToken.VALUE_NUMBER_INT ? "i" : "f");
                    out.writeCharacters(jp.getText());
                    out.writeEndElement();
                    break;
                case VALUE_EMBEDDED_OBJECT:
                case NOT_AVAILABLE:
                    throw new RuntimeException(jp.getCurrentToken().toString());
            }
        }
    } catch (JsonParseException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 43 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project service-proxy by membrane.

the class JSONValidator method validateMessage.

public Outcome validateMessage(Exchange exc, InputStream body, Charset charset, String source) throws Exception {
    List<String> errors;
    boolean success = true;
    try {
        JsonNode node = JsonLoader.fromReader(new InputStreamReader(body, charset));
        ProcessingReport report = schema.validateUnchecked(node);
        success = report.isSuccess();
        errors = new ArrayList<String>();
        for (ProcessingMessage message : report) errors.add(message.getMessage());
    } catch (JsonParseException e) {
        success = false;
        errors = new ArrayList<String>();
        errors.add(e.getMessage());
    }
    if (success) {
        valid.incrementAndGet();
        return Outcome.CONTINUE;
    }
    if (failureHandler == FailureHandler.VOID) {
        StringBuilder message = new StringBuilder();
        message.append(source);
        message.append(": ");
        for (String error : errors) {
            message.append(error);
            message.append(";");
        }
        exc.setProperty("error", message.toString());
        invalid.incrementAndGet();
        return Outcome.ABORT;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator jg = new JsonFactory().createGenerator(baos);
    jg.writeStartObject();
    jg.writeStringField("source", source);
    jg.writeArrayFieldStart("errors");
    for (String message : errors) jg.writeString(message);
    jg.close();
    if (failureHandler != null) {
        failureHandler.handleFailure(new String(baos.toByteArray(), UTF8), exc);
        exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body("{\"error\":\"error\"}".getBytes(UTF8)).build());
    } else {
        exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body(baos.toByteArray()).build());
    }
    invalid.incrementAndGet();
    return Outcome.ABORT;
}
Also used : InputStreamReader(java.io.InputStreamReader) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) ArrayList(java.util.ArrayList) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 44 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project service-proxy by membrane.

the class EtcdResponse method get.

@SuppressWarnings("unchecked")
public String get(String name) {
    JsonParser par = getParser(body);
    String result = null;
    Map<String, Object> respData = null;
    try {
        respData = new ObjectMapper().readValue(par, Map.class);
    } catch (JsonParseException e) {
    } catch (JsonMappingException e) {
    } catch (IOException e) {
    }
    if (respData.containsKey("node")) {
        LinkedHashMap<String, Object> nodeJson = (LinkedHashMap<String, Object>) respData.get("node");
        if (nodeJson.containsKey(name)) {
            result = nodeJson.get(name).toString();
        }
    }
    if (result == null) {
        throw new RuntimeException();
    }
    return result;
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) LinkedHashMap(java.util.LinkedHashMap)

Example 45 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project webprotege by protegeproject.

the class FormDataValueDeserializer method deserialize.

@Override
public FormDataValue deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonNode node = p.readValueAsTree();
    if (node.isTextual()) {
        return FormDataPrimitive.get(node.asText());
    } else if (node.isNumber()) {
        return FormDataPrimitive.get(node.asDouble());
    } else if (node.isBoolean()) {
        return FormDataPrimitive.get(node.asBoolean());
    } else if (node.isObject()) {
        if (node.has("iri")) {
            if (node.has("type")) {
                IRI iri = IRI.create(node.get("iri").asText());
                String type = node.get("type").asText();
                switch(type) {
                    case "Class":
                        return FormDataPrimitive.get(df.getOWLClass(iri));
                    case "ObjectProperty":
                        return FormDataPrimitive.get(df.getOWLObjectProperty(iri));
                    case "DataProperty":
                        return FormDataPrimitive.get(df.getOWLDataProperty(iri));
                    case "AnnotationProperty":
                        return FormDataPrimitive.get(df.getOWLAnnotationProperty(iri));
                    case "Datatype":
                        return FormDataPrimitive.get(df.getOWLDatatype(iri));
                    case "NamedIndividual":
                        return FormDataPrimitive.get(df.getOWLDatatype(iri));
                }
                throw new JsonParseException(p, "Unrecognised entity type: " + type);
            } else {
                return FormDataPrimitive.get(IRI.create(node.get("iri").asText()));
            }
        } else if (node.has("literal")) {
            String literal = node.get("literal").asText();
            String lang = node.get("lang").asText("");
            return FormDataPrimitive.get(df.getOWLLiteral(literal, lang));
        }
    }
    throw new JsonParseException(p, "Cannot parse node as primitive value");
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)120 IOException (java.io.IOException)59 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)53 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)31 JsonParser (com.fasterxml.jackson.core.JsonParser)21 Map (java.util.Map)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)12 HashMap (java.util.HashMap)12 JsonToken (com.fasterxml.jackson.core.JsonToken)11 JsonFactory (com.fasterxml.jackson.core.JsonFactory)9 File (java.io.File)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 Date (java.util.Date)5 GZIPInputStream (java.util.zip.GZIPInputStream)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)4