Search in sources :

Example 11 with JsonArray

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;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 12 with JsonArray

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));
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 13 with JsonArray

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);
    }
}
Also used : AbstractEmitter(com.bluenimble.platform.json.AbstractEmitter) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException) MapScriptContext(com.bluenimble.platform.scripting.MapScriptContext) JsonArray(com.bluenimble.platform.json.JsonArray) JsonEmitter(com.bluenimble.platform.json.JsonEmitter) JsonObject(com.bluenimble.platform.json.JsonObject) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 14 with JsonArray

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;
}
Also used : Status(com.bluenimble.platform.api.ApiResponse.Status) JsonArray(com.bluenimble.platform.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 15 with JsonArray

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;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Fields(com.bluenimble.platform.db.DatabaseObjectSerializer.Fields) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Date(java.util.Date)

Aggregations

JsonArray (com.bluenimble.platform.json.JsonArray)42 JsonObject (com.bluenimble.platform.json.JsonObject)37 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 Map (java.util.Map)8 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)5 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)5 Database (com.bluenimble.platform.db.Database)5 IOException (java.io.IOException)5 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)4 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)4 ApiService (com.bluenimble.platform.api.ApiService)3 DescribeOption (com.bluenimble.platform.api.DescribeOption)3 DatabaseException (com.bluenimble.platform.db.DatabaseException)3 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)3 KeyPair (com.bluenimble.platform.security.KeyPair)3 ArrayList (java.util.ArrayList)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)2