- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
中新经纬|33城预警!京津冀等地14-17日将再现污染过程
-
-
丹东云播|丹东姑娘发现“男友”竟是女儿身~~被骗感情被骗钱……,天呐
-
每日小情书▲早上狗都躲着我!,十条段子:喝多了和狗谈心一晚上
-
-
新资讯|现在都流行“蜜桃臀”?热巴和杨幂同穿修身裙,身姿曲线你选哪个
-
周冬雨|28岁9次拿影后,周冬雨成功的背后,到底经历了什么?
-
-
【北京日报客户端】巩立姣:目标直指东京奥运会金牌,强化体能短板、冬训成果明显
-
-
3岁男孩肺癌晚期,劝告家长:戒烟吧!别把孩子送入“鬼门关”
-
-
-
-
-
海贼资讯|实况球会经理评测:禁区之狐偷猎者,少林足球诚不欺我
-
桑塔纳|不到10万买大众桑塔纳 畅销多年魅力不减 现在入手一点都不晚
-
师祖学|幽默笑话:传说,孙悟空找菩提师祖学本事······
-
-
木瓜健康|宝宝宝妈一夜安眠,家有“睡渣”宝宝?儿科医生:掌握这5个规律