本文整理汇总了Java中com.fasterxml.jackson.databind.ObjectMapper.writeValue方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectMapper.writeValue方法的具体用法?Java ObjectMapper.writeValue怎么用?Java ObjectMapper.writeValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.databind.ObjectMapper
的用法示例。
在下文中一共展示了ObjectMapper.writeValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateConfig
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
void updateConfig() throws IOException {
// we assume we only get called in the root directory of a project
if (!Files.exists(EADL_CONFIG)) {
printNotInitialized();
return;
} else if (!Files.isWritable(EADL_CONFIG)) {
CLI.println("Could not write config file, please check permissions.");
return;
}
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(EADL_CONFIG.toFile(), config);
} catch (IOException e) {
LOG.error("Error writing eadl config to file.", e);
}
}
示例2: objectToString
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static String objectToString (final Object object) {
if (object == null) {
return null;
}
try {
final StringWriter stringWriter = new StringWriter();
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.writeValue(stringWriter, object);
return stringWriter.toString().replaceAll(System.getProperty("line.separator"), "");
} catch (IOException ex) {
STRINGUTIL_LOGGER.info("Sorry. had a error on during Object to String. ("+ex.toString()+")");
return null;
}
}
示例3: readYAMLFile
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Test
public void readYAMLFile() {
// new error-free endpoint
Endpoint endpoint = resources.createEndpoint("GET", "/test-yaml-file", "200");
// write yaml file on temporary directory
ObjectMapper mapperJson = new ObjectMapper(new YAMLFactory());
File file = TempIO.buildFile("read-yaml-file", ".lyre", resources.getDirectory(0));
try {
mapperJson.writeValue(file, endpoint);
} catch (IOException e) {
fail("Couldn't write on temporary file: " + e.getMessage());
}
reader.read(file);
//should have a object node with yaml file on it.
assertThat(reader.getObjectNodes()).isNotEmpty();
assertThat(reader.getObjectNodes().get(file.getAbsolutePath())).isNotNull();
}
示例4: testSerialize
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Test
public void testSerialize() throws Exception {
String expected = "{\"version\":null,\"status\":0,\"routeInstructions\":null,\"routeGeometry\":[[50.753,5.712],[50.653,6.012]]}";
GeoCoordinates[] coords = new GeoCoordinates[2];
coords[0] = new GeoCoordinates(50.753, 5.712);
coords[1] = new GeoCoordinates(50.653, 6.012);
PathData pathData = new PathData.Builder().withRouteGeometry(coords).build();
ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(PathData.class, UraPathDataMixIn.class);
Writer stringWriter = new StringWriter();
try {
mapper.writeValue(stringWriter, pathData);
String json = stringWriter.toString();
assertNotNull(json);
assertEquals(json, expected);
} catch (Exception e) {
fail();
}
}
示例5: artifact
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Override
public SummaryResponse artifact(List<String> checksums, List<String> paths) throws IOException {
if (checksums == null && paths == null) {
return new SummaryResponseImpl();
}
ObjectMapper mapper = ObjectMapperHelper.get();
ArtifactSummaryBody summaryBody = new ArtifactSummaryBody(checksums, paths);
ByteArrayOutputStream out = new ByteArrayOutputStream();
mapper.writeValue(out, summaryBody);
ByteArrayInputStream content = new ByteArrayInputStream(out.toByteArray());
Map<String, String> headers = new HashMap<>();
XrayImpl.addContentTypeJsonHeader(headers);
HttpResponse response = xray.post("summary/artifact", headers, content);
return mapper.readValue(response.getEntity().getContent(), SummaryResponseImpl.class);
}
示例6: verifySuccess
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Test
public void verifySuccess() throws Exception {
final Principal principalWritten = new DefaultPrincipalFactory().createPrincipal("casuser");
final ObjectMapper mapper = new ObjectMapper();
final StringWriter writer = new StringWriter();
mapper.writeValue(writer, principalWritten);
server.andRespond(withSuccess(writer.toString(), MediaType.APPLICATION_JSON));
final HandlerResult res = authenticationHandler.authenticate(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword());
assertEquals(res.getPrincipal().getId(), "casuser");
}
示例7: dataToJson
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static String dataToJson(Object data) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
StringWriter sw = new StringWriter();
mapper.writeValue(sw, data);
return sw.toString();
}
catch (IOException e) {
throw new RuntimeException("IOException while mapping object (" + data + ") to JSON");
}
}
示例8: toString
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static String toString(TrainingDataModel model)
throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(Include.NON_NULL);
StringWriter sw = new StringWriter();
objectMapper.writeValue(sw, model);
return sw.toString();
}
示例9: writeToFile
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static void writeToFile(String jsonOutputFile,Object gson){
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
try {
mapper.writeValue(new File(jsonOutputFile), gson);
System.out.println("Generated: "+ jsonOutputFile);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e.toString());
}
}
示例10: writeToFile
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static void writeToFile(String jsonOutputFile,Object gson){
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(new File(jsonOutputFile), gson);
System.out.println("Generated: "+ jsonOutputFile);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e.toString());
}
}
示例11: toString
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static String toString(Object object) {
StringWriter sw = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
mapper.writeValue(sw, object);
} catch (IOException e) {
log.error("Object to Json error", e);
}
return sw.toString();
}
示例12: generateEdgeLine
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static void generateEdgeLine(StringWriter sw, ObjectMapper mapper, EdgeBean eBean) throws JsonGenerationException, JsonMappingException, IOException {
sw.getBuffer().setLength(0);
sw.append('E'); // Record type
sw.append('=');
mapper.writeValue(sw, eBean);
sw.append('#');
int hashCode = sw.toString().hashCode();
sw.append(toHex(hashCode));
sw.append('\n');
}
示例13: publishMetricFilters
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Override
public int publishMetricFilters(Collection<MetricFilter> metricFilters) {
log.info("Writing metric filters to output file: " + outputFile.getPath());
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
try {
mapper.writeValue(outputFile, metricFilters);
} catch (IOException e) {
log.error("Error writing JSON to output file.", e);
}
return 0;
}
示例14: saveAccounts
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public void saveAccounts(List<Account> accounts) {
try {
final ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
System.out.println(mapper.writeValueAsString(accounts));
mapper.writeValue(rulesFile, accounts);
} catch (IOException ex) {
Logger.getLogger(AccountService.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例15: writeDataToFile
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
private void writeDataToFile() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
File recordFile = getRecordFile(testName);
recordFile.createNewFile();
mapper.writeValue(recordFile, recordedData);
}