001package com.khubla.pragmatach.plugin.json;
002
003import java.io.ByteArrayInputStream;
004import java.io.ByteArrayOutputStream;
005import java.io.InputStream;
006import java.io.OutputStream;
007import java.lang.reflect.Field;
008
009import org.apache.commons.beanutils.PropertyUtils;
010import org.apache.commons.io.IOUtils;
011import org.json.JSONObject;
012
013import com.google.gson.Gson;
014import com.khubla.pragmatach.framework.api.PragmatachException;
015import com.khubla.pragmatach.framework.controller.PragmatachController;
016
017/**
018 * @author tome
019 */
020public class PragmaticControllerSerializer {
021   public static void deserialize(PragmatachController pragmatachController, InputStream inputStream) throws PragmatachException {
022      try {
023         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
024         IOUtils.copy(inputStream, baos);
025         deserialize(pragmatachController, baos.toString());
026      } catch (final Exception e) {
027         throw new PragmatachException("Exception in deserialize", e);
028      }
029   }
030
031   public static void deserialize(PragmatachController pragmatachController, String json) throws PragmatachException {
032      try {
033         final JSONObject jSONObject = new JSONObject(json);
034         final String[] names = JSONObject.getNames(jSONObject);
035         if (null != names) {
036            for (final String name : names) {
037               /*
038                * get the data we need
039                */
040               final Class<?> type = PropertyUtils.getPropertyType(pragmatachController, name);
041               if (null != type) {
042                  final String value = jSONObject.getString(name);
043                  final Object fieldValue = deserializeField(value, type);
044                  /*
045                   * set the field data
046                   */
047                  PropertyUtils.setProperty(pragmatachController, name, fieldValue);
048               }
049            }
050         }
051      } catch (final Exception e) {
052         throw new PragmatachException("Exception in deserialize", e);
053      }
054   }
055
056   private static Object deserializeField(String json, Class<?> type) throws PragmatachException {
057      try {
058         final Gson gson = new Gson();
059         return gson.fromJson(json, type);
060      } catch (final Exception e) {
061         throw new PragmatachException("Exception in deserializeField", e);
062      }
063   }
064
065   public static String serialize(PragmatachController pragmatachController) throws PragmatachException {
066      try {
067         final JSONObject jSONObject = new JSONObject();
068         for (final Field field : pragmatachController.getClass().getDeclaredFields()) {
069            final String fieldValue = serializeField(PropertyUtils.getProperty(pragmatachController, field.getName()));
070            jSONObject.put(field.getName(), fieldValue);
071         }
072         return jSONObject.toString();
073      } catch (final Exception e) {
074         throw new PragmatachException("Exception in serialize", e);
075      }
076   }
077
078   public static void serialize(PragmatachController pragmatachController, OutputStream outputStream) throws PragmatachException {
079      try {
080         final ByteArrayInputStream bais = new ByteArrayInputStream(serialize(pragmatachController).getBytes());
081         IOUtils.copy(bais, outputStream);
082      } catch (final Exception e) {
083         throw new PragmatachException("Exception in serialize", e);
084      }
085   }
086
087   private static String serializeField(Object object) throws PragmatachException {
088      try {
089         final Gson gson = new Gson();
090         return gson.toJson(object);
091      } catch (final Exception e) {
092         throw new PragmatachException("Exception in serializeField", e);
093      }
094   }
095}