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

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

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

ObjectMapper.readValue介绍

[英]Method to deserialize JSON content into a non-container type typically a bean or wrapper type.

Note: this method should NOT be used if the result type is a container ( java.util.Collection or java.util.Map. The reason is that due to type erasure, key and value types can not be introspected when using this method.
[中]方法将JSON内容反序列化为非容器类型,通常为bean或包装器类型。
注意:如果结果类型是容器(java.util.Collection或java.util.Map),则不应使用此方法。原因是由于类型擦除,在使用此方法时无法内省键和值类型。

代码示例

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

@Override
public <T> T fromJson(Object json, Class<T> type) throws Exception {
  if (json instanceof String) {
    return this.objectMapper.readValue((String) json, type);
  }
  else if (json instanceof byte[]) {
    return this.objectMapper.readValue((byte[]) json, type);
  }
  else if (json instanceof char[]) {
    return this.objectMapper.readValue((char[]) json, type);
  }
  else if (json instanceof File) {
    return this.objectMapper.readValue((File) json, type);
  }
  else if (json instanceof InputStream) {
    return this.objectMapper.readValue((InputStream) json, type);
  }
  else if (json instanceof Reader) {
    return this.objectMapper.readValue((Reader) json, type);
  }
  else {
    throw new IllegalArgumentException("'json' argument must be an instance of: " + supportedJsonTypes
        + " , but gotten: " + json.getClass());
  }
}

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

return (T) this.objectMapper.readValue((String) json, (Class<Collection>) classType, contentClassType);
return (T) this.objectMapper.readValue((byte[]) json, (Class<Collection>) classType, contentClassType);
return (T) this.objectMapper.readValue((char[]) json, (Class<Collection>) classType, contentClassType);
return (T) this.objectMapper.readValue((File) json, (Class<Collection>) classType, contentClassType);
return (T) this.objectMapper.readValue((InputStream) json, (Class<Collection>) classType,
    contentClassType);
return (T) this.objectMapper.readValue((Reader) json, (Class<Collection>) classType, contentClassType);

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

@Override
public Map<?,?> _readMap(byte[] input) throws Exception {
  return mapper.readValue(input, Map.class);
}

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

@Override
  public MeasurementPOJO _readItems(String input) throws Exception {
    return mapper.readValue(input, MeasurementPOJO.class);
  }
}

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

@Override
public MeasurementPOJO _readItems(InputStream input) throws Exception {
  return mapper.readValue(input, MeasurementPOJO.class);
}

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

@Override
  public Map<?,?> _readMap(String input) throws Exception {
    return mapper.readValue(input, Map.class);
  }
}

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

@Override
public Map<?,?> _readMap(InputStream input) throws Exception {
  return mapper.readValue(input, Map.class);
}

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

@Override
public MeasurementPOJO _readItems(byte[] input) throws Exception {
  return mapper.readValue(input, MeasurementPOJO.class);
}

代码示例来源:origin: de.svenkubiak/mangooio-core

public Map<String, Object> asJson() {
    return JsonFactory.create().readValue(this.content, Map.class);
  }
}

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

@Test
public void serializingClassFieldCausesSegFault() {
  SomeClass someClassInstance = new SomeClass(Bug287.class);
  ObjectMapper mapper = JsonFactory.create();
  final String json = mapper.toJson(someClassInstance);
  puts(json);
  SomeClass someClassInstance2 = mapper.readValue("{\"clazz\":\"org.boon.bugs.Bug287\"} ", SomeClass.class);
  ok = someClassInstance2.clazz.getName().equals("org.boon.bugs.Bug287");
}

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

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 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

@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();
}

代码示例来源:origin: fabienrenaud/java-json-benchmark

@Benchmark
@Override
public Object boon() throws Exception {
  return JSON_SOURCE().provider().boon().readValue(JSON_SOURCE().nextByteArray(), JSON_SOURCE().pojoType());
}

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

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 ());
}

相关文章