Closed iberodev closed 10 years ago
This is a good question and for large JSON structures a good decision not to serialize in memory.
But unfortunately I'm not yet familiar with Gson. The only example I can give you is from the test cases. I'd highly apreciate if you find a solution for Gson and tell us how it works.
As I don't want to add heavy dependencies to DavidWebb, there could be a way to extend it more or less like this (pseudo-code):
interface StreamRunnable {
void doTheStreaming(OutputStream outputStream);
}
Then one could give an object implementing this interface as the body to DavidWebb and it calls the streaming method at the time it processes the body.
Thank you for your reply hgoebl. The test case is very helpful.
I'm new to GSON as well but it basically allows easily serializing an object into JSON as a stream, which allows the HttpURLConnection to start sending chunks of data as chunked Transfer Encode (without knowing the content length) as soon as each serialized token is available and take advantage of low memory-usage in the client. At least that's what I understood, so it would be great to have DavidWebb wrapper being able to send requests with chunked transfer encoding mechanism.
I think that way of extending it would be very good, but somehow DavidWebb would have to add the Transfer Encoding Http header instead the Content-Length if I'm not mistaken (apart from the Content-Encoding to optimize even more the chunks transfer).
DavidWebb should already set the header apropriately (when it's not able to find out the content length in advance), have a look at this.
If you find a way to tweak Gson so it's possible to use it without a change of DavidWebb, I'd be glad. If not, I'll try to find a workaround like mentioned above. Should not be too hard, but currently I'm very busy, so it could take some time.
Thanks again. It's great that it already deals with the headers from its content length. I'll play around with it and see if I can find a way. I'd let you know if I'm successfull.
Hi, I haven't been able to find any example of using Webb to send an http request with JSON in "streaming" mode. I know it supports JSONObject, JSONArray, etc. but what if instead these classes we are using GSON to write an object as a streaming because JSONObject "DOM-like" serializing uses too much memory?
Usually I would get the output stream from the HttpURLConnection and then write there, but since Webb wraps HttpURLConnection, could you provide an example of sending some stream as the body?
Something like: String urlSend = "http://myUrl"; Gson gson = new Gson(); MyCustomObject customObject = getCustomObjectFromSomewhere(); Webb webb = Webb.create(); //...and now how to stream and send it as body(streamed JSON) ? OutputStream outputStream = new BufferedOutputStream(getOutputStreamFromSomewhere()); JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8")); gson.toJson(customObject , MyCustomObject.class, writer); webb.put(urlSend).ensureSuccess().body(writer); //something like that?
Any reference to more examples would be greatly appreciated.
Thank you!