JSonSerializer.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.lang.annotation.Annotation;
  24. import java.util.List;
  25. import java.util.Set;


  26. import net.sf.json.JSONArray;
  27. import net.sf.json.JSONObject;
  28. import net.sf.json.JsonConfig;


  29. /**
  30.  * Contiene utility per effettuare la serializzazione di un oggetto
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */

  36. public class JSonSerializer implements ISerializer {

  37.     private JsonConfig jsonConfig;
  38.    
  39.     public JSonSerializer() {
  40.         this(new SerializationConfig());
  41.     }
  42.    
  43.     public JSonSerializer(SerializationConfig config) {
  44.        
  45.         JSonSerializer filtroInternoOggettiFiltratiDiversiByteArray = null;
  46.         if(config.getFilter().sizeFiltersByValue()>0 || config.getFilter().sizeFiltersByName()>0) {
  47.             SerializationConfig conf = new SerializationConfig();
  48.             conf.setFilter(new Filter());
  49.             conf.setIdBuilder(config.getIdBuilder());
  50.             conf.setExcludes(config.getExcludes());
  51.             filtroInternoOggettiFiltratiDiversiByteArray = new JSonSerializer(conf);
  52.         }
  53.        
  54.         JsonConfig jsonConfig = new JsonConfig();
  55.         jsonConfig.setJsonPropertyFilter(new PropertyFilter(config.getFilter(),config.getIdBuilder(),filtroInternoOggettiFiltratiDiversiByteArray));
  56.         if(config.getExcludes()!=null){
  57.             jsonConfig.setExcludes(config.getExcludes().toArray(new String[]{}));
  58.         }
  59.         this.jsonConfig = jsonConfig;
  60.     }
  61.    
  62.     @Override
  63.     public String getObject(Object o) throws IOException{
  64.         try{
  65.             Utilities.normalizeDateObjects(o);
  66.             if( (o instanceof Enum) ){
  67.                 JSONArray jsonObject = JSONArray.fromObject( o , this.jsonConfig );
  68.                 return jsonObject.toString();
  69.             }
  70.             else if( (o != null && o.getClass().isArray()) ){
  71.                 JSONArray jsonObject = new JSONArray();
  72.                 Object[] array = (Object[]) o;
  73.                 for (int i = 0; i < array.length; i++) {
  74.                     Object object = array[i];
  75.                     jsonObject.add(JSONObject.fromObject( object , this.jsonConfig ));
  76.                 }
  77.                 return jsonObject.toString();
  78.             }
  79.             else if( (o instanceof List) ){
  80.                 JSONArray jsonObject = new JSONArray();
  81.                 List<?> list = (List<?>) o;
  82.                 for (int i = 0; i < list.size(); i++) {
  83.                     Object object = list.get(i);
  84.                     jsonObject.add(JSONObject.fromObject( object , this.jsonConfig ));
  85.                 }
  86.                 return jsonObject.toString();
  87.             }
  88.             else if( (o instanceof Set) ){
  89.                 Set<?> set = (Set<?>) o;
  90.                 return this.getObject(set.toArray());
  91.             }
  92.             else if((o instanceof Annotation) || o != null && o.getClass().isAnnotation())
  93.                 throw new IOException("'object' is an Annotation.");
  94.             else {
  95.                 JSONObject jsonObject = JSONObject.fromObject( o , this.jsonConfig );
  96.                 return jsonObject.toString();
  97.             }
  98.            
  99.         }catch(Exception e){
  100.             throw new IOException(e.getMessage(),e);
  101.         }
  102.     }
  103.    
  104.     @Override
  105.     public void writeObject(Object o,OutputStream out) throws IOException{
  106.         try{
  107.             //Utilities.normalizeDateObjects(o);
  108.             String s = this.getObject(o);
  109.             out.write(s.getBytes());
  110.         }catch(Exception e){
  111.             throw new IOException(e.getMessage(),e);
  112.         }
  113.     }
  114.    
  115.     @Override
  116.     public void writeObject(Object o,Writer out) throws IOException{
  117.         try{
  118.             //Utilities.normalizeDateObjects(o);
  119.             String s = this.getObject(o);
  120.             out.write(s);
  121.         }catch(Exception e){
  122.             throw new IOException(e.getMessage(),e);
  123.         }
  124.     }

  125. }