mirror of https://github.com/tasks/tasks
Improved JSON message serialization
parent
0622e43ff9
commit
2fcad7c3e3
@ -1,22 +1,33 @@
|
|||||||
package com.todoroo.astrid.actfm.sync.messages;
|
package com.todoroo.astrid.actfm.sync.messages;
|
||||||
|
|
||||||
import org.apache.http.entity.mime.MultipartEntity;
|
import org.apache.http.entity.mime.MultipartEntity;
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class JSONPayloadArray extends JSONArray {
|
public class JSONPayloadBuilder {
|
||||||
|
|
||||||
private final StringBuilder sb = new StringBuilder("["); //$NON-NLS-1$
|
private final StringBuilder sb = new StringBuilder("["); //$NON-NLS-1$
|
||||||
|
private final StringBuilder temp = new StringBuilder();
|
||||||
|
|
||||||
private int messageCount = 0;
|
private int messageCount = 0;
|
||||||
|
|
||||||
public boolean addMessage(ClientToServerMessage<?> message, MultipartEntity entity) {
|
public boolean addMessage(ClientToServerMessage<?> message, MultipartEntity entity) {
|
||||||
try {
|
try {
|
||||||
JSONObject serialized = message.serializeToJSON(entity);
|
JSONObject serialized = message.serializeToJSON(entity);
|
||||||
if (serialized != null) {
|
return addJSONObject(serialized);
|
||||||
sb.append(serialized.toString())
|
} catch (OutOfMemoryError e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean addJSONObject(JSONObject obj) {
|
||||||
|
try {
|
||||||
|
temp.delete(0, temp.length());
|
||||||
|
if (obj != null) {
|
||||||
|
temp.append(obj)
|
||||||
.append(","); //$NON-NLS-1$
|
.append(","); //$NON-NLS-1$
|
||||||
|
|
||||||
|
sb.append(temp);
|
||||||
|
|
||||||
messageCount++;
|
messageCount++;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
Loading…
Reference in New Issue