- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
蜜蜂进家里有什么预兆 蜜蜂进家里有什么预兆能不能赶走
-
-
看看新闻Knews综合|独居老人倒地4天滴水未进 靠敲击脸盆被邻居发现
-
人民日报客户端青海频道|青洽会开启 首次“云签约”
-
波特兰|美国波特兰抗议第83天:约200人扔石块砸玻璃,焚烧政府大楼
-
穿搭|2020年秋天,温柔又舒适的“治愈系”穿搭:最美,最撩人!
-
新华社解放军分社|长湖水位连续超保证水位 武警官兵紧急护堤
-
-
-
-
无马汽车|五菱首款银标旗舰车型!凯捷将于11月1日上市!
-
娱乐中的趣闻|绝对没有第七种,王者起名一定是以下六种类型
-
误删微信聊天记录怎么恢复有免费的没? 如何恢免费复删除的微信聊天记录
-
-
三国两晋南北朝|沮授在袁绍手下不被重用,恨袁绍吗?曹爱才,被抓后为啥不跟曹?
-
长江|7月以来洪涝受灾人次超2000万 后期长江中下游还将复涨
-
检察日报■涉疫诈骗多发!检察机关提示10点防骗建议
-
-
-