Search in sources :

Example 6 with JSONConverter

use of lucee.runtime.converter.JSONConverter in project Lucee by lucee.

the class CallStackGet method call.

public static Object call(PageContext pc, String type) throws PageException {
    Array arr = (Array) call(pc);
    if (type.equalsIgnoreCase("array"))
        return arr;
    if (type.equalsIgnoreCase("json")) {
        try {
            return new JSONConverter(true, null).serialize(pc, arr, false);
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            throw Caster.toPageException(t);
        }
    }
    StringBuilder sb = new StringBuilder(64 * arr.size());
    Struct struct;
    String func;
    Iterator it = arr.valueIterator();
    if (type.equalsIgnoreCase("text") || type.equalsIgnoreCase("string")) {
        while (it.hasNext()) {
            struct = (Struct) it.next();
            sb.append((String) struct.get(KeyConstants._template));
            func = (String) struct.get(KeyConstants._function);
            if (!func.isEmpty()) {
                sb.append('.').append(func).append("()");
            }
            sb.append(':').append(((Double) struct.get(LINE_NUMBER)).intValue());
            if (it.hasNext())
                sb.append("; ");
        }
        return sb.toString();
    }
    if (type.equalsIgnoreCase("html")) {
        sb.append("<ul class='-lucee-array'>");
        while (it.hasNext()) {
            struct = (Struct) it.next();
            sb.append("<li>");
            sb.append((String) struct.get(KeyConstants._template));
            func = (String) struct.get(KeyConstants._function);
            if (!func.isEmpty()) {
                sb.append('.').append(func).append("()");
            }
            sb.append(':').append(((Double) struct.get(LINE_NUMBER)).intValue());
            sb.append("</li>");
        }
        sb.append("</ul>");
        return sb.toString();
    }
    throw new FunctionException(pc, CallStackGet.class.getSimpleName(), 1, "type", "Argument type [" + type + "] is not valid.  Valid types are: [array], text, html, json.");
}
Also used : Array(lucee.runtime.type.Array) Iterator(java.util.Iterator) FunctionException(lucee.runtime.exp.FunctionException) JSONConverter(lucee.runtime.converter.JSONConverter) Struct(lucee.runtime.type.Struct)

Aggregations

JSONConverter (lucee.runtime.converter.JSONConverter)6 IOException (java.io.IOException)4 Charset (java.nio.charset.Charset)3 ConverterException (lucee.runtime.converter.ConverterException)3 ScriptConverter (lucee.runtime.converter.ScriptConverter)3 Array (lucee.runtime.type.Array)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 HTTPResponse (lucee.commons.net.http.HTTPResponse)2 WDDXConverter (lucee.runtime.converter.WDDXConverter)2 XMLConverter (lucee.runtime.converter.XMLConverter)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 FunctionException (lucee.runtime.exp.FunctionException)2 ArrayImpl (lucee.runtime.type.ArrayImpl)2 Struct (lucee.runtime.type.Struct)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 Iterator (java.util.Iterator)1 MimeType (lucee.commons.lang.mimetype.MimeType)1 Header (lucee.commons.net.http.Header)1