- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
-
-
扬子晚报|搬家公司员工坐地起价不成 竟耍赖不走还威胁当事人
-
-
『海宝君传奇』行程太过巧合,网友纷纷留言祝福,杨幂魏大勋酒店密会上热搜
-
-
古人讲“五花八门”,是哪五花和哪八门?被孩子问到可别答不上来
-
北青网综合|新疆克孜勒苏州乌恰县发生3.0级地震 震源深度10千米
-
今日亚洲|王晓鹏:美国再度高调介入南海问题的九重企图
-
-
-
-
-
战略先锋▲这次全部搭载实弹,终于动真格了!我军一支舰艇编队紧急赶赴南海
-
这些地方最好玩:一年进账超5亿,历时15年跻身全球第二,被低估的中国芯片巨头
-
上市公司|29家创业板公司进入减持区间,20%涨跌停下股东是否趁机套现?
-
车晓前夫|车晓前夫李兆会22亿资产四度流拍 曾是山西首富
-
-
『综艺节目』美国劲爆综艺,禁欲一个月可拿10万美金,网友:单身20年想参赛
-
【解放网】学习中国经验!总台记者独家探访伊朗版“方舱医院”