AbstractSerializerWithFactory.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.generic_project.serializer;


  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.OutputStream;

  24. import org.openspcoop2.utils.serialization.ISerializer;
  25. import org.openspcoop2.utils.serialization.SerializationConfig;
  26. import org.openspcoop2.utils.serialization.SerializationFactory;
  27. import org.openspcoop2.utils.serialization.SerializationFactory.SERIALIZATION_TYPE;

  28. /**
  29.  * JsonSerializer
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public abstract class AbstractSerializerWithFactory extends AbstractSerializerBase {

  36.     protected abstract SERIALIZATION_TYPE getSERIALIZATION_TYPE();
  37.    
  38.     @Override
  39.     protected <T> void _objToXml(String fileName, Class<T> c, T object,
  40.             boolean pretty) throws Exception {
  41.         ISerializer serializer = SerializationFactory.getSerializer(this.getSERIALIZATION_TYPE(), new SerializationConfig());
  42.         FileOutputStream fout = null;
  43.         try{
  44.             fout = new FileOutputStream(new File(fileName));
  45.             serializer.writeObject(object, fout);
  46.         }finally{
  47.             try{
  48.                 if(fout!=null) {
  49.                     fout.flush();
  50.                 }
  51.             }catch(Exception e){
  52.                 // close
  53.             }
  54.             try{
  55.                 if(fout!=null) {
  56.                     fout.close();
  57.                 }
  58.             }catch(Exception e){
  59.                 // close
  60.             }
  61.         }
  62.     }

  63.     @Override
  64.     protected <T> void _objToXml(OutputStream out, Class<T> c, T object,
  65.             boolean pretty) throws Exception {
  66.         ISerializer serializer = SerializationFactory.getSerializer(this.getSERIALIZATION_TYPE(), new SerializationConfig());
  67.         try{
  68.             serializer.writeObject(object, out);
  69.         }finally{
  70.             try{
  71.                 if(out!=null) {
  72.                     out.flush();
  73.                 }
  74.             }catch(Exception e){
  75.                 // fluse
  76.             }
  77.         }
  78.     }


  79. }