Skip to content
This repository has been 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

Open
donghuawei opened this issue Mar 19, 2019 · 0 comments

Comments

@donghuawei
Copy link

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;
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant