本文整理了Java中org.boon.json.ObjectMapper.writeValue()
方法的一些代码示例,展示了ObjectMapper.writeValue()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ObjectMapper.writeValue()
方法的具体详情如下:
包路径:org.boon.json.ObjectMapper
类名称:ObjectMapper
方法名:writeValue
[英]Method that can be used to serialize any Java value as JSON output, written to File provided.
[中]方法,该方法可用于将任何Java值序列化为JSON输出,并写入提供的文件中。
代码示例来源:origin: cowtowncoder/java-json-performance-benchmarks
@Override
public int _writeItems(MeasurementPOJO items, Writer out) throws Exception
{
mapper.writeValue(out, items);
return items.size();
}
代码示例来源:origin: boonproject/boon
mapper.writeValue( file, user );
代码示例来源:origin: boonproject/boon
public static void part4IntoAMapFirst() throws Exception {
ObjectMapper mapper = JsonFactory.createUseJSONDates();
puts( mapper.writeValueAsString( user ) );
//Now to write and then read this as a file.
File file = File.createTempFile( "user", ".json" );
mapper.writeValue( file, user );
Object userFromFile = mapper.readValue( file, Object.class );
puts( "userFromFile", "type", userFromFile.getClass(), "value", userFromFile );
Map<String, Object> map = (Map<String, Object>) mapper.readValue( file, Map.class );
puts( "userFromFile", "type", map.getClass(), "value", map );
puts( "userFromFile.name", "type", map.get("name").getClass(),
"value", map.get("name") );
puts( "userFromFile.birthDate", "type", map.get("birthDate").getClass(),
"value", map.get("birthDate") );
puts( "userFromFile.gender", "type", map.get("gender").getClass(),
"value", map.get("gender") );
User userFromMap =
MapObjectConversion.fromMap(
map, User.class);
puts ( userFromMap );
boolean ok = user.equals ( userFromMap ) || die (userFromMap.toString ());
}
代码示例来源:origin: cowtowncoder/java-json-performance-benchmarks
@Override
public int _writeItems(MeasurementPOJO items, OutputStream out) throws Exception
{
mapper.writeValue(out, items);
return items.size();
}
代码示例来源:origin: boonproject/boon
public static void part1ReadAndWriteMyBeanToAFile() throws Exception {
MyBean myBean = new MyBean();
File dst = File.createTempFile( "emp", ".json" );
ObjectMapper mapper = JsonFactory.create();
puts( "json string", mapper.writeValueAsString( myBean ) );
String str = mapper.writeValueAsString ( myBean );
boolean ok = str.contains ( "{\"name\":\"Rick\"" ) || die( str );
mapper.writeValue( dst, myBean ); // where 'dst' can be File, OutputStream or Writer
File src = dst;
MyBean value = mapper.readValue( src, MyBean.class ); // 'src' can be File, InputStream, Reader, String
ok |= value.name.contains ( "Rick" );
puts( "mybean", value );
Object root = mapper.readValue( src, Object.class );
Map<String, Object> rootAsMap = mapper.readValue( src, Map.class );
puts( "root", root );
puts( "rootAsMap", rootAsMap );
MyBean myBean1 = new MyBean();
myBean1.name = "Diana";
MyBean myBean2 = new MyBean();
myBean2.name = "Rick";
dst = File.createTempFile( "empList", ".json" );
final List<MyBean> list = Lists.list( myBean1, myBean2 );
str = mapper.writeValueAsString ( list );
puts ( "json string", mapper.writeValueAsString ( list ) );
ok |= str.contains ( "[{\"name\":\"Diana\"},{\"name\":\"Rick\"}]" ) || die (str);
mapper.writeValue( dst, list );
src = dst;
List<MyBean> beans = mapper.readValue( src, List.class, MyBean.class );
puts( "mybeans", beans );
}
代码示例来源:origin: boonproject/boon
@Test
public void test() {
final ObjectMapper mapper = JsonFactory.createUseProperties(true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mapper.writeValue(stream, FrequentPrimitives.getArray(2));
puts(new String(stream.toByteArray()));
}
代码示例来源:origin: boonproject/boon
public static void part5WorkingWithLists() throws Exception {
puts ("\n\n\n", "\npart5 WorkingWithLists");
ObjectMapper mapper = JsonFactory.createUseJSONDates();
final User diana = BeanUtils.copy( user );
final User rick = BeanUtils.copy( user );
diana.getName().setFirst( "Diana" );
rick.getName().setFirst( "Rick" );
diana.setBirthDate( Dates.getUSDate( 8, 21, 1984 ) );
File file = File.createTempFile( "userList", ".json" );
List<User> users = Lists.list( diana, rick );
mapper.writeValue( file, users );
List<User> userList = mapper.readValue( file, List.class, User.class );
puts (userList);
puts ( mapper.writeValueAsString( userList ) );
boolean ok = users.toString().equals ( userList.toString () ) || die (userList.toString ());
}
代码示例来源:origin: boonproject/boon
@Test
public void test2() {
final ObjectMapper mapper = JsonFactory
.createUseProperties(true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//String uri, String title, int width, int height, Size size
mapper.writeValue(stream, new Image("/foo", "Foo", 5, 10, Image.Size.SMALL));
puts(new String(stream.toByteArray()));
}
代码示例来源:origin: boonproject/boon
public static void part6WorkingWithLists() throws Exception {
puts ("\n\n\n", "\npart6WorkingWithLists");
ObjectMapper mapper = JsonFactory.createUseJSONDates();
final User diana = BeanUtils.copy( user );
final User rick = BeanUtils.copy( user );
diana.getName().setFirst( "Diana" );
diana.setGender( User.Gender.FEMALE );
rick.getName().setFirst( "Rick" );
diana.setBirthDate( Dates.getUSDate( 8, 21, 1984 ) );
File file = File.createTempFile( "userList", ".json" );
List<User> users = Lists.list( diana, rick );
mapper.writeValue( file, users );
List<User> userList = mapper.readValue( file, List.class, User.class );
puts (userList);
puts ( mapper.writeValueAsString( userList ) );
boolean ok = userList.toString().equals ( users.toString() ) || die ( userList.toString() );
}
代码示例来源:origin: fabienrenaud/java-json-benchmark
@Benchmark
@Override
public Object boon() {
ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream();
JSON_SOURCE().provider().boon().writeValue(baos, JSON_SOURCE().nextPojo());
return baos;
}
代码示例来源:origin: boonproject/boon
public static void part7WorkingWithListFromFile() throws Exception {
puts ("\n\n\n", "\npart7WorkingWithListFromFile");
ObjectMapper mapper = JsonFactory.createUseAnnotations( true );
/* Create two users. */
final User diana = BeanUtils.copy( user );
final User rick = BeanUtils.copy( user );
diana.getName().setFirst( "Diana" );
diana.setGender ( User.Gender.FEMALE );
rick.getName().setFirst ( "Rick" );
rick.ssn="IAMSET";
diana.ssn="dianaSSN";
diana.setBirthDate( Dates.getUSDate( 8, 21, 1984 ) );
File file = File.createTempFile( "userList", ".json" );
List<User> users = Lists.list( diana, rick );
/* Inspect the JSON of the users from the file. */
puts ("users", mapper.writeValueAsString( users ) );
/* Write users out to file. */
mapper.writeValue( file, users );
/* Reader Users back from file. */
List<User> userList = mapper.readValue( file, List.class, User.class );
puts ("userListBeansReadFromFile", userList);
/* Inspect the JSON of the users from the file. */
puts ("usersFromFileAsJSON", mapper.writeValueAsString( userList ) );
boolean ok = userList.toString().equals ( users.toString() ) || die ( userList.toString() );
}
代码示例来源:origin: boonproject/boon
public static void main(String... args) throws Exception{
Player kevin = new Player("Kevin", "Cricket", 32, 221,
new int[]{33, 66, 78, 21, 9, 200});
final ObjectMapper mapper = JsonFactory.create();
final File file = File.createTempFile("json", "exmaple.json");
mapper.writeValue(file, kevin);
Player somePlayer = mapper.readValue(file, Player.class);
puts("They are equal", somePlayer.equals(kevin));
}
代码示例来源:origin: boonproject/boon
mapper.writeValue( file, users );
List<User> userList = mapper.readValue( file, List.class, User.class );
puts (userList);
代码示例来源:origin: boonproject/boon
@Test
public void test3() {
final ObjectMapper mapper = JsonFactory
.createUseProperties(true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
MediaContent mediaContent = MediaContent.mediaContent();
//String uri, String title, int width, int height, Size size
mapper.writeValue(stream, mediaContent);
MediaContent mediaContent2 = mapper.readValue(stream.toByteArray(), MediaContent.class);
boolean ok = mediaContent.equals(mediaContent2) || die();
}
内容来源于网络,如有侵权,请联系作者删除!