我正在尝试使用Json.NET创建一个配置读取器类。
这是一堂课:
public sealed class ConfigFile : Dictionary<string, object>
{
public string FileName { get; private set; }
public ConfigFile(string fileName)
{
this.FileName = fileName;
this.Load();
}
private void Load()
{
string contents = File.ReadAllText(this.FileName);
JsonTextReader reader = new JsonTextReader(new StringReader(contents));
string lastKey = "";
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName)
{
lastKey = reader.Value.ToString();
}
else
{
if (this.ContainsKey(lastKey))
{
continue;
}
this.Add(lastKey, reader.Value);
}
}
}
效果很好。然而,它读取逐行。这意味着,如果我有一个像list这样的对象,它就不能正确地解析它。
我有几个问题。
谢谢。
发布于 2015-06-18 07:32:57
我强烈建议挖掘Newtonsoft.Json的源代码。您似乎要寻找的操作在ReadInternal
方法和下面的Parse*
方法中。请看一下这里
当然,通过阅读代码,您可以学到很多东西,看起来finite state machine
主要是允许处理对象读写的抽象。
https://stackoverflow.com/questions/30908908
复制相似问题