Example usage for org.json JSONObject NULL

List of usage examples for org.json JSONObject NULL

Introduction

In this page you can find the example usage for org.json JSONObject NULL.

Prototype

Object NULL

To view the source code for org.json JSONObject NULL.

Click Source Link

Document

It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value.

Usage

From source file:net.dv8tion.jda.core.managers.impl.PresenceImpl.java

@Override
public void setPresence(Game game, boolean idle) {
    JSONObject gameObj = getGameJson(game);
    JSONObject object = new JSONObject();

    if (gameObj == null)
        object.put("game", JSONObject.NULL);
    else/*from www .j a  v a2  s  .  c om*/
        object.put("game", gameObj);
    object.put("afk", idle);
    object.put("since", System.currentTimeMillis());
    update(object);
    this.idle = idle;
    this.game = gameObj == null ? null : game;
}

From source file:net.dv8tion.jda.core.managers.impl.PresenceImpl.java

public JSONObject getFullPresence() {
    JSONObject game = getGameJson(this.game);
    return new JSONObject().put("afk", idle).put("since", System.currentTimeMillis())
            .put("game", game == null ? JSONObject.NULL : game).put("status", getStatus().getKey());
}

From source file:io.github.grahambell.taco.ClientTest.java

@Test
public void testBasicClient() throws TacoException {
    DummyTransport xp = (DummyTransport) this.xp;

    xp.setResponse(new JSONObject().put("action", "result").put("result", "some result"));

    callClassMethod("SomeClass", "someMethod", null, null, Context.SCALAR);

    assertThat(xp.getMessage(),/*from   w  w  w  .ja  v  a 2 s .  c  o m*/
            matchesJson(new JSONObject().put("action", "call_class_method").put("class", "SomeClass")
                    .put("name", "someMethod").put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", "scalar")));

    callClassMethod("SomeOtherClass", "someOtherMethod", null, null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_class_method").put("class", "SomeOtherClass")
                    .put("name", "someOtherMethod").put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));

    callFunction("someFunction", null, null, null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someFunction")
                    .put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));

    callFunction("someFunction", null, null, Context.SCALAR);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someFunction")
                    .put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL).put("context", "scalar")));

    callFunction("someFunction", null, null, Context.LIST);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someFunction")
                    .put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL).put("context", "list")));

    callFunction("someFunction", null, null, Context.MAP);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someFunction")
                    .put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL).put("context", "map")));

    callFunction("someFunction", null, null, Context.VOID);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someFunction")
                    .put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL).put("context", "void")));

    callFunction("someOtherFunction", null, null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someOtherFunction")
                    .put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));

    getClassAttribute("SomeClass", "SOME_ATTR");

    assertThat(xp.getMessage(), matchesJson(new JSONObject().put("action", "get_class_attribute")
            .put("class", "SomeClass").put("name", "SOME_ATTR")));

    getValue("SomeVariable");

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "get_value").put("name", "SomeVariable")));

    importModule("SomeModule", Arrays.asList("alpha", "bravo"), new HashMapC().putc("charlie", "delta"));

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "import_module").put("name", "SomeModule")
                    .put("args", new JSONArray(new String[] { "alpha", "bravo" }))
                    .put("kwargs", new JSONObject().put("charlie", "delta"))));

    setClassAttribute("AnotherClass", "ANOTHER_ATTR", "new value");

    assertThat(xp.getMessage(), matchesJson(new JSONObject().put("action", "set_class_attribute")
            .put("class", "AnotherClass").put("name", "ANOTHER_ATTR").put("value", "new value")));

    setValue("SomeValue", null);

    assertThat(xp.getMessage(), matchesJson(new JSONObject().put("action", "set_value").put("name", "SomeValue")
            .put("value", JSONObject.NULL)));
}

From source file:io.github.grahambell.taco.ClientTest.java

