自定义 JSON.stringify 无法对整个对象进行 Stringify,但在迭代一层深度时可以工作

发布于 2024-11-05 10:50:23 字数 1080 浏览 0 评论 0原文

希望有人能够发现错误,因为我遇到了麻烦

好吧,我为自定义大型对象构建了自己的 JSON.stringify。它可能不完全符合某些边缘情况的规范,但仅适用于我自己构建的大型对象的字符串化。

嗯,它有效,并且对于大多数对象都有效,但是我有一个对象正在尝试字符串化,但它失败并在退出之前打印此内容:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
undefined

不是很有帮助。该对象很好,因为对 JSON.stringify(object) 的常规调用工作正常,当我使用 for (var x in obj) if (obj.hasOwnProperty(x )) { myStringify(obj); } 工作得很好,但是如果我在对象的顶层调用它,那就见鬼了......这对我来说并没有什么意义,我唯一能想到的就是级别如果递归以某种方式破坏了某些东西......

解析器:https://gist.github.com/958776 - 我正在调用的 stringify 函数
ObjectIterator.js : https://gist.github.com/958777 - 主要提供异步迭代

< strong>编辑 因此,我对对象进行了一层深度的迭代,并将生成的字符串与 JSON.stringify(sameLevelDeep) 的字符串进行比较,发现它们是相等的。由于输出相等,我不确定这就是我解析某些东西的方式,但可能它是一个很大的对象或者递归量如此之高?

编辑2 所以,我想我“解决”了这个问题。我没有将每 25 次迭代推送到下一个事件循环,而是每 5 次推送一次。我不确定为什么这会产生影响,但确实如此......我想现在的问题是“为什么这会产生影响”?

Hoping someone can spot the error, because I'm having trouble

Alright, I built my own JSON.stringify for just custom large objects. It may not be exactly to specification for some edge case things, but's only meant for stringify on large objects that I'm building myself.

Well, it works, and works well for most objects, but I have an Object I'm trying to stringify and it's failing and printing this before exiting:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
undefined

Not very helpful. The object is fine because the regular call to JSON.stringify(object) works fine, and when I iterate over the object with for (var x in obj) if (obj.hasOwnProperty(x)) { myStringify(obj); } that works fine, but if I call it on the top level of the object, it goes to hell... It doesn't really make sense to me, and the only thing I can think of is the level if recursion is somehow breaking something...

The Parser : https://gist.github.com/958776 - The stringify function I'm calling
ObjectIterator.js : https://gist.github.com/958777 - Mostly to provide the asynchronous iteration

Edit So, I iterated over the object one level deep and compared the resulting string to the string of JSON.stringify(sameLevelDeep) and they're equal. Since the output is equal, I'm not sure that it's how I'm parsing something, but possible that it's such a large object or the amount of recursion is so high?

Edit 2 So, I "fixed" the problem, I guess. Instead of every 25th iteration being pushed to the next event loop, I push every fifth. I'm not sure why this would make a difference but it does... I guess the question is now "Why does that make a difference"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2024-11-12 10:50:23

好吧,除了帮助一个非常具体的人的一个非常具体的问题之外,我想把这个问题带到另一个地方,这也可能会消除你的问题,也许会帮助其他人。

由于您没有具体说明为什么要经历这个过程,因此我将不得不将其分解并猜测 - 并为每个猜测的想法提供解决方案。

1. (浏览器)您正在尝试使用 JavaScript 来处理数据,并向用户提供结果

下载至少几兆字节的原始数据(“这些对象中的一些对象有 5-1000 万个字符” ") 在网页上处理和显示结果远非最佳,您可能应该在服务器端执行此操作并下载预先计算的结果。

此外,无论你在做什么,JavaScript 都不支持线程。
setTimeout(1, function() { JSON.stringify(data); }); 应该与您正在做的事情没有太大不同。

2. (浏览器)您正在尝试显示下载的内容

您应该尝试使用内置 JSON.stringify 方法下载较小的块,而不是整个 10+ 百万个字符的内容。

3. (非浏览器)您正在尝试将 JavaScript 用于需要线程的应用程序

您应该考虑为此应用程序使用不同的编程语言。

总结

我认为你爬错了山,你可以在不流汗的情况下绕着山走,达到同样的效果。如果你想爬山找乐子,那里有很多山需要你——但不是这座山。

翻译:在架构上努力消除障碍,而不是试图解决它,如果你想解决一个问题,那么有些问题需要解决——但不是这个。

Okay well, beyond it being a very specific question helping a very specific person, I would like to take this to a different place, that might also remove your problem and maybe help others.

Since you are not specifying why you are going through this process, I will have to break it down and guess -- and provide a solution for each guessed idea.

1. (Browser) You are trying to use JavaScript to crunch data, and provide the user with a result

Downloading at least several megabytes of raw data ("some of these objects are 5-10million characters") on a webpage to process and display a result is far from optimal, you should probably be doing this operation on the server side and download the pre-calculated result.

Besides, no matter what you are doing, JavaScript does not support threads.
setTimeout(1, function() { JSON.stringify(data); }); shouldn't be much different from what you are doing.

2. (Browser) You are trying to display the downloaded content

You should attempt downloading smaller chunks instead of the whole 10+ million character content using the built-in JSON.stringify method.

3. (Non-browser) You are trying to use JavaScript for an application that requires threading

You should consider using a different programming language for this application.

In summary

I think you are climbing the wrong mountain, you can achieve the same thing walking around it without breaking sweat. If you want to climb a mountain for kicks, there are mountains out there that need it -- but it's not this one.

Translation: Work on the architecture to obsolete the obstacle instead of trying to solve it, if you want to solve a problem there are problems that need a solving -- but it's not this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文