- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
[黄山市药学会]减肥季节请注意:减肥利器“奥利司他”不要随意吃
-
甘薇|昔日阔太甘薇穿一身名牌跳舞,动作性感妖娆,负债13亿仍过得潇洒
-
衣在配不在贵|脸上一般都有这三个共同点,五官差点也不会觉得丑,骨相好的女人
-
-
|已婚女扮演“知性女博士”出轨已婚男,迷得对方抛家弃子狂砸20余万
-
生产总值|增长1.3%!贵阳市今年上半年生产总值为1833.66亿元
-
太平洋电脑网WWDC 2020将于6月22日开幕,iOS 14要来了!
-
岳云鹏年入5000万,他找妻子商量:我想给5个姐姐各买1套房1辆车
-
-
-
-
【星星看点点】将于12月率先在韩国上市,新一代起亚K5官图
-
居民|香港发行最多150亿港元银色债券息率不少于3.5厘
-
-
四分五裂|争议颇多的朝代,对中国的贡献不容忽视,一招让中国不再四分五裂
-
联想|联想集团前副总裁赵泓加入柔宇科技任COO:负责集团战略制定
-
-
-
-
中年抖音推广二类电商落地页怎么设计?二类电商广告落地页设计技巧