@Test
public void testOoClient() throws TacoException {
    DummyTransport xp = (DummyTransport) this.xp;

    xp.setResponse(//  w  ww  . j a  va 2s .  co  m
            new JSONObject().put("action", "result").put("result", new JSONObject().put("_Taco_Object_", 58)));

    Object obj = constructObject("SomeClass", Arrays.asList("juliette", "alpha"),
            new HashMapC().putc("victor", "alpha"));

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "construct_object").put("class", "SomeClass")
                    .put("args", new JSONArray(new String[] { "juliette", "alpha" }))
                    .put("kwargs", new JSONObject().put("victor", "alpha"))));

    assertEquals("<Taco object 58>", obj.toString());

    xp.setResponse(new JSONObject().put("action", "result").put("result", JSONObject.NULL));

    obj.callMethod("someMethod", null, null, null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_method").put("name", "someMethod")
                    .put("number", 58).put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));

    obj.callMethod("someOtherMethod", null, null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_method").put("name", "someOtherMethod")
                    .put("number", 58).put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));

    obj.getAttribute("someAttribute");

    assertThat(xp.getMessage(), matchesJson(
            new JSONObject().put("action", "get_attribute").put("name", "someAttribute").put("number", 58)));

    obj.setAttribute("someAttribute", "some value");

    assertThat(xp.getMessage(), matchesJson(new JSONObject().put("action", "set_attribute")
            .put("name", "someAttribute").put("number", 58).put("value", "some value")));

    callFunction("someFunction", Arrays.asList(obj), null);

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "someFunction")
                    .put("args",
                            new JSONArray(new java.lang.Object[] { new JSONObject().put("_Taco_Object_", 58) }))
                    .put("kwargs", JSONObject.NULL).put("context", JSONObject.NULL)));

    obj.finalize();

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "destroy_object").put("number", 58)));
}

From source file:io.github.grahambell.taco.ClientTest.java

@Test
public void testShortForms() throws TacoException {
    DummyTransport xp = (DummyTransport) this.xp;

    xp.setResponse(new JSONObject().put("action", "result").put("result", "some result"));

    importModule("ModuleA", Arrays.asList("opt1", "opt2"));

    assertThat(xp.getMessage(),/*from  w  ww . j av a 2  s  .  c  om*/
            matchesJson(new JSONObject().put("action", "import_module").put("name", "ModuleA")
                    .put("args", new JSONArray(new String[] { "opt1", "opt2" }))
                    .put("kwargs", JSONObject.NULL)));

    importModule("ModuleA");

    assertThat(xp.getMessage(), matchesJson(new JSONObject().put("action", "import_module")
            .put("name", "ModuleA").put("args", JSONObject.NULL).put("kwargs", JSONObject.NULL)));
}

From source file:io.github.grahambell.taco.ClientTest.java

@Test
public void testConvenienceInvocables() throws TacoException {
    DummyTransport xp = (DummyTransport) this.xp;

    xp.setResponse(/*from   www  .j a v a2 s.  com*/
            new JSONObject().put("action", "result").put("result", new JSONObject().put("_Taco_Object_", 99)));

    Constructor newObject = constructor("ObjectClass");

    Object obj = newObject.invoke("arg1", "arg2", "arg3");

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "construct_object").put("class", "ObjectClass")
                    .put("args", new JSONArray(new String[] { "arg1", "arg2", "arg3" }))
                    .put("kwargs", JSONObject.NULL)));

    assertEquals("<Taco object 99>", obj.toString());

    Object.Method method = obj.method("someMethod", Context.MAP);

    method.invoke("mike", "alpha", "papa");

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_method").put("name", "someMethod")
                    .put("number", 99).put("args", new JSONArray(new String[] { "mike", "alpha", "papa" }))
                    .put("kwargs", JSONObject.NULL).put("context", "map")));

    Object.Method nullMethod = obj.method("someNullMethod");

    nullMethod.invoke("november", "uniform", "lima", "lima");

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_method").put("name", "someNullMethod")
                    .put("number", 99)
                    .put("args", new JSONArray(new String[] { "november", "uniform", "lima", "lima" }))
                    .put("kwargs", JSONObject.NULL).put("context", JSONObject.NULL)));

    Function function = function("anotherFunction", Context.LIST);

    function.invoke("x", "y", "z");

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "anotherFunction")
                    .put("args", new JSONArray(new String[] { "x", "y", "z" })).put("kwargs", JSONObject.NULL)
                    .put("context", "list")));

    Function nullFunction = function("yetAnotherFunction");

    nullFunction.invoke("o", "p", "q");

    assertThat(xp.getMessage(),
            matchesJson(new JSONObject().put("action", "call_function").put("name", "yetAnotherFunction")
                    .put("args", new JSONArray(new String[] { "o", "p", "q" })).put("kwargs", JSONObject.NULL)
                    .put("context", JSONObject.NULL)));
}

From source file:org.everit.json.schema.NullSchema.java

@Override
public void validate(final Object subject) {
    if (!(subject == null || subject == JSONObject.NULL)) {
        throw new ValidationException("expected: null, found: " + subject.getClass().getSimpleName());
    }/*from   www . j a  v a 2  s  . co m*/
}

