- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
智能家居科技 14还能这么用?揪出凶手,到底谁在窥探你的隐私?,iOS
-
视频|真 · 拦路虎!的哥开车偶遇东北虎,默默对视20分钟……
-
OLED|扭转国产OLED行业口碑!卢伟冰称1.5K将是手机行业新主流
-
-
谷歌|几个月前嘲笑iPhone取消耳机孔的谷歌:终于自己破功了
-
金正恩视察灾区重建工作 胞妹陪同前往 系两个月来首次露面
-
林正英真的会茅山术吗,林正英怎么会懂那么多茅山道术-
-
-
张歆艺|张歆艺穿制服躺床上玩闹,穿短裙单脚卖力抬高,姿势豪迈不怕走光
-
-
产妇没有奶水如何催奶,刚生了孩子没有奶水怎么办怎么催奶-
-
-
未说育儿|这些行为最“伤”胎儿,你每天都在做,孕妈要知道
-
腾讯科技网易聆讯后资料披露董事会成员名单,去年向高管及董事支付1760万薪酬
-
答摩健康|为什么你做的明年计划不靠谱?因为你是用今年的脑子想的!
-
数据宝|仅24只,数说A股:基金扎堆的牛股大跌6%!“小而美”股票名单来了
-
中国基金报|重要信号!货币ETF规模激增330亿 啥情况?
-
三分亮剑|危险决定直接影响邻国安全,韩国启动紧急应对,日本不再遮遮掩掩
-
江苏经济报@江苏227家交通运输企业获融资授信8亿元
-