Search in sources :

Example 31 with JsonMappingException

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

the class JsonUtils method getMapFromJsonString.

public static Map<String, String> getMapFromJsonString(String json) {
    Map<String, String> map = null;
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(factory);
    TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
    };
    try {
        map = mapper.readValue(json, typeRef);
    } catch (JsonParseException e) {
        LOG.failedToGetMapFromJsonString(json, e);
    } catch (JsonMappingException e) {
        LOG.failedToGetMapFromJsonString(json, e);
    } catch (IOException e) {
        LOG.failedToGetMapFromJsonString(json, e);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 32 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project core-util by WSO2Telco.

the class YamlReader method getConfiguration.

/**
 * Gets the configuration.
 *
 * @return the configuration
 */
public static ConfigDTO getConfiguration() {
    File file = new File(DEVICE_MGT_CONFIG_PATH);
    ConfigDTO configPojo = new ConfigDTO();
    // jackson databind
    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try {
        configPojo = mapper.readValue(file, ConfigDTO.class);
    } catch (JsonParseException e) {
        log.error("Yaml Parsing Error", e);
    } catch (JsonMappingException e) {
        log.error("Yaml Mapping Error", e);
    } catch (IOException e) {
        log.error("Yaml File Error", e);
    }
    return configPojo;
}
Also used : ConfigDTO(com.wso2telco.core.pcrservice.model.ConfigDTO) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 33 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project n4js by eclipse.

the class StartSessionResource method createEvent.

@Override
@SuppressWarnings("unchecked")
protected TestEvent createEvent(final String sessionId, final String body) throws ClientResourceException {
    final Map<?, ?> values = newHashMap();
    try {
        if (!isNullOrEmpty(body)) {
            values.putAll(mapper.readValue(body, Map.class));
        }
    } catch (JsonMappingException | JsonParseException e) {
        throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
    } catch (final IOException e) {
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
    final Map<String, String> properties = newHashMap();
    if (null != values.get(PROPERTIES)) {
        if (!(values.get(PROPERTIES) instanceof Map)) {
            throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
        } else {
            ((Map<?, ?>) values.get(PROPERTIES)).entrySet().forEach(new Consumer<Entry<?, ?>>() {

                @Override
                public void accept(final Entry<?, ?> entry) {
                    properties.put(valueOf(entry.getKey()), valueOf(entry.getValue()));
                }
            });
        }
    }
    return new SessionStartedEvent(sessionId, properties);
}
Also used : IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) SessionStartedEvent(org.eclipse.n4js.tester.events.SessionStartedEvent) ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) Entry(java.util.Map.Entry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map)

Example 34 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project n4js by eclipse.

the class StartTestResource method createEvent.

@Override
@SuppressWarnings("unchecked")
protected TestEvent createEvent(final String sessionId, final String testId, final String body) throws ClientResourceException {
    if (isNullOrEmpty(body))
        throw new ClientResourceException(SC_BAD_REQUEST);
    final Map<?, ?> values = newHashMap();
    try {
        values.putAll(mapper.readValue(body, Map.class));
    } catch (JsonMappingException | JsonParseException e) {
        throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
    } catch (final IOException e) {
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
    final Object value = values.get(TIMEOUT_KEY);
    // incorrect schema
    if (null == value) {
        throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
    }
    final Map<String, String> properties = newHashMap();
    if (null != values.get(PROPERTIES)) {
        if (!(values.get(PROPERTIES) instanceof Map)) {
            throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
        } else {
            ((Map<?, ?>) values.get(PROPERTIES)).entrySet().forEach(new Consumer<Entry<?, ?>>() {

                @Override
                public void accept(final Entry<?, ?> entry) {
                    properties.put(valueOf(entry.getKey()), valueOf(entry.getValue()));
                }
            });
        }
    }
    try {
        final long timeout = parseLong(valueOf(value));
        return new TestStartedEvent(sessionId, testId, timeout, properties);
    } catch (final NumberFormatException e) {
        // although schema was valid the data was indeed invalid
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
}
Also used : IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) TestStartedEvent(org.eclipse.n4js.tester.events.TestStartedEvent) Entry(java.util.Map.Entry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap)

Example 35 with JsonMappingException

use of com.fasterxml.jackson.databind.JsonMappingException in project underlx by underlx.

the class API method postPairRequest.

public Pair postPairRequest(PairRequest request) throws APIException {
    try {
        byte[] content = mapper.writeValueAsBytes(request);
        InputStream is = postRequest(endpoint.resolve("pair"), content, false);
        return mapper.readValue(is, Pair.class);
    } catch (JsonParseException e) {
        throw new APIException(e).addInfo("Parse exception");
    } catch (JsonMappingException e) {
        throw new APIException(e).addInfo("Mapping exception");
    } catch (IOException e) {
        throw new APIException(e).addInfo("IOException");
    }
}
Also used : APIException(im.tny.segvault.disturbances.exception.APIException) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

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