- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
「郏县」河南郏县2人核酸检测呈阳性 其中1人曾与漯河新增病例接触
-
IT小灵通|联发科乐观其成,卢伟冰:更香了,小米5G芯片不远了
-
-
##垂钓黄颡鱼,用女士丝袜打窝诱鱼,是利用蚯蚓的腥味来诱鱼入窝
-
陕西咸阳警方破获一起部督特大跨国网络传播淫秽物品牟利案
-
晓娜说娱乐|一般人还真开不到这个程度,搞笑GIF段子:不是我说
-
#地下城与勇士#DNF:土豪开服直接战令88满级,龙袍开始烂大街,天空价格崩盘
-
『中国电子报』金山办公软件股份有限公司董事长葛珂:我国软件业进入生态能力成长阶段
-
-
-
-
炉石传说|炉石传说:这张卡可以直接分了吧?网友:这卡没那么弱!
-
世界最重的人有多少公斤 世界上体重最重的人是谁多少公斤
-
[提醒]紧急提醒!您的这张ETC卡即将失效....
-
-
『』人生第一套新房完工,全屋不做吊顶,一进屋就被玄关迷住,晒晒!
-
为什么康师傅红茶要换成圆瓶,原来方瓶挺好的,现在换完圆瓶还没有方瓶好呢![红茶]
-
-
给我讲一个会哭的故事吧,我已经没有疼痛,没有眼泪了,让我感受一下生命的气息好吗
-