[OpenLayers-Commits] r4757 - in sandbox/context: examples lib lib/OpenLayers lib/OpenLayers/BaseTypes lib/OpenLayers/Format lib/OpenLayers/Layer tests/BaseTypes

commits at openlayers.org commits at openlayers.org
Tue Oct 2 17:22:43 EDT 2007


Author: tschaub
Date: 2007-10-02 17:22:42 -0400 (Tue, 02 Oct 2007)
New Revision: 4757

Added:
   sandbox/context/lib/OpenLayers/Format/OLON.js
Modified:
   sandbox/context/examples/context.html
   sandbox/context/lib/OpenLayers.js
   sandbox/context/lib/OpenLayers/BaseTypes/Bounds.js
   sandbox/context/lib/OpenLayers/BaseTypes/Class.js
   sandbox/context/lib/OpenLayers/BaseTypes/LonLat.js
   sandbox/context/lib/OpenLayers/BaseTypes/Size.js
   sandbox/context/lib/OpenLayers/Layer.js
   sandbox/context/lib/OpenLayers/Layer/Google.js
   sandbox/context/lib/OpenLayers/Layer/Grid.js
   sandbox/context/lib/OpenLayers/Layer/HTTPRequest.js
   sandbox/context/lib/OpenLayers/Layer/Image.js
   sandbox/context/lib/OpenLayers/Layer/Vector.js
   sandbox/context/lib/OpenLayers/Layer/WFS.js
   sandbox/context/lib/OpenLayers/Layer/WMS.js
   sandbox/context/lib/OpenLayers/Map.js
   sandbox/context/lib/OpenLayers/Util.js
   sandbox/context/tests/BaseTypes/test_Class.html
Log:
serialize/deserialize all instances of OpenLayers classes

Modified: sandbox/context/examples/context.html
===================================================================
--- sandbox/context/examples/context.html	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/examples/context.html	2007-10-02 21:22:42 UTC (rev 4757)
@@ -16,6 +16,7 @@
             height: 512px;
         }
     </style>
+    <script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAjpkAC9ePGem0lIq5XcMiuhR_wWLPFku8Ix9i2SXYRVK3e45q1BQUd_beF8dtzKET_EteAjPdGDwqpQ'></script>
     <script src="../lib/OpenLayers.js"></script>
     <script type="text/javascript">
         var lon = 5;
