v3.3.0: improved error handling in JSON loading, remove GDX JSON check
This commit is contained in:
@@ -21,9 +21,6 @@
|
|||||||
|
|
||||||
package com.watabou.utils;
|
package com.watabou.utils;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.JsonReader;
|
|
||||||
import com.badlogic.gdx.utils.JsonValue;
|
|
||||||
import com.badlogic.gdx.utils.JsonWriter;
|
|
||||||
import com.watabou.noosa.Game;
|
import com.watabou.noosa.Game;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
@@ -42,7 +39,6 @@ import java.io.OutputStreamWriter;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
import java.util.zip.GZIPOutputStream;
|
import java.util.zip.GZIPOutputStream;
|
||||||
@@ -517,20 +513,7 @@ public class Bundle {
|
|||||||
}
|
}
|
||||||
String jsonString = jsonBuilder.toString();
|
String jsonString = jsonBuilder.toString();
|
||||||
|
|
||||||
Object json;
|
Object json = new JSONTokener(jsonString).nextValue();
|
||||||
try {
|
|
||||||
json = new JSONTokener(jsonString).nextValue();
|
|
||||||
} catch (Exception e){
|
|
||||||
//TODO support for v1.1.X saves has been dropped, can probably remove this soon
|
|
||||||
//if the string can't be tokenized, it may be written by v1.1.X, which used libGDX JSON.
|
|
||||||
// Some of these are written in a 'minified' format, some have duplicate keys.
|
|
||||||
// We read them in with the libGDX JSON code, fix duplicates, write as full JSON
|
|
||||||
// and then try to read again with org.json
|
|
||||||
Game.reportException(e);
|
|
||||||
JsonValue gdxJSON = new JsonReader().parse(jsonString);
|
|
||||||
killDuplicateKeysInLibGDXJSON(gdxJSON);
|
|
||||||
json = new JSONTokener(gdxJSON.prettyPrint(JsonWriter.OutputType.json, 0)).nextValue();
|
|
||||||
}
|
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
//if the data is an array, put it in a fresh object with the default key
|
//if the data is an array, put it in a fresh object with the default key
|
||||||
@@ -538,6 +521,10 @@ public class Bundle {
|
|||||||
json = new JSONObject().put( DEFAULT_KEY, json );
|
json = new JSONObject().put( DEFAULT_KEY, json );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!(json instanceof JSONObject)){
|
||||||
|
throw new JSONException("Malformed JSON Object: " + jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
return new Bundle( (JSONObject) json );
|
return new Bundle( (JSONObject) json );
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Game.reportException(e);
|
Game.reportException(e);
|
||||||
@@ -545,24 +532,6 @@ public class Bundle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void killDuplicateKeysInLibGDXJSON(JsonValue val){
|
|
||||||
HashSet<String> keys = new HashSet<>();
|
|
||||||
while(val != null) {
|
|
||||||
if (val.name != null && keys.contains(val.name)){
|
|
||||||
//delete the duplicate key
|
|
||||||
val.prev.next = val.next;
|
|
||||||
if (val.next != null) val.next.prev = val.prev;
|
|
||||||
val.parent.size--;
|
|
||||||
} else {
|
|
||||||
keys.add(val.name);
|
|
||||||
if (val.child != null){
|
|
||||||
killDuplicateKeysInLibGDXJSON(val.child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val = val.next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean write( Bundle bundle, OutputStream stream ){
|
public static boolean write( Bundle bundle, OutputStream stream ){
|
||||||
return write(bundle, stream, compressByDefault);
|
return write(bundle, stream, compressByDefault);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user