package io.yawp.commons.utils.json;

import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonWriter;
import io.yawp.repository.LazyJson;

import java.io.IOException;

public class LazyJsonTypeAdapterFactory extends CustomTypeAdapterFactory<LazyJson> {
    public LazyJsonTypeAdapterFactory() {
        super(LazyJson.class);
    }

    @Override
    protected void write(JsonWriter out, LazyJson value, TypeAdapter<JsonElement> elementAdapter, TypeAdapter<LazyJson> delegate) throws IOException {
        if (value == null || value.getJson() == null) {
            out.nullValue();
            return;
        }

        // This is done to avoid json parsing to JsonObject, since we already have the json as a String.
        // Could not find any better GSON API. JsonWriter wont let me write string without scaping then.
        CustomJsonWriter customWriter = (CustomJsonWriter) out;
        out.beginObject();
        customWriter.write(value.getJson().substring(1));
    }

}

@@ -938,19 +938,28 @@
         
-out
+customWriter
 .beginOb
@@ -966,24 +966,92 @@
 ject();%0A
-   
+%09%09String substring = value.getJson().substring(1);//Remove %7B and  %7D%0A
      
+   
 customWr
@@ -1065,36 +1065,86 @@
 ite(
-value.getJson().substring(1)
+substring.substring(0, substring.length()-1));%0A        customWriter.endObject(
 );%0A 

fix custom writer