JsonJacksonDeserializer.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.InputStream;
  22. import java.io.Reader;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import com.fasterxml.jackson.databind.BeanDescription;
  26. import com.fasterxml.jackson.databind.DeserializationConfig;
  27. import com.fasterxml.jackson.databind.DeserializationFeature;
  28. import com.fasterxml.jackson.databind.ObjectMapper;
  29. import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder;
  30. import com.fasterxml.jackson.databind.deser.BeanDeserializerFactory;
  31. import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
  32. import com.fasterxml.jackson.databind.deser.DefaultDeserializationContext;
  33. import com.fasterxml.jackson.databind.deser.DeserializerFactory;
  34. import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;

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

  42. public class JsonJacksonDeserializer implements IDeserializer{

  43.    
  44.     private static class BeanDeserializerModifierForIgnorables extends BeanDeserializerModifier {

  45.         private static final long serialVersionUID = 1L;
  46.         private List<String> ignorables;

  47.         public BeanDeserializerModifierForIgnorables(List<String> properties) {
  48.             if(properties!=null)
  49.                 this.ignorables = properties;
  50.             else
  51.                 this.ignorables = new ArrayList<>();
  52.         }

  53.         @Override
  54.         public BeanDeserializerBuilder updateBuilder(
  55.                 DeserializationConfig config, BeanDescription beanDesc,
  56.                 BeanDeserializerBuilder builder) {

  57.             for(String ignorable : this.ignorables) {
  58.                 builder.addIgnorable(ignorable);                
  59.             }

  60.             return builder;
  61.         }

  62.         @Override
  63.         public List<BeanPropertyDefinition> updateProperties(
  64.                 DeserializationConfig config, BeanDescription beanDesc,
  65.                 List<BeanPropertyDefinition> propDefs) {

  66.             List<BeanPropertyDefinition> newPropDefs = new ArrayList<>();
  67.             for(BeanPropertyDefinition propDef : propDefs) {
  68.                 if(!this.ignorables.contains(propDef.getName())) {
  69.                     newPropDefs.add(propDef);
  70.                 }
  71.             }
  72.             return newPropDefs;
  73.         }
  74.     }


  75.     private ObjectMapper mapper;

  76.     public JsonJacksonDeserializer() {
  77.         this(new SerializationConfig());
  78.     }
  79.     public JsonJacksonDeserializer(SerializationConfig config) {
  80.         BeanDeserializerModifier modifier = new BeanDeserializerModifierForIgnorables(config.getExcludes());
  81.         DeserializerFactory dFactory = BeanDeserializerFactory.instance.withDeserializerModifier(modifier);

  82.         this.mapper = new ObjectMapper(null, null, new DefaultDeserializationContext.Impl(dFactory));
  83.         this.mapper.setDateFormat(config.getDf());
  84.         if(config.isSerializeEnumAsString())
  85.             this.mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);

  86.     }

  87.     @Override
  88.     public Object getObject(String s, Class<?> classType) throws IOException{
  89.         try {
  90.             return this.mapper.readValue(s, classType);
  91.         } catch (java.io.IOException e) {
  92.             throw new IOException(e);
  93.         }
  94.     }

  95.     @Override
  96.     public Object readObject(InputStream is, Class<?> classType) throws IOException {
  97.         try {
  98.             return this.mapper.readValue(is, classType);
  99.         } catch (java.io.IOException e) {
  100.             throw new IOException(e);
  101.         }
  102.     }

  103.     @Override
  104.     public Object readObject(Reader reader, Class<?> classType) throws IOException {
  105.         try {
  106.             return this.mapper.readValue(reader, classType);
  107.         } catch (java.io.IOException e) {
  108.             throw new IOException(e);
  109.         }
  110.     }
  111. }