用于JSON解析的ReadAllTextFile与StreamReader(ReadAllTextFile vs. StreamReader for JSON Parsing)

编程入门 行业动态 更新时间:2024-10-18 03:37:35
用于JSON解析的ReadAllTextFile与StreamReader(ReadAllTextFile vs. StreamReader for JSON Parsing)

这更像是一个理论问题,但我很好奇这两种读取文件的方法之间的区别是什么以及为什么我要选择一种文件而不是另一种。

我正在解析JSON配置文件(从本地磁盘)。 这是一种方法:

// Uses JSON.NET Serializer + StreamReader using(var s = new StreamReader(file)) { var jtr = new JsonTextReader(sr); var jsonSerializer = new JsonSerializer(); return jsonSerializer.Deserialize<Configuration>(jtr); }

......,第二个选择......

// Reads the entire file and deserializes. var json = File.ReadAllText(file); return JsonConvert.DeserializeObject<JsonVmrConfigurationProvider>(json);

一个比另一个好吗? 是否应该使用其中一种?

同样,这更具理论性,但是,我意识到我并不真正知道它的答案,而且在线搜索并没有产生令我满意的结果。 如果文件很大(它不是),我可以看到第二个是坏的,因为它被一次性读入内存。 还有其他原因吗?

This is more of a theoretical question, but I am curious what the difference between these two methods of reading a file is and why I would want to choose one over the other.

I am parsing a JSON configuration file (from local disk). Here is one method of doing it:

// Uses JSON.NET Serializer + StreamReader using(var s = new StreamReader(file)) { var jtr = new JsonTextReader(sr); var jsonSerializer = new JsonSerializer(); return jsonSerializer.Deserialize<Configuration>(jtr); }

...and, the second option...

// Reads the entire file and deserializes. var json = File.ReadAllText(file); return JsonConvert.DeserializeObject<JsonVmrConfigurationProvider>(json);

Is one any better than the other? Is there a case where one or the other should be used?

Again, this is more theoretical, but, I realized I don't really know the answer to it, and a search online didn't produce results that satisfied me. I could see the second being bad if the file was large (it isn't) since it's being read into memory in one shot. Any other reasons?

最满意答案

通过阅读代码,您发现字符串中的deserializaton最终到达:

public static object DeserializeObject(string value, Type type, JsonSerializerSettings settings) { ValidationUtils.ArgumentNotNull(value, "value"); JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings); // by default DeserializeObject should check for additional content if (!jsonSerializer.IsCheckAdditionalContentSet()) jsonSerializer.CheckAdditionalContent = true; using (var reader = new JsonTextReader(new StringReader(value))) { return jsonSerializer.Deserialize(reader, type); } }

那就是创建一个JsonTextReader。

所以差异似乎实际上是处理大文件。

- 以前的评论脾气:

JsonTextReader重写JsonReader.Close()并处理流(如果CloseInput为true),但不仅如此。

默认情况下,CloseInput应为true,因为StringReader未在前一代码片段中显式处理

by reading the code you found that deserializaton from a string finally reach:

public static object DeserializeObject(string value, Type type, JsonSerializerSettings settings) { ValidationUtils.ArgumentNotNull(value, "value"); JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings); // by default DeserializeObject should check for additional content if (!jsonSerializer.IsCheckAdditionalContentSet()) jsonSerializer.CheckAdditionalContent = true; using (var reader = new JsonTextReader(new StringReader(value))) { return jsonSerializer.Deserialize(reader, type); } }

That is the creation of a JsonTextReader.

So the difference seems to effectively be the handling of huge files.

-- previous comment temper:

JsonTextReader overrides JsonReader.Close() and handles the stream (if CloseInput is true), but not only.

CloseInput should be true by default as the StringReader is not explicitly disposed in previous fragment of code

更多推荐

本文发布于:2023-08-01 00:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ReadAllTextFile   JSON   Parsing   StreamReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!