Search in sources :

Example 1 with JsonParseException

use of org.codehaus.jackson.JsonParseException in project openhab1-addons by openhab.

the class OpenPathsBinding method getUserLocation.

@SuppressWarnings("unchecked")
private Location getUserLocation(String accessKey, String secretKey) {
    // build the OAuth service using the access/secret keys
    OAuthService service = new ServiceBuilder().provider(new OpenPathsApi()).apiKey(accessKey).apiSecret(secretKey).build();
    // build the request
    OAuthRequest request = new OAuthRequest(Verb.GET, "https://openpaths.cc/api/1");
    service.signRequest(Token.empty(), request);
    request.addQuerystringParameter("num_points", "1");
    // send the request and check we got a successful response
    Response response = request.send();
    if (!response.isSuccessful()) {
        logger.error("Failed to request the OpenPaths location, response code: " + response.getCode());
        return null;
    }
    // parse the response to build our location object
    Map<String, Object> locationData;
    String toParse = "{}";
    try {
        ObjectMapper jsonReader = new ObjectMapper();
        toParse = response.getBody();
        toParse = toParse.substring(1, toParse.length() - 2);
        locationData = jsonReader.readValue(toParse, Map.class);
    } catch (JsonParseException e) {
        logger.error("Error parsing JSON:\n" + toParse, e);
        return null;
    } catch (JsonMappingException e) {
        logger.error("Error mapping JSON:\n" + toParse, e);
        return null;
    } catch (IOException e) {
        logger.error("An I/O error occured while decoding JSON:\n" + response.getBody());
        return null;
    }
    float latitude = Float.parseFloat(locationData.get("lat").toString());
    float longitude = Float.parseFloat(locationData.get("lon").toString());
    String device = locationData.get("device").toString();
    return new Location(latitude, longitude, device);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) ServiceBuilder(org.scribe.builder.ServiceBuilder) Response(org.scribe.model.Response) OAuthService(org.scribe.oauth.OAuthService) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with JsonParseException

use of org.codehaus.jackson.JsonParseException in project meteo by pierre.

the class TopicListener method onMessage.

@Override
public void onMessage(Message message) {
    if (message instanceof TextMessage) {
        TextMessage txtMsg = (TextMessage) message;
        String txt = null;
        try {
            txt = txtMsg.getText();
            log.debug("Received a message, yay!\n" + txt);
            Map event = mapper.readValue(txt, Map.class);
            esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
        } catch (JMSException ex) {
            log.warn("Got an error from the message queue", ex);
        } catch (ClassCastException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (JsonMappingException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (JsonParseException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (IOException ex) {
            log.warn("Got an error from the message queue", ex);
        }
    } else if (message instanceof BytesMessage) {
        final BytesMessage byteMessage = (BytesMessage) message;
        long llen;
        try {
            llen = byteMessage.getBodyLength();
        } catch (JMSException e) {
            log.warn("Unable to get message length", e);
            return;
        }
        if (llen > Integer.MAX_VALUE) {
            // should never occur but...
            log.error("Ridiculously huge message payload, above 32-bit length");
        } else {
            final int len = (int) llen;
            final byte[] data = new byte[len];
            final int readLen;
            try {
                readLen = byteMessage.readBytes(data);
            } catch (JMSException e) {
                log.warn("Unable to get message bytes", e);
                return;
            }
            if (readLen < len) {
                log.error("Failed to read byte message contents; read {}, was trying to read {}", readLen, data.length);
            } else {
                final Map event;
                try {
                    event = mapper.readValue(data, Map.class);
                    esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
                } catch (IOException e) {
                    log.error("Failed to convert message to Esper Event", readLen, data.length);
                }
            }
        }
    } else {
        log.error("Unexpected message type '{}' from AMQ broker: must skip", message.getClass().getName());
    }
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Example 3 with JsonParseException

use of org.codehaus.jackson.JsonParseException in project flink by apache.

the class PreviewPlanDumpTest method dump.

private void dump(Plan p) {
    try {
        List<DataSinkNode> sinks = Optimizer.createPreOptimizedPlan(p);
        PlanJSONDumpGenerator dumper = new PlanJSONDumpGenerator();
        String json = dumper.getPactPlanAsJSON(sinks);
        JsonParser parser = new JsonFactory().createJsonParser(json);
        while (parser.nextToken() != null) ;
    } catch (JsonParseException e) {
        e.printStackTrace();
        Assert.fail("JSON Generator produced malformatted output: " + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("An error occurred in the test: " + e.getMessage());
    }
}
Also used : PlanJSONDumpGenerator(org.apache.flink.optimizer.plandump.PlanJSONDumpGenerator) DataSinkNode(org.apache.flink.optimizer.dag.DataSinkNode) JsonFactory(org.codehaus.jackson.JsonFactory) JsonParseException(org.codehaus.jackson.JsonParseException) JsonParseException(org.codehaus.jackson.JsonParseException) JsonParser(org.codehaus.jackson.JsonParser)

Example 4 with JsonParseException

use of org.codehaus.jackson.JsonParseException in project ranger by apache.

the class JSONUtil method readListToString.

public String readListToString(List<?> list) {
    ObjectMapper mapper = new ObjectMapper();
    String jsonString = null;
    try {
        jsonString = mapper.writeValueAsString(list);
    } catch (JsonParseException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    } catch (JsonMappingException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    } catch (IOException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    }
    return jsonString;
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 5 with JsonParseException

use of org.codehaus.jackson.JsonParseException in project ranger by apache.

the class RangerDataHistService method writeObjectAsString.

public String writeObjectAsString(RangerBaseModelObject vObj) {
    ObjectMapper mapper = new ObjectMapper();
    String jsonStr;
    try {
        jsonStr = mapper.writeValueAsString(vObj);
        return jsonStr;
    } catch (JsonParseException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    } catch (JsonMappingException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    } catch (IOException e) {
        throw restErrorUtil.createRESTException("Invalid input data: " + e.getMessage(), MessageEnums.INVALID_INPUT_DATA);
    }
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

JsonParseException (org.codehaus.jackson.JsonParseException)68 IOException (java.io.IOException)59 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)56 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)30 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)9 TypeReference (org.codehaus.jackson.type.TypeReference)9 List (java.util.List)7 Map (java.util.Map)7 ClientResponse (com.sun.jersey.api.client.ClientResponse)5 CertificationChargingList (com.itrus.portal.entity.CertificationChargingList)3 CertificationChargingWrap (com.itrus.portal.entity.CertificationChargingWrap)3 ChargingPriceList (com.itrus.portal.entity.ChargingPriceList)3 ServiceNameList (com.itrus.portal.entity.ServiceNameList)3 JsonParser (org.codehaus.jackson.JsonParser)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)3