From source file:com.android.quicksearchbox.JsonBackedSuggestionExtras.java

public JsonBackedSuggestionExtras(SuggestionExtras extras) throws JSONException {
    mExtras = new JSONObject();
    mColumns = extras.getExtraColumnNames();
    for (String column : extras.getExtraColumnNames()) {
        String value = extras.getExtra(column);
        mExtras.put(column, value == null ? JSONObject.NULL : value);
    }//from   w  w w.  j a  v a  2 s.c  om
}

From source file:fi.elfcloud.sci.container.Vault.java

public Vault(Client client, JSONObject object) throws JSONException {
    this.client = client;
    this.id = object.getInt("id");
    this.name = object.getString("name");
    this.childCount = object.getInt("descendants");
    this.dataItemCount = object.getInt("dataitems");
    this.permissions = object.getJSONArray("permissions");
    this.last_accessed_date = (object.get("last_accessed_date") != JSONObject.NULL
            ? object.getString("last_accessed_date")
            : "");
    this.last_modified_date = (object.get("modified_date") != JSONObject.NULL
            ? object.getString("modified_date")
            : "");
    this.vaultType = (object.get("vault_type") != JSONObject.NULL ? object.getString("vault_type") : "");
    this.owner = new User(object.getJSONObject("owner"));
}

From source file:net.dv8tion.jda.core.managers.WebhookManagerUpdatable.java

/**
 * Creates a new {@link net.dv8tion.jda.core.requests.RestAction RestAction} instance
 * that will apply <b>all</b> changes that have been made to this manager instance.
 * <br>If no changes have been made this will simply return {@link net.dv8tion.jda.core.requests.RestAction.EmptyRestAction EmptyRestAction}.
 *
 * <p>Before applying new changes it is recommended to call {@link #reset()} to reset previous changes.
 * <br>This is automatically called if this method returns successfully.
 *
 * <p>Possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses} for this
 * update include the following://w  ww  .j  ava  2  s.  co m
 * <ul>
 *      <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#UNKNOWN_CHANNEL UNKNOWN_CHANNEL}
 *      <br>If the TextChannel was deleted before finishing the task</li>
 *
 *      <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
 *      <br>If the currently logged in account was removed from the Guild before finishing the task</li>
 *
 *      <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
 *      <br>If the currently logged in account loses the {@link net.dv8tion.jda.core.Permission#MANAGE_WEBHOOKS MANAGE_WEBHOOKS Permission}</li>
 * </ul>
 *
 * @throws net.dv8tion.jda.core.exceptions.PermissionException
 *         If the currently logged in account does not have the Permission {@link net.dv8tion.jda.core.Permission#MANAGE_WEBHOOKS MANAGE_WEBHOOKS}
 *         in either the current or selected new TextChannel.
 *
 * @return {@link net.dv8tion.jda.core.requests.restaction.AuditableRestAction AuditableRestAction}
 *         <br>Applies all changes that have been made in a single api-call.
 */
@CheckReturnValue
public AuditableRestAction<Void> update() {
    Member self = getGuild().getSelfMember();
    if (!self.hasPermission(webhook.getChannel(), Permission.MANAGE_WEBHOOKS))
        throw new PermissionException(Permission.MANAGE_WEBHOOKS);
    if (channel.isSet() && !self.hasPermission(channel.getValue(), Permission.MANAGE_WEBHOOKS))
        throw new PermissionException(Permission.MANAGE_WEBHOOKS,
                "Permission not available in selected new channel");
    if (!shouldUpdate())
        return new AuditableRestAction.EmptyRestAction<>(getJDA(), null);

    JSONObject data = new JSONObject();
    data.put("name", name.getOriginalValue());

    if (channel.shouldUpdate())
        data.put("channel_id", channel.getValue().getId());
    if (name.shouldUpdate())
        data.put("name", name.getValue());
    if (avatar.shouldUpdate()) {
        Icon value = avatar.getValue();
        data.put("avatar", value != null ? value.getEncoding() : JSONObject.NULL);
    }

    Route.CompiledRoute route = Route.Webhooks.MODIFY_WEBHOOK.compile(webhook.getId());
    return new AuditableRestAction<Void>(getJDA(), route, data) {
        @Override
        protected void handleResponse(Response response, Request<Void> request) {
            if (response.isOk())
                request.onSuccess(null);
            else
                request.onFailure(response);
        }
    };
}