- 主页 > 生活百科 > >
我的朋友因为 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.
推荐阅读
-
-
-
秀美阡陌网|张一山宋妍霏疑似同居是真的吗?张一山宋妍霏什么关系
-
和田玉籽料|和田玉籽料 松风啸 天地动 猛虎下山 不怒而威
-
贝克汉姆|贝克汉姆夫妇曾感染新冠并传染多人,一度怀疑自己是“超级传播者”
-
HR月薪1万很普遍?你一个月挣多少?你们公司薪酬制度是怎样的?
-
-
张继科|14时官宣,张继科被抓悬念揭晓,爸爸现身回应,景甜做最好选择
-
有个关系还不错,但是帮她带饭、买东西从来忘了给钱,如果问她要也会给的同学,好像也不是故意不给,咋办
-
小小说体育|场上两镜头给出答案,尤文0-2揪出一大漏勺!29岁铁卫太业余
-
幼年丧父、家产被夺、母妹被逼服毒,她挣下万贯家财,终生未婚
-
-
带着妮妮看世界|而不区分开住?列车员无奈道出缘由!,为何火车卧铺都是男女混住
-
-
-
『小七说篮球』有望成为现实!,最刺激的季后赛要来了?两大“黑八”会师总决赛
-
中华小神兵|停火协议只是摆设,刚生效数分钟就发射炮弹,阿塞拜疆不宣而战
-
王者荣耀|边路射手大变天,守约虞姬沦为T3,最强射手之王回来了
-
-