/** * * @param result a Map<String, Object> contains http request/response info which needs to be logged. * @param loggerFunc Consuer<T> getLoggerFuncBasedOnLevel(config.getLogLevel()) */ private static void logResultUsingJson(Map<String, Object> result, Consumer<String> loggerFunc) { ObjectMapper mapper = new ObjectMapper(); String resultJson = ""; try { resultJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result); } catch (JsonProcessingException e) { logger.error(e.toString()); } if(StringUtils.isNotBlank(resultJson)){ loggerFunc.accept("Dump Info:\n" + resultJson); } }
@Override public String apply(Statistics t) { try { return t.toJson(); } catch (JsonProcessingException e) { Assert.fail(e.toString()); } return null; } });
@Override public String apply(Statistics t) { try { return Statistics.toJson(t); } catch (JsonProcessingException e) { Assert.fail(e.toString()); } return null; } });
/** * Returns a JSON serialization of the marked insertions and deletions of * statements, in the format required by the Wikibase "wbeditentity" action. * * @return JSON serialization of updates */ @JsonIgnore public String getJsonUpdateString() { try { return mapper.writeValueAsString(this); } catch (JsonProcessingException e) { return ("Failed to serialize statement update to JSON: " + e.toString()); } }
/** * Returns a JSON serialization of the marked insertions and deletions of * statements, in the format required by the Wikibase "wbeditentity" action. * * @return JSON serialization of updates */ @JsonIgnore public String getJsonUpdateString() { try { return mapper.writeValueAsString(this); } catch (JsonProcessingException e) { return ("Failed to serialize statement update to JSON: " + e.toString()); } }
@Override public void write(OutputStream output) throws IOException, WebApplicationException { JsonGenerator generator = jsonFactory.createGenerator(output); generator.writeStartObject(); generator.writeFieldName("error"); generator.writeString(ex.toString()); generator.writeEndObject(); } }).build());
/** * Serializes the given object in JSON and returns the resulting string. In * case of errors, null is returned. In particular, this happens if the * object is not based on a Jackson-annotated class. An error is logged in * this case. * * @param object * object to serialize * @return JSON serialization or null */ protected static String jacksonObjectToString(Object object) { try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { logger.error("Failed to serialize JSON data: " + e.toString()); return null; } }
/** * Serializes the given object in JSON and returns the resulting string. In * case of errors, null is returned. In particular, this happens if the * object is not based on a Jackson-annotated class. An error is logged in * this case. * * @param object * object to serialize * @return JSON serialization or null */ protected static String jacksonObjectToString(Object object) { try { return mapper.writeValueAsString(object); } catch (JsonProcessingException e) { logger.error("Failed to serialize JSON data: " + e.toString()); return null; } }
private static String toJson(Statistics stats) { try { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(StatisticsEntity.from(stats)); } catch (JsonProcessingException ex) { // don't do this in real live return ex.toString(); } }
private void addExceptionToResponse(final ResponseBuilder responseBuilder, final BillingExceptionJson exception) { try { responseBuilder.entity(mapper.writeValueAsString(exception)).type(MediaType.APPLICATION_JSON); } catch (JsonProcessingException e) { log.warn("Unable to serialize exception", exception); responseBuilder.entity(e.toString()).type(MediaType.TEXT_PLAIN_TYPE); } }
public static String getJSON(Object o) { try { return getInstance().writerWithDefaultPrettyPrinter().writeValueAsString(o); } catch (JsonProcessingException ex) { log.error("Error generating model JSON: " + ex.toString()); return null; } }
public static String getJSONRaw(Object o) { try { return getInstance().writer().writeValueAsString(o); } catch (JsonProcessingException ex) { log.error("Error generating model JSON: " + ex.toString()); return null; } }
@Override public String toJson(final Object value) { try { return new ObjectMapper().writeValueAsString(value); } catch (JsonProcessingException e) { if (log.isDebugEnabled()) { log.warn("Could not serialize value to JSON", e); } else { log.warn("Could not serialize value to JSON: {}", e.toString()); } throw new JsonSerializationException("Could not serialize value to JSON", e); } } }
public static void writeJSON(Object o, Resource res) { try { getInstance().writerWithDefaultPrettyPrinter().writeValue(res.getOutputStream(), o); } catch (JsonProcessingException ex) { log.error("Error saving model JSON: " + ex.toString()); } catch (IOException ex) { log.error("Error saving model JSON: " + ex.toString()); } }
public static void writeJSONRaw(Object o, Resource res) { try { getInstance().writer().writeValue(res.getOutputStream(), o); } catch (JsonProcessingException ex) { log.error("Error saving model JSON: " + ex.toString()); } catch (IOException ex) { log.error("Error saving model JSON: " + ex.toString()); } }
/** * @param value json string * @param collectionType class describing how to deserialize collection of objects * @param <T> type * @return deserialized T object */ @SuppressWarnings("unchecked") public static <T> T toObject(String value, final CollectionType collectionType) throws IOException { try { return (T)jsonMapper.readValue(value, collectionType); } catch (JsonGenerationException | JsonMappingException e) { throw new SwiftJsonMarshallingException(e.toString() + " source: " + value, e); } }
/** * 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 | JsonMappingException e) { throw new SwiftJsonMarshallingException(e.toString(), e); } }
@Override public String toJSON(TCAPIVersion version, Boolean pretty) { ObjectWriter writer = Mapper.getWriter(pretty); try { return writer.writeValueAsString(this.toJSONNode(version)); } catch (JsonProcessingException ex) { return "Exception in JSONBase Class: " + ex.toString(); } }
protected ElideResponse buildResponse(Pair<Integer, JsonNode> response) { try { JsonNode responseNode = response.getRight(); Integer responseCode = response.getLeft(); String body = responseNode == null ? null : mapper.writeJsonApiDocument(responseNode); return new ElideResponse(responseCode, body); } catch (JsonProcessingException e) { return new ElideResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString()); } }
protected ElideResponse buildResponse(Pair<Integer, JsonNode> response) { try { JsonNode responseNode = response.getRight(); Integer responseCode = response.getLeft(); String body = responseNode == null ? null : mapper.writeJsonApiDocument(responseNode); return new ElideResponse(responseCode, body); } catch (JsonProcessingException e) { return new ElideResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString()); } }