Search in sources :

Example 61 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project atsd-api-test by axibase.

the class TableMetaDataDeserializer method deserialize.

@Override
public TableMetaData deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    String jsonText = jsonParser.readValueAsTree().toString();
    TableMetaData result;
    try {
        result = SqlTableParser.parseMeta(new JSONObject(jsonText));
    } catch (JSONException je) {
        throw new JsonParseException(jsonParser, je.getMessage());
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 62 with JsonParseException

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

the class SampleNode method parseD3Json.

// not that weak here...
@SuppressFBWarnings("WEM_WEAK_EXCEPTION_MESSAGING")
private static void parseD3Json(final JsonParser jsonP, final BiConsumer<Method, SampleNode> consumer) throws IOException {
    String methodName = null;
    SampleNode sn = new SampleNode(-1);
    while (true) {
        JsonToken nextToken = jsonP.nextToken();
        if (nextToken == JsonToken.FIELD_NAME) {
            String fieldName = jsonP.getCurrentName();
            switch(fieldName) {
                case "name":
                    consume(jsonP, JsonToken.VALUE_STRING);
                    methodName = jsonP.getText();
                    break;
                case "value":
                    consume(jsonP, JsonToken.VALUE_NUMBER_INT);
                    sn.sampleCount = jsonP.getIntValue();
                    break;
                case "children":
                    consume(jsonP, JsonToken.START_ARRAY);
                    while (jsonP.nextToken() != JsonToken.END_ARRAY) {
                        parseD3Json(jsonP, sn::put);
                    }
                    break;
                default:
                    throw new JsonParseException(jsonP, "Unexpected field name : " + fieldName);
            }
        } else if (nextToken == JsonToken.END_OBJECT) {
            if (methodName == null) {
                throw new JsonParseException(jsonP, "name field not found");
            }
            if (sn.sampleCount < 0) {
                throw new JsonParseException(jsonP, "value field not found");
            }
            consumer.accept(Methods.from(methodName), sn);
            return;
        } else {
            throw new JsonParseException(jsonP, "Unexpected " + nextToken);
        }
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParseException(com.fasterxml.jackson.core.JsonParseException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 63 with JsonParseException

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

the class SampleNode method parseInto.

public static void parseInto(final JsonParser jsonP, final SampleNode parentNode) throws IOException {
    consume(jsonP, JsonToken.FIELD_NAME);
    String name = jsonP.getCurrentName();
    consume(jsonP, JsonToken.VALUE_NUMBER_INT);
    int sc = jsonP.getIntValue();
    JsonToken nextToken = jsonP.nextToken();
    Method method = Methods.from(name);
    SampleNode sn = parentNode.get(method);
    if (sn == null) {
        sn = new SampleNode(sc);
        parentNode.put(method, sn);
    } else {
        sn.sampleCount += sc;
    }
    if (nextToken == JsonToken.END_OBJECT) {
        return;
    } else if (nextToken == JsonToken.FIELD_NAME) {
        consume(jsonP, JsonToken.START_ARRAY);
        while (jsonP.nextToken() != JsonToken.END_ARRAY) {
            parseInto(jsonP, sn);
        }
        consume(jsonP, JsonToken.END_OBJECT);
    } else {
        throw new JsonParseException(jsonP, "Expected field name or end Object, not: " + nextToken);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) Method(org.spf4j.base.avro.Method) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 64 with JsonParseException

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

the class ParseSupport method parseUInt64.

/**
 * Parsers a uint64 value out of the input.
 */
public static long parseUInt64(JsonParser parser) throws IOException {
    // cover the vast majority of cases.
    try {
        long result = parseLong(parser);
        if (result >= 0) {
            // Only need to check the uint32 range if the parsed long is negative.
            return result;
        }
    } catch (JsonParseException | InputCoercionException | NumberFormatException e) {
    // Fall through.
    }
    final BigInteger value;
    try {
        BigDecimal decimal = new BigDecimal(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
        value = decimal.toBigIntegerExact();
    } catch (ArithmeticException | NumberFormatException e) {
        throw new InvalidProtocolBufferException("Not an uint64 value: " + parser.getText());
    }
    if (value.compareTo(BigInteger.ZERO) < 0 || value.compareTo(MAX_UINT64) > 0) {
        throw new InvalidProtocolBufferException("Out of range uint64 value: " + parser.getText());
    }
    return value.longValue();
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InputCoercionException(com.fasterxml.jackson.core.exc.InputCoercionException) BigInteger(java.math.BigInteger) JsonParseException(com.fasterxml.jackson.core.JsonParseException) BigDecimal(java.math.BigDecimal)

Example 65 with JsonParseException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException in project qpid-broker-j by apache.

the class CompressedResponsesTest method doCompressionTest.

private void doCompressionTest(final boolean allowCompression, final boolean acceptCompressed) throws Exception {
    final boolean expectCompression = allowCompression && acceptCompressed;
    getHelper().submitRequest("plugin/httpManagement", "POST", Collections.singletonMap("compressResponses", expectCompression), SC_OK);
    HttpURLConnection conn = getHelper().openManagementConnection("/service/metadata", "GET");
    try {
        if (acceptCompressed) {
            conn.setRequestProperty("Accept-Encoding", "gzip");
        }
        conn.connect();
        String contentEncoding = conn.getHeaderField("Content-Encoding");
        if (expectCompression) {
            assertEquals("gzip", contentEncoding);
        } else {
            if (contentEncoding != null) {
                assertEquals("identity", contentEncoding);
            }
        }
        byte[] bytes;
        try (ByteArrayOutputStream contentBuffer = new ByteArrayOutputStream()) {
            ByteStreams.copy(conn.getInputStream(), contentBuffer);
            bytes = contentBuffer.toByteArray();
        }
        try (InputStream jsonStream = expectCompression ? new GZIPInputStream(new ByteArrayInputStream(bytes)) : new ByteArrayInputStream(bytes)) {
            ObjectMapper mapper = new ObjectMapper();
            try {
                mapper.readValue(jsonStream, LinkedHashMap.class);
            } catch (JsonParseException | JsonMappingException e) {
                fail("Message was not in correct format");
            }
        }
    } finally {
        conn.disconnect();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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