@@ -24,32 +25,82 @@
         var map, layer, graphic;
 
         function init(){
-            map = new OpenLayers.Map('map');
-            var options = {numZoomLevels: 3};
-            
-            layer = new OpenLayers.Layer.WMS(
-                "OpenLayers WMS", 
-                "http://labs.metacarta.com/wms/vmap0",
-                {layers: 'basic'},
-                {maxExtent: new OpenLayers.Bounds(-100, -80, 100, 80)}
+            var options = {
+                projection: "EPSG:900913",
+                units: "m",
+                maxResolution: 156543.0339,
+                maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
+                                                 20037508, 20037508.34)
+            };
+            map = new OpenLayers.Map('map', options);
+
+            // create Google Mercator layers
+            var gmap = new OpenLayers.Layer.Google(
+                "Google Streets",
+                {'sphericalMercator': true}
             );
-            graphic = new OpenLayers.Layer.Image(
-                'City Lights',
-                'http://earthtrends.wri.org/images/maps/4_m_citylights_lg.gif',
-                new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
-                new OpenLayers.Size(580, 288),
-                options
+            //var gsat = new OpenLayers.Layer.Google(
+            //    "Google Sattelite",
+            //    {type: G_SATELLITE_MAP, 'sphericalMercator': true}
+            //);
+            //var ghyb = new OpenLayers.Layer.Google(
+            //    "Google Hybrid",
+            //    {type: G_HYBRID_MAP, 'sphericalMercator': true}
+            //);
+            //
+            //// create Virtual Earth layers
+            //var veroad = new OpenLayers.Layer.VirtualEarth(
+            //    "Virtual Earth Raods",
+            //    {'type': VEMapStyle.Road, 'sphericalMercator': true}
+            //); 
+            //var veaer = new OpenLayers.Layer.VirtualEarth(
+            //    "Virtual Earth Aerial",
+            //    {'type': VEMapStyle.Aerial, 'sphericalMercator': true}
+            //); 
+            //var vehyb = new OpenLayers.Layer.VirtualEarth(
+            //    "Virtual Earth Hybrid",
+            //    {'type': VEMapStyle.Hybrid, 'sphericalMercator': true}
+            //); 
+            //
+            //// create Yahoo layer
+            //var yahoo = new OpenLayers.Layer.Yahoo(
+            //    "Yahoo Street",
+            //    {'sphericalMercator': true}
+            //);
+            //var yahoosat = new OpenLayers.Layer.Yahoo(
+            //    "Yahoo Sattelite",
+            //    {'type': YAHOO_MAP_SAT, 'sphericalMercator': true}
+            //);
+            //var yahoohyb = new OpenLayers.Layer.Yahoo(
+            //    "Yahoo Hybrid",
+            //    {'type': YAHOO_MAP_HYB, 'sphericalMercator': true}
+            //);
+
+            // create WMS layer
+            var wms = new OpenLayers.Layer.WMS(
+                "World Map",
+                "http://world.freemap.in/tiles/",
+                {'layers': 'factbook-overlay', 'format':'png'},
+                {
+                    'reproject': false, 'opacity': 0.4,
+                    'isBaseLayer': false,'wrapDateLine': true
+                }
             );
-            map.addLayers([layer, graphic]);
 
-            map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
-            map.addControl( new OpenLayers.Control.LayerSwitcher() );
+            // create a vector layer for drawing
+            var vector = new OpenLayers.Layer.Vector("Editable Vectors");
+            
+            map.addLayers([gmap, wms, vector]);
+            //map.addLayers([gmap, gsat, ghyb, veroad, veaer, vehyb,
+            //               yahoo, yahoosat, yahoohyb, wms]);
+            map.addControl(new OpenLayers.Control.LayerSwitcher());
+            map.addControl(new OpenLayers.Control.EditingToolbar(vector));
+            map.zoomToMaxExtent()
         }
         
-        var json = new OpenLayers.Format.JSON();
+        var olon = new OpenLayers.Format.OLON();
         function serialize() {
-            var data = map.serialize();
-            var str = json.write(data, true);
+            var str = olon.write(map, true);
             var element = document.getElementById('output');
             element.value = str;
         }
@@ -62,8 +113,7 @@
             clear();
             var element = document.getElementById('output');
             var str = element.value;
-            var data = json.read(str);
-            var obj = OpenLayers.Class.deserialize(data);
+            var obj = olon.read(str);
             map.addLayers(obj.layers);
         }
     </script>

Modified: sandbox/context/lib/OpenLayers/BaseTypes/Bounds.js
===================================================================
--- sandbox/context/lib/OpenLayers/BaseTypes/Bounds.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/BaseTypes/Bounds.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -41,6 +41,14 @@
     top: null,    
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "args": ["left", "bottom", "right", "top"]
+    },
+
+    /**
      * Constructor: OpenLayers.Bounds
      * Construct a new bounds object.
      *
@@ -553,11 +561,3 @@
     
     return opp;
 };
-
-/**
- * APIProperty: OpenLayers.Bounds.serializable
- * {Object}
- */
-OpenLayers.Bounds.serializable = {
-    "args": ["left", "bottom", "right", "top"]
-};

