- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
小米品玩师▲直男最爱,秒杀瑞士军刀,小米169元万能工具箱!8种工具2.1kg
-
|一个干净整洁的厨房,不能少了这6个“居家小点子”,实用又方便
-
安装|天然气在农村安装又方便又有补贴,为什么有些村民还是不愿
-
「旅客正在有序进站2020」湖北武汉:汉口火车站“解封”首日平稳有序(组图)
-
解放网|进门见游鱼、闻花香……公厕变美了,公厕承包制今年将在黄浦全面推广
-
兰兰聊育儿|新手妈妈要谨记,“新生儿剪了指甲伤元气?”宝宝剪指甲三大误区
-
『工作』图制|昆明市人大会常委会工作报告说了啥?一张图Get√
-
-
#孕妇补铁,缺铁,孕妇补铁补多长时间#孕妇补铁补多长时间 补铁可选择两款食谱
-
谈资|陈羽凡近照曝光,身材严重发福显油腻,脸有之前两个大
-
七天娱乐煮|哥哥演《还珠格格》成名,弟弟最红时病逝,同父同母的亲兄弟
-
"会进化"的神车引发全民抢购 第三代哈弗H6即将登陆北京车展
-
情感困惑的解答|凭什么我来”,“以后别指望给你养老”,“别人家都是丈母娘照顾
-
档案|永安市创新基本公共卫生服务,探索慢性病患者健康档案质量控制模式
-
虎扑|我觉得还不错?,90!范戴克晒个人FIFA能力值:你们怎么看
-
谎称卖二手报废车骗取定金,提供给别人的照片是路边的僵尸车
-
-
-
-