- 主页 > 生活百科 > >
我的朋友因为 JSON.stringify 差点丢了奖金( 三 )
let detected = false const detect = (obj) => { // If it is not an object, we can skip it directly if (obj && typeof obj != 'object') { return } // When the object to be checked already exists in the stackSet, // it means that there is a circular reference if (stackSet.has(obj)) { return detected = true } // save current obj to stackSet stackSet.add(obj) for (let key in obj) { // check all property of `obj` if (obj.hasOwnProperty(key)) { detect(obj[key]) } } // After the detection of the same level is completed, // the current object should be deleted to prevent misjudgment /* For example: different properties of an object may point to the same reference, which will be considered a circular reference if not deleted let tempObj = { name: 'bytefish' } let obj4 = { obj1: tempObj, obj2: tempObj } */ stackSet.delete(obj) } detect(obj) return detected } // Throws a TypeError ("cyclic object value") exception when a circular reference is found. if (isCyclic(data)) { throw new TypeError('Converting circular structure to JSON') } // Throws a TypeError when trying to stringify a BigInt value. if (typeof data =https://www.isolves.com/it/cxkf/bk/2022-11-07/== 'bigint') { throw new TypeError('Do not know how to serialize a BigInt') } const type = typeof data const commonKeys1 = ['undefined', 'function', 'symbol'] const getType = (s) => { return Object.prototype.toString.call(s).replace(/[object (.*?)]/, '$1').toLowerCase() } if (type !== 'object' || data =https://www.isolves.com/it/cxkf/bk/2022-11-07/== null) { let result = data // The numbers Infinity and NaN, as well as the value null, are all considered null. if ([NaN, Infinity, null].includes(data)) { result = 'null' // undefined, arbitrary functions, and symbol values are converted individually and return undefined } else if (commonKeys1.includes(type)) { return undefined } else if (type === 'string') { result = '"' + data + '"' } return String(result) } else if (type === 'object') { // If the target object has a toJSON() method, it's responsible to define what data will be serialized. // The instances of Date implement the toJSON() function by returning a string (the same as date.toISOString()). Thus, they are treated as strings. if (typeof data.toJSON === 'function') { return jsonstringify(data.toJSON()) } else if (Array.isArray(data)) { let result = data.map((it) => { // 3# undefined, Functions, and Symbols are not valid JSON values. If any such values are encountered during conversion they are either omitted (when found in an object) or changed to null (when found in an array). return commonKeys1.includes(typeof it) ? 'null' : jsonstringify(it) }) return `[${result}]`.replace(/'/g, '"') } else { // 2# Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.
推荐阅读
-
-
【PlanetHolidayTB】美食是旅途中的亮点
-
-
-
羊肉放冷冻要洗吗长期保存方法 羊肉放冰箱冷冻前洗吗
-
-
闻谈历史|那秃顶和光头的人该怎么办?看完你就知道了,清朝一律要留辫子
-
学生|美国西雅图一大学宿舍区超100名学生感染新冠肺炎
-
网通社汽车频道 Q7 霸气归来 全面升级诠释王者无畏,全新奥迪
-
-
中国新闻网|?7月多地收紧楼市 中国百城房价环比增速放缓
-
-
大学|9岁高考,10岁上大学的神童女孩,撕开了神童最后一块遮羞布
-
「天都」28岁小伙腹痛入院,查出病情大哭不止!父亲:一年有200天都吃它
-
新民网|上海降温啦!雨水这波“返场”满分 明最高温将降回“2”字头
-
每天小情书我去劝架!,十条段子:你去把剩下的衣服晾好
-
杭州女子家中离奇失联十多天!电梯井、地下室、楼顶……更多细节揭露
-
事项|交易异动!航发控制:不存在应披露而未披露的重大事项 近3个交易日上涨21.89%
-
-