Modified: sandbox/context/lib/OpenLayers/BaseTypes/Class.js
===================================================================
--- sandbox/context/lib/OpenLayers/BaseTypes/Class.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/BaseTypes/Class.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -37,11 +37,11 @@
     // All instances of OpenLayers classes inherit from the extended prototype.    
     var extended = {
         /**
-         * APIMethod: Class.serialize
+         * APIMethod: Class.prototype.serialize
          * This is an instance method that returns a data structure representing
-         *     the instance.  This data structure can be passed to the appropriate
-         *     deserialize class method to return a new instance which is the
-         *     equivalent of a clone of this instance.
+         *     the instance.  This data structure can be passed to the
+         *     appropriate deserialize class method to return a new instance
+         *     which is the equivalent of a clone of this instance.
          *
          * Returns:
          * {Object} A data structure representing the instance.
@@ -49,102 +49,224 @@
         serialize: function() {
             var data = {};
             var olClass = eval(this.CLASS_NAME);
-            var obj = olClass.serializable;
+            var obj = olClass.prototype.serializable;
             // serialize the args and props
-            var array, index;
-            for(var key in obj) {
-                array = obj[key];
-                for(index=0; index<array.length; ++index) {
-                    data[array[index]] = OpenLayers.Util.serialize(this[array[index]]);
-                }
+            var index;
+            var args = obj.args;
+            for(index=0; index<args.length; ++index) {
+                data[args[index]] = OpenLayers.Class.serialize(this[args[index]]);
             }
+            var props = obj.props;
+            for(index=0; index<props.length; ++index) {
+                data[props[index]] = OpenLayers.Class.serialize(this[props[index]]);
+            }
             // add in the class name
             data.CLASS_NAME = this.CLASS_NAME;
             return data;
         }
     };
+    
+    // get the serializable object on the prototype
+    var proto = arguments[arguments.length - 1];
+    /**
+     * Property: Class.prototype.serializable
+     * {Object} Object that maps serializalble properties of an instance
+     *     of this class.
+     *
+     * The serializable object has three properties:
+     * args - {Array} Property names to be applied to the constructor.
+     *     All required argument names must be supplied here.  There is
+     *     no inheritance of the args array from any parent classes.
+     * props - {Array} All additional properties to set on the new
+     *     instance.  These will be set by direct assignment.  The props
+     *     array inherits from its parent by merging arrays.  Any new
+     *     properties specified in a child props array will be added
+     *     to the properties inherited from a parent class.
+     * except - {Array}  List of properties from a parent class' props
+     *     array that will not be merged with the props array of a child
+     *     class.
+     */
+    var serializable = {
+        "args": (proto.serializable && proto.serializable.args) ?
+            proto.serializable.args : [],
+        "props": (proto.serializable && proto.serializable.props) ?
+            proto.serializable.props : [],
+        "except": (proto.serializable && proto.serializable.except) ?
+            proto.serializable.except : []
+    };
 
-    var parent;
+    // inherit from all arguments
+    var parent = {};
     for(var i=0; i<arguments.length; ++i) {
         if(typeof arguments[i] == "function") {
             // get the prototype of the superclass
             parent = arguments[i].prototype;
+            // All OpenLayers classes are serializable.
+            // Inheriting from others still works.
+            if(parent.serializable) {
+                // props are merged with the parent
+                var prop;
+                var indexOf = OpenLayers.Util.indexOf;
+                for(var j=0; j<parent.serializable.props.length; ++j) {
+                    prop = parent.serializable.props[j];
+                    if(indexOf(serializable.except, prop) == -1) {
+                        if(indexOf(serializable.props, prop) == -1) {
+                            serializable.props.push(prop);
+                        }
+                    }
+                }
+                // args are inherited only if not specified in the prototype
+                if(!serializable.args || serializable.args.length == 0) {
+                    serializable.args = parent.serializable.args;
+                }
+            }
         } else {
             // in this case we're extending with the prototype
             parent = arguments[i];
         }
         OpenLayers.Util.extend(extended, parent);
     }
-    Class.prototype = extended;
-    
+    extended.serializable = serializable;
+
+    Class.prototype = extended;    
     return Class;
 }
 
+/*
+ * APIProperty: OpenLayers.Class.recursionDepth
+ * Class property that limits the recursion depth for serializing instances
+ *     of OpenLayers classes.  Defaults to 5.
+ */
+OpenLayers.Class.recursionDepth = 5;
+
 /**
- * APIMethod: OpenLayers.Class.deserialize
+ * APIFunction: OpenLayers.Class.serialize
+ * Recursively serialize an object.  Stops recursion at a depth of
+ *     <OpenLayers.Class.recursionDepth>.
  *
  * Parameters:
+ * obj - {Object} An object to be serialized.
+ *
+ * Returns:
+ * {Object} An object without circular references or methods.
+ */
+OpenLayers.Class.serialize = function(obj, depth) {
+    var data;
+    depth = (depth != undefined) ? depth + 1 : 0;
+    if(!(obj instanceof Function)) {
+        if(depth <= OpenLayers.Class.recursionDepth) { 
+            if(obj && obj.serialize instanceof Function) {
+                data = obj.serialize();
+            } else {
+                if(obj instanceof Array) {
+                    data = new Array(obj.length);
+                    for(var i=0; i<obj.length; ++i) {
+                        data[i] = OpenLayers.Class.serialize(obj[i], depth);
+                    }
+                } else if(obj instanceof Object) {
+                    data = {};
+                    var value;
+                    for(var key in obj) {
+                        value = OpenLayers.Class.serialize(obj[key], depth);
+                        if(value !== undefined) {
+                            data[key] = OpenLayers.Class.serialize(obj[key], depth);
+                        }
+                    }
+                } else {
+                    data = obj;
+                }
+            }
+        }
+    }
+    return data;
+};
+
+/**
+ * APIFunction: OpenLayers.Class.deserialize
+ * Class method that deserializes any data structure into an instance of
+ *     an OpenLayers class.  In the case of arrays and objects, an object
+ *     with the same data structure is returned, with each item or proerty
+ *     value deserialized.
+ *
+ * Parameters:
  * struct - {Object} Data structure representing an object.
  * 
  * Returns:
- * {Object} An object based on the data structure.
+ * {Object} An instance of an OpenLayers class based on the data structure.
  */
