这更像是一个理论问题,但我很好奇这两种读取文件的方法之间的区别是什么以及为什么我要选择一种文件而不是另一种。
我正在解析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
更多推荐
发布评论