Bug 709339 - First mostly functional drop of native Sync. a=mobile

This commit is contained in:
Richard Newman
2012-01-14 09:20:31 -08:00
parent 83e8e5d5ed
commit 5fe8ffe843
89 changed files with 5497 additions and 2295 deletions

View File

@@ -45,6 +45,7 @@ import java.io.StringReader;
import java.util.Map;
import java.util.Map.Entry;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@@ -59,8 +60,7 @@ public class ExtendedJSONObject {
public JSONObject object;
public static Object parse(InputStreamReader reader) throws IOException, ParseException {
Object parseOutput = new JSONParser().parse(reader);
private static Object processParseOutput(Object parseOutput) {
if (parseOutput instanceof JSONObject) {
return new ExtendedJSONObject((JSONObject) parseOutput);
} else {
@@ -68,6 +68,15 @@ public class ExtendedJSONObject {
}
}
public static Object parse(String string) throws IOException, ParseException {
return processParseOutput(new JSONParser().parse(string));
}
public static Object parse(InputStreamReader reader) throws IOException, ParseException {
return processParseOutput(new JSONParser().parse(reader));
}
public static Object parse(InputStream stream) throws IOException, ParseException {
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
return ExtendedJSONObject.parse(reader);
@@ -119,6 +128,31 @@ public class ExtendedJSONObject {
return (Long) this.get(key);
}
/**
* Return a server timestamp value as milliseconds since epoch.
* @param string
* @return A Long, or null if the value is non-numeric or doesn't exist.
*/
public Long getTimestamp(String key) {
Object val = this.object.get(key);
// This is absurd.
if (val instanceof Double) {
double millis = ((Double) val).doubleValue() * 1000;
return new Double(millis).longValue();
}
if (val instanceof Float) {
double millis = ((Float) val).doubleValue() * 1000;
return new Double(millis).longValue();
}
if (val instanceof Number) {
// Must be an integral number.
return ((Number) val).longValue() * 1000;
}
return null;
}
public boolean containsKey(String key) {
return this.object.containsKey(key);
}
@@ -139,6 +173,9 @@ public class ExtendedJSONObject {
public ExtendedJSONObject getObject(String key) throws NonObjectJSONException {
Object o = this.object.get(key);
if (o == null) {
return null;
}
if (o instanceof ExtendedJSONObject) {
return (ExtendedJSONObject) o;
}
@@ -174,4 +211,15 @@ public class ExtendedJSONObject {
public Iterable<Entry<String, Object>> entryIterable() {
return this.object.entrySet();
}
public org.json.simple.JSONArray getArray(String key) throws NonArrayJSONException {
Object o = this.object.get(key);
if (o == null) {
return null;
}
if (o instanceof JSONArray) {
return (JSONArray) o;
}
throw new NonArrayJSONException(o);
}
}