-OpenLayers.Class.deserialize = function(struct) {
+OpenLayers.Class.deserialize = function(struct, depth) {
     var instance, index, key;
-    if(!struct) {
-        instance = struct;
-    } else if(!struct.CLASS_NAME ||
-              struct.CLASS_NAME.indexOf("OpenLayers") != 0) {
-        if(struct instanceof Array) {
-            instance = new Array(struct.length);
-            for(index=0; index<struct.length; ++index) {
-                instance[index] = OpenLayers.Class.deserialize(struct[index]);
+    depth = (depth != undefined) ? depth + 1 : 1;
+    if(struct instanceof Function) {
+        var msg = "Cannot serialize functions";
+        OpenLayers.Console.error(msg);
+    } else if(depth <= OpenLayers.Class.recursionDepth) {
+        if(!struct) {
+            // deal with null and all else that evaluates to false
+            instance = struct;
+        } else if(!struct.CLASS_NAME ||
+                  struct.CLASS_NAME.indexOf("OpenLayers") != 0) {
+            if(struct instanceof Array) {
+                // deserialize items in an array
+                instance = new Array(struct.length);
+                for(index=0; index<struct.length; ++index) {
+                    instance[index] = OpenLayers.Class.deserialize(
+                        struct[index], depth
+                    );
+                }
+            } else if(struct instanceof Object) {
+                // deserialize properties of an object
+                instance = {};
+                for(key in struct) {
+                    instance[key] = OpenLayers.Class.deserialize(
+                        struct[key], depth
+                    );
+                }
+            } else {
+                // all other literals
+                instance = struct;
             }
-        } else if(struct instanceof Object) {
-            instance = {};
-            for(key in struct) {
-                instance[key] = OpenLayers.Class.deserialize(struct[key]);
+        } else {
+            // deal with instances of OpenLayers classes
+            var olClass = eval(struct.CLASS_NAME);
+            if(!olClass.prototype.serializable) {
+                var msg = "Unable to deserialize.  Bad CLASS_NAME: " +
+                    struct.CLASS_NAME;
+                throw Error(msg);
             }
-        } else {
-            instance = struct;
-        }
-    } else {
-        var olClass = eval(struct.CLASS_NAME);
-        if(!olClass.serializable) {
-            var msg = "Unable to deserialize.  Bad CLASS_NAME: " +
-                      struct.CLASS_NAME;
-            throw msg;
-        }
-        // deserialize the args
-        var argArray = olClass.serializable.args;
-        var args = [];
-        if(argArray) {
-            for(index=0; index<argArray.length; ++index) {
-                key = argArray[index];
-                if(struct[key] === undefined) {
-                    var msg = "Deserializing failed.  " + struct.CLASS_NAME + 
-                              " requires argument: " + key;
-                    throw msg;
+            // deserialize the args
+            var argArray = olClass.prototype.serializable.args;
+            var args = [];
+            if(argArray) {
+                for(index=0; index<argArray.length; ++index) {
+                    key = argArray[index];
+                    if(struct[key] === undefined) {
+                        var msg = "Deserializing failed.  " +
+                            struct.CLASS_NAME + " requires argument: " + key;
+                        throw Error(msg);
+                    }
+                    args.push(OpenLayers.Class.deserialize(struct[key], depth));
                 }
-                args.push(OpenLayers.Class.deserialize(struct[key]));
             }
-        }
-        // create a prototype with properties deserialized from the structure
-        var proto = {};
-        var propArray = olClass.serializable.props;
-        if(propArray) {
-            for(index=0; index<propArray.length; ++index) {
-                key = propArray[index];
-                proto[key] = OpenLayers.Class.deserialize(struct[key]);
+            // create prototype with properties deserialized from the structure
+            var proto = {};
+            var propArray = olClass.prototype.serializable.props;
+            if(propArray) {
+                for(index=0; index<propArray.length; ++index) {
+                    key = propArray[index];
+                    proto[key] = OpenLayers.Class.deserialize(
+                        struct[key], depth
+                    );
+                }
             }
+            // construct our instance (except for maps at this point)
+            if(struct.CLASS_NAME == "OpenLayers.Map") {
+                instance = proto;
+            } else {
+                OpenLayers.Util.applyDefaults(proto, olClass.prototype);
+                instance = olClass.apply(proto, args);
+            }
         }
-        // construct our instance (except for maps at this point)
-        if(struct.CLASS_NAME == "OpenLayers.Map") {
-            instance = proto;
-        } else {
-            OpenLayers.Util.applyDefaults(proto, olClass.prototype);
-            instance = olClass.apply(proto, args);
-        }
     }
     return instance;
 };

Modified: sandbox/context/lib/OpenLayers/BaseTypes/LonLat.js
===================================================================
--- sandbox/context/lib/OpenLayers/BaseTypes/LonLat.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/BaseTypes/LonLat.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -21,6 +21,14 @@
     lat: 0.0,
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "args": ["lon", "lat"]
+    },
+
+    /**
      * Constructor: OpenLayers.LonLat
      * Create a new map location.
      *
@@ -164,11 +172,3 @@
     return new OpenLayers.LonLat(parseFloat(pair[0]), 
                                  parseFloat(pair[1]));
 };
-
-/**
- * APIProperty: OpenLayers.LonLat.serializable
- * {Object}
- */
-OpenLayers.LonLat.serializable = {
-    "args": ["lon", "lat"]
-};
\ No newline at end of file

Modified: sandbox/context/lib/OpenLayers/BaseTypes/Size.js
===================================================================
--- sandbox/context/lib/OpenLayers/BaseTypes/Size.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/BaseTypes/Size.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -20,6 +20,13 @@
      */
     h: 0.0,
 
+    /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "args": ["w", "h"]
+    },
 
     /**
      * Constructor: OpenLayers.Size
@@ -82,11 +89,3 @@
 
     CLASS_NAME: "OpenLayers.Size"
 });
-
-/**
- * APIProperty: OpenLayers.Size.serializable
- * {Object}
- */
-OpenLayers.Size.serializable = {
-    "args": ["w", "h"]
-};
\ No newline at end of file

Added: sandbox/context/lib/OpenLayers/Format/OLON.js
===================================================================
--- sandbox/context/lib/OpenLayers/Format/OLON.js	                        (rev 0)
+++ sandbox/context/lib/OpenLayers/Format/OLON.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -0,0 +1,78 @@
+/* Copyright (c) 2006-2007 MetaCarta, Inc., published under a modified BSD license.
+ * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
+ * for the full text of the license. */
+
+/**
+ * Note:
+ * This work draws heavily from the public domain JSON serializer/deserializer
+ *     at http://www.json.org/json.js. Rewritten so that it doesn't modify
+ *     basic data prototypes.
+ */
+
+/**
+ * @requires OpenLayers/Format/JSON.js
+ *
+ * Class: OpenLayers.Format.OLON
+ * A parser to read/write OLON safely.  Create a new instance with the
+ *     <OpenLayers.Format.OLON> constructor.
+ *
+ * Inherits from:
+ *  - <OpenLayers.Format.JSON>
+ */
+OpenLayers.Format.OLON = OpenLayers.Class(OpenLayers.Format.JSON, {
+    
+    /**
+     * Constructor: OpenLayers.Format.OLON
+     * Create a new parser for OLON.
+     *
+     * Parameters:
+     * options - {Object} An optional object whose properties will be set on
+     *     this instance.
+     */
+    initialize: function(options) {
+        OpenLayers.Format.JSON.prototype.initialize.apply(this, [options]);
+    },
+
+    /**
+     * APIMethod: read
+     * Deserialize an OpenLayers JSON string.
+     *
+     * Parameters:
+     * str - {String} An OLON string
+     * filter - {Function} A function which will be called for every key and
+     *     value at every level of the final result. Each value will be
+     *     replaced by the result of the filter function. This can be used to
+     *     reform generic objects into instances of classes, or to transform
+     *     date strings into Date objects.
+     *     
+     * Returns:
+     * {Object} An object, array, string, or number.
+     */
+    read: function(str, filter) {
+        var data = OpenLayers.Format.JSON.prototype.read(str, filter);
+        var instance = OpenLayers.Class.deserialize(data);
+        return instance;
+    },
+
+    /**
+     * APIMethod: write
+     * Serialize an object into an OLON string.
+     *
+     * Parameters:
+     * value - {String} The object, array, string, number, boolean or date
+     *     to be serialized.
+     * pretty - {Boolean} Structure the output with newlines and indentation.
+     *     Default is false.
+     *
+     * Returns:
+     * {String} The OpenLayers JSON string representation of the input value.
+     */
+    write: function(value, pretty) {
+        var data = OpenLayers.Class.serialize(value);
+        var str = OpenLayers.Format.JSON.prototype.write(data, pretty);
+        return str;
+    },
+    
+    CLASS_NAME: "OpenLayers.Format.OLON" 
+
+});     

Modified: sandbox/context/lib/OpenLayers/Layer/Google.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer/Google.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer/Google.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -73,6 +73,13 @@
      */
     sphericalMercator: false, 
 
+    /**
+     * Property: serializable
+     */
+    serializable: {
+        "props": ["sphericalMercator"]
+    },
+
     /** 
      * Constructor: OpenLayers.Layer.Google
      * 
@@ -504,15 +511,3 @@
 
     CLASS_NAME: "OpenLayers.Layer.Google"
 });
-
-/**
- * APIProperty: OpenLayers.Layer.Google.serializable
- * {Object}
- */
-OpenLayers.Layer.Google.serializable = {
-    "args": ["name"],
-    "props": [
-        "sphericalMercator",
-        "isBaseLayer"
-    ]
-};
\ No newline at end of file

Modified: sandbox/context/lib/OpenLayers/Layer/Grid.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer/Grid.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer/Grid.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -58,6 +58,14 @@
     numLoadingTiles: 0,
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "props": ["singleTile"]
+    },
+
+    /**
      * Constructor: OpenLayers.Layer.Grid
      * Create a new grid layer
      *

Modified: sandbox/context/lib/OpenLayers/Layer/HTTPRequest.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer/HTTPRequest.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer/HTTPRequest.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -46,6 +46,14 @@
     reproject: false,
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "args": ["name", "url", "params"]
+    },
+
+    /**
      * Constructor: OpenLayers.Layer.HTTPRequest
      * 
      * Parameters:

Modified: sandbox/context/lib/OpenLayers/Layer/Image.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer/Image.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer/Image.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -52,6 +52,14 @@
     aspectRatio: null,
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "args": ["name", "url", "extent", "size"]
+    },
+
+    /**
      * Constructor: OpenLayers.Layer.Image
      * Create a new image layer
      *
@@ -204,16 +212,3 @@
 
     CLASS_NAME: "OpenLayers.Layer.Image"
 });
-
-/**
- * APIProperty: OpenLayers.Image.serializable
- * {Object}
- */
-OpenLayers.Layer.Image.serializable = {
-    "args": ["name", "url", "extent", "size"],
-    "props": [
-        "maxExtent",
-        "maxResolution",
-        "numZoomLevels"
-    ]
-};
\ No newline at end of file

Modified: sandbox/context/lib/OpenLayers/Layer/Vector.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer/Vector.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer/Vector.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -92,6 +92,14 @@
     drawn: false,
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "args": ["name"]
+    },
+
+    /**
      * Constructor: OpenLayers.Layer.Vector
      * Create a new vector layer
      *
@@ -435,12 +443,3 @@
 
     CLASS_NAME: "OpenLayers.Layer.Vector"
 });
-
-/**
- * APIProperty: OpenLayers.Layer.Vector.serializable
- * {Object}
- */
-OpenLayers.Layer.Vector.serializable = {
-    "args": ["name"],
-    "props": ["isBaseLayer"]
-};
\ No newline at end of file

Modified: sandbox/context/lib/OpenLayers/Layer/WFS.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer/WFS.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer/WFS.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -75,6 +75,15 @@
     extractAttributes: false,
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "args": ["name", "url", "params"],
+        "props": ["extractAttributes", "vectorMode"]
+    },
+
+    /**
      * Constructor: OpenLayers.Layer.WFS
      *
      * Parameters:
@@ -479,18 +488,3 @@
 
     CLASS_NAME: "OpenLayers.Layer.WFS"
 });
-
-/**
- * APIProperty: OpenLayers.Layer.WMS.serializable
- * {Object}
- */
-OpenLayers.Layer.WMS.serializable = {
-    "args": ["name", "url", "params"],
-    "props": [
-        "vectorMode",
-        "maxExtent",
-        "maxResolution",
-        "numZoomLevels",
-        "isBaseLayer"
-    ]
-};
\ No newline at end of file

Modified: sandbox/context/lib/OpenLayers/Layer/WMS.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer/WMS.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer/WMS.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -52,7 +52,7 @@
      * but some services want it that way. Default false.
      */
     encodeBBOX: false,
- 
+
     /**
      * Constructor: OpenLayers.Layer.WMS
      * Create a new WMS layer object
@@ -216,18 +216,3 @@
 
     CLASS_NAME: "OpenLayers.Layer.WMS"
 });
-
-/**
- * APIProperty: OpenLayers.Layer.WMS.serializable
- * {Object}
- */
-OpenLayers.Layer.WMS.serializable = {
-    "args": ["name", "url", "params"],
-    "props": [
-        "maxExtent",
-        "maxResolution",
-        "numZoomLevels",
-        "isBaseLayer",
-        "singleTile"
-    ]
-};

Modified: sandbox/context/lib/OpenLayers/Layer.js
===================================================================
--- sandbox/context/lib/OpenLayers/Layer.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Layer.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -220,6 +220,15 @@
      */
     wrapDateLine: false,
     
+    /**
+     * Property: serializable
+     */
+    serializable: {
+        "args": ["name"],
+        "props": ["numZoomLevels", "maxResolution", "minResolution",
+                  "maxExtent", "wrapDateLine", "displayOutsideMaxExtent",
+                  "isBaseLayer", "displayInLayerSwitcher", "opacity"]
+    },
     
     /**
      * Constructor: OpenLayers.Layer

Modified: sandbox/context/lib/OpenLayers/Map.js
===================================================================
--- sandbox/context/lib/OpenLayers/Map.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Map.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -248,6 +248,14 @@
     fallThrough: false,
 
     /**
+     * Property: serializable
+     * {Object}
+     */
+    serializable: {
+        "props": ["layers", "center", "resolution"]
+    },
+    
+    /**
      * Constructor: OpenLayers.Map
      * Constructor for a new OpenLayers.Map instance.
      *
@@ -1689,11 +1697,3 @@
  * {Integer} 256 Default tile height (unless otherwise specified)
  */
 OpenLayers.Map.TILE_HEIGHT = 256;
-
-/**
- * APIProperty: OpenLayers.Map.serializable
- * {Object}
- */
-OpenLayers.Map.serializable = {
-    "props": ["layers", "center", "resolution"]
-};

Modified: sandbox/context/lib/OpenLayers/Util.js
===================================================================
--- sandbox/context/lib/OpenLayers/Util.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers/Util.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -1285,38 +1285,3 @@
     return browserName;
 };
 
-/**
- * Function: OpenLayers.Util.serialize
- * Recursively serialize an object.  Stops recursion 
- *
- * Parameters:
- * obj - {Object}
- * depth - {Integer}
- *
- * Returns:
- * {Object} An object without circular references or methods.
- */
-OpenLayers.Util.serialize = function(obj, depth) {
-    var out = {};
-    depth = (depth) ? depth + 1 : 1;
-    if(depth < 5) { 
-        if(obj && typeof obj.serialize == "function") {
-            out = obj.serialize();
-        } else {
-            if(obj instanceof Array) {
-                out = new Array(obj.length);
-                for(var i=0; i<obj.length; ++i) {
-                    out[i] = OpenLayers.Util.serialize(obj[i], depth);
-                }
-            } else if(obj instanceof Object) {
-                out = {};
-                for(var key in obj) {
-                    out[key] = OpenLayers.Util.serialize(obj[key], depth);
-                }
-            } else {
-                out = obj;
-            }
-        }
-    }
-    return out;
-};
\ No newline at end of file

Modified: sandbox/context/lib/OpenLayers.js
===================================================================
--- sandbox/context/lib/OpenLayers.js	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/lib/OpenLayers.js	2007-10-02 21:22:42 UTC (rev 4757)
@@ -172,6 +172,7 @@
             "OpenLayers/Format/WKT.js",
             "OpenLayers/Format/JSON.js",
             "OpenLayers/Format/GeoJSON.js",
+            "OpenLayers/Format/OLON.js",
             "OpenLayers/Layer/WFS.js",
             "OpenLayers/Control/MouseToolbar.js",
             "OpenLayers/Control/NavToolbar.js",

Modified: sandbox/context/tests/BaseTypes/test_Class.html
===================================================================
--- sandbox/context/tests/BaseTypes/test_Class.html	2007-10-02 17:59:52 UTC (rev 4756)
+++ sandbox/context/tests/BaseTypes/test_Class.html	2007-10-02 21:22:42 UTC (rev 4757)
@@ -262,8 +262,129 @@
         }
         t.eq( objC.mixed, true, "class C has mixin properties" );
     }
