- 主页 > 生活百科 > >
我的朋友因为 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年都没补回来,语文究竟该怎么学
-
恒大|许家印:恒大要全面实施现楼销售 10年内转型新能源车企
-
-
中国侨网微信公众号|在“异乡”的“家乡”,幸好有这些侨胞们……
-
「正商参阅」涨跌全看特朗普?,全球五一“劫”!美股5月“开门黑”
-
我感觉二十来岁的年纪很迷茫,特别想赚钱,又不知道咋办
-
新华网|我国新一代地铁智能化直流牵引供电系统试运营
-
法学|这几个专业已经严重饱和,难就业市场行情差,23届高考生谨慎报考!
-
-
七旬阿尔茨海默病老人在雪夜里走失 民警接力让其与家人团聚
-
借壳上市|刘备集团借壳上市后, 赵云用一番话, 把自己丢出管理层!
-
-
虞书欣@其他人都想过虞书欣的人生,反观虞书欣想体验谁,理由很让人服气
-
穿越火线|最后一局不留遗憾!18开局三杀还以颜色,宠儿4杀独木难支
-
-
-
-
健身■还在羡慕别人的倒三角身材?三个动作,帮你完美打造背部肌群
-
|还是对阿里出招?我外交部这次的话不简单,特朗普终于忍不住了
-
鲜酵母1克是多少干酵母,酵母粉怎么用,一斤面粉放多少酵母粉?