package com.sandbox.runtime.js.serializers;

import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

/**
 * Created by nickhoughton on 16/07/2015.
 */
public class NashornSerializer {

    private static final Logger logger = LoggerFactory.getLogger(NashornSerializer.class);

    private ScriptObjectMirror jsonParser;

    public NashornSerializer(ScriptObjectMirror jsonParser) {
        this.jsonParser = jsonParser;
    }

    private static NashornSerializer instance = null;
    public static NashornSerializer instance(){
        if(instance == null){
            try {
                ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(new String[]{"--no-java"});
                instance = new NashornSerializer((ScriptObjectMirror) engine.eval("JSON"));
            } catch (ScriptException e) {
                logger.error("Error starting nashorn serialiser!", e);
            }
        }
        return instance;
    }

    public String serialize(Object value){
        Object result = jsonParser.callMember("stringify", value, null, 2);
        return (String) result;
    }

}

@@ -1254,16 +1254,172 @@
 ll, 2);%0A
+        if(!(result instanceof String))%7B%0A            result = %22null%22;%0A            logger.warn(%22Serialized unsupported object, returning null..%22);%0A        %7D%0A
         

Return null for undefined values.