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))'
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));
Related
I am creating an app in Xamarin and am having issues querying a general JSON document from my CosmosDB. I am able to query my DB with a known structure (very similar to what we see in the Xamarin ToDo Example):
public async static Task<List<ToDoItem>> GetToDoItems()
{
var todos = new List<ToDoItem>();
if (!await Initialize())
return todos;
**var todoQuery = docClient.CreateDocumentQuery<ToDoItem>(
UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.Where(todo => todo.Completed == false)
.AsDocumentQuery();**
while (todoQuery.HasMoreResults)
{
var queryResults = await todoQuery.ExecuteNextAsync<ToDoItem>();
todos.AddRange(queryResults);
}
return todos;
}
The problem I see with this "code fixed scheme" approach is that if the scheme of your JSON file changes throughout development, older versions of code will overwrite the newer scheme in CosmosDB since writes to the DB are on a document level and not a property level. Instead, it would be helpful for older versions of the code to be able to pull down the latest scheme and work with the properties that it knows about without having to force the user to update.
Does anyone know how to query a schema-less JSON document out of CosmosDB? Thanks!
Use JObject as the type to query a schema-less JSON document out of CosmosDB, and use following code to pull the raw JSON data into your object:
JsonConvert.DeserializeObject<Class>(JSONString);
The AngularJS documentation has a Deprecation Notice for the $http success and error methods. Is there a specific reason this abstraction was removed from the library?
The problem was that .success and .error methods are not chainable because they ignore return values. This caused problems for people familiar with chaining and encouraged poor code from people unfamiliar with chaining. Witness all the examples on StackOverflow that use the deferred anti-pattern.
To quote one of the AngularJS team:
IMO .success and .error were a bad bit of API design in the first place. This issue highlights a number of situations where developers get confused because they either expect .success and .error to work the same way as .then or vice versa.
In a perfect world I would rather just ditch these $http specific "promises". Instead we could encourage developers to use the standard $q promise API .then and .catch. There is very little benefit IMO in working with explicit parameters over working with the response object.
— AngularJS Issue #10508 $http .success/.error dissimilar from how .then works.
Deprecation Notice (v1.5)
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
— AngularJS $http Service API Reference -- deprecation notice
UPDATE
The deprecated .success and .error methods have been removed from AngularJS 1.6.
Due to b54a39, $http's deprecated custom callback methods - .success() and .error() - have been removed. You can use the standard .then()/.catch() promise methods instead, but note that the method signatures and return values are different.
$http(...)
.then(function onSuccess(response) {
// Handle success
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
...
}).catch(function onError(response) {
// Handle error
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
...
});
— AngularJS Developer Guide - Migrating to v1.6 - http
The pattern that javascript it using related to promises is only with .then(successCallback, errorCallback), so they are probably aiming to use js pattern.
The AngularJS documentation has a Deprecation Notice for the $http success and error methods. Is there a specific reason this abstraction was removed from the library?
The problem was that .success and .error methods are not chainable because they ignore return values. This caused problems for people familiar with chaining and encouraged poor code from people unfamiliar with chaining. Witness all the examples on StackOverflow that use the deferred anti-pattern.
To quote one of the AngularJS team:
IMO .success and .error were a bad bit of API design in the first place. This issue highlights a number of situations where developers get confused because they either expect .success and .error to work the same way as .then or vice versa.
In a perfect world I would rather just ditch these $http specific "promises". Instead we could encourage developers to use the standard $q promise API .then and .catch. There is very little benefit IMO in working with explicit parameters over working with the response object.
— AngularJS Issue #10508 $http .success/.error dissimilar from how .then works.
Deprecation Notice (v1.5)
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
— AngularJS $http Service API Reference -- deprecation notice
UPDATE
The deprecated .success and .error methods have been removed from AngularJS 1.6.
Due to b54a39, $http's deprecated custom callback methods - .success() and .error() - have been removed. You can use the standard .then()/.catch() promise methods instead, but note that the method signatures and return values are different.
$http(...)
.then(function onSuccess(response) {
// Handle success
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
...
}).catch(function onError(response) {
// Handle error
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
...
});
— AngularJS Developer Guide - Migrating to v1.6 - http
The pattern that javascript it using related to promises is only with .then(successCallback, errorCallback), so they are probably aiming to use js pattern.
The Aurelia fetch client docs have a basic example of getting json data:
bind() {
let client = new HttpClient();
return client.fetch('data.json')
.then(response => response.json())
.then(data => {
console.log(data[1]);
});
}
The above works fine yet the following does not:
files = [];
bind() {
let client = new HttpClient();
return client.fetch('data.json')
.then(response => response.json())
.then(files => this.files = files);
}
Gulp now complains "error TS2322: Type 'Response' is not assignable to type 'any[]'."
Even more odd is that I now get XHR 404 errors in my console. This makes no sense; the data.json file had no issue being found and fetched the first time. The only difference in the second code snippet is that instead of logging the data to the console, I'm actually trying to do something with it.
I believe your specific issue may be caused by an older version of TypeScript (2.1, the latest is 2.5). If you have an opportunity to do so, you can try updating it.
response in the statement response => is of type Response defined by Aurelia. When you are running this.files = files, it seems like TypeScript thinks that files is of type Response. You are already implicitly declaring this.files as type any[], so the assignment is not allowed.
You can get around this by setting an explicit type for files, or even just using any:
.then((files: any[]) => this.files = files);
I would try to avoid using any to get around type safety and work with the types, but the issue you're running into appears to be a bug in the version of TypeScript and/or Aurelia that you're using.
As I went trough the client channel code (inside the phoenix.js file) I saw that it uses ES6. Sample code:
let chan = socket.chan("rooms:123", {token: roomToken})
// chan.on("new_msg", msg => console.log("Got message", msg) )
// $input.onEnter( e => {
// chan.push("new_msg", {body: e.target.val})
// .receive("ok", (message) => console.log("created message", message) )
// .receive("error", (reasons) => console.log("create failed", reasons) )
// .after(10000, () => console.log("Networking issue. Still waiting...") )
this.onError( reason => {
this.socket.log("channel", `error ${this.topic}`, reason)
this.state = CHAN_STATES.errored
this.rejoinTimer.setTimeout()
})
That means that it won't run natively in IE and Safari (ate least). Shouldn't I use some kind of polyfills?
What's the best approach/polyfill?
Also, I'm under the impression that polyfills cover classes/let/...but not arrow functions/new string interpolation. Should I change those myself?
Since ES6 adds new syntax to the language, there is no way to polyfill arrow functions.
However, when creating a new application, Phoenix installs a library called Brunch which is used for combining assets. It includes a wrapper for Babel which will transpile ES6 to JavaScript that will run in the browser.
If you look at priv/static/app.js (the compiled output) instead of web/static/app.js (the source) then you will see it does not have the new ES6 syntax.
One thing you may find if you use certain functions then you may need to include babel-polyfill.js which you can read about at https://babeljs.io/docs/advanced/caveats/
This was introduced in Phoenix 0.10.0 and you can read more about it in the announcement post http://www.phoenixframework.org/v0.14.0/blog/phoenix-0100-released-with-assets-handling-generat