use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class Json method jsonEqual.
public static boolean jsonEqual(JsonObject left, JsonObject right) {
if (Json.isNullOrEmpty(left) && Json.isNullOrEmpty(right)) {
return true;
}
if (Json.isNullOrEmpty(left) && !Json.isNullOrEmpty(right)) {
return false;
}
if (!Json.isNullOrEmpty(left) && Json.isNullOrEmpty(right)) {
return false;
}
Iterator<String> leftKeys = left.keys();
while (leftKeys.hasNext()) {
String key = leftKeys.next();
Object leftValue = left.get(key);
Object rightValue = right.get(key);
if (rightValue == null) {
return false;
}
boolean areEqual = areEqual(leftValue, rightValue);
if (!areEqual) {
return false;
}
}
Iterator<String> rightKeys = right.keys();
while (rightKeys.hasNext()) {
boolean hasKey = left.containsKey(rightKeys.next());
if (!hasKey) {
return false;
}
}
return true;
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class Json method resolve.
public static Object resolve(Object obj, ExpressionCompiler compiler, VariableResolver vr) {
if (obj == null || vr == null) {
return obj;
}
if (obj instanceof JsonObject) {
JsonObject o = (JsonObject) obj;
if (o.isEmpty()) {
return o;
}
Iterator<String> keys = o.keys();
while (keys.hasNext()) {
String key = keys.next();
o.set(key, resolve(o.get(key), compiler, vr));
}
return o;
} else if (obj instanceof JsonArray) {
JsonArray array = (JsonArray) obj;
for (int i = 0; i < array.count(); i++) {
Object resolved = resolve(array.get(i), compiler, vr);
array.remove(i);
array.add(i, resolved);
}
return array;
} else {
String exp = String.valueOf(obj);
if (Lang.isNullOrEmpty(exp)) {
return obj;
}
return compiler.compile(exp, null).eval(vr);
}
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class Lang method toError.
public static JsonObject toError(Throwable th) {
if (th == null) {
return null;
}
Throwable first = th;
th = Lang.getRootCause(th);
String message = th.getMessage();
if (message == null) {
StackTraceElement ste = th.getStackTrace()[0];
message = th.getClass().getSimpleName() + Lang.GREATER + Lang.SPACE + ste.getClassName() + Lang.SPACE + ", Method: " + ste.getMethodName() + ", Line: " + ste.getLineNumber();
}
JsonObject error = new JsonObject();
error.set(ApiResponse.Error.Message, message);
if (!Lang.isDebugMode()) {
return error;
}
JsonArray trace = new JsonArray();
error.set(ApiResponse.Error.Trace, trace);
if (th instanceof NashornException) {
if (UndefinedMessage.equals(message)) {
error.set(ApiResponse.Error.Message, UndefinedMessage + " variable");
} else if (message.startsWith(TypeError)) {
error.set(ApiResponse.Error.Message, Lang.replace(message, TypeError, Lang.BLANK));
}
readNashornError(th, first, trace);
return error;
}
StackTraceElement[] stack = th.getStackTrace();
for (StackTraceElement line : stack) {
JsonObject oLine = new JsonObject();
trace.add(oLine);
oLine.set(ApiResponse.Error.Line, line.getLineNumber());
oLine.set(ApiResponse.Error.Function, line.getMethodName());
String className = line.getClassName();
int indeOfNashornScript = className.indexOf(NashornScript);
if (indeOfNashornScript > 0) {
String file = line.getFileName();
// if the error is coming from the platform core scripts
int indexOfPath = file.indexOf(PlatformPath);
if (indexOfPath <= 0) {
indexOfPath = file.indexOf(ApiResourcePath);
}
if (indexOfPath > 0) {
file = file.substring(indexOfPath);
}
oLine.set(ApiResponse.Error.File, file);
// break here
break;
} else {
int indexOfDot = className.lastIndexOf(Lang.DOT);
if (indexOfDot > 0) {
className = className.substring(indexOfDot + 1);
}
oLine.set(ApiResponse.Error.Clazz, className);
}
}
return error;
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class DatabaseApiRequestTrack method update.
@Override
public void update(ApiConsumer consumer) {
if (consumer == null) {
return;
}
JsonObject oConsumer = new JsonObject();
oConsumer.set(ApiConsumer.Fields.Type, consumer.get(ApiConsumer.Fields.Type));
oConsumer.set(ApiConsumer.Fields.Id, consumer.get(ApiConsumer.Fields.Id));
oConsumer.set(ApiConsumer.Fields.Token, consumer.get(ApiConsumer.Fields.Token));
oConsumer.set(ApiConsumer.Fields.AccessKey, consumer.get(ApiConsumer.Fields.AccessKey));
track.set(Fields.Consumer, oConsumer);
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class DatabaseApiRequestTrack method put.
@Override
public void put(String name, Object value) {
if (Lang.isNullOrEmpty(name) || value == null) {
return;
}
JsonObject custom = Json.getObject(track, Fields.Custom);
if (custom == null) {
custom = new JsonObject();
track.set(Fields.Custom, custom);
}
custom.set(name, value);
}
Aggregations