lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ovsdb.rfc.utils; |
| 17 | |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| 22 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 23 | import com.fasterxml.jackson.databind.DeserializationFeature; |
| 24 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | |
| 26 | /** |
| 27 | * ObjectMapper utility class. |
| 28 | */ |
| 29 | public 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 |
lishuai | 2f19743 | 2015-07-31 16:27:58 +0800 | [diff] [blame] | 36 | * public or default constructor, otherwise IDE will compile unsuccessfully. This |
lishuai | 91d986c | 2015-07-28 09:45:20 +0800 | [diff] [blame] | 37 | * 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 | } |