- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
中医养生秘法 记住这一步,蒸出来的地瓜又香又甜!,地瓜不要直接上锅“蒸”
-
-
「教育部」2020年高考人数或将达到千万,考生面临四大挑战,考生:我太难了
-
-
苏宁vs京东,谁的胜算更大? 苏宁易购跟京东商城哪个好
-
-
M&|M&G Plc减持纷美包装股份约58.10万股
-
滴滴出行|阿里打工8年,辞职创立滴滴,4年狂赚千亿,全因他精通孙子兵法
-
-
鲫鱼@郑州一男子凌晨垂钓鱼竿落水,下河打捞鱼竿时溺亡
-
第一财经|统计局答一财:企业预期向好,疏通小微企业融资支持
-
小畜播报|刘晓庆拜师书协大师,苦练书法3年,如今写得怎样?
-
潇湘晨报|员工凌晨猝死,曾连续两个月每天工作10小时以上……属工伤范围吗?
-
成品|新娘在梯子上模仿天使,看到拍照成品后,新娘一脸不高兴要求退钱
-
矮了18厘米|收起嬉笑的霍华德:将是湖人夺冠路上的护航者
-
婚姻不是两个人的事,是两个家庭?婚姻不是两个人的事情,是一个人
-
-
无尾象和猫@为啥现在如此平静?专家:有3个原因,平常敢喝恒河水的印度人
-
下山打老虎|若身体步入“衰老期”,脸上会有5个异样,男人30岁以后
-
人民日报客户端|外交部回应,蓬佩奥称美做好了带头对抗中共的准备