diff --git a/src/ol/format/jsonformat.exports b/src/ol/format/jsonformat.exports new file mode 100644 index 0000000000..d45558a5dd --- /dev/null +++ b/src/ol/format/jsonformat.exports @@ -0,0 +1,8 @@ +@exportProperty ol.format.JSON.prototype.readFeature +@exportProperty ol.format.JSON.prototype.readFeatures +@exportProperty ol.format.JSON.prototype.readFeaturesAsync +@exportProperty ol.format.JSON.prototype.readGeometry +@exportProperty ol.format.JSON.prototype.readProjection +@exportProperty ol.format.JSON.prototype.writeFeature +@exportProperty ol.format.JSON.prototype.writeFeatures +@exportProperty ol.format.JSON.prototype.writeGeometry diff --git a/src/ol/format/jsonformat.js b/src/ol/format/jsonformat.js new file mode 100644 index 0000000000..59ac11b62f --- /dev/null +++ b/src/ol/format/jsonformat.js @@ -0,0 +1,155 @@ +goog.provide('ol.format.JSON'); + +goog.require('goog.asserts'); +goog.require('goog.json'); +goog.require('ol.format.Format'); +goog.require('ol.format.FormatType'); + + + +/** + * @constructor + * @extends {ol.format.Format} + */ +ol.format.JSON = function() { + goog.base(this); +}; +goog.inherits(ol.format.JSON, ol.format.Format); + + +/** + * @param {Document|Node|Object|string} source Source. + * @private + * @return {Object} Object. + */ +ol.format.JSON.prototype.getObject_ = function(source) { + if (goog.isObject(source)) { + return source; + } else if (goog.isString(source)) { + var object = goog.json.parse(source); + return goog.isDef(object) ? object : null; + } else { + goog.asserts.fail(); + return null; + } +}; + + +/** + * @inheritDoc + */ +ol.format.JSON.prototype.getType = function() { + return ol.format.FormatType.JSON; +}; + + +/** + * @inheritDoc + */ +ol.format.JSON.prototype.readFeature = function(source) { + return this.readFeatureFromObject(this.getObject_(source)); +}; + + +/** + * @inheritDoc + */ +ol.format.JSON.prototype.readFeatures = function(source) { + return this.readFeaturesFromObject(this.getObject_(source)); +}; + + +/** + * @param {Object} object Object. + * @protected + * @return {ol.Feature} Feature. + */ +ol.format.JSON.prototype.readFeatureFromObject = goog.abstractMethod; + + +/** + * @param {Object} object Object. + * @protected + * @return {Array.