似乎json.net正在编写无效的json,但如果是由于我的误用,我也不会感到惊讶。
它似乎在重复json的最后几个字符:
/* ... */ "Teaser":"\nfoo.\n","Title":"bar","ImageSrc":null,"Nid":44462,"Vid":17}]}4462,"Vid":17}]}
重复字符串是:
4462,"Vid":17}]}
我把它打印到控制台上,所以我不认为这是visual studio的文本可视化工具中的一个bug。
序列化代码:
static IDictionary<int, ObservableCollection<Story>> _sectionStories;
private static void writeToFile()
{
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.OpenOrCreate))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(JsonConvert.SerializeObject(_sectionStories));
}
}
#if DEBUG
StreamReader reader = new StreamReader(storage.OpenFile(STORIES_FILE, FileMode.Open));
string contents = reader.ReadToEnd();
JObject data = JObject.Parse(contents);
string result = "";
foreach (char c in contents.Skip(contents.Length - 20))
{
result += c;
}
Debug.WriteLine(result);
// crashes here with ArgumentException
// perhaps because JSON is invalid?
var foo = JsonConvert.DeserializeObject<Dictionary<int, List<Story>>>(contents);
#endif
}
我在这里做错什么了吗?还是虫子?有已知的解决方法吗?
奇怪的是,
JObject.Parse()
不会抛出任何错误。
我正在为WindowsPhone7构建一个Silverlight应用程序。