public static void applyFixup(final Object object, final JSONArray fixup,
final JSONArray original) throws JSONException {
int last = fixup.length() - 1;
if (last < 0) {
throw new JSONException("fixup path must contain at least 1 reference");
}
final Object originalObject = traverse(object, original, false);
final Object fixupParent = traverse(object, fixup, true);
// the last ref in the fixup needs to be created
// it will be either a string or number depending on if the fixupParent is a
// JSONObject or JSONArray
if (fixupParent instanceof JSONObject) {
String objRef = fixup.optString(last, null);
if (objRef == null) {
throw new JSONException("last fixup reference not a string");
}
((JSONObject) fixupParent).put(objRef, originalObject);
} else {
int arrRef = fixup.optInt(last, -1);
if (arrRef == -1) {
throw new JSONException("last fixup reference not a valid array index");
}
((JSONArray) fixupParent).put(arrRef, originalObject);
}
}