Search in sources :

Example 21 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project knox by apache.

the class DefaultServiceRegistryService method getMapFromJsonString.

private HashMap<String, HashMap<String, RegEntry>> getMapFromJsonString(String json) {
    Registry map = null;
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(factory);
    TypeReference<Registry> typeRef = new TypeReference<Registry>() {
    };
    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 : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 22 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException 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 23 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException 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 24 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException 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 25 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException 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)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)120 IOException (java.io.IOException)59 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)53 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)31 JsonParser (com.fasterxml.jackson.core.JsonParser)21 Map (java.util.Map)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)12 HashMap (java.util.HashMap)12 JsonToken (com.fasterxml.jackson.core.JsonToken)11 JsonFactory (com.fasterxml.jackson.core.JsonFactory)9 File (java.io.File)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 Date (java.util.Date)5 GZIPInputStream (java.util.zip.GZIPInputStream)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)4