1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. 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 distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
20 import java.io.IOException;
21 import java.io.StringWriter;
22 import java.util.List;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
28 import com.fasterxml.jackson.annotation.PropertyAccessor;
29 import com.fasterxml.jackson.core.JsonGenerationException;
30 import com.fasterxml.jackson.core.JsonGenerator.Feature;
31 import com.fasterxml.jackson.databind.DeserializationFeature;
32 import com.fasterxml.jackson.databind.JsonMappingException;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.SerializationFeature;
37 * This class is used to define default for JSON Serialization and Deserialization for the project at a single place
39 public class JsonMapperBase extends ObjectMapper {
41 private static final long serialVersionUID = 1L;
42 private static final Logger LOG = LoggerFactory.getLogger(JsonMapperBase.class);
44 public JsonMapperBase() {
46 setVisibility(PropertyAccessor.ALL, Visibility.NONE);
47 setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
50 configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
51 configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
54 configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
55 getFactory().configure(Feature.ESCAPE_NON_ASCII, true);
58 public JsonMapperBase(int t) {
64 setVisibility(PropertyAccessor.ALL, Visibility.NONE);
65 setVisibility(PropertyAccessor.FIELD, Visibility.DEFAULT);
68 setVisibility(PropertyAccessor.ALL, Visibility.NONE);
69 setVisibility(PropertyAccessor.FIELD, Visibility.PROTECTED_AND_PUBLIC);
72 setVisibility(PropertyAccessor.ALL, Visibility.NONE);
73 setVisibility(PropertyAccessor.GETTER, Visibility.ANY);
74 setVisibility(PropertyAccessor.IS_GETTER, Visibility.ANY);
77 setVisibility(PropertyAccessor.ALL, Visibility.NONE);
78 setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
84 configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
85 configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
88 configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
89 getFactory().configure(Feature.ESCAPE_NON_ASCII, true);
94 public String objectToJson( Object object ) {
99 res = writeValueAsString(object);
101 } catch (JsonGenerationException e) {
102 LOG.debug(e.toString());
103 } catch (JsonMappingException e) {
104 LOG.debug(e.toString());
105 } catch (IOException e) {
106 LOG.debug(e.toString());
107 } catch (Exception e) {
108 LOG.debug(e.toString());
114 public String objectListToJson( List<? extends Object> objectList ) {
119 StringWriter stringEmp = new StringWriter();
120 writeValue(stringEmp, objectList);
121 res = stringEmp.toString();
124 } catch (JsonGenerationException e) {
125 LOG.debug(e.toString());
126 } catch (JsonMappingException e) {
127 LOG.debug(e.toString());
128 } catch (IOException e) {
129 LOG.debug(e.toString());
130 } catch (Exception e) {
131 LOG.debug(e.toString());