- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
[创作的魅力]今天拿白菜还债,我认宰了,网友:天大的笑话,十年前借走五千元
-
-
-
发布以来的设备,Google,Pixel,5a新机
-
-
芊菁小姐美搭搭|芊菁街拍:苗条美丽很迷人的小姐姐,颜值令人赏心悦目
-
-
她是赵丽颖替身,趁赵丽颖怀孕时疯狂接戏,如今30岁2人平起平坐
-
潮品汇 10岁的倍思凭什么能赢?,引领氮化镓快充潮流
-
直播吧■新增3.6万确诊新增2千1死亡,美国累计新冠确诊超50万例
-
熊妈育熊娃|46岁生下三胎后,一家合照羡煞旁人,有种“幸福”叫蔡少芬生娃
-
周冬雨|美图分享|“冬叔”周冬雨,少女外表大汉心,率性而为,自在坦荡
-
砍柴网追梦之路转折前行 , 三星 Z Fold2 见证张天爱的开合人生
-
机械博览|河南省首台纯电动重型工程车在中国一拖下线并交付使用
-
一族|八皇商之一的范氏一族,为何能受康熙、雍正、乾隆三朝皇帝的赏识
-
-
-
笑小新科技|144Hz屏+骁龙865,配色五彩缤纷价格感人,红米再添新成员
-
三国两晋南北朝|三国曹操手下猛将,为曹魏立下战功,曾在樊城之战中战胜关羽
-
生肖▲未来一月,迎来“财运大爆发”,金银铺路,路路畅通的生肖,获得大笔财富