org.boon.json.ObjectMapper.writeValueAsString()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(150)

本文整理了Java中org.boon.json.ObjectMapper.writeValueAsString()方法的一些代码示例,展示了ObjectMapper.writeValueAsString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ObjectMapper.writeValueAsString()方法的具体详情如下:
包路径:org.boon.json.ObjectMapper
类名称:ObjectMapper
方法名:writeValueAsString

ObjectMapper.writeValueAsString介绍

[英]Method that can be used to serialize any Java value as a String. Functionally equivalent to calling #writeValue(Writer,Object) with java.io.StringWriterand constructing String, but more efficient.
[中]方法,该方法可用于将任何Java值序列化为字符串。在功能上相当于用java调用#writeValue(Writer,Object)。伊奥。StringWriter和构造字符串,但效率更高。

代码示例

代码示例来源:origin: spring-projects/spring-integration

@Override
public String toJson(Object value) throws Exception {
  return this.objectMapper.writeValueAsString(value);
}

代码示例来源:origin: org.springframework.integration/spring-integration-core

@Override
public String toJson(Object value) throws Exception {
  return this.objectMapper.writeValueAsString(value);
}

代码示例来源:origin: cowtowncoder/java-json-performance-benchmarks

@Override
  public String _writeAsString(MeasurementPOJO items) throws Exception {
    return mapper.writeValueAsString(items);
  }
}

代码示例来源:origin: boonproject/boon

@Test
public void test198() {
  Data original = new Data(Integer.MAX_VALUE, Long.MAX_VALUE);
  System.out.println("original: \n" + original + "\n");
  ObjectMapper boon = JsonFactory.create();
  String serialized = boon.writeValueAsString(original);
  System.out.println("serialized: \n" + serialized + "\n");
  Data deserialized = boon.readValue(serialized, Data.class);
  System.out.println("deserialized: \n" + deserialized + "\n");
  String reserialized = boon.writeValueAsString(deserialized);
  System.out.println("reserialized: \n" + reserialized + "\n");
}

代码示例来源:origin: boonproject/boon

public static void main(String[] args) throws IOException {
  Builder builder = new Builder();
  MetadataImpl metadataImpl = builder.getMetadata();
  JsonSerializerFactory jsonSerializerFactory=new JsonSerializerFactory().usePropertyOnly();
  ObjectMapper mapper = JsonFactory.create(null, jsonSerializerFactory);
  String json = mapper.writeValueAsString(metadataImpl);
  System.out.println("=============" + json);
  File file = new File("metadata.json");
  FileWriter writer = new FileWriter(file);
  mapper.toJson(metadataImpl, writer);
  writer.close();
  Path path = Paths.get(file.toString());
  InputStream inputStream = Files.newInputStream(path);
  MetadataImpl object = JsonFactory.create().readValue(inputStream,
      MetadataImpl.class);
  inputStream.close();
  System.out.println("after deserialization"
      + mapper.writeValueAsString(object));
}

代码示例来源:origin: boonproject/boon

public static void part3_1() throws Exception {
  ObjectMapper mapper = JsonFactory.create();
  puts( mapper.writeValueAsString( user ) );
  User user2 = mapper.readValue( mapper.writeValueAsString( user ), User.class );
  puts( user2 );
  boolean ok = user.equals ( user2 ) || die (user.toString ());
}

代码示例来源:origin: boonproject/boon

public static void part3_2() throws Exception {
  ObjectMapper mapper = JsonFactory.createUseJSONDates();
  puts( mapper.writeValueAsString( user ) );
  User user2 = mapper.readValue( mapper.writeValueAsString( user ), User.class );
  puts( user2 );
  boolean ok = user.equals ( user2 ) || die (user.toString ());
}

代码示例来源: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: boonproject/boon

@Test
  public void test() {

    JsonSerializerFactory factory = new JsonSerializerFactory();
    factory.usePropertyOnly();
    ObjectMapper mapper = JsonFactory.create(null, factory);

    User user = new User();
    EnumMap<Gender,User> map = new EnumMap<Gender, User>(Gender.class);
    map.put(Gender.FEMALE, user);

    puts(map);
    puts(mapper.writeValueAsString( map));
  }
}

代码示例来源:origin: boonproject/boon

puts( mapper.writeValueAsString( user ) );

代码示例来源: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

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

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 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: boonproject/boon

List<User> userList = mapper.readValue( file, List.class, User.class  );
puts (userList);
puts ( mapper.writeValueAsString( userList ) );

相关文章