diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtil.java b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtil.java index 371ac6660bc0..d653dc22fb83 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtil.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtil.java @@ -8,6 +8,7 @@ import org.wordpress.android.util.AppLog.T; import java.util.ArrayList; +import java.util.Iterator; public class JSONUtil { private static String QUERY_SEPERATOR="."; @@ -231,4 +232,25 @@ public static JSONObject getJSONChild(final JSONObject jsonParent, final String } return jsonChild; } + + // Returns a copy of the passed JSONObject + @SuppressWarnings("unchecked") + public static JSONObject copyJSONObject(JSONObject object) { + JSONObject objectCopy = new JSONObject(); + + if (object == null) return objectCopy; + + Iterator iterator = object.keys(); + while (iterator.hasNext()) { + try { + String key = iterator.next(); + Object value = object.get(key); + objectCopy.put(key, value); + } catch (JSONException e) { + AppLog.e(T.UTILS, e); + } + } + + return objectCopy; + } } \ No newline at end of file