Search in sources :

Example 51 with Array

use of lucee.runtime.type.Array in project Lucee by lucee.

the class SerializeJSON method _call.

private static String _call(PageContext pc, Object var, Object options, Charset charset) throws PageException {
    try {
        JSONConverter json = new JSONConverter(true, charset);
        if (Decision.isBoolean(options))
            return json.serialize(pc, var, Caster.toBoolean(options));
        if (Decision.isQuery(var)) {
            if (Decision.isSimpleValue(options)) {
                String opt = Caster.toString(options);
                if ("struct".equalsIgnoreCase(opt)) {
                    Array arr = new ArrayImpl();
                    ForEachQueryIterator it = new ForEachQueryIterator((Query) var, pc.getId());
                    try {
                        while (it.hasNext()) {
                            // append each record from the query as a struct
                            arr.append(it.next());
                        }
                    } finally {
                        it.reset();
                    }
                    return json.serialize(pc, arr, false);
                }
            } else if (Decision.isBoolean(options)) {
                return json.serialize(pc, var, Caster.toBoolean(options));
            } else
                throw new FunctionException(pc, SerializeJSON.class.getSimpleName(), 2, "options", "When var is a Query, argument [options] must be either a boolean value or a string with the value of [struct]");
        }
        // var is not a query so options doesn't make a difference here
        return json.serialize(pc, var, false);
    } catch (ConverterException e) {
        throw Caster.toPageException(e);
    }
}
Also used : Array(lucee.runtime.type.Array) ForEachQueryIterator(lucee.runtime.type.it.ForEachQueryIterator) ConverterException(lucee.runtime.converter.ConverterException) ArrayImpl(lucee.runtime.type.ArrayImpl) FunctionException(lucee.runtime.exp.FunctionException) JSONConverter(lucee.runtime.converter.JSONConverter)

Example 52 with Array

use of lucee.runtime.type.Array in project Lucee by lucee.

the class FormImpl method makeComparable.

private String makeComparable(String key) {
    key = StringUtil.trim(key, "");
    // form.x
    if (StringUtil.startsWithIgnoreCase(key, "form."))
        key = key.substring(5).trim();
    // form . x
    try {
        Array array = ListUtil.listToArray(key, '.');
        if (array.size() > 1 && array.getE(1).toString().trim().equalsIgnoreCase("form")) {
            array.removeE(1);
            key = ListUtil.arrayToList(array, ".").trim();
        }
    } catch (PageException e) {
    }
    return key;
}
Also used : Array(lucee.runtime.type.Array) PageException(lucee.runtime.exp.PageException)

Example 53 with Array

use of lucee.runtime.type.Array in project Lucee by lucee.

the class ScopeSupport method _fill.

private Struct _fill(final Struct parent, String name, Object value, boolean isLast, boolean scriptProteced, boolean sameAsArray) {
    Object curr;
    boolean isArrayDef = sameAsArray;
    Collection.Key key = KeyImpl.init(name);
    // script protect
    if (scriptProteced && value instanceof String) {
        value = ScriptProtect.translate((String) value);
    }
    if (name.length() > 2 && name.endsWith("[]")) {
        isArrayDef = true;
        name = name.substring(0, name.length() - 2);
        key = KeyImpl.getInstance(name);
        curr = parent.get(key, null);
    } else {
        curr = parent.get(key, null);
    }
    if (curr == null) {
        if (isArrayDef) {
            Array arr = new ArrayImpl();
            arr.appendEL(value);
            parent.setEL(key, arr);
        } else
            parent.setEL(key, value);
    } else if (curr instanceof Array) {
        ((Array) curr).appendEL(value);
    } else if (curr instanceof CastableStruct) {
        if (isLast)
            ((CastableStruct) curr).setValue(value);
        else
            return (Struct) curr;
    } else if (curr instanceof Struct) {
        if (isLast)
            parent.setEL(key, value);
        else
            return (Struct) curr;
    } else if (curr instanceof String) {
        if (isArrayDef) {
            Array arr = new ArrayImpl();
            arr.appendEL(curr);
            arr.appendEL(value);
            parent.setEL(key, arr);
        } else if (value instanceof Struct) {
            parent.setEL(key, value);
        } else {
            if (!StringUtil.isEmpty(value)) {
                String existing = Caster.toString(curr, "");
                if (StringUtil.isEmpty(existing))
                    parent.setEL(key, value);
                else
                    parent.setEL(key, Caster.toString(curr, "") + ',' + value);
            }
        }
    }
    if (!isLast) {
        return (Struct) value;
    }
    return null;
}
Also used : Array(lucee.runtime.type.Array) ArrayImpl(lucee.runtime.type.ArrayImpl) Collection(lucee.runtime.type.Collection) CastableStruct(lucee.runtime.type.CastableStruct) Struct(lucee.runtime.type.Struct) CastableStruct(lucee.runtime.type.CastableStruct)

Example 54 with Array

use of lucee.runtime.type.Array in project Lucee by lucee.

the class ArrayUtil method toDoubleArray.

public static double[] toDoubleArray(Object obj) throws PageException {
    if (obj instanceof double[])
        return (double[]) obj;
    Array arr = Caster.toArray(obj);
    double[] tarr = new double[arr.size()];
    for (int i = 0; i < tarr.length; i++) {
        tarr[i] = Caster.toDoubleValue(arr.getE(i + 1));
    }
    return tarr;
}
Also used : Array(lucee.runtime.type.Array)

Example 55 with Array

use of lucee.runtime.type.Array in project Lucee by lucee.

the class ArrayUtil method toIntArray.

public static int[] toIntArray(Object obj) throws PageException {
    if (obj instanceof int[])
        return (int[]) obj;
    Array arr = Caster.toArray(obj);
    int[] tarr = new int[arr.size()];
    for (int i = 0; i < tarr.length; i++) {
        tarr[i] = Caster.toIntValue(arr.getE(i + 1));
    }
    return tarr;
}
Also used : Array(lucee.runtime.type.Array)

Aggregations

Array (lucee.runtime.type.Array)169 ArrayImpl (lucee.runtime.type.ArrayImpl)79 Struct (lucee.runtime.type.Struct)49 StructImpl (lucee.runtime.type.StructImpl)25 Iterator (java.util.Iterator)24 PageException (lucee.runtime.exp.PageException)23 Entry (java.util.Map.Entry)22 ArrayList (java.util.ArrayList)20 Key (lucee.runtime.type.Collection.Key)20 ListIterator (java.util.ListIterator)16 Query (lucee.runtime.type.Query)16 List (java.util.List)14 ApplicationException (lucee.runtime.exp.ApplicationException)14 FunctionException (lucee.runtime.exp.FunctionException)14 ForEachQueryIterator (lucee.runtime.type.it.ForEachQueryIterator)14 Resource (lucee.commons.io.res.Resource)13 IOException (java.io.IOException)12 Collection (lucee.runtime.type.Collection)10 QueryImpl (lucee.runtime.type.QueryImpl)10 JavaObject (lucee.runtime.java.JavaObject)8