Add sdnr wt devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / base / database / JsonMapperBase.java
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
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
19
20 import java.io.IOException;
21 import java.io.StringWriter;
22 import java.util.List;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
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;
35
36 /**
37  * This class is used to define default for JSON Serialization and Deserialization for the project at a single place
38  */
39 public class JsonMapperBase extends ObjectMapper {
40
41     private static final long serialVersionUID = 1L;
42     private static final Logger LOG = LoggerFactory.getLogger(JsonMapperBase.class);
43
44     public JsonMapperBase() {
45
46         setVisibility(PropertyAccessor.ALL, Visibility.NONE);
47         setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
48
49         // Deserialization
50         configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
51         configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
52
53         // Serialization
54         configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
55         getFactory().configure(Feature.ESCAPE_NON_ASCII, true);
56     }
57
58     public JsonMapperBase(int t) {
59
60         switch(t) {
61                 case 0:
62                         break;
63                 case 1:
64                         setVisibility(PropertyAccessor.ALL, Visibility.NONE);
65                         setVisibility(PropertyAccessor.FIELD, Visibility.DEFAULT);
66                         break;
67                 case 2:
68                         setVisibility(PropertyAccessor.ALL, Visibility.NONE);
69                         setVisibility(PropertyAccessor.FIELD, Visibility.PROTECTED_AND_PUBLIC);
70                         break;
71                 case 3:
72                         setVisibility(PropertyAccessor.ALL, Visibility.NONE);
73                         setVisibility(PropertyAccessor.GETTER, Visibility.ANY);
74                         setVisibility(PropertyAccessor.IS_GETTER, Visibility.ANY);
75                         break;
76                 default:
77                         setVisibility(PropertyAccessor.ALL, Visibility.NONE);
78                     setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
79                         break;
80
81         }
82
83         // Deserialization
84         configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
85         configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
86
87         // Serialization
88         configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
89         getFactory().configure(Feature.ESCAPE_NON_ASCII, true);
90     }
91
92
93
94     public String objectToJson( Object object ) {
95         String res = null;
96
97         try {
98
99                 res = writeValueAsString(object);
100
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());
109         }
110
111         return res;
112     }
113
114     public String objectListToJson( List<? extends Object> objectList ) {
115         String res = null;
116
117         try {
118
119             StringWriter stringEmp = new StringWriter();
120             writeValue(stringEmp, objectList);
121             res = stringEmp.toString();
122             stringEmp.close();
123
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());
132         }
133
134         return res;
135     }
136
137 }