org.boon.json.ObjectMapper类的使用及代码示例

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

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

ObjectMapper介绍

[英]This mapper (or, data binder, or codec) provides functionality for converting between Java objects (instances of JDK provided core classes, beans), and matching JSON constructs. It will use instances of JsonParserAndMapper and org.boon.json.JsonSerializerfor implementing actual reading/writing of JSON.
[中]这个映射器(或、数据绑定器或编解码器)提供了在Java对象(JDK提供的核心类、bean的实例)和匹配的JSON构造之间进行转换的功能。它将使用JsonParserAndMapper和org的实例。恩惠json。JSONSerializer用于实现JSON的实际读取/写入。

代码示例

代码示例来源:origin: json-path/JsonPath

  1. public Result runBoon() {
  2. String result = null;
  3. String error = null;
  4. long time;
  5. Iterator<Object> query = null;
  6. long now = System.currentTimeMillis();
  7. try {
  8. if (!optionAsValues) {
  9. throw new UnsupportedOperationException("Not supported!");
  10. }
  11. io.gatling.jsonpath.JsonPath jsonPath = JsonPath$.MODULE$.compile(path).right().get();
  12. JsonParser jsonParser = new JsonParserCharArray();
  13. Object jsonModel = jsonParser.parse(json);
  14. query = jsonPath.query(jsonModel);
  15. } catch (Exception e) {
  16. error = getError(e);
  17. } finally {
  18. time = System.currentTimeMillis() - now;
  19. if (query != null) {
  20. List<Object> res = new ArrayList<Object>();
  21. while (query.hasNext()) {
  22. res.add(query.next());
  23. }
  24. ObjectMapper mapper = new ObjectMapperImpl();
  25. result = mapper.toJson(res);
  26. }
  27. return new Result("boon", time, result, error);
  28. }
  29. }

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

  1. @Override
  2. public <T> T fromJson(Object json, Class<T> type) throws Exception {
  3. if (json instanceof String) {
  4. return this.objectMapper.readValue((String) json, type);
  5. }
  6. else if (json instanceof byte[]) {
  7. return this.objectMapper.readValue((byte[]) json, type);
  8. }
  9. else if (json instanceof char[]) {
  10. return this.objectMapper.readValue((char[]) json, type);
  11. }
  12. else if (json instanceof File) {
  13. return this.objectMapper.readValue((File) json, type);
  14. }
  15. else if (json instanceof InputStream) {
  16. return this.objectMapper.readValue((InputStream) json, type);
  17. }
  18. else if (json instanceof Reader) {
  19. return this.objectMapper.readValue((Reader) json, type);
  20. }
  21. else {
  22. throw new IllegalArgumentException("'json' argument must be an instance of: " + supportedJsonTypes
  23. + " , but gotten: " + json.getClass());
  24. }
  25. }

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

  1. public static Object fromJson(String str) {
  2. return json.fromJson(str);
  3. }

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

  1. @Override
  2. @SuppressWarnings({ "unchecked", "rawtypes" })
  3. public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception {
  4. JsonParserAndMapper parser = this.objectMapper.parser();
  5. return (T) this.objectMapper.readValue((String) json, (Class<Collection>) classType, contentClassType);
  6. return (T) this.objectMapper.readValue((byte[]) json, (Class<Collection>) classType, contentClassType);
  7. return (T) this.objectMapper.readValue((char[]) json, (Class<Collection>) classType, contentClassType);
  8. return (T) this.objectMapper.readValue((File) json, (Class<Collection>) classType, contentClassType);
  9. return (T) this.objectMapper.readValue((InputStream) json, (Class<Collection>) classType,
  10. contentClassType);
  11. return (T) this.objectMapper.readValue((Reader) json, (Class<Collection>) classType, contentClassType);

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

  1. @Test
  2. public void test_caseInsensitiveProperty_lowercase() {
  3. String json = "{\"typename\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]} ";
  4. ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class);
  5. puts(json);
  6. puts(objectMapper.toJson(map));
  7. assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}")));
  8. }

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

  1. puts( mapper.writeValueAsString( user ) );
  2. mapper.writeValue( file, user );
  3. User userFromFile = mapper.readValue( file, User.class );
  4. InputStream inputStream = Files.newInputStream( path );
  5. User userFromInput = mapper.readValue( inputStream, User.class );
  6. puts( "userFromInput", userFromInput );
  7. User userFromReader = mapper.readValue( reader, User.class );

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

  1. private static void part12Gson() {
  2. ObjectMapper gson = JsonFactory.createUseAnnotations( true );
  3. puts ( gson.toJson ( 1 ) );
  4. puts ( gson.toJson ( "abcd" ) );
  5. puts ( gson.toJson ( new Long ( 10 ) ) );
  6. int[] values = { 1 };
  7. puts ( gson.toJson ( values ) );
  8. int ione = gson.fromJson("1", int.class);
  9. Integer oneI = gson.fromJson("1", Integer.class);
  10. Boolean wrapper = gson.fromJson("false", Boolean.class);
  11. String str = gson.fromJson("\"abc\"", String.class);
  12. String anotherStr = (String)gson.fromJson("[\"abc\"]", List.class).get ( 0 );
  13. BagOfPrimitives obj = new BagOfPrimitives();
  14. String json = gson.toJson(obj);
  15. puts (json);
  16. int[] ints = {1, 2, 3, 4, 5};
  17. String[] strings = {"abc", "def", "ghi"};
  18. puts ( gson.toJson ( ints ) ); // ==> prints [1,2,3,4,5]
  19. puts ( gson.toJson ( strings ) ); // ==> prints ["abc", "def", "ghi"]
  20. Collection<Integer> ints2 = Lists.list(1,2,3,4,5);
  21. puts ( gson.toJson(ints) ) ;// ==> json is [1,2,3,4,5]
  22. puts ( gson.parser ().parseList ( Integer.class, "[1,2,3,4,5]" ));
  23. //Serializing and Deserializing Generic Types TODO missing from GSON manual
  24. //Left off here https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Generic-Types
  25. }

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

  1. public static void main(String[] args) throws IOException {
  2. Builder builder = new Builder();
  3. MetadataImpl metadataImpl = builder.getMetadata();
  4. JsonSerializerFactory jsonSerializerFactory=new JsonSerializerFactory().usePropertyOnly();
  5. ObjectMapper mapper = JsonFactory.create(null, jsonSerializerFactory);
  6. String json = mapper.writeValueAsString(metadataImpl);
  7. System.out.println("=============" + json);
  8. File file = new File("metadata.json");
  9. FileWriter writer = new FileWriter(file);
  10. mapper.toJson(metadataImpl, writer);
  11. writer.close();
  12. Path path = Paths.get(file.toString());
  13. InputStream inputStream = Files.newInputStream(path);
  14. MetadataImpl object = JsonFactory.create().readValue(inputStream,
  15. MetadataImpl.class);
  16. inputStream.close();
  17. System.out.println("after deserialization"
  18. + mapper.writeValueAsString(object));
  19. }

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

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

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

  1. @Test
  2. public void test198() {
  3. Data original = new Data(Integer.MAX_VALUE, Long.MAX_VALUE);
  4. System.out.println("original: \n" + original + "\n");
  5. ObjectMapper boon = JsonFactory.create();
  6. String serialized = boon.writeValueAsString(original);
  7. System.out.println("serialized: \n" + serialized + "\n");
  8. Data deserialized = boon.readValue(serialized, Data.class);
  9. System.out.println("deserialized: \n" + deserialized + "\n");
  10. String reserialized = boon.writeValueAsString(deserialized);
  11. System.out.println("reserialized: \n" + reserialized + "\n");
  12. }

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

  1. public static void main(String... args) throws Exception{
  2. Player kevin = new Player("Kevin", "Cricket", 32, 221,
  3. new int[]{33, 66, 78, 21, 9, 200});
  4. final ObjectMapper mapper = JsonFactory.create();
  5. final File file = File.createTempFile("json", "exmaple.json");
  6. mapper.writeValue(file, kevin);
  7. Player somePlayer = mapper.readValue(file, Player.class);
  8. puts("They are equal", somePlayer.equals(kevin));
  9. }

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

  1. public static <T> List<T> fromJsonArray(String str, Class<T> clazz) {
  2. return json.parser().parseList(clazz, str);
  3. }

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

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

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

  1. @Override
  2. public int _writeItems(MeasurementPOJO items, Writer out) throws Exception
  3. {
  4. mapper.writeValue(out, items);
  5. return items.size();
  6. }

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

  1. @Test
  2. public void test_arrayProperty_fromSingleValue() {
  3. String json = "{\"typeName\":\"Processes\",\"fields\":{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}} ";
  4. ApiDynamicType map = objectMapper.fromJson(json, ApiDynamicType.class);
  5. puts(json);
  6. puts(objectMapper.toJson(map));
  7. assertThat(objectMapper.fromJson(objectMapper.toJson(map)), is(objectMapper.fromJson("{\"typeName\":\"Processes\",\"fields\":[{\"name\":\"process\",\"type\":\"ConversionRateProcess[]\",\"properties\":[\"REQUIRED\"]}]}")));
  8. }

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

  1. public static void part1ReadAndWriteMyBeanToAFile() throws Exception {
  2. MyBean myBean = new MyBean();
  3. File dst = File.createTempFile( "emp", ".json" );
  4. ObjectMapper mapper = JsonFactory.create();
  5. puts( "json string", mapper.writeValueAsString( myBean ) );
  6. String str = mapper.writeValueAsString ( myBean );
  7. boolean ok = str.contains ( "{\"name\":\"Rick\"" ) || die( str );
  8. mapper.writeValue( dst, myBean ); // where 'dst' can be File, OutputStream or Writer
  9. File src = dst;
  10. MyBean value = mapper.readValue( src, MyBean.class ); // 'src' can be File, InputStream, Reader, String
  11. ok |= value.name.contains ( "Rick" );
  12. puts( "mybean", value );
  13. Object root = mapper.readValue( src, Object.class );
  14. Map<String, Object> rootAsMap = mapper.readValue( src, Map.class );
  15. puts( "root", root );
  16. puts( "rootAsMap", rootAsMap );
  17. MyBean myBean1 = new MyBean();
  18. myBean1.name = "Diana";
  19. MyBean myBean2 = new MyBean();
  20. myBean2.name = "Rick";
  21. dst = File.createTempFile( "empList", ".json" );
  22. final List<MyBean> list = Lists.list( myBean1, myBean2 );
  23. str = mapper.writeValueAsString ( list );
  24. puts ( "json string", mapper.writeValueAsString ( list ) );
  25. ok |= str.contains ( "[{\"name\":\"Diana\"},{\"name\":\"Rick\"}]" ) || die (str);
  26. mapper.writeValue( dst, list );
  27. src = dst;
  28. List<MyBean> beans = mapper.readValue( src, List.class, MyBean.class );
  29. puts( "mybeans", beans );
  30. }

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

  1. @Test
  2. public void testWithMapUsingFactory() {
  3. JsonParserFactory jsonParserFactory = new JsonParserFactory()
  4. .useFieldsFirst()
  5. .lax() //allow loose parsing of JSON like JSON Smart
  6. .setCharset( StandardCharsets.UTF_8 ) //Set the standard charset, defaults to UTF_8
  7. .setLazyChop( true ) //similar to chop but only does it after map.get
  8. ;
  9. JsonSerializerFactory jsonSerializerFactory = new JsonSerializerFactory()
  10. .useFieldsFirst() //one of these
  11. //.addPropertySerializer( ) customize property output
  12. //.addTypeSerializer( ) customize type output
  13. .useJsonFormatForDates() //use json dates
  14. //.addFilter( ) add a property filter to exclude properties
  15. .includeEmpty().includeNulls().includeDefaultValues() //override defaults
  16. .handleComplexBackReference() //uses identity map to track complex back reference and avoid them
  17. .setHandleSimpleBackReference( true ) //looks for simple back reference for parent
  18. .setCacheInstances( true ) //turns on caching for immutable objects
  19. ;
  20. final ObjectMapper objectMapper = JsonFactory.create(jsonParserFactory, jsonSerializerFactory);
  21. MyClass myClass = new MyClass();
  22. final String json = objectMapper.toJson(myClass);
  23. puts(json);
  24. final MyClass myClass1 = objectMapper.readValue(json, MyClass.class);
  25. assertEquals("foo", myClass1.string);
  26. assertEquals(1, myClass1.integer);
  27. assertNull(myClass1.map);
  28. }

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

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

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

  1. @Override
  2. @SuppressWarnings({ "unchecked", "rawtypes" })
  3. public <T> T fromJson(Object json, Map<String, Object> javaTypes) throws Exception {
  4. JsonParserAndMapper parser = this.objectMapper.parser();
  5. return (T) this.objectMapper.readValue((String) json, (Class<Collection>) classType, contentClassType);
  6. return (T) this.objectMapper.readValue((byte[]) json, (Class<Collection>) classType, contentClassType);
  7. return (T) this.objectMapper.readValue((char[]) json, (Class<Collection>) classType, contentClassType);
  8. return (T) this.objectMapper.readValue((File) json, (Class<Collection>) classType, contentClassType);
  9. return (T) this.objectMapper.readValue((InputStream) json, (Class<Collection>) classType,
  10. contentClassType);
  11. return (T) this.objectMapper.readValue((Reader) json, (Class<Collection>) classType, contentClassType);

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

  1. @Test
  2. public void test3() {
  3. final ObjectMapper mapper = JsonFactory
  4. .createUseProperties(true);
  5. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  6. MediaContent mediaContent = MediaContent.mediaContent();
  7. //String uri, String title, int width, int height, Size size
  8. mapper.writeValue(stream, mediaContent);
  9. MediaContent mediaContent2 = mapper.readValue(stream.toByteArray(), MediaContent.class);
  10. boolean ok = mediaContent.equals(mediaContent2) || die();
  11. }

相关文章