Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.stringify 循环引用 bug & TypeError: Converting circular structure to JSON #74

Open
xgqfrms opened this issue Feb 7, 2020 · 8 comments
Labels
circular-json circular-json decycle decycle JSON.stringify 数据丢失 bug JSON.stringify 数据丢失 bug Object.stringify 循环引用 bug Object.stringify 循环引用 bug

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Feb 7, 2020

TypeError: Converting circular structure to JSON

Object.stringify 循环引用 bug

貌似是 Object.stringify 这个方法打印的 对象存在循环引用的情况;

var obj = {
  a: "foo",
};
// undefined

obj;
//{a: "foo"}

obj.b = obj;
// {a: "foo", b: {…}}

JSON.stringify(obj);

/*
VM205:1 Uncaught TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    --- property 'b' closes the circle
    at JSON.stringify (<anonymous>)
    at <anonymous>:1:6
*/

image

@xgqfrms xgqfrms added the Object.stringify 循环引用 bug Object.stringify 循环引用 bug label Feb 7, 2020
@xgqfrms xgqfrms changed the title TypeError: Converting circular structure to JSON Object.stringify 循环引用 bug & TypeError: Converting circular structure to JSON Feb 7, 2020
@xgqfrms
Copy link
Owner Author

xgqfrms commented Feb 7, 2020

@xgqfrms
Copy link
Owner Author

xgqfrms commented Feb 7, 2020

@xgqfrms
Copy link
Owner Author

xgqfrms commented Feb 7, 2020

@xgqfrms
Copy link
Owner Author

xgqfrms commented Feb 7, 2020

@xgqfrms
Copy link
Owner Author

xgqfrms commented Feb 7, 2020

nuxt.js & TypeError: Converting circular structure to JSON

image

nuxt/nuxt#541
https://cmty.app/nuxt/nuxt.js/issues/c466

axios/axios#836 (comment)

const axios = require('axios');
const CircularJSON = require('circular-json');

axios.get(url, config)
.then((response)=>{
  let json = CircularJSON.stringify(response);
  res.send(json);
}).catch((error)=>{
  console.log(error);
});

@xgqfrms

This comment has been minimized.

Repository owner locked and limited conversation to collaborators Feb 7, 2020
@xgqfrms xgqfrms added circular-json circular-json decycle decycle labels Mar 15, 2022
@xgqfrms
Copy link
Owner Author

xgqfrms commented Mar 15, 2022

https://www.cnblogs.com/xgqfrms/p/16010654.html

https://codepen.io/xgqfrms/pen/PobVdxo?editors=1111

JSON.stringify 数据丢失 bug

const ruleForm = {
  testKey1: null,
  testKey2: undefined,
  // ❌  value 是 undefined 的 key 序列化后会被删除掉
  func: function () {
    console.log('function');
  },
  // ❌  value 是 function 的 key 序列化后会被删除掉
  desc: Symbol('symbol'),
  // ❌  value 是 symbol 的 key 序列化后会被删除掉
  testKey3: NaN,
  // ✅ value 是 NaN 的 key 序列化后会被转换成 null
  str: '',
  bool: false,
  arr: [],
  obj: {},
};

console.log(form data =, JSON.stringify(ruleForm, null, 4));
/*
form data = {
"testKey1": null,
"testKey3": null,
// ✅ value 是 NaN 的 key 序列化后会被转换成 null
"str": "",
"bool": false,
"arr": [],
"obj": {}
}
*/

@xgqfrms xgqfrms added the JSON.stringify 数据丢失 bug JSON.stringify 数据丢失 bug label Mar 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
circular-json circular-json decycle decycle JSON.stringify 数据丢失 bug JSON.stringify 数据丢失 bug Object.stringify 循环引用 bug Object.stringify 循环引用 bug
Projects
None yet
Development

No branches or pull requests

1 participant