DefaultHandler.sendResponse(ctx, request, response, HttpResponseStatus.OK); } catch (JsonMappingException e) { log.debug(String.format("Exception %s", e.toString())); response = String.format("Invalid Data: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST);
/** * Converting object to JSON string. If errors appears throw * MeshinException runtime exception. * * @param object The object to convert. * @return The JSON string representation. * @throws IOException IO issues * @throws SwiftJsonMarshallingException failure to generate JSON */ public static String toJSON(Object object) throws IOException { Writer json = new StringWriter(); try { jsonMapper.writeValue(json, object); return json.toString(); } catch (JsonGenerationException e) { throw new SwiftJsonMarshallingException(e.toString(), e); } catch (JsonMappingException e) { throw new SwiftJsonMarshallingException(e.toString(), e); } }
/** * @param value json string * @param collectionType class describing how to deserialize collection of objects * @param <T> type * @return deserialized T object */ public static <T> T toObject(String value, final CollectionType collectionType) throws IOException { try { return jsonMapper.readValue(value, collectionType); } catch (JsonGenerationException e) { throw new SwiftJsonMarshallingException(e.toString() + " source: " + value, e); } catch (JsonMappingException e) { throw new SwiftJsonMarshallingException(e.toString() + " source: " + value, e); } }
/** * Convert string representation to object. If errors appears throw * Exception runtime exception. * * @param value The JSON string. * @param klazz The class to convert. * @return The Object of the given class. */ public static <T> T toObject(String value, Class<T> klazz) throws IOException { try { return jsonMapper.readValue(value, klazz); } catch (JsonGenerationException e) { throw new SwiftJsonMarshallingException(e.toString() + " source: " + value, e); } catch (JsonMappingException e) { throw new SwiftJsonMarshallingException(e.toString() + " source: " + value, e); } }
throw new WebSocketException("JSON serialization error (" + e.toString() + ")");