Bug 708683 - Improve JSON generation by using JSONStringer. r=pcwalton a=java-only
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 09 Dec 2011 22:58:10 -0500
changeset 84077 73979783bac9a681856af5feae6093b8a3f9a412
parent 84076 4850eb9ce32a7c9aac6f7ae509b6a9e868a5203f
child 84080 33984b2775542b6b82d0d0fd0c94080b94fd82ab
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspcwalton, java-only
bugs708683
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 708683 - Improve JSON generation by using JSONStringer. r=pcwalton a=java-only
mobile/android/base/gfx/ViewportMetrics.java
--- a/mobile/android/base/gfx/ViewportMetrics.java
+++ b/mobile/android/base/gfx/ViewportMetrics.java
@@ -43,23 +43,26 @@ import android.graphics.PointF;
 import android.graphics.Rect;
 import android.graphics.RectF;
 import org.mozilla.gecko.FloatUtils;
 import org.mozilla.gecko.gfx.FloatSize;
 import org.mozilla.gecko.gfx.LayerController;
 import org.mozilla.gecko.gfx.RectUtils;
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.json.JSONStringer;
 import android.util.Log;
 
 /**
  * ViewportMetrics manages state and contains some utility functions related to
  * the page viewport for the Gecko layer client to use.
  */
 public class ViewportMetrics {
+    private static final String LOGTAG = "GeckoViewportMetrics";
+
     private FloatSize mPageSize;
     private RectF mViewportRect;
     private PointF mViewportOffset;
     private float mZoomFactor;
 
     public ViewportMetrics() {
         mPageSize = new FloatSize(LayerController.TILE_WIDTH,
                                   LayerController.TILE_HEIGHT);
@@ -227,21 +230,27 @@ public class ViewportMetrics {
         result.mPageSize = mPageSize.interpolate(to.mPageSize, t);
         result.mZoomFactor = FloatUtils.interpolate(mZoomFactor, to.mZoomFactor, t);
         result.mViewportRect = RectUtils.interpolate(mViewportRect, to.mViewportRect, t);
         result.mViewportOffset = PointUtils.interpolate(mViewportOffset, to.mViewportOffset, t);
         return result;
     }
 
     public String toJSON() {
-        return "{ \"x\" : " + mViewportRect.left +
-               ", \"y\" : " + mViewportRect.top +
-               ", \"width\" : " + mViewportRect.width() +
-               ", \"height\" : " + mViewportRect.height() +
-               ", \"pageWidth\" : " + mPageSize.width +
-               ", \"pageHeight\" : " + mPageSize.height +
-               ", \"offsetX\" : " + mViewportOffset.x +
-               ", \"offsetY\" : " + mViewportOffset.y +
-               ", \"zoom\" : " + mZoomFactor +
-               " }";
+        try {
+            return new JSONStringer().object()
+                .key("x").value(mViewportRect.left)
+                .key("y").value(mViewportRect.top)
+                .key("width").value(mViewportRect.width())
+                .key("height").value(mViewportRect.height())
+                .key("pageWidth").value(mPageSize.width)
+                .key("pageHeight").value(mPageSize.height)
+                .key("offsetX").value(mViewportOffset.x)
+                .key("offsetY").value(mViewportOffset.y)
+                .key("zoom").value(mZoomFactor)
+                .endObject().toString();
+        } catch (JSONException je) {
+            Log.e(LOGTAG, "Error serializing viewportmetrics", je);
+            return "";
+        }
     }
 }