- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
-
-
霸气的小猪佩琦|生死战23分惨案!湖人连赢4场太狠,休城面临重建
-
「久期财经」展望“稳定”,惠誉:确认蓝色光标(300058.SZ)“B+”长期本外币发行人评级
-
|84岁老爷爷参加高考,考前自估630,得知成绩后欲哭无泪
-
-
-
好声音■《好声音》导师阵容确定,没了沈腾贾玲搭档,他能适应吗?
-
利利讲快乐|东方卫视新综艺亮相,放弃《欢乐喜剧人》了吗,90%喜剧大咖聚齐
-
特斯拉|金钱在燃烧 马斯克:特斯拉柏林、德州超级工厂正亏损数十亿美元
-
开屏|杨丽萍到公园偶遇孔雀,孔雀纷纷开屏迎接,万物皆有灵!
-
-
尤文图斯|虽然来得晚些,尤文图斯仍如愿捧杯,C罗续写着他的辉煌
-
睡前常做4种行为,容易诱发失眠,做的越多,失眠的情况越严重
-
演唱会|经典音乐人:开演唱会都不用自己开口的人!全是观众在唱——伍佰
-
-
-
-
-