Browse Source

Merge pull request #24 from square/serialize-on-main-thread

Serializing to json immediately in JsonTypedBytes constructor.
Patrick Forhan 7 years ago
parent
commit
b0241b3d36
1 changed files with 5 additions and 12 deletions
  1. 5
    12
      modules/http/src/retrofit/http/HttpRequestBuilder.java

+ 5
- 12
modules/http/src/retrofit/http/HttpRequestBuilder.java View File

@@ -1,6 +1,5 @@
1 1
 package retrofit.http;
2 2
 
3
-import com.google.inject.Inject;
4 3
 import com.google.inject.name.Named;
5 4
 import org.apache.http.NameValuePair;
6 5
 import org.apache.http.client.methods.HttpUriRequest;
@@ -18,7 +17,6 @@ import java.util.ArrayList;
18 17
 import java.util.HashSet;
19 18
 import java.util.List;
20 19
 import java.util.Set;
21
-import java.util.logging.Level;
22 20
 import java.util.logging.Logger;
23 21
 import java.util.regex.Matcher;
24 22
 import java.util.regex.Pattern;
@@ -47,7 +45,7 @@ final class HttpRequestBuilder {
47 45
   private List<NameValuePair> nonPathParams;
48 46
   private RequestLine requestLine;
49 47
   private TypedBytes singleEntity;
50
-  
48
+
51 49
   HttpRequestBuilder(Gson gson) {
52 50
     this.gson = gson;
53 51
   }
@@ -255,10 +253,10 @@ final class HttpRequestBuilder {
255 253
   }
256 254
 
257 255
   private class JsonTypedBytes implements TypedBytes {
258
-    private Object obj;
256
+    private String json;
259 257
 
260 258
     public JsonTypedBytes(Object obj) {
261
-      this.obj = obj;
259
+      this.json = gson.toJson(obj);
262 260
     }
263 261
 
264 262
     @Override public MimeType mimeType() {
@@ -266,17 +264,12 @@ final class HttpRequestBuilder {
266 264
     }
267 265
 
268 266
     @Override public int length() {
269
-      return toJson().length();
267
+      return json.length();
270 268
     }
271 269
 
272 270
     @Override public void writeTo(OutputStream out) throws IOException {
273
-      final String str = toJson();
274 271
       // TODO use requested encoding?
275
-      out.write(str.getBytes("UTF-8"));
276
-    }
277
-
278
-    protected String toJson() {
279
-      return gson.toJson(obj);
272
+      out.write(json.getBytes("UTF-8"));
280 273
     }
281 274
   }
282 275
 }

Loading…
Cancel
Save