- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
运输中的刘哥■家庭小炒之平平淡淡过一生的简单小凉拌和炒菜
-
-
搭配化妆师|穿黄颜色卡通卫衣裙,又被大幂幂给可爱到了!,杨幂上综艺
-
-
新机发布传iPhone 12于11月中旬发货 HomePod mini卖99美元
-
-
很文艺范的句子,喜欢就转发吧?有意境文艺范的句子正能量
-
看点精彩资讯|中国:三个条件,永远跪在中国土地上的日本兵!日本多次要求拆除
-
【大众报业·海报新闻】世界粮食日丨全球8亿人“喊饿”,劝君更惜“盘中餐”
-
王者荣耀|王者荣耀玩家不满新版本,辛苦刷场次说改就改,强行暴露个人隐私
-
-
高台卫生健康■高台县骆驼城镇卫生院开展“儿童预防接种日”科普宣传活动
-
-
小米|小米要用 高通骁龙8 Gen2新品展望:采用台积电4nm工艺
-
-
邦邦说|张雨绮力挺鹿晗公开恋情:有些朋友就是干得漂亮
-
-
哥哥|披荆斩棘的哥哥什么时候播 披荆斩棘的哥哥嘉宾名单
-
#啮花熊#特朗普下令:格杀勿论,对伊朗快艇忍无可忍?实则被卫星发射吓到
-
体坛焦点|三条线各一人,包括皇马目标人物,曝巴萨新帅确定三大引援目标