circular-dependency - 在 JSON 模式中检查循环依赖的任何工具

标签 circular-dependency circular-reference

我正在使用 JSON 文件并在 Swagger 2.0 Parser and validator 上对其进行了验证
它验证它但给出循环引用错误,是否有任何免费工具或网站来检测文件中循环引用的位置。

最佳答案

我想你要找的已经得到了回答 here .
只需打开您的浏览器控制台并输入以下 javascript :

function isCyclic(obj) {
  var keys = [];
  var stack = [];
  var stackSet = new Set();
  var detected = false;

  function detect(obj, key) {
    if (typeof obj != 'object') { return; }

    if (stackSet.has(obj)) { // it's cyclic! Print the object and its locations.
      var oldindex = stack.indexOf(obj);
      var l1 = keys.join('.') + '.' + key;
      var l2 = keys.slice(0, oldindex + 1).join('.');
      console.log('CIRCULAR: ' + l1 + ' = ' + l2 + ' = ' + obj);
      console.log(obj);
      detected = true;
      return;
    }

    keys.push(key);
    stack.push(obj);
    stackSet.add(obj);
    for (var k in obj) { //dive on the object's children
      if (obj.hasOwnProperty(k)) { detect(obj[k], k); }
    }

    keys.pop();
    stack.pop();
    stackSet.delete(obj);
    return;
  }

  detect(obj, 'obj');
  return detected;
}

然后你打电话IsCyclic(/*Json String*/) ,结果将显示循环引用的位置。

关于circular-dependency - 在 JSON 模式中检查循环依赖的任何工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43199331/

相关文章:

Python 委托(delegate)模式——如何避免循环引用?

Golang 循环导入结构

ios - Cocos2d 2.x : messed up with circular references in my game hierarchy even if I am using ARC enabled code

javascript - ES6 循环依赖

java - Micronaut 循环依赖

python - 如何使用 Python 3 循环导入

c# - 我应该如何在 .NET 中安排我的项目/类以避免循环依赖

c# - 同一程序集中的循环引用是一件坏事吗?

c++ - 带有类类型 vector 的前向声明 - 不允许指向不完整类类型的指针

angular - 使用构造函数 'Subscriber' 从对象开始将循环结构转换为 JSON