Search in sources :

Example 6 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project pinot by linkedin.

the class Summary method main.

public static void main(String[] argc) {
    String oFileName = "Cube.json";
    int answerSize = 10;
    boolean doOneSideError = true;
    int maxDimensionSize = 3;
    Cube cube = null;
    try {
        cube = Cube.fromJson(oFileName);
        System.out.println("Restored Cube:");
        System.out.println(cube);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    Summary summary = new Summary(cube);
    try {
        SummaryResponse response = summary.computeSummary(answerSize, doOneSideError, maxDimensionSize);
        System.out.print("JSon String: ");
        System.out.println(new ObjectMapper().writeValueAsString(response));
        System.out.println("Object String: ");
        System.out.println(response.toString());
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    summary.testCorrectnessOfWowValues();
}
Also used : Cube(com.linkedin.thirdeye.client.diffsummary.Cube) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project hadoop by apache.

the class JsonSerDeser method fromBytes.

/**
   * Deserialize from a byte array, optionally checking for a marker string.
   * <p>
   * If the marker parameter is supplied (and not empty), then its presence
   * will be verified before the JSON parsing takes place; it is a fast-fail
   * check. If not found, an {@link InvalidRecordException} exception will be
   * raised
   * @param path path the data came from
   * @param bytes byte array
   * @param marker an optional string which, if set, MUST be present in the
   * UTF-8 parsed payload.
   * @return The parsed record
   * @throws IOException all problems
   * @throws EOFException not enough data
   * @throws InvalidRecordException if the JSON parsing failed.
   * @throws NoRecordException if the data is not considered a record: either
   * it is too short or it did not contain the marker string.
   */
public T fromBytes(String path, byte[] bytes, String marker) throws IOException, NoRecordException, InvalidRecordException {
    int len = bytes.length;
    if (len == 0) {
        throw new NoRecordException(path, E_NO_DATA);
    }
    if (StringUtils.isNotEmpty(marker) && len < marker.length()) {
        throw new NoRecordException(path, E_DATA_TOO_SHORT);
    }
    String json = new String(bytes, 0, len, UTF_8);
    if (StringUtils.isNotEmpty(marker) && !json.contains(marker)) {
        throw new NoRecordException(path, E_MISSING_MARKER_STRING + marker);
    }
    try {
        return fromJson(json);
    } catch (JsonProcessingException e) {
        throw new InvalidRecordException(path, e.toString(), e);
    }
}
Also used : NoRecordException(org.apache.hadoop.registry.client.exceptions.NoRecordException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InvalidRecordException(org.apache.hadoop.registry.client.exceptions.InvalidRecordException)

Example 8 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project hadoop by apache.

the class SlowPeerTracker method getJson.

/**
   * Retrieve all valid reports as a JSON string.
   * @return serialized representation of valid reports. null if
   *         serialization failed.
   */
public String getJson() {
    Collection<ReportForJson> validReports = getJsonReports(MAX_NODES_TO_REPORT);
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        return objectMapper.writeValueAsString(validReports);
    } catch (JsonProcessingException e) {
        // Failed to serialize. Don't log the exception call stack.
        LOG.debug("Failed to serialize statistics" + e);
        return null;
    }
}
Also used : JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project druid by druid-io.

the class LookupCoordinatorManager method updateAllOnHost.

void updateAllOnHost(final URL url, Map<String, Map<String, Object>> knownLookups) throws IOException, InterruptedException, ExecutionException {
    final AtomicInteger returnCode = new AtomicInteger(0);
    final AtomicReference<String> reasonString = new AtomicReference<>(null);
    final byte[] bytes;
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Loading up %d lookups to %s", knownLookups.size(), url);
        }
        bytes = smileMapper.writeValueAsBytes(knownLookups);
    } catch (JsonProcessingException e) {
        throw Throwables.propagate(e);
    }
    try (final InputStream result = httpClient.go(new Request(HttpMethod.POST, url).addHeader(HttpHeaders.Names.ACCEPT, SmileMediaTypes.APPLICATION_JACKSON_SMILE).addHeader(HttpHeaders.Names.CONTENT_TYPE, SmileMediaTypes.APPLICATION_JACKSON_SMILE).setContent(bytes), makeResponseHandler(returnCode, reasonString), lookupCoordinatorManagerConfig.getHostUpdateTimeout()).get()) {
        if (!httpStatusIsSuccess(returnCode.get())) {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                StreamUtils.copyAndClose(result, baos);
            } catch (IOException e2) {
                LOG.warn(e2, "Error reading response");
            }
            throw new IOException(String.format("Bad update request to [%s] : [%d] : [%s]  Response: [%s]", url, returnCode.get(), reasonString.get(), StringUtils.fromUtf8(baos.toByteArray())));
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Update on [%s], Status: %s reason: [%s]", url, returnCode.get(), reasonString.get());
            }
            final Map<String, Object> resultMap = smileMapper.readValue(result, MAP_STRING_OBJ_TYPE);
            final Object missingValuesObject = resultMap.get(LookupModule.FAILED_UPDATES_KEY);
            if (null == missingValuesObject) {
                throw new IAE("Update result did not have field for [%s]", LookupModule.FAILED_UPDATES_KEY);
            }
            final Map<String, Object> missingValues = smileMapper.convertValue(missingValuesObject, MAP_STRING_OBJ_TYPE);
            if (!missingValues.isEmpty()) {
                throw new IAE("Lookups failed to update: %s", smileMapper.writeValueAsString(missingValues.keySet()));
            } else {
                LOG.debug("Updated all lookups on [%s]", url);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IAE(io.druid.java.util.common.IAE) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 10 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project KaiZen-OpenAPI-Editor by RepreZen.

the class Model method parse.

public static Model parse(CompositeSchema schema, JsonNode document) {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    String text = null;
    try {
        text = mapper.writeValueAsString(document);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return parseYaml(schema, text);
}
Also used : YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)648 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)205 IOException (java.io.IOException)156 HashMap (java.util.HashMap)105 Map (java.util.Map)77 ArrayList (java.util.ArrayList)67 JsonNode (com.fasterxml.jackson.databind.JsonNode)54 List (java.util.List)50 Test (org.junit.Test)44 InputStream (java.io.InputStream)24 Collectors (java.util.stream.Collectors)24 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)23 Json (com.sequenceiq.cloudbreak.domain.json.Json)21 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)20 Function (java.util.function.Function)18 Test (org.testng.annotations.Test)18 File (java.io.File)17 Date (java.util.Date)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)15 URL (java.net.URL)15