- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
#胎位检查的最佳时间是几周#胎位检查的最佳时间是几周
-
「2050年的波尔多葡萄酒」2050年的波尔多葡萄酒什么味?
-
科技一哥|| 5G进入到第二阶段了吗?,总编谈5G
-
中医刘医师|武汉小伙三年不换洗枕套,睫毛上住满螨虫!自家被套,多久洗一次
-
《小满生活》大结局:剧中一共6对CP,4对终成眷属,2对走向陌路
-
「小康视野」悲剧太深刻,子弹为何不能朝天射击?2012年鸣枪''助兴''
-
-
-
梅西|重磅!梅西?最新想法曝光:巴萨成大赢家,迎来队史最和谐时刻
-
-
鲁建国|振德医疗:预计2020年上半年净利润比上年同期增加9.25亿元~9.55亿元
-
独行小路|涨停了......,今日市值超300亿的榨菜
-
-
最新趣闻|李一桐晒《甄嬛传》剧照恶搞陈建斌,隔空喊话:嬛嬛,朕有夏贵人了
-
“国产苹果”做到头了?6000门店相继关门,网友的吐槽亮了
-
微哪瀚说@华为这3款比较“坑爹”!买了就等于是入坑,购机须谨慎
-
行圆汽车大全|极星正在构建它的电动帝国,未来与当下
-
松鼠热点|“烈火英雄”变“肌肉型男”,深圳宝安消防员健美大赛
-
工人日报|老年人受困“数字鸿沟”,别把问题都推给新技术
-
「晨报大小事」暖心!留观人员在集中隔离点,过了一个难忘的生日