Search in sources :

Example 1 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project druid by druid-io.

the class QueryGranularityTest method testSerializeDuration.

@Test
public void testSerializeDuration() throws Exception {
    ObjectMapper mapper = new DefaultObjectMapper();
    String json = "{ \"type\": \"duration\", \"duration\": \"3600000\" }";
    Granularity gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new DurationGranularity(3600000, null), gran);
    json = "{ \"type\": \"duration\", \"duration\": \"5\", \"origin\": \"2012-09-01T00:00:00.002Z\" }";
    gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new DurationGranularity(5, 2), gran);
    DurationGranularity expected = new DurationGranularity(5, 2);
    Assert.assertEquals(expected, mapper.readValue(mapper.writeValueAsString(expected), Granularity.class));
    String illegalJson = "{ \"type\": \"duration\", \"duration\": \"0\" }";
    try {
        mapper.readValue(illegalJson, Granularity.class);
        Assert.fail();
    } catch (JsonMappingException e) {
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) Granularity(io.druid.java.util.common.granularity.Granularity) DurationGranularity(io.druid.java.util.common.granularity.DurationGranularity) PeriodGranularity(io.druid.java.util.common.granularity.PeriodGranularity) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project moco by dreamhead.

the class CollectionReader method read.

public <T> ImmutableList<T> read(final InputStream is, final Class<T> elementClass) {
    try {
        CollectionType type = factory.constructCollectionType(List.class, elementClass);
        List<T> sessionSettings = mapper.readValue(is, type);
        return copyOf(sessionSettings);
    } catch (UnrecognizedPropertyException e) {
        logger.info("Unrecognized field: {}", e.getMessage());
        throw new RuntimeException(format("Unrecognized field [ %s ], please check!", e.getPropertyName()));
    } catch (JsonMappingException e) {
        logger.info("{} {}", e.getMessage(), e.getPathReference());
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        closeQuietly(is);
    }
}
Also used : CollectionType(com.fasterxml.jackson.databind.type.CollectionType) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) IOException(java.io.IOException)

Example 3 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project hadoop by apache.

the class RemoteSASKeyGenerationResponse method makeRemoteRequest.

/**
   * Helper method to make a remote request.
   * @param uri - Uri to use for the remote request
   * @return RemoteSASKeyGenerationResponse
   */
private RemoteSASKeyGenerationResponse makeRemoteRequest(URI uri) throws SASKeyGenerationException {
    try {
        String responseBody = remoteCallHelper.makeRemoteGetRequest(new HttpGet(uri));
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(responseBody, RemoteSASKeyGenerationResponse.class);
    } catch (WasbRemoteCallException remoteCallEx) {
        throw new SASKeyGenerationException("Encountered RemoteCallException" + " while retrieving SAS key from remote service", remoteCallEx);
    } catch (JsonParseException jsonParserEx) {
        throw new SASKeyGenerationException("Encountered JsonParseException " + "while parsing the response from remote" + " service into RemoteSASKeyGenerationResponse object", jsonParserEx);
    } catch (JsonMappingException jsonMappingEx) {
        throw new SASKeyGenerationException("Encountered JsonMappingException" + " while mapping the response from remote service into " + "RemoteSASKeyGenerationResponse object", jsonMappingEx);
    } catch (IOException ioEx) {
        throw new SASKeyGenerationException("Encountered IOException while " + "accessing remote service to retrieve SAS Key", ioEx);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project OpenAM by OpenRock.

the class SubjectTypesResource method jsonify.

/**
     * Transforms a subclass of {@link EntitlementSubject} in to a JsonSchema representation.
     * This schema is then combined with the Subject's name (taken as the resourceId) and all this is
     * compiled together into a new {@link JsonValue} object until "title" and "config" fields respectively.
     *
     * @param subjectClass The class whose schema to produce.
     * @param resourceId The ID of the resource to return
     * @return A JsonValue containing the schema of the EntitlementSubject
     */
private JsonValue jsonify(Class<? extends EntitlementSubject> subjectClass, String resourceId, boolean logical) {
    try {
        final JsonSchema schema = mapper.generateJsonSchema(subjectClass);
        //this will remove the 'subjectName' attribute from those subjects which incorporate it unnecessarily
        final JsonNode node = schema.getSchemaNode().get("properties");
        if (node instanceof ObjectNode) {
            final ObjectNode alter = (ObjectNode) node;
            alter.remove("subjectName");
        }
        return JsonValue.json(JsonValue.object(JsonValue.field(JSON_OBJ_TITLE, resourceId), JsonValue.field(JSON_OBJ_LOGICAL, logical), JsonValue.field(JSON_OBJ_CONFIG, schema)));
    } catch (JsonMappingException e) {
        if (debug.errorEnabled()) {
            debug.error("SubjectTypesResource :: JSONIFY - Error applying " + "jsonification to the Subject class representation.", e);
        }
        return null;
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project OpenAM by OpenRock.

the class JsonPolicyParser method parsePrivilege.

private Privilege parsePrivilege(String providedName, JsonValue jsonValue) throws EntitlementException {
    try {
        // Note: this is a bit ugly as we re-serialise the JsonValue back into a JSON String to then parse it
        // again using Jackson. Unfortunately, that appears to be the easiest way as JsonValue does not support
        // data binding.
        JsonPolicy policy = MAPPER.readValue(jsonValue.toString(), JsonPolicy.class);
        Privilege privilege = policy.asPrivilege();
        if (isBlank(privilege.getName())) {
            privilege.setName(providedName);
        }
        if (isBlank(privilege.getName())) {
            throw new EntitlementException(EntitlementException.MISSING_PRIVILEGE_NAME);
        }
        // Validate the condition if present
        if (privilege.getCondition() != null) {
            privilege.getCondition().validate();
        }
        return privilege;
    } catch (UnrecognizedPropertyException ex) {
        throw new EntitlementException(EntitlementException.INVALID_VALUE, new Object[] { ex.getUnrecognizedPropertyName() });
    } catch (JsonMappingException ex) {
        throw new EntitlementException(EntitlementException.INVALID_JSON, ex, ex.getMessage());
    } catch (IOException e) {
        throw new EntitlementException(EntitlementException.UNABLE_TO_CREATE_POLICY, e);
    }
}
Also used : JsonPolicy(org.forgerock.openam.entitlement.rest.model.json.JsonPolicy) EntitlementException(com.sun.identity.entitlement.EntitlementException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) IOException(java.io.IOException) Privilege(com.sun.identity.entitlement.Privilege)

Aggregations

JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)147 IOException (java.io.IOException)75 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)62 JsonParseException (com.fasterxml.jackson.core.JsonParseException)53 Test (org.junit.Test)28 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)23 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 File (java.io.File)14 Map (java.util.Map)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 InputStream (java.io.InputStream)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 List (java.util.List)7 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)7 Test (org.junit.jupiter.api.Test)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)5