Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why have warning Newtonsoft.Json.JsonConvert.DeserializeObjectAsync

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))'

like image 787
RaymondLe Avatar asked Dec 19 '22 16:12

RaymondLe


1 Answers

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));
like image 147
Peter Ritchie Avatar answered Jan 27 '23 04:01

Peter Ritchie