package com.devicehive.json.adapters;


import com.devicehive.model.JsonStringWrapper;
import com.google.gson.*;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class JsonStringWrapperAdapterFactory implements TypeAdapterFactory {
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        if (!JsonStringWrapper.class.isAssignableFrom(type.getRawType())) {
            return null;
        }
        ParameterizedType parameterizedType = (ParameterizedType) type.getType();
        Type internalType = parameterizedType.getActualTypeArguments()[0];

        return (TypeAdapter<T>) new JsonStringWrapperAdapter(gson, internalType);
    }

    private static class JsonStringWrapperAdapter extends TypeAdapter<JsonStringWrapper> {

        private Type internalType;
        private Gson gson;

        private JsonStringWrapperAdapter(Gson gson, Type internalType) {
            this.gson = gson;
            this.internalType = internalType;
        }

        @Override
        public void write(JsonWriter out, JsonStringWrapper value) throws IOException {
            if (value == null) {
                out.nullValue();
            } else {
                Streams.write(new JsonParser().parse(value.getJsonString()), out);
            }

        }

        @Override
        public JsonStringWrapper read(JsonReader in) throws IOException {
            return new JsonStringWrapper(Streams.parse(in).toString());
        }
    }
}

@@ -107,9 +107,126 @@
 son.
-*
+Gson;%0Aimport com.google.gson.JsonParser;%0Aimport com.google.gson.TypeAdapter;%0Aimport com.google.gson.TypeAdapterFactory
 ;%0Aim
@@ -422,83 +422,8 @@
 ion;
-%0Aimport java.lang.reflect.ParameterizedType;%0Aimport java.lang.reflect.Type;
 %0A%0Apu
@@ -695,166 +695,8 @@
   %7D%0A
-        ParameterizedType parameterizedType = (ParameterizedType) type.getType();%0A        Type internalType = parameterizedType.getActualTypeArguments()%5B0%5D;%0A%0A
     
@@ -756,26 +756,8 @@
 ter(
-gson, internalType
 );%0A 
@@ -858,230 +858,8 @@
  %7B%0A%0A
-        private Type internalType;%0A        private Gson gson;%0A%0A        private JsonStringWrapperAdapter(Gson gson, Type internalType) %7B%0A            this.gson = gson;%0A            this.internalType = internalType;%0A        %7D%0A
 %0A   

JsonStringWrapperAdapterFactory is simplified