JsonJacksonSerializer.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.serialization;

  21. import java.io.OutputStream;
  22. import java.io.Writer;
  23. import java.util.HashSet;

  24. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  25. import com.fasterxml.jackson.databind.ObjectMapper;
  26. import com.fasterxml.jackson.databind.ObjectWriter;
  27. import com.fasterxml.jackson.databind.SerializationFeature;
  28. import com.fasterxml.jackson.databind.introspect.Annotated;
  29. import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
  30. import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
  31. import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
  32. import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
  33. import com.fasterxml.jackson.databind.type.TypeFactory;
  34. import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;



  35. /**
  36.  * Contiene utility per effettuare la serializzazione di un oggetto
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */

  42. public class JsonJacksonSerializer implements ISerializer {


  43.     private static final String DEFAULT = "__default";
  44.     private ObjectWriter writer;

  45.    
  46.     public JsonJacksonSerializer() {
  47.         this(new SerializationConfig());
  48.     }
  49.    
  50.     public JsonJacksonSerializer(SerializationConfig config) {
  51.         ObjectMapper mapper = new ObjectMapper().setAnnotationIntrospector(
  52.                 new AnnotationIntrospectorPair(
  53.                         new JacksonAnnotationIntrospector() {

  54.                             private static final long serialVersionUID = 1L;

  55.                             @Override
  56.                             public String findFilterId(Annotated a) {
  57.                                 return DEFAULT;
  58.                             }

  59.                         }, new JaxbAnnotationIntrospector(TypeFactory.defaultInstance())
  60.                         )
  61.                 );

  62.         if(config.isSerializeEnumAsString())
  63.             mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);

  64.         mapper.setDateFormat(config.getDf());      
  65.        
  66.         if(config.getIgnoreNullValues() == null || config.getIgnoreNullValues())
  67.             mapper.setSerializationInclusion(Include.NON_NULL);
  68.        
  69.         SimpleFilterProvider filters = new SimpleFilterProvider();
  70.         if( (
  71.                 config.getFilter() != null
  72.                 &&
  73.                 ( (config.getFilter().sizeFiltersByName()>0) || (config.getFilter().sizeFiltersByValue()>0))
  74.             )
  75.                 ||
  76.             config.getExcludes() != null) {
  77.             filters = filters.addFilter(DEFAULT, new JacksonSimpleBeanPropertyFilter(config, new JsonJacksonSerializer()));
  78.         } else if(config.getIncludes()!=null) {
  79.             HashSet<String> hashSet = new HashSet<String>();
  80.             hashSet.addAll(config.getIncludes());
  81.             filters = filters.addFilter(DEFAULT, SimpleBeanPropertyFilter.filterOutAllExcept(hashSet));
  82.         }
  83.        
  84.         filters = filters.setFailOnUnknownId(false);

  85.         mapper.setFilterProvider(filters);
  86.         if(config.isPrettyPrint()) {
  87.             this.writer = mapper.writer().withDefaultPrettyPrinter();
  88.         }
  89.         else {
  90.             this.writer = mapper.writer();
  91.         }
  92.     }

  93.     @Override
  94.     public String getObject(Object o) throws IOException{
  95.         try{
  96.             return this.writer.writeValueAsString(o);
  97.         }catch(Exception e){
  98.             throw new IOException(e.getMessage(),e);
  99.         }
  100.     }

  101.     @Override
  102.     public void writeObject(Object o,OutputStream out) throws IOException{
  103.         try{
  104.             this.writer.writeValue(out, o);
  105.         }catch(Exception e){
  106.             throw new IOException(e.getMessage(),e);
  107.         }
  108.     }

  109.     @Override
  110.     public void writeObject(Object o,Writer out) throws IOException{
  111.         try{
  112.             this.writer.writeValue(out, o);
  113.         }catch(Exception e){
  114.             throw new IOException(e.getMessage(),e);
  115.         }
  116.     }

  117. }