JSONUtils.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.io.Serializable;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.TimeZone;

  26. import org.openspcoop2.utils.SemaphoreLock;
  27. import org.slf4j.Logger;

  28. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  29. import com.fasterxml.jackson.databind.ObjectMapper;
  30. import com.fasterxml.jackson.databind.ObjectWriter;
  31. import com.fasterxml.jackson.databind.SerializationFeature;
  32. import com.fasterxml.jackson.datatype.joda.JodaModule;
  33. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

  34. /**
  35.  * JSONUtils
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class JSONUtils extends AbstractUtils {
  42.    
  43.     private static JSONUtils jsonUtils = null;
  44.     private static JSONUtils jsonUtilsPretty = null;
  45.     private static synchronized void init(boolean prettyPrint){
  46.         if(prettyPrint) {
  47.             if(JSONUtils.jsonUtilsPretty==null){
  48.                 JSONUtils.jsonUtilsPretty = new JSONUtils(true);
  49.             }
  50.         }
  51.         else {
  52.             if(JSONUtils.jsonUtils==null){
  53.                 JSONUtils.jsonUtils = new JSONUtils(false);
  54.             }
  55.         }
  56.     }
  57.     public static JSONUtils getInstance(){
  58.         return getInstance(false);
  59.     }
  60.     public static JSONUtils getInstance(boolean prettyPrint){
  61.         if(prettyPrint) {
  62.             if(JSONUtils.jsonUtilsPretty==null){
  63.                 // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  64.                 synchronized (JSONUtils.class) {
  65.                     JSONUtils.init(true);
  66.                 }
  67.             }
  68.             return JSONUtils.jsonUtilsPretty;
  69.         }
  70.         else {
  71.             if(JSONUtils.jsonUtils==null){
  72.                 // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  73.                 synchronized (JSONUtils.class) {
  74.                     JSONUtils.init(false);
  75.                 }
  76.             }
  77.             return JSONUtils.jsonUtils;
  78.         }
  79.     }
  80.    

  81.     private static org.openspcoop2.utils.Semaphore semaphore = new org.openspcoop2.utils.Semaphore("JSONUtils");
  82.     private static ObjectMapper internalMapper;
  83.     private static synchronized void initSyncMapper()  {
  84.         if(internalMapper==null){
  85.             internalMapper = new ObjectMapper();
  86.             internalMapper.setTimeZone(TimeZone.getDefault());
  87.             internalMapper.setSerializationInclusion(Include.NON_NULL);
  88.             internalMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
  89.             internalMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
  90.                     WRITE_DATES_AS_TIMESTAMPS , false);
  91.             // Since 2.1.4, the field exampleSetFlag appears in json produced with ObjectMapper#writeValue(File, Object) when serializing an object of type OpenApi.
  92.             // "exampleSetFlag" : false
  93.             // Con il codice sottostante, nelle classi Mixin l'attributo 'getExampleSetFlag' viene ignorato
  94.             internalMapper.addMixIn(io.swagger.v3.oas.models.media.Schema.class, io.swagger.v3.core.jackson.mixin.SchemaMixin.class);
  95.             internalMapper.addMixIn(io.swagger.v3.oas.models.media.MediaType.class, io.swagger.v3.core.jackson.mixin.MediaTypeMixin.class);
  96.         }
  97.     }
  98.     private static void initMapper()  {
  99.         SemaphoreLock lock = semaphore.acquireThrowRuntime("initMapper");
  100.         try {
  101.             if(internalMapper==null){
  102.                 initSyncMapper();
  103.             }
  104.         }finally {
  105.             semaphore.release(lock, "initMapper");
  106.         }
  107.     }
  108.     public static void setMapperTimeZone(TimeZone timeZone) {
  109.         if(internalMapper==null){
  110.             initMapper();
  111.         }
  112.         SemaphoreLock lock = semaphore.acquireThrowRuntime("setMapperTimeZone");
  113.         try {
  114.             internalMapper.setTimeZone(timeZone);
  115.         }finally {
  116.             semaphore.release(lock, "setMapperTimeZone");
  117.         }
  118.     }
  119.     public static void registerJodaModule() {
  120.         if(internalMapper==null){
  121.             initMapper();
  122.         }
  123.         SemaphoreLock lock = semaphore.acquireThrowRuntime("registerJodaModule");
  124.         try {
  125.             internalMapper.registerModule(new JodaModule());
  126.         }finally {
  127.             semaphore.release(lock, "registerJodaModule");
  128.         }
  129.     }
  130.     public static void registerJavaTimeModule() {
  131.         if(internalMapper==null){
  132.             initMapper();
  133.         }
  134.         SemaphoreLock lock = semaphore.acquireThrowRuntime("registerJavaTimeModule");
  135.         try {
  136.             internalMapper.registerModule(new JavaTimeModule());
  137.         }finally {
  138.             semaphore.release(lock, "registerJavaTimeModule");
  139.         }
  140.     }
  141.    
  142.     public static ObjectMapper getObjectMapper() {
  143.         if(internalMapper==null){
  144.             initMapper();
  145.         }
  146.         return internalMapper;
  147.     }
  148.    
  149.     private static ObjectWriter writer;
  150.     private static synchronized void initWriter()  {
  151.         if(internalMapper==null){
  152.             initMapper();
  153.         }
  154.         if(writer==null){
  155.             writer = internalMapper.writer();
  156.         }
  157.     }
  158.     public static ObjectWriter getObjectWriter() {
  159.         if(writer==null){
  160.             initWriter();
  161.         }
  162.         return writer;
  163.     }
  164.    
  165.     private static ObjectWriter writerPrettyPrint;
  166.     private static synchronized void initWriterPrettyPrint()  {
  167.         if(internalMapper==null){
  168.             initMapper();
  169.         }
  170.         if(writerPrettyPrint==null){
  171.             writerPrettyPrint = internalMapper.writer().withDefaultPrettyPrinter();
  172.         }
  173.     }
  174.     public static ObjectWriter getObjectWriterPrettyPrint() {
  175.         if(writerPrettyPrint==null){
  176.             initWriterPrettyPrint();
  177.         }
  178.         return writerPrettyPrint;
  179.     }
  180.    
  181.    
  182.    
  183.     private JSONUtils(boolean prettyPrint) {
  184.         super(prettyPrint);
  185.     }
  186.    
  187.     @Override
  188.     protected void _initMapper() {
  189.         initMapper();
  190.     }
  191.     @Override
  192.     protected void _initWriter(boolean prettyPrint) {
  193.         if(prettyPrint) {
  194.             initWriterPrettyPrint();
  195.         }
  196.         else {
  197.             initWriter();
  198.         }
  199.     }
  200.    
  201.     @Override
  202.     protected ObjectMapper _getObjectMapper() {
  203.         return getObjectMapper();
  204.     }
  205.     @Override
  206.     protected ObjectWriter _getObjectWriter(boolean prettyPrint) {
  207.         if(prettyPrint) {
  208.             return getObjectWriterPrettyPrint();
  209.         }
  210.         else {
  211.             return getObjectWriter();
  212.         }
  213.     }
  214.    
  215.    
  216.     // IS
  217.    
  218.     public boolean isJson(byte[]jsonBytes){
  219.         return this.isValid(jsonBytes);
  220.     }
  221.    
  222.     public boolean isJson(String jsonString){
  223.         return this.isValid(jsonString);
  224.     }
  225.    
  226.    
  227.     // CONVERT TO MAP
  228.    
  229.     public Map<String, Serializable> convertToMap(Logger log, String source, String raw) {
  230.         return this.convertToMap(log, source, raw, null);
  231.     }
  232.     public Map<String, Serializable> convertToMap(Logger log, String source, String raw, List<String> claimsToConvert) {
  233.         if(this.isJson(raw)) {
  234.             return super.convertToMapEngine(log, source, raw, claimsToConvert);
  235.         }
  236.         else {
  237.             return new HashMap<>(); // empty return
  238.         }  
  239.     }
  240.    
  241.     public Map<String, Serializable> convertToMap(Logger log, String source, byte[]raw) {
  242.         return this.convertToMap(log, source, raw, null);
  243.     }
  244.     public Map<String, Serializable> convertToMap(Logger log, String source, byte[]raw, List<String> claimsToConvert) {
  245.         if(this.isJson(raw)) {
  246.             return super.convertToMapEngine(log, source, raw, claimsToConvert);
  247.         }
  248.         else {
  249.             return new HashMap<>(); // empty return
  250.         }  
  251.     }
  252.    
  253. }