Search in sources :

Example 91 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project service-proxy by membrane.

the class EtcdResponse method getDirectories.

@SuppressWarnings("unchecked")
public ArrayList<String> getDirectories() {
    JsonParser par = getParser(body);
    String baseKey = originalRequest.baseKey;
    String module = originalRequest.module;
    ArrayList<String> directories = new ArrayList<String>();
    Map<String, Object> respData = null;
    try {
        respData = new ObjectMapper().readValue(par, Map.class);
    } catch (JsonParseException e) {
    } catch (JsonMappingException e) {
    } catch (IOException e) {
    }
    if (respData.containsKey("node")) {
        LinkedHashMap<String, Object> nodeJson = (LinkedHashMap<String, Object>) respData.get("node");
        if (nodeJson.containsKey("nodes")) {
            ArrayList<Object> nodesArray = (ArrayList<Object>) nodeJson.get("nodes");
            for (Object object : nodesArray) {
                LinkedHashMap<String, Object> dirs = (LinkedHashMap<String, Object>) object;
                if (dirs.containsKey("key")) {
                    String servicePath = dirs.get("key").toString();
                    String uuid = servicePath.replaceAll(baseKey + module, "");
                    directories.add(uuid);
                }
            }
        }
    }
    return directories;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) LinkedHashMap(java.util.LinkedHashMap) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 92 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project n4js by eclipse.

the class PingSessionResource method createEvent.

@Override
@SuppressWarnings("unchecked")
protected TestEvent createEvent(final String sessionId, 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 Object comment = values.get(COMMENT_KEY);
    try {
        final long timeout = parseLong(Objects.toString(value));
        return new SessionPingedEvent(sessionId, timeout, null == comment ? null : valueOf(comment));
    } catch (final NumberFormatException e) {
        // although schema was valid the data was indeed invalid
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
}
Also used : ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) SessionPingedEvent(org.eclipse.n4js.tester.events.SessionPingedEvent) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map)

Example 93 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project n4js by eclipse.

the class PingTestResource 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 Object comment = values.get(COMMENT_KEY);
    try {
        final long timeout = parseLong(Objects.toString(value));
        return new TestPingedEvent(sessionId, testId, timeout, null == comment ? null : valueOf(comment));
    } catch (final NumberFormatException e) {
        // although schema was valid the data was indeed invalid
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
}
Also used : ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) TestPingedEvent(org.eclipse.n4js.tester.events.TestPingedEvent) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map)

Example 94 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project Corgi by kevinYin.

the class JsonUtil method isJSON.

// /**
// * 把JSON文本parse成JavaBean集合
// */
// public static <T> List<T> parseArray(String text, final Class<T> clazz) {
// try {
// //			JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz);
// return mapper.readValue(text, new ArrayT<T>(clazz));
// } catch (Exception e) {
// throw new RuntimeException(e);// 查看 mapper 成员变量上的注释
// }
// }
// 
// public static <T> Map<String, T> parseMap(String text, final Class<T> valueClazz){
// try {
// return mapper.readValue(text, new MapT<T>(valueClazz));
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
// 
// public static <K, V> Map<K, V> parseMap(String text, final Class<K> keyClazz, final Class<V> valueClazz) {
// try {
// return mapper.readValue(text, new MapKV<K, V>(keyClazz, valueClazz));
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
public static boolean isJSON(final String json) {
    boolean valid = false;
    try {
        final JsonParser parser = new ObjectMapper().getFactory().createParser(json);
        while (parser.nextToken() != null) {
        }
        valid = true;
    } catch (JsonParseException jpe) {
        jpe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return valid;
}
Also used : IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 95 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project herd by FINRAOS.

the class BusinessObjectDefinitionServiceIndexTest method testIndexSpotCheckPercentageValidationBusinessObjectDefinitionsObjectMappingException.

@Test
public void testIndexSpotCheckPercentageValidationBusinessObjectDefinitionsObjectMappingException() throws Exception {
    List<BusinessObjectDefinitionEntity> businessObjectDefinitionEntityList = new ArrayList<>();
    businessObjectDefinitionEntityList.add(businessObjectDefinitionDaoTestHelper.createBusinessObjectDefinitionEntity(NAMESPACE, BDEF_NAME, DATA_PROVIDER_NAME, BDEF_DESCRIPTION, businessObjectDefinitionServiceTestHelper.getNewAttributes()));
    businessObjectDefinitionEntityList.add(businessObjectDefinitionDaoTestHelper.createBusinessObjectDefinitionEntity(NAMESPACE, BDEF_NAME_2, DATA_PROVIDER_NAME_2, BDEF_DESCRIPTION_2, businessObjectDefinitionServiceTestHelper.getNewAttributes()));
    // Mock the call to external methods
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_BDEF_SPOT_CHECK_PERCENTAGE, Double.class)).thenReturn(0.05);
    when(businessObjectDefinitionDao.getPercentageOfAllBusinessObjectDefinitions(0.05)).thenReturn(businessObjectDefinitionEntityList);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_BDEF_DOCUMENT_TYPE, String.class)).thenReturn(SEARCH_INDEX_DOCUMENT_TYPE);
    when(jsonHelper.objectToJson(any())).thenThrow(new IllegalStateException(new JsonParseException("Failed to Parse", new JsonLocation("SRC", 100L, 1, 2))));
    when(indexFunctionsDao.isValidDocumentIndex(any(), any(), any(), any())).thenReturn(false);
    // Call the method under test
    boolean isSpotCheckPercentageValid = businessObjectDefinitionService.indexSpotCheckPercentageValidationBusinessObjectDefinitions(SEARCH_INDEX_NAME);
    assertThat("Business object definition service index spot check random validation is true when it should have been false.", isSpotCheckPercentageValid, is(false));
    // Verify the calls to external methods
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_BDEF_SPOT_CHECK_PERCENTAGE, Double.class);
    verify(businessObjectDefinitionDao).getPercentageOfAllBusinessObjectDefinitions(0.05);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_BDEF_DOCUMENT_TYPE, String.class);
    verify(businessObjectDefinitionHelper, times(2)).safeObjectMapperWriteValueAsString(any(BusinessObjectDefinitionEntity.class));
    verify(indexFunctionsDao, times(2)).isValidDocumentIndex(any(), any(), any(), any());
    verifyNoMoreInteractionsHelper();
}
Also used : JsonLocation(com.fasterxml.jackson.core.JsonLocation) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) ArrayList(java.util.ArrayList) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Test(org.junit.Test)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)143 IOException (java.io.IOException)73 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)58 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)35 JsonParser (com.fasterxml.jackson.core.JsonParser)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)18 Map (java.util.Map)18 JsonToken (com.fasterxml.jackson.core.JsonToken)15 InputStream (java.io.InputStream)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 File (java.io.File)11 HashMap (java.util.HashMap)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 JsonFactory (com.fasterxml.jackson.core.JsonFactory)7 JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 Date (java.util.Date)5 GZIPInputStream (java.util.zip.GZIPInputStream)5