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);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations