本文整理汇总了Java中org.codehaus.jackson.map.ObjectMapper.writeValueAsString方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectMapper.writeValueAsString方法的具体用法?Java ObjectMapper.writeValueAsString怎么用?Java ObjectMapper.writeValueAsString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.jackson.map.ObjectMapper
的用法示例。
在下文中一共展示了ObjectMapper.writeValueAsString方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testValidateResponseJsonErrorUnknownException
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
@Test
public void testValidateResponseJsonErrorUnknownException()
throws IOException {
Map<String, Object> json = new HashMap<String, Object>();
json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, "FooException");
json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, "foo.FooException");
json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX");
Map<String, Object> response = new HashMap<String, Object>();
response.put(HttpExceptionUtils.ERROR_JSON, json);
ObjectMapper jsonMapper = new ObjectMapper();
String msg = jsonMapper.writeValueAsString(response);
InputStream is = new ByteArrayInputStream(msg.getBytes());
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
Mockito.when(conn.getErrorStream()).thenReturn(is);
Mockito.when(conn.getResponseMessage()).thenReturn("msg");
Mockito.when(conn.getResponseCode()).thenReturn(
HttpURLConnection.HTTP_BAD_REQUEST);
try {
HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
Assert.fail();
} catch (IOException ex) {
Assert.assertTrue(ex.getMessage().contains("EX"));
Assert.assertTrue(ex.getMessage().contains("foo.FooException"));
}
}
示例2: testValidateResponseJsonErrorKnownException
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
@Test
public void testValidateResponseJsonErrorKnownException() throws IOException {
Map<String, Object> json = new HashMap<String, Object>();
json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, IllegalStateException.class.getSimpleName());
json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, IllegalStateException.class.getName());
json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX");
Map<String, Object> response = new HashMap<String, Object>();
response.put(HttpExceptionUtils.ERROR_JSON, json);
ObjectMapper jsonMapper = new ObjectMapper();
String msg = jsonMapper.writeValueAsString(response);
InputStream is = new ByteArrayInputStream(msg.getBytes());
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
Mockito.when(conn.getErrorStream()).thenReturn(is);
Mockito.when(conn.getResponseMessage()).thenReturn("msg");
Mockito.when(conn.getResponseCode()).thenReturn(
HttpURLConnection.HTTP_BAD_REQUEST);
try {
HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
Assert.fail();
} catch (IllegalStateException ex) {
Assert.assertEquals("EX", ex.getMessage());
}
}
示例3: signIn
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
@Override
public boolean signIn(String u, String p) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
ObjectMapper mapper=ObjectMapperPool.getMapper();
String json="";
try {
json = mapper.writeValueAsString(new LoginRequest(u, p, fingerPrint.getFingerPrint()));
HttpEntity<String> entity = new HttpEntity<String>(json, headers);
ResponseEntity<String> response=restTemplate.postForEntity(serverPath.concat(Constants.SIGN_IN_SUFFIX), entity, String.class);
LoginResponse resp=mapper.readValue(response.getBody(), LoginResponse.class);
logger.debug("Got login response {}",resp.getResult());
return resp.getResult();
} catch (Exception e) {
logger.error("Parse exception", e);
}
return false;
}
示例4: createNewUser
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
@Override
public boolean createNewUser(String u, String p) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
ObjectMapper mapper=ObjectMapperPool.getMapper();
String json="";
try {
json = mapper.writeValueAsString(new LoginRequest(u, p, fingerPrint.getFingerPrint()));
HttpEntity<String> entity = new HttpEntity<String>(json, headers);
ResponseEntity<String> response=restTemplate.postForEntity(serverPath.concat(Constants.CREATE_USER_SUFFIX), entity, String.class);
CreateUserResponse resp=mapper.readValue(response.getBody(), CreateUserResponse.class);
logger.debug("Got create user response {}",resp.getStatus());
return resp.isResult();
} catch (Exception e) {
logger.error("Parse exception", e);
}
return false;
}
示例5: getSchedulingDecisionSummary
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
public String getSchedulingDecisionSummary() {
Map<Object, Integer> decisions = scheduleCacheRef.get();
if (decisions == null) {
return "{}";
} else {
try {
ObjectMapper om = new ObjectMapper();
return om.writeValueAsString(decisions);
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
}
示例6: testSerialization
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
@Test
public void testSerialization()
throws JsonGenerationException, JsonMappingException, IOException {
PerformanceEvaluation.TestOptions options = new PerformanceEvaluation.TestOptions();
assertTrue(!options.isAutoFlush());
options.setAutoFlush(true);
ObjectMapper mapper = new ObjectMapper();
String optionsString = mapper.writeValueAsString(options);
PerformanceEvaluation.TestOptions optionsDeserialized =
mapper.readValue(optionsString, PerformanceEvaluation.TestOptions.class);
assertTrue(optionsDeserialized.isAutoFlush());
}
示例7: toJsonString
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
public static String toJsonString(final List<XAttr> xAttrs,
final XAttrCodec encoding) throws IOException {
final Map<String, Object> finalMap = new TreeMap<String, Object>();
finalMap.put("XAttrs", toJsonArray(xAttrs, encoding));
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(finalMap);
}
示例8: generatePartialResourceRequest
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
private HttpEntity<MultiValueMap<String, ?>> generatePartialResourceRequest(UploadApplicationPayload application,
CloudResources knownRemoteResources) throws IOException {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>(2);
body.add("application", application);
ObjectMapper mapper = new ObjectMapper();
String knownRemoteResourcesPayload = mapper.writeValueAsString(knownRemoteResources);
body.add("resources", knownRemoteResourcesPayload);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
return new HttpEntity<MultiValueMap<String, ?>>(body, headers);
}
示例9: mapToJson
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
private static String mapToJson(Map map){
ObjectMapper mapperObj = new ObjectMapper();
String jsonResp = "";
try {
jsonResp = mapperObj.writeValueAsString(map);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResp;
}
示例10: getCallVolumeSummary
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
public String getCallVolumeSummary() {
try {
ObjectMapper om = new ObjectMapper();
return om.writeValueAsString(callCounts);
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
示例11: getContentSummary
import org.codehaus.jackson.map.ObjectMapper; //导入方法依赖的package包/类
/**
* Return the JSON formatted ContentSummary of the specified path.
* @param path a path specifies a file or directory
* @return JSON formatted ContentSummary
* @throws IOException if failed to serialize ContentSummary to JSON.
*/
String getContentSummary(String path) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return "{\"ContentSummary\":\n"
+ mapper.writeValueAsString(getContentSummaryMap(path)) + "\n}\n";
}