1 /*********************************************************************************
2 * Copyright © 2015, highstreet technologies GmbH
5 * http://www.highstreet-technologies.com/
7 * The reproduction, transmission or use of this document or its contents is not
8 * permitted without express written authority. Offenders will be liable for
9 * damages. All rights, including rights created by patent grant or registration
10 * of a utility model or design, are reserved. Technical modifications possible.
11 * Technical specifications and features are binding only insofar as they are
12 * specifically and expressly agreed upon in a written contract.
14 * @author: Martin Skorupski [martin@skorupski.de]
15 *********************************************************************************/
16 package org.opendaylight.mwtn.base.database;
18 import java.io.IOException;
19 import java.io.StringWriter;
20 import java.util.List;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
26 import com.fasterxml.jackson.annotation.PropertyAccessor;
27 import com.fasterxml.jackson.core.JsonGenerationException;
28 import com.fasterxml.jackson.core.JsonGenerator.Feature;
29 import com.fasterxml.jackson.databind.DeserializationFeature;
30 import com.fasterxml.jackson.databind.JsonMappingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.SerializationFeature;
35 * This class is used to define default for JSON Serialization and Deserialization for the project at a single place
37 public class JsonMapperBase extends ObjectMapper {
39 private static final long serialVersionUID = 1L;
40 private static final Logger LOG = LoggerFactory.getLogger(JsonMapperBase.class);
42 public JsonMapperBase() {
44 setVisibility(PropertyAccessor.ALL, Visibility.NONE);
45 setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
48 configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
49 configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
52 configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
53 getFactory().configure(Feature.ESCAPE_NON_ASCII, true);
57 public String objectToJson( Object object ) {
62 StringWriter stringEmp = new StringWriter();
63 writeValue(stringEmp, object);
64 res = stringEmp.toString();
67 } catch (JsonGenerationException e) {
68 LOG.debug(e.toString());
69 } catch (JsonMappingException e) {
70 LOG.debug(e.toString());
71 } catch (IOException e) {
72 LOG.debug(e.toString());
73 } catch (Exception e) {
74 LOG.debug(e.toString());
80 public String objectListToJson( List<? extends Object> objectList ) {
85 StringWriter stringEmp = new StringWriter();
86 writeValue(stringEmp, objectList);
87 res = stringEmp.toString();
90 } catch (JsonGenerationException e) {
91 LOG.debug(e.toString());
92 } catch (JsonMappingException e) {
93 LOG.debug(e.toString());
94 } catch (IOException e) {
95 LOG.debug(e.toString());
96 } catch (Exception e) {
97 LOG.debug(e.toString());