+    
+    function test_OpenLayers_Class_serialize(t) {
+        t.plan(11);
+        
+        // test literals
+        var str = "foo";
+        var num = Math.random();
+        var bool = Math.random() > 0.5;
+        var nul = null;
+        var undef = undefined;
+        
+        t.eq(OpenLayers.Class.serialize(str), str, "string serializes");
+        t.eq(OpenLayers.Class.serialize(num), num, "number serializes");
+        t.eq(OpenLayers.Class.serialize(bool), bool, "boolean serializes");
+        t.eq(OpenLayers.Class.serialize(nul), nul, "null serializes");
+        t.eq(OpenLayers.Class.serialize(undef), undef, "undefined serializes");
+        
+        // test objects
+        var obj = {str: str, num: num, bool: bool, nul: nul};
+        var arr = [str, num, bool, nul, undef];
+        var date = new Date();
+        
+        t.eq(OpenLayers.Class.serialize(obj), obj, "object serializes");
+        t.eq(OpenLayers.Class.serialize(arr), arr, "array serializes");
+        t.eq(OpenLayers.Class.serialize(date), {}, "date doesn't serialize");
+        
+        // test functions
+        var func = function() {};
+        var foo = {
+            bar: func
+        };
+        t.eq(OpenLayers.Class.serialize(func), undefined,
+             "returns undefined for serialized function");
+        t.eq(OpenLayers.Class.serialize(foo).bar, undefined,
+             "leaves undefined properties whose values are functions");
 
+        // test recursion
+        var foo = {};
+        var bar = {foo: foo};
+        foo.bar = bar;
+        var serialized = OpenLayers.Class.serialize(foo);
+        var stop = OpenLayers.Class.recursionDepth;
+        var depth = 0;
+        var key;
+        var deeper = true;
+        while(deeper) {
+            deeper = false;
+            for(key in serialized) {
+                serialized = serialized[key];
+                deeper = true;
+                ++depth;
+                break;
+            }
+            // don't count full depth in case recursion is infinite
+            if(depth > stop + 5) {
+                break;
+            }
+        }
+        t.eq(depth, stop, "serialize respects OpenLayers.Class.recursionDepth");
+        
+    }
 
