下面列出了com.fasterxml.jackson.core.JsonProcessingException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 将对象转换成json字符串。
* <p>Title: pojoToJson</p>
* <p>Description: </p>
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
@Test
public void testCreate() {
Map<String, String> props = new HashMap<>();
Map<DimensionMap, String> auxiliaryRecipients = new TreeMap<>();
DimensionMap dimensionMap1 = new DimensionMap();
dimensionMap1.put(GROUP_BY_DIMENSION_NAME, "V1");
auxiliaryRecipients.put(dimensionMap1, EMAIL1);
DimensionMap dimensionMap2 = new DimensionMap();
dimensionMap2.put(GROUP_BY_DIMENSION_NAME, "V2");
auxiliaryRecipients.put(dimensionMap2, EMAIL2);
DimensionMap dimensionMap3 = new DimensionMap();
dimensionMap3.put(GROUP_BY_DIMENSION_NAME, "V1");
dimensionMap3.put("K2", "V3");
auxiliaryRecipients.put(dimensionMap3, EMAIL_NOT_USED);
try {
ObjectMapper OBJECT_MAPPER = new ObjectMapper();
String writeValueAsString = OBJECT_MAPPER.writeValueAsString(auxiliaryRecipients);
props.put(DimensionalAlertGroupAuxiliaryRecipientProvider.AUXILIARY_RECIPIENTS_MAP_KEY, writeValueAsString);
recipientProvider = new DimensionalAlertGroupAuxiliaryRecipientProvider();
recipientProvider.setParameters(props);
NavigableMap<DimensionMap, String> auxiliaryRecipientsRecovered = recipientProvider.getAuxiliaryEmailRecipients();
// Test the map of auxiliary recipients
Assert.assertEquals(auxiliaryRecipientsRecovered.get(dimensionMap1), EMAIL1);
Assert.assertEquals(auxiliaryRecipientsRecovered.get(dimensionMap2), EMAIL2);
Assert.assertEquals(auxiliaryRecipientsRecovered.get(dimensionMap3), EMAIL_NOT_USED);
} catch (JsonProcessingException e) {
e.printStackTrace();
Assert.fail();
}
}
/**
*
* @Description:将对象转换成json字符串
* @param data
* @return
* @author:Yeauty
* @time:2017年8月18日 下午5:50:03
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onSpanFinished(MockSpan mockSpan) {
try {
String json = objectMapper.writeValueAsString(mockSpan);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, JsonNode parameter, JdbcType jdbcType) throws SQLException {
String str = null;
try {
str = mapper.writeValueAsString(parameter);
} catch (JsonProcessingException e) {
e.printStackTrace();
str = "{}";
}
ps.setString(i, str);
}
@Override
public ItemSource create(Object event, ObjectWriter objectWriter) {
try {
return new LayoutTestItemSource(objectWriter.writeValueAsString(event));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
private static void printJSON(Object object) {
String result;
try {
result = om.writerWithDefaultPrettyPrinter().writeValueAsString(object);
System.out.println(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
public boolean updateRating(Rating persisted) {
try {
valueOps.set("rating-" + persisted.getId(), jsonMapper.writeValueAsString(persisted));
return true;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return false;
}
public static void printJson(Object value) {
try {
System.out.println(new ObjectMapper().writeValueAsString(value));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
/**
* 将对象转换成json字符串。
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
@Override
public void callBack(Response response) {
try {
LoggerUtil.commonLog.info("收到区块高度更新消息:{}", JSONUtils.obj2json(response));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
HashMap result = ((HashMap) response.getResponseData());
blockService.newBlockHeight(result);
}
public static String toJson(Object data) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(data);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
public static String object2json(Object obj){
try {
return JSON_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
public static String bean2Json(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
/**
* formatQuoteListToJSON - convert quote list to JSON
*
* @param quote list
* @return JSON formatted quote list
*/
private String formatQuoteListToJSON(List<StreamingQuote> quoteList) {
String jsonData = null;
ObjectMapper mapper = new ObjectMapper();
try {
jsonData = mapper.writeValueAsString(quoteList);
} catch (JsonProcessingException e) {
System.out.println(
"TimeRangeStreamingQuoteActionHandler.formatQuoteListToJSON(): ERROR: JsonProcessingException on quote list !!!");
e.printStackTrace();
}
return jsonData;
}
@GET
@UnitOfWork(transactional = false, readOnly = true, flushMode = FlushMode.MANUAL)
public Response get(@RobeAuth Credentials credentials) {
Response.ResponseBuilder builder = Response.ok();
builder.status(Response.Status.ACCEPTED);
try {
builder.entity(OBJECT_MAPPER.writeValueAsString(environment.metrics()));
} catch (JsonProcessingException e) {
builder.status(Response.Status.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
return builder.build();
}
/**
* Method to set the data for all chart types except {@link com.github.appreciated.apexcharts.config.chart.Type#pie} and {@link com.github.appreciated.apexcharts.config.chart.Type#donut}.
* For these both types use setSeries{@link #setSeries(Double[])}
* @param series the data series to assign to the Chart
*/
public void setSeries(Series... series) {
try {
getModel().setSeries(objectMapper.writeValueAsString(series));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
public static String toJson(Object obj) {
try {
return JSON.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
public static String toJSON(ApiCallRc apiCallRc)
{
ObjectMapper objectMapper = new ObjectMapper();
ArrayList<ApiCallData> jsonData = new ArrayList<>();
for (ApiCallRc.RcEntry rc : apiCallRc.getEntries())
{
jsonData.add(new ApiCallData(
rc.getReturnCode(),
rc.getMessage(),
rc.getCause(),
rc.getCorrection(),
rc.getDetails(),
rc.getObjRefs(),
rc.getErrorIds()
));
}
String ret = null;
try
{
ret = objectMapper.writeValueAsString(jsonData);
}
catch (JsonProcessingException exc)
{
exc.printStackTrace();
}
return ret;
}
public static String bean2Json(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return "";
}
}