com.fasterxml.jackson.databind.deser.DefaultDeserializationContext Java Examples
The following examples show how to use
com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Jackson2Tokenizer.java From spring-analysis-note with MIT License | 6 votes |
/** * Tokenize the given {@code Flux<DataBuffer>} into {@code Flux<TokenBuffer>}. * @param dataBuffers the source data buffers * @param jsonFactory the factory to use * @param objectMapper the current mapper instance * @param tokenizeArrayElements if {@code true} and the "top level" JSON object is * an array, each element is returned individually immediately after it is received * @return the resulting token buffers */ public static Flux<TokenBuffer> tokenize(Flux<DataBuffer> dataBuffers, JsonFactory jsonFactory, ObjectMapper objectMapper, boolean tokenizeArrayElements) { try { JsonParser parser = jsonFactory.createNonBlockingByteArrayParser(); DeserializationContext context = objectMapper.getDeserializationContext(); if (context instanceof DefaultDeserializationContext) { context = ((DefaultDeserializationContext) context).createInstance( objectMapper.getDeserializationConfig(), parser, objectMapper.getInjectableValues()); } Jackson2Tokenizer tokenizer = new Jackson2Tokenizer(parser, context, tokenizeArrayElements); return dataBuffers.concatMapIterable(tokenizer::tokenize).concatWith(tokenizer.endOfInput()); } catch (IOException ex) { return Flux.error(ex); } }
Example #2
Source File: ObjectMapperJDK7IT.java From pinpoint with Apache License 2.0 | 6 votes |
@Test public void testConstructor() throws Exception { ObjectMapper mapper1 = new ObjectMapper(); ObjectMapper mapper2 = new ObjectMapper(new JsonFactory()); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); Constructor<?> omConstructor = ObjectMapper.class.getConstructor(JsonFactory.class, DefaultSerializerProvider.class, DefaultDeserializationContext.class); Constructor<?> omConstructor1 = ObjectMapper.class.getConstructor(); Constructor<?> omConstructor2 = ObjectMapper.class.getConstructor(JsonFactory.class); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor)); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor1)); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor)); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor2)); verifier.verifyTraceCount(0); }
Example #3
Source File: ObjectMapperIT.java From pinpoint with Apache License 2.0 | 6 votes |
@Test public void testConstructor() throws Exception { ObjectMapper mapper1 = new ObjectMapper(); ObjectMapper mapper2 = new ObjectMapper(new JsonFactory()); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); Constructor<?> omConstructor = ObjectMapper.class.getConstructor(JsonFactory.class, DefaultSerializerProvider.class, DefaultDeserializationContext.class); Constructor<?> omConstructor1 = ObjectMapper.class.getConstructor(); Constructor<?> omConstructor2 = ObjectMapper.class.getConstructor(JsonFactory.class); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor)); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor1)); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor)); verifier.verifyTrace(event(SERVICE_TYPE, omConstructor2)); verifier.verifyTraceCount(0); }
Example #4
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testResponse() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "ModelTest.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); ResponseDeserializer responseDeserializer = new ResponseDeserializer(); try { responseDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example #5
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testDeserializeSecurityDef() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Security.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); SecurityDefinitionDeserializer securityDefinitionDeserializer = new SecurityDefinitionDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { securityDefinitionDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example #6
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testDeserializeModel() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "ModelTest.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); ModelDeserializer modelDeserializer = new ModelDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); Assert.assertNotNull(modelDeserializer.deserialize(jsonParser, deserializationContext)); }
Example #7
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Test public void testDeserializePetStoreFile() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(new File(path)); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); Assert.assertNotNull(propertyDeserializer.propertyFromNode(jsonNode)); propertyDeserializer.deserialize(jsonParser, deserializationContext); ObjectNode obj = Mockito.mock(ObjectNode.class); Mockito.when(obj.get("name")).thenReturn(jsonNode); Mockito.when(obj.get("namespace")).thenReturn(jsonNode); Mockito.when(obj.get("prefix")).thenReturn(jsonNode); Mockito.when(obj.get("attribute")).thenReturn(jsonNode); Mockito.when(obj.get("wrapped")).thenReturn(jsonNode); Mockito.when(obj.get("xml")).thenReturn(obj); Assert.assertNotNull(propertyDeserializer.getXml(obj)); }
Example #8
Source File: ObjectMapperProvider.java From icure-backend with GNU General Public License v2.0 | 6 votes |
@Override public ObjectMapper getContext(Class<?> type) { // TypeResolver SimpleAbstractTypeResolver abstractTypeResolver = new SimpleAbstractTypeResolver(); abstractTypeResolver.addMapping(List.class, ArrayList.class); abstractTypeResolver.addMapping(Set.class, HashSet.class); abstractTypeResolver.addMapping(Map.class, HashMap.class); // Serializer DefaultSerializerProvider serializerProvider = new DefaultSerializerProvider.Impl(); // Deserializer DeserializerFactoryConfig deserializerFactoryConfig = new DeserializerFactoryConfig().withAbstractTypeResolver(abstractTypeResolver); BeanDeserializerFactory deserializerFactory = new BeanDeserializerFactory(deserializerFactoryConfig); DefaultDeserializationContext.Impl deserializationContext = new DefaultDeserializationContext.Impl(deserializerFactory); // ObjectMapper ObjectMapper objectMapper = new ObjectMapper(null, serializerProvider, deserializationContext); objectMapper = objectMapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE, "type"); objectMapper = objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); objectMapper = objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); return objectMapper; }
Example #9
Source File: AWSUtil.java From flink with Apache License 2.0 | 5 votes |
/** * Set all prefixed properties on {@link ClientConfiguration}. * @param config * @param configProps */ public static void setAwsClientConfigProperties(ClientConfiguration config, Properties configProps) { Map<String, Object> awsConfigProperties = new HashMap<>(); for (Map.Entry<Object, Object> entry : configProps.entrySet()) { String key = (String) entry.getKey(); if (key.startsWith(AWS_CLIENT_CONFIG_PREFIX)) { awsConfigProperties.put(key.substring(AWS_CLIENT_CONFIG_PREFIX.length()), entry.getValue()); } } // Jackson does not like the following properties String[] ignorableProperties = {"secureRandom"}; BeanDeserializerModifier modifier = new BeanDeserializerModifierForIgnorables( ClientConfiguration.class, ignorableProperties); DeserializerFactory factory = BeanDeserializerFactory.instance.withDeserializerModifier( modifier); ObjectMapper mapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(factory)); JsonNode propTree = mapper.convertValue(awsConfigProperties, JsonNode.class); try { mapper.readerForUpdating(config).readValue(propTree); } catch (IOException ex) { throw new RuntimeException(ex); } }
Example #10
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 5 votes |
@Test public void testDeserializeArrays() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); String array = "\"indicators\": {\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"$ref\": \"#/definitions/Indicator\"\n" + " }\n" + " }"; JsonNode jsonNode = mapper.readTree(array); ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { propertyDeserializer.propertyFromNode(jsonNode); propertyDeserializer.deserialize(jsonParser, deserializationContext); propertyDeserializer.getXml(jsonNode); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example #11
Source File: AWSUtil.java From flink with Apache License 2.0 | 5 votes |
/** * Set all prefixed properties on {@link ClientConfiguration}. * @param config * @param configProps */ public static void setAwsClientConfigProperties(ClientConfiguration config, Properties configProps) { Map<String, Object> awsConfigProperties = new HashMap<>(); for (Map.Entry<Object, Object> entry : configProps.entrySet()) { String key = (String) entry.getKey(); if (key.startsWith(AWS_CLIENT_CONFIG_PREFIX)) { awsConfigProperties.put(key.substring(AWS_CLIENT_CONFIG_PREFIX.length()), entry.getValue()); } } // Jackson does not like the following properties String[] ignorableProperties = {"secureRandom"}; BeanDeserializerModifier modifier = new BeanDeserializerModifierForIgnorables( ClientConfiguration.class, ignorableProperties); DeserializerFactory factory = BeanDeserializerFactory.instance.withDeserializerModifier( modifier); ObjectMapper mapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(factory)); JsonNode propTree = mapper.convertValue(awsConfigProperties, JsonNode.class); try { mapper.readerForUpdating(config).readValue(propTree); } catch (IOException ex) { throw new RuntimeException(ex); } }
Example #12
Source File: AWSUtil.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * Set all prefixed properties on {@link ClientConfiguration}. * @param config * @param configProps */ public static void setAwsClientConfigProperties(ClientConfiguration config, Properties configProps) { Map<String, Object> awsConfigProperties = new HashMap<>(); for (Map.Entry<Object, Object> entry : configProps.entrySet()) { String key = (String) entry.getKey(); if (key.startsWith(AWS_CLIENT_CONFIG_PREFIX)) { awsConfigProperties.put(key.substring(AWS_CLIENT_CONFIG_PREFIX.length()), entry.getValue()); } } // Jackson does not like the following properties String[] ignorableProperties = {"secureRandom"}; BeanDeserializerModifier modifier = new BeanDeserializerModifierForIgnorables( ClientConfiguration.class, ignorableProperties); DeserializerFactory factory = BeanDeserializerFactory.instance.withDeserializerModifier( modifier); ObjectMapper mapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(factory)); JsonNode propTree = mapper.convertValue(awsConfigProperties, JsonNode.class); try { mapper.readerForUpdating(config).readValue(propTree); } catch (IOException ex) { throw new RuntimeException(ex); } }
Example #13
Source File: ObjectMapperFactoryV2.java From aws-athena-query-federation with Apache License 2.0 | 5 votes |
private StrictObjectMapper(BlockAllocator allocator) { super(JSON_FACTORY); _serializerFactory = SERIALIZER_FACTORY; ImmutableMap<Class<?>, JsonDeserializer<?>> desers = ImmutableMap.of( FederationRequest.class, createRequestDeserializer(allocator), FederationResponse.class, createResponseDeserializer(allocator), LambdaFunctionException.class, new LambdaFunctionExceptionSerDe.Deserializer()); SimpleDeserializers deserializers = new SimpleDeserializers(desers); DeserializerFactoryConfig dConfig = new DeserializerFactoryConfig().withAdditionalDeserializers(deserializers); _deserializationContext = new DefaultDeserializationContext.Impl(new StrictDeserializerFactory(dConfig)); // required by LambdaInvokerFactory disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); }
Example #14
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 4 votes |
@Test public void testDeserializeArray() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); String array = "{\n" + " \"properties\": {\n" + " \"type\": {\n" + " \"type\": \"string\",\n" + " \"default\": \"BBANDS\"\n" + " },\n" + " \"computeOn\": {\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"type\": \"string\"\n" + " },\n" + " \"default\": [\n" + " \"close\"\n" + " ]\n" + " },\n" + " \"parameters\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"timeperiod\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 5\n" + " },\n" + " \"nbdevup\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 2\n" + " },\n" + " \"nbdevdn\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 2\n" + " },\n" + " \"matype\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"default\": 0\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}"; JsonNode jsonNode = mapper.readTree(array); ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); Assert.assertNotNull(propertyDeserializer.propertyFromNode(jsonNode)); propertyDeserializer.deserialize(jsonParser, deserializationContext); propertyDeserializer.getXml(jsonNode); }
Example #15
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 4 votes |
@Test public void testDeserializePropertyFromNodeArray() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Pet.json"; JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); String arrayJson = "{\"parameters\": [\n" + "{\n" + " \"name\": \"petId\",\n" + " \"in\": \"path\",\n" + " \"description\": \"ID of pet to update\",\n" + " \"required\": true,\n" + " \"type\": \"integer\",\n" + " \"format\": \"int64\"\n" + "},\n" + "{\n" + "\"name\": \"additionalMetadata\",\n" + "\"in\": \"formData\",\n" + "\"description\": \"Additional data to pass to server\",\n" + "\"required\": false,\n" + "\"type\": \"string\"\n" + "},\n" + "{\n" + "\"name\": \"file\",\n" + "\"in\": \"formData\",\n" + "\"description\": \"file to upload\",\n" + "\"required\": false,\n" + "\"type\": \"file\"\n" + "}\n" + "]}"; JsonNode root = mapper.readTree(arrayJson.getBytes()); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); PropertyDeserializer propertyDeserializer = new PropertyDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { propertyDeserializer.propertyFromNode(jsonNode); propertyDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } }
Example #16
Source File: JsonSerializationTest.java From carbon-apimgt with Apache License 2.0 | 4 votes |
@Test public void testDeserializeParameter() throws Exception { String path = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "Parameter.json"; String headerNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"header\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String queryNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"query\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String bodyNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"body\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String cookieNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"cookie\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String pathNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"path\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; String formDataNode = "{\n" + " \"name\": \"api_key\",\n" + " \"in\": \"formData\",\n" + " \"description\": \"\",\n" + " \"required\": false,\n" + " \"type\": \"string\"\n" + " }"; Map<String, String> stringMap = new HashMap<String, String>(); stringMap.put("Header", headerNode); stringMap.put("Body", bodyNode); stringMap.put("Path", pathNode); stringMap.put("FormData", formDataNode); stringMap.put("Cookie", cookieNode); stringMap.put("Query", queryNode); for (String nodeString : stringMap.values()) { JsonParser jsonParser = new JsonFactory().createParser(new File(path)); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(nodeString.getBytes()); JsonNode jsonNode = root; ObjectCodec objectCodec = Mockito.mock(ObjectCodec.class); Mockito.when(objectCodec.readTree(jsonParser)).thenReturn(jsonNode); jsonParser.setCodec(objectCodec); ParameterDeserializer parameterDeserializer = new ParameterDeserializer(); DefaultDeserializationContext deserializationContext = Mockito.mock(DefaultDeserializationContext.class); try { parameterDeserializer.deserialize(jsonParser, deserializationContext); } catch (JsonProcessingException e) { Assert.assertFalse(true); } } }
Example #17
Source File: ObjectReader.java From lams with GNU General Public License v2.0 | 2 votes |
/** * Internal helper method called to create an instance of {@link DeserializationContext} * for deserializing a single root value. * Can be overridden if a custom context is needed. */ protected DefaultDeserializationContext createDeserializationContext(JsonParser p) { return _context.createInstance(_config, p, _injectableValues); }