-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async methods in JsonConvert should be removed #66
Comments
+1 |
Agreed, until true async versions are implemented, current "async" methods should be removed. |
I can't remove them because that will be a breaking change. I've added a note to the intelli-sense that serialization will happen on a new thread. Edit: They're now marked as obsolete. |
Could I suggest running the serialization synchronously and returning a completed Task as a non-breaking change? |
I agree that you can't remove these methods because some people probably use them, but I think you should mark them as deprecated. |
I agree that they need to be marked Obsolete. They are also misleading because they share the naming convention of async/await and so they appear, at first glance, good candidates for improving scalability. They don't. The intelli-sense doesn't help (as I just ran into this). "Deserialization will happen on a different thread" isn't enough. Without looking at the implementation, it's still could imply async/await. If you don't want to mark them Obsolete right away, consider making the intelli-sense more direct like "WARNING: This will be marked Obsolete in a future release. Please use the synchronous version instead." |
A dumb question: What to use instead? I am using (Assembly version) version 6.0.0.0? Any hints? Docs? |
@Injac, you should use the synchronous version. If you need to offload it to a new thread, do it explicitly with |
Thanks for the tip. This is what I have done unfortunately. Well, next time better read the warnings in VS. |
I'm afraid, i didnt get the point about scalability (in the article http://blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx) while calling sync methods asyncly. It is okay if they would "using a bit more, since there’s overhead incurred to scheduling something", unless ui is not blocked. For example, i have a fat-ass Json which i want to parse: it is better if parsing would take 2 seconds on the background thread than 1 second on the ui thread, isnt it? Or is it purely about resource consumption, kinda "2 seconds of work is more than 1 second of work" ? |
And second question. Here ( http://developer.nokia.com/community/wiki/Asynchronous_Programming_For_Windows_Phone_8 ) it is said that using TaskCompletionSource would be enough for making sync call async. So, if i'll use private async Task ExecuteAsync(RestRequest request) would it be okay from both Scalability and Offloading from the previous article? |
@Nearga You should only worry about scalability when you're writing an application which creates a lot of threads and consumes lots of CPU power. It's common for servers generating responses for thousands of clients. When you're writing a simple app for Windows Phone 8, you normally worry only about offloading. |
@Athari Okay, but what about first question? And about whole topic in general? So, Async methods were removed because of crossplatform Newtonsoft.Json usage? It is ok to use public static Task SerializeObjectAsync(object value, Formatting formatting, JsonSerializerSettings settings) on the WP? Or should i add TaskCompletionSource like in previous post? |
@Nearga, yes, it's OK to offload the serialization to another thread in a client app, in order to keep the UI thread responsive. But that's something you should do yourself, not something that should be in the API, because an async method suggests that the method will scale well (which is important for web apps, for instance), even though it won't (since it uses a thread). |
@thomaslevesque Thanks, now its clear. So, async is for io operations (not blocking ui, running in the same thread) and Tasks are for cpu-heavy things (running in another thread)? But how (in theory) to make the serialization well-scalable? PS: sorry, edited question, restored it. |
@Nearga, no. A Task is just a way to represent an operation that will complete at a later time; it could be running on a separate thread, or not; it could also just be waiting (in a non-blocking way) for the completion of an IO operation. When keeping the UI responsive is the primary concern, CPU-heavy tasks should be offloaded to another thread. But when all threads are equally important (such as in a server environment), you gain nothing by offloading to another thread, and you incur useless overhead. On the other hand, if a Task is using asynchronous IO, then it's not using a thread, so it's good for scalability. |
@thomaslevesque So, in general, well-scalable functions are just based on lowlevel async calls? Kinda Reader.Read() } and "well-scalable" is DeserializeAsync() await Reader.ReadAsync() } |
@Nearga, basically, to make a method scalable, it must not do any blocking calls that cause the thread to be blocked doing nothing. When you call Stream.Read, the CPU does nothing until the IO operation completes, but the thread is still using resources; threads are a valuable resource on a server, you can't afford to waste them by making them wait uselessly... On the other hand, if you use Stream.ReadAsync, the thread can be reused to do something else while the IO operation completes. |
@thomaslevesque Great, thanks. Now i understand, why it was removed. |
I don't get this. |
It could, but it's not that simple. I tried to do it a few years ago, but gave up after a while. Basically, the problem is that while the sync and async implementations are almost identical, it's hard to extract the common code, so you end up with almost duplicated implementations, which is hard to maintain. And in this case, you need to add async implementations all the way down to JsonReader. In the typical case, JSON documents are not very large, so the benefit of an async implementation would be marginal, compared to the cost of writing and maintaining the implementation. |
Now that async support is implemented, couldn't these methods be re-introduced but now with a real async implementation? I'm not sure how to tackle this though... I have a DeserializeObjectFromStream method like this:
How would I go about changing this to use the JObject.LoadAsync method, but still uses the provided serializersettings? |
The helper methods read and write strings which don't have any blocking IO. There is no point. |
Thanks for the response! But what about that code example in my previous comment? |
This is in regards to |
@TylerBrinkley I intend to make that my project for the holiday season. |
@JonHanna Any news on async support for the |
@petterton my OSS output is a bit down of late as my new son isn't as keen on me coding while he sleeps on my chest as my second-youngest was (my output actually went up when second-youngest was born, as I had long periods where coding was the only thing I could do, while he snuggled). It's still on my to-do list, but as I've less coding time right now nobody should see it as a licked cookie if they want to do it. |
Now that |
Not terribly. I've a start made, but haven't had a chance to do the necessary tests |
Any progress on this @JonHanna ? I am very interested in an asynchronous serializer! |
System.Text.Json is a way to async serialization-deserialization now. |
@sungam3r does this mean there is no plans to add a async |
I'm 99,99% sure. Just look at the history of this repo. Last significant commit was in November 2019. 475 open issues, 51 open PRs and these numbers are constantly growing. I'm not afraid to say this repo is dead. I have been observing its evolution for quite some time, to be entitled to assert this. The only thing I don't understand is why the maintainers won't make an official statement about status of Newtonsoft.Json repo so that people no longer waste their time and energy on creating PRs that no one will ever merge. I read the official MSDN documentation on comparing Newtonsoft.Json and System.Text.Json and there the issue of obsoleting the former was diplomatically bypassed. It was obvious, though. Don't get me wrong, I don't blame anyone for this situation. This is a natural course of things, some projects go down in history, some appear to replace them. |
Because of a question on SO, I went to investigate how does async work in JSON.NET. When I looked at the code (specifically JsonConvert.cs), I noticed that the async methods (like
SerializeObjectAsync()
) just call the synchronous version usingTask.Factory.StartNew()
.This is generally not a good idea, because
Async
methods are usually more scalable than their synchronous version, but this isn't true for your implementation. For more information (and a better explanation), see Stephen Toub's article Should I expose asynchronous wrappers for synchronous methods?Because of that, I think you should remove the Async versions of your methods from the library.
Where adding
Async
versions would make sense is when working with streams, but doing so would require a lot of changes in classes likeJsonTextReader
(and I'm not sure how to do it without duplicating a lot of code in a way that doesn't break JSON.NET for older versions of .Net).The text was updated successfully, but these errors were encountered: