- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
-
无人机网慧明捷参与四川省“全省人防指挥通信比武竞赛”活动保障
-
湘江北去|中共湖南群林科技有限公司支部委员会党员大会隆重举行
-
环球网|韩国赴华乘客登机核酸检测证明时限调整 中使馆提醒
-
数据|万国数据赴港二次上市首日涨3% 收入高增长4年连亏
-
-
-
-
孕妇|明明有风险成分,还鼓吹着“孕妇专用”?有些洗面奶就该丢垃圾桶
-
-
央视新闻|北京新发地批发市场终末消杀工作已完成 结果合格
-
『印度』新官上任三把火,印上将抛出:百万大军调转枪口,专家:62年悲剧或重演
-
快科技:SE比最快的Android手机都要快,库克:iPhone
-
詹姆斯|复制詹皇之路?字母哥社媒取关雄鹿队友,轰动NBA交易要出现了
-
如何取消优酷会员自动续费服务,怎么取消优酷会员的自动续费功能
-
美国修改留学生签证新规,体现了特朗普政府的短视与算计
-
中商情报网|2020年全球医疗器械市场现状分析及市场规模预测
-
成龙|直到去世时,李连杰成龙还没原谅他,网友∶这是有多大的仇怨!
-
抖音|今年上半年全球最赚钱、下载量最大App出炉:都来自中国
-
微博网友|?快看!国家制止餐饮浪费将立法 你的消费习惯要改了