/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved. * This program are made available under the terms of the Apache License, Version 2.0 * which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/ import { SuperMap } from '../SuperMap'; import { Format } from './Format'; /** * @class SuperMap.Format.JSON * @classdesc å®å ¨ç读å JSON çè§£æç±»ãä½¿ç¨ {@link SuperMap.Format.JSON} æé 彿°å建æ°å®ä¾ã * @category BaseTypes Format * @param {Object} [options] - åæ°ã * @param {string} [options.indent=" "] - ç¨äºæ ¼å¼åè¾åºï¼indent å符串ä¼å¨æ¯æ¬¡ç¼©è¿çæ¶å使ç¨ä¸æ¬¡ã * @param {string} [options.space=" "] - ç¨äºæ ¼å¼åè¾åºï¼space å符串ä¼å¨åå¼å¯¹ç ":" å边添å ã * @param {string} [options.newline="\n"] - ç¨äºæ ¼å¼åè¾åº, newline å符串ä¼ç¨å¨æ¯ä¸ä¸ªåå¼å¯¹ææ°ç»é¡¹æ«å°¾ã * @param {number} [options.level=0] - ç¨äºæ ¼å¼åè¾åº, è¡¨ç¤ºçæ¯ç¼©è¿çº§å«ã * @param {boolean} [options.pretty=false] - æ¯å¦å¨åºååçæ¶å使ç¨é¢å¤çç©ºæ ¼æ§å¶ç»æãå¨ write æ¹æ³ä¸ä½¿ç¨ã * @param {boolean} [options.nativeJSON] - éè¦è¢«æ³¨åççå¬å¨å¯¹è±¡ã * @extends {SuperMap.Format} */ export class JSONFormat extends Format { constructor(options) { super(options); /** * @member {string} [SuperMap.Format.JSON.prototype.indent=" "] * @description ç¨äºæ ¼å¼åè¾åºï¼indent å符串ä¼å¨æ¯æ¬¡ç¼©è¿çæ¶å使ç¨ä¸æ¬¡ã */ this.indent = " "; /** * @member {string} [SuperMap.Format.JSON.prototype.space=" "] * @description ç¨äºæ ¼å¼åè¾åºï¼space å符串ä¼å¨åå¼å¯¹ç ":" å边添å ã */ this.space = " "; /** * @member {string} [SuperMap.Format.JSON.prototype.newline="\n"] * @description ç¨äºæ ¼å¼åè¾åº, newline å符串ä¼ç¨å¨æ¯ä¸ä¸ªåå¼å¯¹ææ°ç»é¡¹æ«å°¾ã */ this.newline = "\n"; /** * @member {integer} [SuperMap.Format.JSON.prototype.level=0] * @description ç¨äºæ ¼å¼åè¾åº, è¡¨ç¤ºçæ¯ç¼©è¿çº§å«ã */ this.level = 0; /** * @member {boolean} [SuperMap.Format.JSON.prototype.pretty=false] * @description æ¯å¦å¨åºååçæ¶å使ç¨é¢å¤çç©ºæ ¼æ§å¶ç»æãå¨ write æ¹æ³ä¸ä½¿ç¨ã */ this.pretty = false; /** * @member {boolean} SuperMap.Format.JSON.prototype.nativeJSON * @description 夿æµè§å¨æ¯å¦åçæ¯æ JSON æ ¼å¼æ°æ®ã */ this.nativeJSON = (function () { return !!(window.JSON && typeof JSON.parse === "function" && typeof JSON.stringify === "function"); })(); this.CLASS_NAME = "SuperMap.Format.JSON"; /** * @member SuperMap.Format.JSON.prototype.serialize * @description æä¾ä¸äºç±»å对象转 JSON åç¬¦ä¸²çæ¹æ³ã */ this.serialize = { /** * @function SuperMap.Format.JSON.serialize.object * @description æå¯¹è±¡è½¬æ¢ä¸º JSON å符串ã * @param {Object} object - å¯åºååç对象ã * @returns {string} JSON å符串ã */ 'object': function (object) { // three special objects that we want to treat differently if (object == null) { return "null"; } if (object.constructor === Date) { return this.serialize.date.apply(this, [object]); } if (object.constructor === Array) { return this.serialize.array.apply(this, [object]); } var pieces = ['{']; this.level += 1; var key, keyJSON, valueJSON; var addComma = false; for (key in object) { if (object.hasOwnProperty(key)) { // recursive calls need to allow for sub-classing keyJSON = this.write.apply(this, [key, this.pretty]); valueJSON = this.write.apply(this, [object[key], this.pretty]); if (keyJSON != null && valueJSON != null) { if (addComma) { pieces.push(','); } pieces.push(this.writeNewline(), this.writeIndent(), keyJSON, ':', this.writeSpace(), valueJSON); addComma = true; } } } this.level -= 1; pieces.push(this.writeNewline(), this.writeIndent(), '}'); return pieces.join(''); }, /** * @function SuperMap.Format.JSON.serialize.array * @description ææ°ç»è½¬æ¢æ JSON å符串ã * @param {Array} array - å¯åºååçæ°ç»ã * @returns {string} JSON å符串ã */ 'array': function (array) { var json; var pieces = ['[']; this.level += 1; for (var i = 0, len = array.length; i < len; ++i) { // recursive calls need to allow for sub-classing json = this.write.apply(this, [array[i], this.pretty]); if (json != null) { if (i > 0) { pieces.push(','); } pieces.push(this.writeNewline(), this.writeIndent(), json); } } this.level -= 1; pieces.push(this.writeNewline(), this.writeIndent(), ']'); return pieces.join(''); }, /** * @function SuperMap.Format.JSON.serialize.string * @description æåç¬¦ä¸²è½¬æ¢æ JSON å符串ã * @param {string} string - å¯åºååçå符串ã * @returns {string} JSON å符串ã */ 'string': function (string) { // If the string contains no control characters, no quote characters, and no // backslash characters, then we can simply slap some quotes around it. // Otherwise we must also replace the offending characters with safe // sequences. var m = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' }; /*eslint-disable no-control-regex*/ if (/["\\\x00-\x1f]/.test(string)) { return '"' + string.replace(/([\x00-\x1f\\"])/g, function (a, b) { var c = m[b]; if (c) { return c; } c = b.charCodeAt(); return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); }) + '"'; } return '"' + string + '"'; }, /** * @function SuperMap.Format.JSON.serialize.number * @description ææ°åè½¬æ¢æ JSON å符串ã * @param {number} number - å¯åºååçæ°åã * @returns {string} JSON å符串ã */ 'number': function (number) { return isFinite(number) ? String(number) : "null"; }, /** * @function SuperMap.Format.JSON.serialize.boolean * @description Transform a boolean into a JSON string. * @param {boolean} bool - The boolean to be serialized. * @returns {string} A JSON string representing the boolean. */ 'boolean': function (bool) { return String(bool); }, /** * @function SuperMap.Format.JSON.serialize.object * @description å°æ¥æå¯¹è±¡è½¬æ¢æ JSON å符串ã * @param {Date} date - å¯åºååçæ¥æå¯¹è±¡ã * @returns {string} JSON å符串ã */ 'date': function (date) { function format(number) { // Format integers to have at least two digits. return (number < 10) ? '0' + number : number; } return '"' + date.getFullYear() + '-' + format(date.getMonth() + 1) + '-' + format(date.getDate()) + 'T' + format(date.getHours()) + ':' + format(date.getMinutes()) + ':' + format(date.getSeconds()) + '"'; } }; } /** * @function SuperMap.Format.JSON.prototype.read * @description å°ä¸ä¸ªç¬¦å JSON ç»æçå符串è¿è¡è§£æã * @param {string} json - 符å JSON ç»æçå符串ã * @param {function} filter - è¿æ»¤æ¹æ³ï¼æç»ç»æçæ¯ä¸ä¸ªé®å¼å¯¹é½ä¼è°ç¨è¯¥è¿æ»¤æ¹æ³ï¼å¹¶å¨å¯¹åºçå¼çä½ç½®æ¿æ¢æè¯¥æ¹æ³è¿åçå¼ã * @returns {Object} å¯¹è±¡ï¼æ°ç»ï¼åç¬¦ä¸²ææ°åã */ read(json, filter) { var object; if (this.nativeJSON) { try { object = JSON.parse(json, filter); } catch (e) { // Fall through if the regexp test fails. } } if (this.keepData) { this.data = object; } return object; } /** * @function SuperMap.Format.JSON.prototype.write * @description åºååä¸ä¸ªå¯¹è±¡å°ä¸ä¸ªç¬¦å JSON æ ¼å¼çå符串ã * @param {(object|string|Array|number|boolean)} value - éè¦è¢«åºååçå¯¹è±¡ï¼æ°ç»ï¼åç¬¦ä¸²ï¼æ°åï¼å¸å°å¼ã * @param {boolean} [pretty=false] - æ¯å¦å¨åºååçæ¶å使ç¨é¢å¤çç©ºæ ¼æ§å¶ç»æãå¨ write æ¹æ³ä¸ä½¿ç¨ã * @returns {string} 符å JSON æ ¼å¼çå符串ã * */ write(value, pretty) { this.pretty = !!pretty; var json = null; var type = typeof value; if (this.serialize[type]) { try { json = (!this.pretty && this.nativeJSON) ? JSON.stringify(value) : this.serialize[type].apply(this, [value]); } catch (err) { //SuperMap.Console.error("Trouble serializing: " + err); } } return json; } /** * @function SuperMap.Format.JSON.prototype.writeIndent * @description æ ¹æ®ç¼©è¿çº§å«è¾åºä¸ä¸ªç¼©è¿å符串ã * @private * @returns {string} ä¸ä¸ªéå½ç缩è¿å符串ã */ writeIndent() { var pieces = []; if (this.pretty) { for (var i = 0; i < this.level; ++i) { pieces.push(this.indent); } } return pieces.join(''); } /** * @function SuperMap.Format.JSON.prototype.writeNewline * @description 卿 ¼å¼åè¾åºæ¨¡å¼æ åµä¸è¾åºä»£è¡¨æ°ä¸è¡çå符串ã * @private * @returns {string} 代表æ°çä¸è¡çå符串ã */ writeNewline() { return (this.pretty) ? this.newline : ''; } /** * @function SuperMap.Format.JSON.prototype.writeSpace * @private * @description 卿 ¼å¼åè¾åºæ¨¡å¼æ åµä¸è¾åºä¸ä¸ªä»£è¡¨ç©ºæ ¼çå符串ã * @returns {string} ä¸ä¸ªç©ºæ ¼ã */ writeSpace() { return (this.pretty) ? this.space : ''; } } SuperMap.Format.JSON = JSONFormat;