package org.sql2o.extensions.postgres.converters;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.postgresql.util.PGobject;
import org.sql2o.converters.Converter;
import org.sql2o.converters.ConverterException;
import org.sql2o.converters.ConvertersProvider;
import org.sql2o.converters.StringConverter;

import java.util.Map;

/**
 * User: dimzon
 * Date: 4/25/14
 * Time: 12:08 AM
 */
public class JSONConverter implements Converter<JsonElement>, ConvertersProvider {
    public void fill(Map<Class<?>, Converter<?>> mapToFill) {
        mapToFill.put(JsonElement.class, this);
    }

    public JsonElement convert(Object val) throws ConverterException {
        if (val == null) return null;
        if (val instanceof JsonElement) return (JsonElement) val;
        String jsonString;
        if (val instanceof PGobject) {
            jsonString = ((PGobject) val).getValue();
        } else {
            jsonString = stringConverterHolder.converter.convert(val);
        }
        return parserHolder.parser.parse(jsonString);
    }

    public Object toDatabaseParam(JsonElement val) {
        if (val == null) return null;
        final StringBuilder stringBuilder = new StringBuilder();
        writerHolder.gson.toJson(val, stringBuilder);
        return stringBuilder.toString();
    }

    static class parserHolder {
        static final JsonParser parser = new JsonParser();
    }

    static class writerHolder {
        static final Gson gson = new Gson();
    }

    static class stringConverterHolder {
        static final StringConverter converter = new StringConverter();
    }
}

@@ -877,19 +877,261 @@
 eof 
-PGobject) %7B
+String) %7B%0A            jsonString = (String) val;%0A        %7D else if (val instanceof PGobject) %7B%0A            // at one side this is just a demo%0A            // the better way is to unwrap PGObject to String%0A            // with type==json in Quirks.getRSVal
 %0A   

wrong service name