+    function test_OpenLayers_Class_deserialize(t) {
+        t.plan(11);
+        
+        // test literals
+        var str = "foo";
+        var num = Math.random();
+        var bool = Math.random() > 0.5;
+        var nul = null;
+        var undef = undefined;
+        
+        t.eq(OpenLayers.Class.deserialize(str), str, "string deserializes");
+        t.eq(OpenLayers.Class.deserialize(num), num, "number deserializes");
+        t.eq(OpenLayers.Class.deserialize(bool), bool, "boolean deserializes");
+        t.eq(OpenLayers.Class.deserialize(nul), nul, "null deserializes");
+        t.eq(OpenLayers.Class.deserialize(undef), undef, "undefined deserializes");
+        
+        // test objects
+        var obj = {str: str, num: num, bool: bool, nul: nul};
+        var arr = [str, num, bool, nul, undef];
+        var date = new Date();
+        
+        t.eq(OpenLayers.Class.deserialize(obj), obj, "object deserializes");
+        t.eq(OpenLayers.Class.deserialize(arr), arr, "array deserializes");
+        t.eq(OpenLayers.Class.deserialize(date), {}, "date doesn't deserialize");
+        
+        // test functions
+        var func = function() {};
+        var foo = {
+            bar: func
+        };
+        t.eq(OpenLayers.Class.deserialize(func), undefined,
+             "returns undefined for deserialized function");
+        t.eq(OpenLayers.Class.deserialize(foo).bar, undefined,
+             "leaves undefined properties whose values are functions");
+
+        // test recursion
+        var foo = {};
+        var bar = {foo: foo};
+        foo.bar = bar;
+        var deserialized = OpenLayers.Class.deserialize(foo);
+        var stop = OpenLayers.Class.recursionDepth;
+        var depth = 0;
+        var key;
+        var deeper = true;
+        while(deeper) {
+            deeper = false;
+            for(key in deserialized) {
+                deserialized = deserialized[key];
+                deeper = true;
+                ++depth;
+                break;
+            }
+            // don't count full depth in case recursion is infinite
+            if(depth > stop + 5) {
+                break;
+            }
+        }
+        t.eq(depth, stop, "deserialize respects OpenLayers.Class.recursionDepth");
+        
+    }
+
   </script>
 </head>
 <body>



More information about the Commits mailing list