Why have warning Newtonsoft.Json.JsonConvert.DeserializeObjectAsync

ⅰ亾dé卋堺 提交于 2019-12-07 16:45:57

问题


I'm using JSON.NET version 6.0.1 and here my code below

var text = await FileHelper.ReadFileAsync(folderSetting, fileName);
var items = await JsonConvert.DeserializeObjectAsync<ObservableCollection<ItemModel>>(text);

But my Visual Studio Warning

Warning 7 'Newtonsoft.Json.JsonConvert.DeserializeObjectAsync(string)' is obsolete: 'DeserializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to deserialize JSON asynchronously: Task.Factory.StartNew(() => DeserializeObject(value))'


回答1:


The library authors decided that it was not the responsibility of the library to provide asynchronous wrappers and marked them obsolete. (see http://blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx). In future versions these methods will be removed. You should do something like this instead:

var result = await 
    Task.Factory.StartNew(() => JsonConvert.DeserializeObject<MyObject>(jsonText));


来源:https://stackoverflow.com/questions/22726970/why-have-warning-newtonsoft-json-jsonconvert-deserializeobjectasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!