[OpenLayers-Dev] native JSON

Slawomir Messner slawomir.messner at staff.uni-marburg.de
Thu Feb 17 07:01:02 EST 2011


Hi,
thanks to the quick answer and testing. We don't use the GeoJSON format 
directly, only a derived class. The write method looks quite same:
     write: function (obj, pretty, layerStyle) {
         var geojson = {
             "type": null,
             "properties": new Object()
         };
         if (obj instanceof Array) {
             geojson.type = "FeatureCollection";
                                     geojson.defaultStyle = layerStyle;
             var numFeatures = obj.length;
             geojson.features = new Array(numFeatures);
             for (var i = 0; i < numFeatures; ++i) {
                 var element = obj[i];
                 if (!element instanceof OpenLayers.Feature.Vector) {
                     var msg = "FeatureCollection only supports 
collections " +
                               "of features: " + element;
                     throw msg;
                 }

                             for (var a in element.attributes.style) {
                                if (element.attributes.style[a] == 
geojson.defaultStyle[a]) {
                                     delete (element.attributes.style[a]);
                                 }
                             }
                 geojson.features[i] = this.extract.feature.apply(
                     this, [element]
                 );
             }
         } else if (obj.CLASS_NAME.indexOf("OpenLayers.Geometry") == 0) {
             geojson = this.extract.geometry.apply(this, [obj]);
         } else if (obj instanceof OpenLayers.Feature.Vector) {
             geojson = this.extract.feature.apply(this, [obj]);
             if (obj.layer && obj.layer.projection) {
                 geojson.crs = this.createCRSObject(obj);
             }
         }
         var jsonFormatter =new OpenLayers.Format.JSON();
         return jsonFormatter.write(geojson, pretty);
     },
We pass an array of features, false and the default style of the layer. ...
Now I tested a little and when I pass true not false for pretty all is fine.
Thanks
Slawomir


Am 17.02.2011 11:04, schrieb Andreas Hocevar:
> Hi Slawomir,
>
> you must be doing something wrong when serializing. OpenLayers.Format.GeoJSON::write takes an array of features as argument. I just verified that it works as expected if you use it this ways.
>
> It looks like you are serializing your arrays separately in some way. Maybe you can share your code to shed some light on what you're doing?
>
> Regards,
> Andreas.
>
> On Feb 17, 2011, at 09:07 , Slawomir Messner wrote:
>
>> Hi,
>> some revisions ago the JSON.js is changed to use the native browser possibilities. But now arrays are in double quotes(see example below) and our backend has problems with deserialize it. As I know it is that the JSON format has no double quotes around arrays. So where is the error, is the native serializer(Firefox and Chrome) not producing a standard format? With the old code (before native browser function is used) all was fine.
>> The object we pass to the OL.Format.JSON.write has the features-attribute as array I tested it in firebug/chrome-dev-tools. I don't know why it should be a string after serializing.
>>
>> Example:
>> {"type":"FeatureCollection","properties":{},"defaultStyle":{"pointRadius":"4","fillColor":"#008700","fillOpacity":"0.6","stroke":"True","strokeColor":"#21007E","strokeOpacity":"0.9","strokeWidth":"1","strokeDashstyle":"solid","label":"","fontFamily":"Arial","labelAlign":"lb","display":""},
>>
>> "features":"[{\"type\": \"Feature\", \"id\": \"OpenLayers.Feature.Vector_339\", \"properties\": {\"gid\": 101225, \"title\": \"VREDEN\", \"ownerType\": 1, \"style\": {\"label\": \"<font color='#FF0000' bold='false' italic='false' sizeFactor='1'>[Â</font>\", \"fontFamily\": \"SprachGIS-Symbole2\"}, \"symbols\": [26373, 26367], \"wktGeometry\": \"POINT(759321.378649999 6806065.527048201)\"}, \"geometry\": {\"type\": \"Point\", \"coordinates\": [759321.378649999, 6806065.527048201]}}, {\"type\": \"Feature\", \"id\": \"OpenLayers.Feature.Vector_341\", \"properties\": {\"gid\": 101252, \"title\": \"MILLINGEN\", \"ownerType\": 1, \"style\": {\"label\": \"<font color='#FF0000' bold='false' italic='false' sizeFactor='1'>Â</font>\"...
>> Has someone the same experience?
>> Thanks for help in advance.
>> Slawomir
>>
>> _______________________________________________
>> Dev mailing list
>> Dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/openlayers-dev
>
>



More information about the Dev mailing list