Search in sources :

Example 36 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project syndesis by syndesisio.

the class JsonRecordSupport method jsonStreamToRecords.

public static void jsonStreamToRecords(Set<String> indexes, String dbPath, InputStream is, Consumer<JsonRecord> consumer) throws IOException {
    try (JsonParser jp = new JsonFactory().createParser(is)) {
        jsonStreamToRecords(indexes, jp, dbPath, consumer);
        JsonToken jsonToken = jp.nextToken();
        if (jsonToken != null) {
            throw new JsonParseException(jp, "Document did not terminate as expected.");
        }
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 37 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project debezium by debezium.

the class JacksonReader method parseDocument.

private Document parseDocument(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the fields in the top-level document ...
    BasicDocument doc = new BasicDocument();
    JsonToken token = null;
    if (!nested) {
        // We expect the START_OBJECT token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object, but was " + token);
        }
    }
    String fieldName = null;
    token = parser.nextToken();
    while (token != JsonToken.END_OBJECT) {
        switch(token) {
            case FIELD_NAME:
                fieldName = parser.getCurrentName();
                break;
            case START_OBJECT:
                doc.setDocument(fieldName, parseDocument(parser, true));
                break;
            case START_ARRAY:
                doc.setArray(fieldName, parseArray(parser, true));
                break;
            case VALUE_STRING:
                doc.setString(fieldName, parser.getValueAsString());
                break;
            case VALUE_TRUE:
                doc.setBoolean(fieldName, true);
                break;
            case VALUE_FALSE:
                doc.setBoolean(fieldName, false);
                break;
            case VALUE_NULL:
                doc.setNull(fieldName);
                break;
            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT:
                switch(parser.getNumberType()) {
                    case FLOAT:
                        if (handleFloatNumbersAsText) {
                            doc.setString(fieldName, parser.getText());
                        } else {
                            doc.setNumber(fieldName, parser.getFloatValue());
                        }
                        break;
                    case DOUBLE:
                        if (handleFloatNumbersAsText) {
                            doc.setString(fieldName, parser.getText());
                        } else {
                            doc.setNumber(fieldName, parser.getDoubleValue());
                        }
                        break;
                    case BIG_DECIMAL:
                        if (handleFloatNumbersAsText) {
                            doc.setString(fieldName, parser.getText());
                        } else {
                            doc.setNumber(fieldName, parser.getDecimalValue());
                        }
                        break;
                    case INT:
                        doc.setNumber(fieldName, parser.getIntValue());
                        break;
                    case LONG:
                        doc.setNumber(fieldName, parser.getLongValue());
                        break;
                    case BIG_INTEGER:
                        doc.setNumber(fieldName, parser.getBigIntegerValue());
                        break;
                }
                break;
            case VALUE_EMBEDDED_OBJECT:
                // disregard this, since it's an extension ...
                break;
            case NOT_AVAILABLE:
                throw new JsonParseException(parser, "Non-blocking parsers are not supported", parser.getCurrentLocation());
            case END_ARRAY:
                throw new JsonParseException(parser, "Not expecting an END_ARRAY token", parser.getCurrentLocation());
            case END_OBJECT:
                throw new JsonParseException(parser, "Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return doc;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 38 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project debezium by debezium.

the class JacksonReader method parseArray.

private Array parseArray(JsonParser parser, boolean nested) throws IOException {
    // Iterate over the values in the array ...
    BasicArray array = new BasicArray();
    JsonToken token = null;
    if (!nested) {
        // We expect the START_ARRAY token ...
        token = parser.nextToken();
        if (!nested && token != JsonToken.START_ARRAY) {
            throw new IOException("Expected data to start with an Array, but was " + token);
        }
    }
    token = parser.nextToken();
    while (token != JsonToken.END_ARRAY) {
        switch(token) {
            case START_OBJECT:
                array.add(parseDocument(parser, true));
                break;
            case START_ARRAY:
                array.add(parseArray(parser, true));
                break;
            case VALUE_STRING:
                array.add(parser.getValueAsString());
                break;
            case VALUE_TRUE:
                array.add(true);
                break;
            case VALUE_FALSE:
                array.add(false);
                break;
            case VALUE_NULL:
                array.addNull();
                break;
            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT:
                switch(parser.getNumberType()) {
                    case FLOAT:
                        if (handleFloatNumbersAsText) {
                            array.add(parser.getText());
                        } else {
                            array.add(parser.getFloatValue());
                        }
                        break;
                    case DOUBLE:
                        if (handleFloatNumbersAsText) {
                            array.add(parser.getText());
                        } else {
                            array.add(parser.getDoubleValue());
                        }
                        break;
                    case BIG_DECIMAL:
                        if (handleFloatNumbersAsText) {
                            array.add(parser.getText());
                        } else {
                            array.add(parser.getDecimalValue());
                        }
                        break;
                    case INT:
                        array.add(parser.getIntValue());
                        break;
                    case LONG:
                        array.add(parser.getLongValue());
                        break;
                    case BIG_INTEGER:
                        array.add(parser.getBigIntegerValue());
                        break;
                }
                break;
            case VALUE_EMBEDDED_OBJECT:
                // disregard this, since it's an extension ...
                break;
            case NOT_AVAILABLE:
                throw new JsonParseException(parser, "Non-blocking parsers are not supported", parser.getCurrentLocation());
            case FIELD_NAME:
                throw new JsonParseException(parser, "Not expecting a FIELD_NAME token", parser.getCurrentLocation());
            case END_ARRAY:
                throw new JsonParseException(parser, "Not expecting an END_ARRAY token", parser.getCurrentLocation());
            case END_OBJECT:
                throw new JsonParseException(parser, "Not expecting an END_OBJECT token", parser.getCurrentLocation());
        }
        token = parser.nextToken();
    }
    return array;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 39 with JsonParseException

use of org.apache.flink.shaded.jackson2.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 40 with JsonParseException

use of org.apache.flink.shaded.jackson2.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)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)145 IOException (java.io.IOException)75 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)58 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)36 JsonParser (com.fasterxml.jackson.core.JsonParser)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)20 Map (java.util.Map)19 JsonToken (com.fasterxml.jackson.core.JsonToken)15 InputStream (java.io.InputStream)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 HashMap (java.util.HashMap)12 File (java.io.File)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 JsonFactory (com.fasterxml.jackson.core.JsonFactory)7 JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 Date (java.util.Date)5 GZIPInputStream (java.util.zip.GZIPInputStream)5