Search in sources :

Example 86 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project drill by apache.

the class JSONFormatPlugin method writeStatistics.

@Override
public void writeStatistics(TableStatistics statistics, FileSystem fs, Path statsTablePath) throws IOException {
    FSDataOutputStream stream = null;
    JsonGenerator generator = null;
    try {
        JsonFactory factory = new JsonFactory();
        stream = fs.create(statsTablePath);
        ObjectMapper mapper = DrillStatsTable.getMapper();
        generator = factory.createGenerator((OutputStream) stream).useDefaultPrettyPrinter().setCodec(mapper);
        mapper.writeValue(generator, statistics);
    } catch (com.fasterxml.jackson.core.JsonGenerationException ex) {
        logger.error("Unable to create file (JSON generation error): " + statsTablePath.getName(), ex);
        throw ex;
    } catch (com.fasterxml.jackson.databind.JsonMappingException ex) {
        logger.error("Unable to create file (JSON mapping error): " + statsTablePath.getName(), ex);
        throw ex;
    } catch (IOException ex) {
        logger.error("Unable to create file " + statsTablePath.getName(), ex);
    } finally {
        if (generator != null) {
            generator.flush();
        }
        if (stream != null) {
            stream.close();
        }
    }
}
Also used : FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 87 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project seldon-core by SeldonIO.

the class DeploymentWatcher method removeCreationTimestampField.

private String removeCreationTimestampField(String json) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = mapper.getFactory();
        JsonParser parser = factory.createParser(json);
        JsonNode obj = mapper.readTree(parser);
        if (obj.has("metadata") && obj.get("metadata").has("creationTimestamp")) {
            ((ObjectNode) obj.get("metadata")).remove("creationTimestamp");
            return mapper.writeValueAsString(obj);
        } else
            return json;
    } catch (JsonParseException e) {
        logger.error("Failed to remove creationTimestamp");
        return json;
    } catch (IOException e) {
        logger.error("Failed to remove creationTimestamp");
        return json;
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 88 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project seldon-core by SeldonIO.

the class TestJsonParse method multiDimTest.

@Test
public void multiDimTest() throws JsonProcessingException, IOException {
    String json = "{\"request\":{\"values\":[[1.0]]}}";
    System.out.println(json);
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonParser parser = factory.createParser(json);
    JsonNode j = mapper.readTree(parser);
    JsonNode values = j.get("request").get("values");
    double[][] v = mapper.readValue(values.toString(), double[][].class);
    double[] vs = Doubles.concat(v);
    int[] shape = { v.length, v[0].length };
    ((ObjectNode) j.get("request")).replace("values", mapper.valueToTree(vs));
    ((ObjectNode) j.get("request")).set("shape", mapper.valueToTree(shape));
    System.out.println(j.toString());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 89 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project presto by prestodb.

the class PrometheusQueryResponse method parsePrometheusQueryResponse.

private void parsePrometheusQueryResponse(InputStream response) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    JsonParser parser = new JsonFactory().createParser(response);
    while (!parser.isClosed()) {
        JsonToken jsonToken = parser.nextToken();
        if (FIELD_NAME.equals(jsonToken)) {
            if (parser.getCurrentName().equals(parseResultStatus)) {
                parser.nextToken();
                if (parser.getValueAsString().equals(parseResultSuccess)) {
                    this.status = true;
                    while (!parser.isClosed()) {
                        parser.nextToken();
                        if (FIELD_NAME.equals(jsonToken)) {
                            if (parser.getCurrentName().equals(parseResultType)) {
                                parser.nextToken();
                                resultType = ResultType.valueOf(parser.getValueAsString());
                            }
                            if (parser.getCurrentName().equals(parseResult)) {
                                parser.nextToken();
                                ArrayNode node = mapper.readTree(parser);
                                result = node.toString();
                                break;
                            }
                        }
                    }
                } else {
                    // error path
                    String parsedStatus = parser.getValueAsString();
                    // parsing json is key-value based, so first nextToken is advanced to the key, nextToken advances to the value.
                    // for "errorType" key
                    parser.nextToken();
                    // for "errorType" key's value
                    parser.nextToken();
                    errorType = parser.getValueAsString();
                    // advance to "error" key
                    parser.nextToken();
                    // advance to "error" key's value
                    parser.nextToken();
                    error = parser.getValueAsString();
                    throw new PrestoException(PROMETHEUS_PARSE_ERROR, "Unable to parse Prometheus response: " + parsedStatus + " " + errorType + " " + error);
                }
            }
        }
        if (result != null) {
            break;
        }
    }
    if (result != null && resultType != null) {
        switch(resultType) {
            case matrix:
            case vector:
                results = mapper.readValue(result, new TypeReference<List<PrometheusMetricResult>>() {
                });
                break;
            case scalar:
            case string:
                PrometheusTimeSeriesValue stringOrScalarResult = mapper.readValue(result, new TypeReference<PrometheusTimeSeriesValue>() {
                });
                Map<String, String> madeUpMetricHeader = new HashMap<>();
                madeUpMetricHeader.put("__name__", resultType.toString());
                PrometheusTimeSeriesValueArray timeSeriesValues = new PrometheusTimeSeriesValueArray(singletonList(stringOrScalarResult));
                results = singletonList(new PrometheusMetricResult(madeUpMetricHeader, timeSeriesValues));
        }
    }
}
Also used : HashMap(java.util.HashMap) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JsonFactory(com.fasterxml.jackson.core.JsonFactory) PrestoException(com.facebook.presto.spi.PrestoException) JsonToken(com.fasterxml.jackson.core.JsonToken) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 90 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project presto by prestodb.

the class TestJsonExtract method doExtract.

private static String doExtract(JsonExtractor<Slice> jsonExtractor, String json) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser jsonParser = jsonFactory.createParser(json);
    // Advance to the first token
    jsonParser.nextToken();
    Slice extract = jsonExtractor.extract(jsonParser);
    return (extract == null) ? null : extract.toStringUtf8();
}
Also used : Slice(io.airlift.slice.Slice) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)266 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)102 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)91 JsonParser (com.fasterxml.jackson.core.JsonParser)78 Test (org.junit.Test)65 IOException (java.io.IOException)62 StringWriter (java.io.StringWriter)60 Map (java.util.Map)27 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 List (java.util.List)18 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)16 JsonToken (com.fasterxml.jackson.core.JsonToken)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 InputStreamReader (java.io.InputStreamReader)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8