use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class OrientDatabase method describe.
@Override
public JsonObject describe() {
JsonObject describe = new JsonObject();
describe.set(Describe.Size, db.getSize());
Collection<OClass> entities = db.getMetadata().getSchema().getClasses();
if (entities == null || entities.isEmpty()) {
return describe;
}
JsonArray aEntities = new JsonArray();
describe.set(Describe.Entities, aEntities);
for (OClass entity : entities) {
if (SystemEntities.contains(entity.getName())) {
continue;
}
JsonObject oEntity = new JsonObject();
oEntity.set(Describe.Name, entity.getName());
oEntity.set(Describe.Count, entity.count());
aEntities.add(oEntity);
}
return describe;
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class Create method main.
public static void main(String[] args) throws DatabaseException {
Database db = new DatabaseServer().get();
DatabaseObject employee = db.create("Employees");
JsonArray names = new JsonArray();
names.add(new JsonObject().set("number", "4098776623").set("weight", 40));
employee.set("name", "Simo");
employee.set("age", 34);
employee.set("active", true);
employee.set("salary", 43.98);
employee.set("names", names);
employee.save();
System.out.println(employee.toJson(null));
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class JavascriptEngine method write.
@Override
public void write(ApiConsumer consumer, ApiRequest request, final ApiResponse response, ApiOutput output, final ApiResource template, final JsonObject mediaSpec) throws TemplateEngineException {
try {
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, null);
MapScriptContext vars = new MapScriptContext();
vars.set(Json.getString(features, I18n, I18n), api.i18n(request.getLang()));
vars.set(Json.getString(features, Request, Request), request.toJson());
if (consumer != null) {
vars.set(Json.getString(features, Consumer, Consumer), consumer.toJson());
}
JsonObject jOutput = null;
if (output != null) {
jOutput = output.data();
}
if (output != null) {
vars.set(Json.getString(features, Output, Output), jOutput);
vars.set(Json.getString(features, Meta, Meta), output.meta());
}
vars.set(Json.getString(features, Error, Error), response.getError());
vars.set(Json.getString(features, Response, Response), response);
JsonObject result;
Object oResult = engine.eval(ScriptingEngine.Supported.Javascript, api, template, vars);
if (oResult != null) {
if (oResult instanceof JsonObject) {
result = (JsonObject) oResult;
} else if (oResult instanceof JsonArray) {
result = new JsonObject();
result.set(ApiOutput.Defaults.Items, oResult);
} else {
result = new JsonObject();
result.set(Output, oResult);
}
} else {
result = jOutput;
}
if (response.getError() != null) {
result = response.getError();
}
boolean doNotWrite = Json.getBoolean(jOutput, DoNotWrite, false);
if (!doNotWrite) {
doNotWrite = Json.getBoolean(mediaSpec, DoNotWrite, false);
}
if (result != null && !doNotWrite) {
response.flushHeaders();
result.write(new AbstractEmitter() {
@Override
public JsonEmitter write(String chunk) {
try {
response.write(chunk);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return this;
}
});
}
} catch (Exception e) {
throw new TemplateEngineException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class MediaRoutingUtils method processMedia.
public static JsonObject processMedia(final ApiRequest request, ApiResponse response, Lang.VariableResolver vr, JsonObject mediaDef, Tracer tracer) {
if (mediaDef == null || (!mediaDef.containsKey(Success) && !mediaDef.containsKey(Error))) {
return null;
}
String selectProcessor = (String) request.get(ApiRequest.MediaSelector);
if (Lang.isNullOrEmpty(selectProcessor)) {
selectProcessor = Lang.STAR;
}
JsonObject mediaSelections = null;
if (response.getError() == null) {
mediaSelections = Json.getObject(mediaDef, Success);
} else {
mediaSelections = Json.getObject(mediaDef, Error);
if (mediaSelections == null) {
mediaSelections = Json.getObject(mediaDef, Success);
}
}
JsonObject media = Json.getObject(mediaSelections, selectProcessor);
if (media != null) {
int status = Json.getInteger(media, Status, 0);
if (status > 0) {
response.setStatus(new Status(status, Lang.BLANK));
}
JsonObject headers = Json.getObject(media, Headers);
if (headers != null && !headers.isEmpty()) {
Iterator<String> names = headers.keys();
while (names.hasNext()) {
String name = names.next();
Object hv = headers.get(name);
if (hv == null) {
hv = Lang.BLANK;
}
if (hv instanceof JsonArray) {
JsonArray arr = (JsonArray) hv;
if (arr.isEmpty()) {
continue;
}
List<String> values = new ArrayList<String>();
for (int i = 0; i < arr.count(); i++) {
values.add(Lang.resolve(String.valueOf(arr.get(i)), vr));
}
response.set(name, values);
} else {
hv = Lang.resolve(hv.toString(), vr);
if (XLocation.toLowerCase().equals(name.toLowerCase())) {
if (status <= 0) {
response.setStatus(ApiResponse.MOVED_PERMANENTLY);
}
response.set(ApiHeaders.Location, hv);
}
if (ApiHeaders.Location.toLowerCase().equals(name.toLowerCase())) {
if (status <= 0) {
response.setStatus(ApiResponse.MOVED_PERMANENTLY);
}
response.set(ApiHeaders.Location, ApiUtils.apiEndpoint(request) + hv);
} else {
response.set(name, hv);
}
}
}
}
}
return media;
}
use of com.bluenimble.platform.json.JsonArray in project serverless by bluenimble.
the class DatabaseObjectImpl method toJson.
@SuppressWarnings({ "unchecked", "rawtypes" })
private JsonObject toJson(DatabaseObjectImpl dbo, DatabaseObjectSerializer serializer, int level) {
if (serializer == null) {
serializer = DatabaseObjectSerializer.Default;
}
String entity = dbo.entity();
Fields fields = serializer.fields(level);
if (fields == null || Fields.None.equals(fields)) {
return null;
}
String[] fieldNames = null;
if (Fields.All.equals(fields)) {
fieldNames = dbo.document.fieldNames();
} else {
fieldNames = MinimalFields;
}
JsonObject json = serializer.create(entity, level);
if (json == null) {
return null;
}
for (String f : fieldNames) {
Object v = dbo.get(f);
if (v == null) {
continue;
}
if (v instanceof Date) {
v = Lang.toUTC((Date) v);
} else if (v instanceof Map) {
v = new JsonObject((Map<String, Object>) v, true);
} else if (v instanceof List) {
List<Object> list = (List<Object>) v;
if (list.isEmpty()) {
continue;
}
JsonArray arr = new JsonArray();
for (Object o : list) {
if (o == null) {
continue;
}
if (o instanceof DatabaseObjectImpl) {
arr.add(toJson((DatabaseObjectImpl) o, serializer, level + 1));
} else {
if (o instanceof Date) {
arr.add(Lang.toUTC((Date) o));
} else if (Map.class.isAssignableFrom(o.getClass())) {
arr.add(new JsonObject((Map) o, true));
} else {
arr.add(o);
}
}
}
v = arr;
} else if (v instanceof DatabaseObjectImpl) {
v = toJson((DatabaseObjectImpl) v, serializer, level + 1);
}
serializer.set(entity, json, f, v);
}
return json;
}
Aggregations