blob: e80a52bc5ac75270b8c9cc2ed6002a03612c9c88 [file] [log] [blame]
lishuai91d986c2015-07-28 09:45:20 +08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ovsdb.rfc.utils;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import com.fasterxml.jackson.annotation.JsonInclude.Include;
22import com.fasterxml.jackson.core.JsonProcessingException;
23import com.fasterxml.jackson.databind.DeserializationFeature;
24import com.fasterxml.jackson.databind.ObjectMapper;
25
26/**
27 * ObjectMapper utility class.
28 */
29public final class ObjectMapperUtil {
30
31 private static final Logger log = LoggerFactory
32 .getLogger(ObjectMapperUtil.class);
33
34 /**
35 * Constructs a ObjectMapperUtil object. Utility classes should not have a
lishuai2f197432015-07-31 16:27:58 +080036 * public or default constructor, otherwise IDE will compile unsuccessfully. This
lishuai91d986c2015-07-28 09:45:20 +080037 * class should not be instantiated.
38 */
39 private ObjectMapperUtil() {
40 }
41
42 /**
43 * get ObjectMapper entity.
44 * @return ObjectMapper entity
45 */
46 public static ObjectMapper getObjectMapper() {
47 ObjectMapper objectMapper = new ObjectMapper();
48 objectMapper
49 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
50 false);
51 objectMapper.setSerializationInclusion(Include.NON_NULL);
52 return objectMapper;
53 }
54
55 /**
56 * get ObjectMapper entity.
57 * @param flag configure
58 * @return ObjectMapper entity
59 */
60 public static ObjectMapper getObjectMapper(boolean flag) {
61 ObjectMapper objectMapper = new ObjectMapper();
62 objectMapper
63 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
64 flag);
65 return objectMapper;
66 }
67
68 /**
69 * get ObjectMapper entity.
70 * @param flag configure
71 * @param incl setSerializationInclusion
72 * @return ObjectMapper entity
73 */
74 public static ObjectMapper getObjectMapper(boolean flag, Include incl) {
75 ObjectMapper objectMapper = new ObjectMapper();
76 objectMapper
77 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
78 flag);
79 objectMapper.setSerializationInclusion(incl);
80 return objectMapper;
81 }
82
83 /**
84 * convert Object into String.
85 * @param obj Object
86 * @return String
87 */
88 public static String convertToString(Object obj) {
89 ObjectMapper objectMapper = new ObjectMapper();
90 try {
91 return objectMapper.writeValueAsString(obj);
92 } catch (JsonProcessingException e) {
93 log.error("JsonProcessingException while converting Entity into string", e);
94 }
95 return null;
96 }
97
98}