JacksonJsonUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.json;

  21. import java.util.TimeZone;

  22. import org.openspcoop2.utils.jaxrs.JacksonJsonProviderCustomized;

  23. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  24. import com.fasterxml.jackson.databind.ObjectMapper;
  25. import com.fasterxml.jackson.databind.ObjectWriter;

  26. /**
  27.  * JSONUtils
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class JacksonJsonUtils extends AbstractUtils {
  34.    
  35.    
  36.     private static TimeZone timeZone = TimeZone.getDefault();
  37.     public static TimeZone getTimeZone() {
  38.         return timeZone;
  39.     }
  40.     public static void setTimeZone(TimeZone timeZoneParam) {
  41.         timeZone = timeZoneParam;
  42.     }
  43.     public static String getTimeZoneId() {
  44.         return timeZone.getID();
  45.     }
  46.     public static void setTimeZoneById(String timeZoneId) {
  47.         timeZone = TimeZone.getTimeZone(timeZoneId);
  48.     }
  49.    
  50.     private static JacksonJsonUtils jsonUtils = null;
  51.     private static JacksonJsonUtils jsonUtilsPretty = null;
  52.     private static synchronized void init(boolean prettyPrint){
  53.         if(prettyPrint) {
  54.             if(JacksonJsonUtils.jsonUtilsPretty==null){
  55.                 JacksonJsonUtils.jsonUtilsPretty = new JacksonJsonUtils(true);
  56.             }
  57.         }
  58.         else {
  59.             if(JacksonJsonUtils.jsonUtils==null){
  60.                 JacksonJsonUtils.jsonUtils = new JacksonJsonUtils(false);
  61.             }
  62.         }
  63.     }
  64.     public static JacksonJsonUtils getInstance(){
  65.         return getInstance(false);
  66.     }
  67.     public static JacksonJsonUtils getInstance(boolean prettyPrint){
  68.         if(prettyPrint) {
  69.             if(JacksonJsonUtils.jsonUtilsPretty==null){
  70.                 // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  71.                 synchronized (JacksonJsonUtils.class) {
  72.                     JacksonJsonUtils.init(true);
  73.                 }
  74.             }
  75.             return JacksonJsonUtils.jsonUtilsPretty;
  76.         }
  77.         else {
  78.             if(JacksonJsonUtils.jsonUtils==null){
  79.                 // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  80.                 synchronized (JacksonJsonUtils.class) {
  81.                     JacksonJsonUtils.init(false);
  82.                 }
  83.             }
  84.             return JacksonJsonUtils.jsonUtils;
  85.         }
  86.     }
  87.    

  88.     private static ObjectMapper mapper;
  89.     private static synchronized void initMapper()  {
  90.         if(mapper==null){
  91.             mapper = JacksonJsonProviderCustomized.getObjectMapper(false, timeZone);
  92.             mapper.setSerializationInclusion(Include.NON_NULL);
  93.         }
  94.     }
  95.     public static ObjectMapper getObjectMapper() {
  96.         if(mapper==null){
  97.             initMapper();
  98.         }
  99.         return mapper;
  100.     }
  101.    
  102.     private static ObjectWriter writer;
  103.     private static synchronized void initWriter()  {
  104.         if(mapper==null){
  105.             initMapper();
  106.         }
  107.         if(writer==null){
  108.             writer = mapper.writer();
  109.         }
  110.     }
  111.     public static ObjectWriter getObjectWriter() {
  112.         if(writer==null){
  113.             initWriter();
  114.         }
  115.         return writer;
  116.     }
  117.    
  118.     private static ObjectWriter writerPrettyPrint;
  119.     private static synchronized void initWriterPrettyPrint()  {
  120.         if(mapper==null){
  121.             initMapper();
  122.         }
  123.         if(writerPrettyPrint==null){
  124.             writerPrettyPrint = mapper.writer().withDefaultPrettyPrinter();
  125.         }
  126.     }
  127.     public static ObjectWriter getObjectWriterPrettyPrint() {
  128.         if(writerPrettyPrint==null){
  129.             initWriterPrettyPrint();
  130.         }
  131.         return writerPrettyPrint;
  132.     }
  133.    
  134.    
  135.    
  136.     private JacksonJsonUtils(boolean prettyPrint) {
  137.         super(prettyPrint);
  138.     }
  139.    
  140.     @Override
  141.     protected void _initMapper() {
  142.         initMapper();
  143.     }
  144.     @Override
  145.     protected void _initWriter(boolean prettyPrint) {
  146.         if(prettyPrint) {
  147.             initWriterPrettyPrint();
  148.         }
  149.         else {
  150.             initWriter();
  151.         }
  152.     }
  153.    
  154.     @Override
  155.     protected ObjectMapper _getObjectMapper() {
  156.         return getObjectMapper();
  157.     }
  158.     @Override
  159.     protected ObjectWriter _getObjectWriter(boolean prettyPrint) {
  160.         if(prettyPrint) {
  161.             return getObjectWriterPrettyPrint();
  162.         }
  163.         else {
  164.             return getObjectWriter();
  165.         }
  166.     }
  167.    
  168.    
  169.     // IS
  170.    
  171.     public boolean isJson(byte[]jsonBytes){
  172.         return this.isValid(jsonBytes);
  173.     }
  174.    
  175.     public boolean isJson(String jsonString){
  176.         return this.isValid(jsonString);
  177.     }
  178.    
  179. }