2 * =============LICENSE_START===================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * Copyright (c) 2022 Wipro Limited Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
21 package org.onap.dcae.analytics.model.util.json;
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.databind.DeserializationFeature;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.jayway.jsonpath.Configuration;
27 import com.jayway.jsonpath.Option;
28 import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
29 import com.jayway.jsonpath.spi.json.JsonProvider;
30 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
31 import com.jayway.jsonpath.spi.mapper.MappingProvider;
33 import java.util.EnumSet;
35 import java.util.function.Supplier;
37 import org.onap.dcae.analytics.model.util.json.module.CommonEventFormatModule;
38 import org.onap.dcae.analytics.model.util.json.module.CommonEventFormatModuleV7;
39 import org.onap.dcae.analytics.model.util.json.module.ConfigBindingServiceModule;
40 import org.onap.dcae.analytics.model.util.json.module.DynamicPropertiesModule;
43 * Base Object mapper supplies Jackson {@link ObjectMapper} that specializes in serialize and deserialize -
44 * Analytics model objects. Various analytics components should inherit from this supplier and register
45 * their custom modules
47 * @author Rajiv Singla
49 public abstract class BaseObjectMapperSupplier implements Supplier<ObjectMapper> {
52 * Class that can used to configure Json Path configuration
54 public static class JsonPathConfiguration implements Configuration.Defaults {
56 private final JsonProvider jsonProvider;
57 private final MappingProvider mappingProvider;
58 private final Set<Option> options;
60 private JsonPathConfiguration(final ObjectMapper objectMapper, final Set<Option> options) {
61 jsonProvider = new JacksonJsonProvider(objectMapper);
62 mappingProvider = new JacksonMappingProvider(objectMapper);
63 this.options = options;
68 public JsonProvider jsonProvider() {
73 public Set<Option> options() {
78 public MappingProvider mappingProvider() {
79 return mappingProvider;
83 public abstract void registerCustomModules(final ObjectMapper objectMapper);
86 public ObjectMapper get() {
88 final ObjectMapper objectMapper = new ObjectMapper();
90 // Ignore null values during serialization. Null values will not be included in serialized JSON object
91 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
92 // Don't fail on unknown properties
93 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
95 // register dynamic properties module
96 objectMapper.registerModule(new DynamicPropertiesModule());
97 // register config binding service module
98 objectMapper.registerModule(new ConfigBindingServiceModule());
99 // register common event format module
100 objectMapper.registerModule(new CommonEventFormatModule());
101 // register common event format module for v7
102 objectMapper.registerModule(new CommonEventFormatModuleV7());
104 // register custom modules
105 registerCustomModules(objectMapper);
107 // Setup JsonPath default config
108 setupJsonPathDefaultConfig(objectMapper);
114 * Setups up default Config for Json Path
116 * @param objectMapper Jackson object mapper
118 private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {
120 Configuration.setDefaults(new JsonPathConfiguration(objectMapper, EnumSet.of(
121 Option.DEFAULT_PATH_LEAF_TO_NULL, // missing properties are tolerated
122 Option.SUPPRESS_EXCEPTIONS, // Json Path exceptions are suppressed
123 Option.ALWAYS_RETURN_LIST // always return results as list