简体   繁体   English

为什么 JsonElement 是 struct 类型?

[英]why JsonElement is struct type?

in many JSON parsers, the element of json is presented as object(reference) type.在许多 JSON 解析器中,json 的元素呈现为对象(引用)类型。

However in C#, System.Text.Json JsonElement is struct type.然而在C#中,System.Text.Json JsonElementstruct类型。 Why JsonElement is struct type?为什么 JsonElement 是 struct 类型?

I'm worried about this could cause unnecessary memory copy.我担心这会导致不必要的 memory 复制。
What if JsonElement has very big data?如果 JsonElement 有非常大的数据怎么办? what if JsonElement has array that contains over 1000 objects?如果 JsonElement 的数组包含超过 1000 个对象怎么办?

If you look at the source code you'll see that JsonElement is very lightweight.如果查看源代码,您会发现JsonElement非常轻量级。 It only has two fields: a reference to the containing document, and an index into the document.它只有两个字段:对包含文档的引用和文档的索引。 In particular, it doesn't directly contain all the data for the element.特别是,它直接包含元素的所有数据。

This avoids creating a lot of memory churn, as it's efficient to (for example) iterate over all the elements in a document, without it having to create large numbers of objects.避免了产生大量 memory 流失,因为(例如)迭代文档中的所有元素是高效的,而不必创建大量对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM