/** * Type-safe overloaded method, basically alias for {@link #readValue(JsonParser, ResolvedType)}. *<p> * NOTE: this method never tries to auto-detect format, since actual * (data-format specific) parser is given. */ @SuppressWarnings("unchecked") public <T> T readValue(JsonParser p, JavaType valueType) throws IOException { return (T) forType(valueType).readValue(p); }
/** * 当JSON里只含有Bean的部分属性時,更新一個已存在Bean,只覆盖該部分的属性. */ public void update(String jsonString, Object object) { try { mapper.readerForUpdating(object).readValue(jsonString); } catch (JsonProcessingException e) { logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e); } catch (IOException e) { logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e); } }
/** * 当JSON里只含有Bean的部分属性時,更新一個已存在Bean,只覆盖該部分的属性. */ public void update(String jsonString, Object object) { try { mapper.readerForUpdating(object).readValue(jsonString); } catch (JsonProcessingException e) { logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e); } catch (IOException e) { logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e); } }
/** * Convenience method that binds content read using given parser, using * configuration of this reader, except that expected value type * is specified with the call (instead of currently configured root type). * Value return is either newly constructed, or root value that * was specified with {@link #withValueToUpdate(Object)}. *<p> * NOTE: this method never tries to auto-detect format, since actual * (data-format specific) parser is given. */ @Override @SuppressWarnings("unchecked") public <T> T readValue(JsonParser p, ResolvedType valueType) throws IOException { return (T) forType((JavaType)valueType).readValue(p); }
@Override public T convert(ResponseBody value) throws IOException { try { return adapter.readValue(value.charStream()); } finally { value.close(); } } }
public <T> T readValue(Class<T> type, String text) throws IOException { ObjectReader reader = DeserializerStringCache.init( Optional.ofNullable(objectReaderByClass.get(type)).map(Supplier::get).orElseGet(()->mapper.readerFor(type)) ); try { return reader.readValue(text); } finally { DeserializerStringCache.clear(reader, CacheScope.GLOBAL_SCOPE); } }
public <T> T readValue(Class<T> type, InputStream entityStream) throws IOException { ObjectReader reader = DeserializerStringCache.init( Optional.ofNullable(objectReaderByClass.get(type)).map(Supplier::get).orElseGet(()->mapper.readerFor(type)) ); try { return reader.readValue(entityStream); } finally { DeserializerStringCache.clear(reader, CacheScope.GLOBAL_SCOPE); } }
@Override public <T> T readValue(InputStream src, Class<T> valueType) throws IOException, JsonParseException, JsonMappingException { return readerFor(type).with(schema).readValue(src); }
@Override public <T> T treeToValue(TreeNode n, Class<T> valueType) throws JsonProcessingException { try { return readValue(treeAsTokens(n), valueType); } catch (JsonProcessingException e) { throw e; } catch (IOException e) { // should not occur, no real i/o... throw JsonMappingException.fromUnexpectedIOE(e); } }
@Override public <T> T readValue(InputStream src, Class<T> valueType) throws IOException, JsonParseException, JsonMappingException { return readerFor(type).with(schema).readValue(src); }
@Test public void testSerialization() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); BingTile tile = fromCoordinates(1, 2, 3); String json = objectMapper.writeValueAsString(tile); assertEquals("{\"x\":1,\"y\":2,\"zoom\":3}", json); assertEquals(tile, objectMapper.readerFor(BingTile.class).readValue(json)); }
@Test public void testSerDesBoundFilter() throws IOException { Injector defaultInjector = GuiceInjectors.makeStartupInjector(); ObjectMapper mapper = defaultInjector.getInstance(Key.get(ObjectMapper.class, Json.class)); String serBetweenDimFilter = mapper.writeValueAsString(boundDimFilter); BoundDimFilter actualBoundDimFilter = mapper.readerFor(DimFilter.class).readValue(serBetweenDimFilter); Assert.assertEquals(boundDimFilter, actualBoundDimFilter); }
public Consumer<DataBuffer> pojoConsumer(Pojo expected) { return dataBuffer -> { try { Pojo actual = this.mapper.reader().forType(Pojo.class) .readValue(DataBufferTestUtils.dumpBytes(dataBuffer)); assertEquals(expected, actual); release(dataBuffer); } catch (IOException ex) { throw new UncheckedIOException(ex); } }; }
@Test public void testSerDes() throws IOException { ObjectMapper mapper = new DefaultObjectMapper(); Assert.assertEquals(lookupExtractor, mapper.readerFor(LookupExtractor.class).readValue(mapper.writeValueAsBytes(lookupExtractor))); }
@Test public void testDeserialization() throws IOException { final InDimFilter actualInDimFilter = mapper.readerFor(DimFilter.class).readValue(actualInFilter); final InDimFilter expectedInDimFilter = new InDimFilter("dimTest", Arrays.asList("good", "bad"), null); Assert.assertEquals(expectedInDimFilter, actualInDimFilter); }
private <T> Consumer<DataBuffer> expect(T expected, Class<T> expectedType) { return dataBuffer -> { try { Object actual = this.mapper.reader().forType(expectedType) .readValue(dataBuffer.asInputStream()); assertEquals(expected, actual); } catch (IOException e) { throw new UncheckedIOException(e); } finally { release(dataBuffer); } }; }
@Test public void TestSerDesr() throws IOException { LookupConfig lookupConfig = new LookupConfig(temporaryFolder.newFile().getAbsolutePath()); Assert.assertEquals( lookupConfig, mapper.readerFor(LookupConfig.class).readValue(mapper.writeValueAsString(lookupConfig)) ); }
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) throws IOException { try { if (inputMessage instanceof MappingJacksonInputMessage) { Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView(); if (deserializationView != null) { return this.objectMapper.readerWithView(deserializationView).forType(javaType). readValue(inputMessage.getBody()); } } return this.objectMapper.readValue(inputMessage.getBody(), javaType); } catch (InvalidDefinitionException ex) { throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex); } catch (JsonProcessingException ex) { throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex, inputMessage); } }
@Test public void testSerDeserMapLookupExtractorFactory() throws IOException { ObjectMapper mapper = new DefaultObjectMapper(); mapper.registerSubtypes(MapLookupExtractorFactory.class); LookupExtractorFactory lookupExtractorFactory = new MapLookupExtractorFactory(ImmutableMap.of("key", "value"), true); Assert.assertEquals(lookupExtractorFactory, mapper.readerFor(LookupExtractorFactory.class).readValue(mapper.writeValueAsString(lookupExtractorFactory))); } }
@Parameters @Test public void testSerDesr(DimensionSpec lookupDimSpec) throws IOException { ObjectMapper mapper = new DefaultObjectMapper(); mapper.registerSubtypes(new NamedType(LookupDimensionSpec.class, "lookup")); InjectableValues injectableValues = new InjectableValues.Std().addValue( LookupReferencesManager.class, LOOKUP_REF_MANAGER ); String serLookup = mapper.writeValueAsString(lookupDimSpec); Assert.assertEquals(lookupDimSpec, mapper.readerFor(DimensionSpec.class).with(injectableValues).readValue(serLookup)); }