如何序列化和反序列化工作的内部?序列化、工作

2023-09-08 08:35:54 作者:折奉

我有很多的遗留code其中 JSON 手动使用for循环解析。这需要 O(N),一般的时间。我知道 json.net 将在以下方面更好地时间空格,但获取它是如何工作的深入了解,将有助于我作出其是否值得的实际继续前进,投入的时间和人力来移动一切json.net一个明智的决定。

I have a lot of legacy code where json is parsed manually by using a for loop. This takes O(n) time in general. I know json.net would be better in terms of time and space but gaining an insight about how it works, would help me make an informed decision whether its worth the effort to actually go ahead and invest the time and man power to move everything to json.net.

推荐答案

要套用你的问题变成一个更一般的,让我们假设你要找的建议而JSON序列化的实现,选择了各种方案。

To paraphrase your question into a more general one, lets assume you were looking for advice on which JSON serialization implementation to choose for various scenarios.

我所知道的三个明显的回答这个问题:

I'm aware of three obvious answers to this question:

NewtonSoft JSON.NET 提供的功能和出色的性能丰富 NewtonSoft JSON.NET Provides an abundance of features and excellent performance 提供简单和炽烈的性能 避免了对第三方库的依赖,但显著慢

如果你不关心的第三方库的依赖,去的第一选择,因为它会给你的性能和功能。如果你不需要一吨的特点,评估ServiceStack.Text无论做什么,你需要它(如果不确定,请与JSON.NET)。在任何其他情况下,坚持你所拥有的。

If you don't care about the 3rd party library dependency, go for the first option as it will give you performance and functionality. If you don't need a ton of features, evaluate whether ServiceStack.Text does what you need it to (if unsure, go with JSON.NET). In any other case, stick with what you have.

另外,不要花时间让你的code更快地更换您的JSON code之前,你知道,这个特殊的区域是性能瓶颈(或其他权证替代,例如,因为它是一个维护问题)。如果你正在考虑更换code以提高性能,找出一些方法来分析和衡量你目前的$ C $对相似的场景使用备用实施或库,以避免基于假设的决定℃。

Also, don't spend time making your code faster by replacing your JSON code before you know that this particular area is a performance bottleneck (or otherwise warrants replacement, e.g. because it's a maintenance problem). If you are considering replacing code to gain performance, isolate a few methods to profile and benchmark your current code against similar scenarios using the alternate implementation or library, in order to avoid making a decision based on assumptions.

最后,知道它的内部工作不应该是在你的决策过程中的一个因素,除非您特别计划能够修改它的源代码(或其他需要能够理解它)。

Last, knowing how it works internally should not be a factor in your decision process unless you specifically are planning to be able to modify the source of it (or otherwise need to be able to understand it).