package com.martinchamarro.lazystorage.internal.database;

import com.google.gson.Gson;
import com.martinchamarro.lazystorage.internal.exception.JsonConversionException;
import com.martinchamarro.lazystorage.internal.exception.LazyStorageException;

final class TwoWaysJsonConverter {

    private Gson gson;

    public TwoWaysJsonConverter() {
        gson = new Gson();
    }

    public String convert(Object object) throws LazyStorageException {
        try {
            return gson.toJson(object);
        } catch (Exception e) {
            throw new JsonConversionException("It's not possible to convert the object to Json.", e);
        }
    }

    public <T> T convert(String json, Class<T> classOfT) throws LazyStorageException {
        T object = gson.fromJson(json, classOfT);
        if (object == null) throw new JsonConversionException("Exception trying to convert JSON to object.");
        return object;
    }

}

@@ -748,18 +748,32 @@
     
-T object =
+try %7B%0A            return
  gso
@@ -812,27 +812,43 @@
     
-if (object == null)
+%7D catch (Exception e) %7B%0A           
  thr
@@ -912,11 +912,11 @@
 rt J
-SON
+son
  to 
@@ -923,16 +923,19 @@
 object.%22
+, e
 );%0A     
@@ -941,22 +941,9 @@
     
-return object;
+%7D
 %0A   

FIX -Changes to avoid possible Gson fromJson exceptions