GWT Deep Copy Overlay

What is the best way to make a deep copy of the gwt overlay type?

I am looking for a function or library that checks for GWT overlay and clones it. It should be able to clone the contained arrays or objects.

thank

+2
source share
3 answers

Based on the Lineman78 answer and taking this other answer from A. Levy into account , I created the following function:

public class JsoUtils {

    @SuppressWarnings("unchecked")
    public static <T extends JavaScriptObject> T deepCopy(T obj)
    {
        return (T) deepCopyImpl(obj);
    }

    private static native JavaScriptObject deepCopyImpl(JavaScriptObject obj) /*-{

        if (obj == null) return obj;

        var copy;        

        if (obj instanceof Date) {
            // Handle Date
            copy = new Date();
            copy.setTime(obj.getTime());
        } else if (obj instanceof Array) {
             // Handle Array
            copy = [];
            for (var i = 0, len = obj.length; i < len; i++) {
                if (obj[i] == null || typeof obj[i] != "object") copy[i] = obj[i];
                else copy[i] = @com.amindea.noah.client.utils.JsoUtils::deepCopyImpl(Lcom/google/gwt/core/client/JavaScriptObject;)(obj[i]);
            }
        } else {
            // Handle Object
            copy = {};
            for (var attr in obj) {
                if (obj.hasOwnProperty(attr)) {
                    if (obj[attr] == null || typeof obj[attr] != "object") copy[attr] = obj[attr];
                    else copy[attr] = @com.amindea.noah.client.utils.JsoUtils::deepCopyImpl(Lcom/google/gwt/core/client/JavaScriptObject;)(obj[attr]);
                }
            }
        }        
        return copy;
    }-*/;

} 

It supports a deep copy of Object, Array, Date, String, Number or Boolean. As explained by A. Levy, the function will work as long as the data in the objects and arrays form a tree structure.

+2
source

, . JSON, :

public native MyOverlayType deepCopy()/*-{
  return JSON.parse(JSON.stringify(this));
}-*/;

public static native MyOverlayType fromJson(String json)/*-{
      return JSON.parse(json);
}-*/;
public native String getJson()/*-{
      return JSON.stringify(this);
}-*/;
public MyOverlayType deepCopy(){
      return fromJson(getJson());
}

- javascript-, , , , .

public class JsoUtils
{
    @SuppressWarnings("unchecked")
    public static <T extends JavaScriptObject> T deepCopy(T obj)
    {
        return (T) deepCopyImpl(obj);
    }

    private static native JavaScriptObject deepCopyImpl(JavaScriptObject obj)/*-{
        if (typeof obj !== 'object' || obj === null) {
            return obj;
        }

        var c = obj instanceof Array ? [] : {};

        for (var i in obj) {
            if (obj.hasOwnProperty(i)) {
                if (typeof obj[i] !== 'object' || obj[i] === null)
                    c[i] = obj[i];
                else
                    c[i] = @com.example.gwt.client.JsoUtils::deepCopyImpl(Lcom/google/gwt/core/client/JavaScriptObject;)(obj[i]);
            }
        }

        return c;
    }-*/;
}
+5

I found the easiest way to clone JavaScriptObject using the JsonUtils class provided by GWT:

import com.google.gwt.core.client.JsonUtils;

final String taskJson = JsonUtils.stringify(selectedTask);
TaskJso task = JsonUtils.safeEval(taskJson).cast();
0
source

All Articles