use of org.structr.core.Result in project structr by structr.
the class ToJsonFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
if (sources != null && sources.length >= 1 && sources.length <= 3) {
try {
final SecurityContext securityContext = ctx.getSecurityContext();
final Value<String> view = new StaticValue<>("public");
if (sources.length > 1) {
view.set(securityContext, sources[1].toString());
}
int outputDepth = 3;
if (sources.length > 2 && sources[2] instanceof Number) {
outputDepth = ((Number) sources[2]).intValue();
}
final StreamingJsonWriter jsonStreamer = new StreamingJsonWriter(view, true, outputDepth);
final StringWriter writer = new StringWriter();
if (sources[0] instanceof GraphObject) {
jsonStreamer.streamSingle(securityContext, writer, (GraphObject) sources[0]);
} else if (sources[0] instanceof List) {
final List list = (List) sources[0];
jsonStreamer.stream(securityContext, writer, new Result(list, list.size(), true, false), null);
} else if (sources[0] instanceof Map) {
final GraphObjectMap map = new GraphObjectMap();
this.recursivelyConvertMapToGraphObjectMap(map, (Map) sources[0], outputDepth);
jsonStreamer.stream(securityContext, writer, new Result(map, false), null);
}
return writer.getBuffer().toString();
} catch (Throwable t) {
logException(caller, t, sources);
}
return "";
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return usage(ctx.isJavaScriptContext());
}
Aggregations