开发者

How can you stream results as json string downloads?

I'm interested in presenting results faster in my mobile app. Is it possible to stream results out as the string downloads? I'm thinking about implementing an IObservable to push out the results as they are downloaded, but I don't know what algorithm to use to properly piece together the data which could be incomplete at any given point.

Hop开发者_JAVA百科e that was clear enough.

CLARIFICATION: Guess it wasn't clear enough. My issue is the string downloaded is quite long. It can often take 15-20 seconds or more to download. I want to reflect changes faster to my user, so I would like to use reactive extensions to pump out entities as soon as a complete one is received.

My issue is I dont know how to build the parser that can pick out complete entities from an incomplete response string.


I'm going to go out on a limb here and say that it's technically possible, but you won't get it work with any existing framework without a tremendous amount of hacking. Every element in JSON must be properly closed before it is considered valid. If you try to deserialize half a JSON string it will not be a syntactically valid JSON string. It's not even just adding a few "}" closing tags, since you might have partial elements nested in the partial strings. So you'll have to sanitize your current JSON string for partial elements, manually close every remaining JSON object and only then deserialize.

In WP7 JSON deserialization even for relatively simple objects in the high performance frameworks (JSON.Net) takes a few hundred milliseconds. It's pretty pointless to deserialize your entire accumulated JSON string every time a new data chunk is available from the server. Since by the time you're done deserliaizing there's a new JSON string that'll need to be deserialized.

If you're really interested in streaming webservice results to a client so it can read partial results I don't think JSON is the format you'd need. Probably a simpler CSV format would make more sense.

It might be best if instead of using Chunking+CSV, that you simply consider creating 2 types of Webmethods: InitialLoad webmethod and Paging webmethod. That is how the majority of mobile webservices I'm familiar with deal with this problem.


EDIT: Just noticed you meant WP7. Check out Reactive Extensions for .NET http://msdn.microsoft.com/en-us/devlabs/ee794896

for JS

http://codebetter.com/matthewpodwysocki/2010/03/15/introduction-to-the-reactive-extensions-for-javascript-wikipedia-lookup/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