- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
科技鉴品资讯号苹果5G遭遇当头棒喝,如果不换TYPE-C接口可能将面临禁售?
-
-
没蜡笔的小新|张子萱生二胎容貌大变,苹果肌肿胀鼻子像匹诺曹,脸每次都不一样
-
高跟鞋■同样的衣服,怎么别人穿就那么有气质呢?那是你没有掌握这些技巧
-
-
问董秘|技术上有什么壁垒吗?,投资者提问:贵公司如果生产蚀刻类电子材料
-
小杨聊星座|好运统统来,4属相吉星近身,贵人相助,生活有惊喜,步入下周
-
21cn|2200件棋牌游戏涉赌投诉背后:通过“银商”上下分,最高输了700万元
-
文汇报|日本德仁天皇在“8·15”战败日表示深刻反省
-
东京|东京奥运会协调委员会主席:不管有无疫苗,东京奥运都在2021年举办
-
生猪|连跌三月后猪肉价格反弹 批发商:高价不会持续太久
-
-
马云|蔡天凤闺蜜参与碎尸案,在深圳被抓获,和天王嫂方媛多次同框
-
-
-
|注意!岱勒新材:董事周家华拟减持不超过约8.55万股
-
沃格尔|沃格尔:球队在来比赛路上遭遇交通堵塞 感觉不像正常总决赛
-
-
-
明星大小事@我的上班小助手防盗又智能,小牛U+电动车