Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Redundant JsonElement handling in method Record:set if pass JsonElement as value #138

Description

@donghuawei

Let say there is deepstream client and JsonElement object jsonObj, then update record with jsonObj.

 Record record = client.record.getRecord(recordName);
 record.set(jsonObj);

Please refer to following code that copied from Record.java, as we can see, record.set(jsonObj) will invoke inner private method set(String path, Object value, boolean force), In runtime the last else condition will be matched. "element = this.gson.toJsonTree(value)" will construct JsonWriter, and write value into JsonWriter, then get JsonElement from JsonWriter. But the value is already JsonElement.

Following code is copied from Record.java


    @ObjectiveCName("set:")
    public Record set(JsonElement value) throws DeepstreamRecordDestroyedException {
        return this.set((String)null, value, false);
    }


    @ObjectiveCName("set:value:force:")
    private Record set(String path, Object value, boolean force) throws DeepstreamRecordDestroyedException {
        this.throwExceptionIfDestroyed("set");
        Object element;
        if (value instanceof String) {
            element = new JsonPrimitive((String)value);
        } else if (value instanceof Number) {
            element = new JsonPrimitive((Number)value);
        } else if (value instanceof Boolean) {
            element = new JsonPrimitive((Boolean)value);
        }  else {
            element = this.gson.toJsonTree(value);
        }

        JsonElement object = this.path.get(path);
        if (!force) {
            if (object != null && object.equals(value)) {
                return this;
            }

            if (path == null && this.data.equals(value)) {
                return this;
            }
        }

        Map<String, JsonElement> oldValues = this.beginChange();
        this.path.set(path, (JsonElement)element);
        this.data = this.path.getCoreElement();
        this.sendUpdate(path, value);
        this.completeChange(oldValues);
        return this;
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions