package bamboo.util;

import bamboo.core.Config;
import freemarker.core.HTMLOutputFormat;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import spark.Request;

public class Freemarker {
    public static Configuration config = init();

    private static Configuration init() {
        Configuration config = new Configuration(Configuration.VERSION_2_3_22);
        config.setOutputEncoding("utf-8");
        config.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
        config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
        config.setAutoEscapingPolicy(Configuration.ENABLE_IF_SUPPORTED_AUTO_ESCAPING_POLICY);
        config.setOutputFormat(HTMLOutputFormat.INSTANCE);
        config.addAutoInclude("/bamboo/views/layout.ftl");
        BeansWrapper beansWrapper = new BeansWrapper(Configuration.VERSION_2_3_26);
        beansWrapper.setExposeFields(true);
        config.setObjectWrapper(beansWrapper);
        config.setClassForTemplateLoading(Freemarker.class, "/");
        return config;
    }

    public static String render(Request request, String view, Object... keysAndValues) {
        Map<String, Object> model = new HashMap<>();
        model.put("request", request);
        for (int i = 0; i < keysAndValues.length; i += 2) {
            model.put((String) keysAndValues[i], keysAndValues[i + 1]);
        }
        StringWriter sw = new StringWriter();
        try {
            config.getTemplate(view).process(model, sw);
        } catch (TemplateException | IOException e) {
            throw new RuntimeException(view, e);
        }
        return sw.toString();
    }
}

@@ -42,16 +42,45 @@
 Config;%0A
+import com.google.gson.Gson;%0A
 import f
@@ -451,16 +451,65 @@
 arker %7B%0A
+    private static final Gson gson = new Gson();%0A
     publ
@@ -1439,24 +1439,142 @@
 ndValues) %7B%0A
+        if (%22json%22.equals(request.queryParams(%22format%22))) %7B%0A            return gson.toJson(keysAndValues);%0A        %7D%0A%0A
         Map%3C

Add json rendering option