- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
-
半导体业|22家半导体行业!机构重仓高达81%(603228)低估值
-
问董秘|贵公司最近产品订单满负荷吗?,投资者提问:您好
-
【地评线】京彩好评:自贸试验区“再扩容” 为开放提速发展助力
-
细狗|绰号“细狗”“乌龟”等18人涉黑!赣州警方重奖征集违法犯罪线索,附照片
-
-
-
[安全带]浙江高速交警集中整治司乘违法:后排乘客不系安全带将被
-
-
-
手机之家荣耀30系列联合新浪科技长焦逐日 影像见证十年一遇日环食
-
行者车视觉:为啥最后都买大众车?懂车人终于说了实话,都说丰田最耐用
-
-
电视剧|琅琊榜:谢玉为什么不辅佐誉王,还有一个潜在原因,很难看出来!
-
微信|微信发布公众号新规:这四种教你赚钱的文章禁发
-
-
e公司|买苹果就辞退!公司这样回应…,真另类!江苏一酒企发通知:限定使用国产机
-
|建议:别往厨房塞一堆塑料袋了,学聪明人做法,干净美观易整理
-
参考消息|英媒:中国资产“抢手”提升人民币全球地位
-
妮妮|差距太大!欧阳娜娜姐姐新剧开播 被嘲像陈汉典