v1.1.0: Switched to libGDX's JSON library, addresses iOS crashes

This commit is contained in:
Evan Debenham
2021-12-01 18:45:11 -05:00
parent 2b04d49010
commit ebc8d7ecb5
2 changed files with 99 additions and 128 deletions

View File

@@ -8,7 +8,4 @@ dependencies {
//in order to do this I have to remove 100% of libGDX API access from core //in order to do this I have to remove 100% of libGDX API access from core
api "com.badlogicgames.gdx:gdx:$gdxVersion" api "com.badlogicgames.gdx:gdx:$gdxVersion"
implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
//noinspection GradleDependency , later JSON versions cause crashes on old versions of android
implementation "org.json:json:20170516"
} }

View File

@@ -21,13 +21,10 @@
package com.watabou.utils; package com.watabou.utils;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.watabou.noosa.Game; import com.watabou.noosa.Game;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@@ -39,7 +36,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.Set;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@@ -51,17 +47,17 @@ public class Bundle {
private static HashMap<String,String> aliases = new HashMap<>(); private static HashMap<String,String> aliases = new HashMap<>();
private JSONObject data; private JsonValue data;
public Bundle() { public Bundle() {
this( new JSONObject() ); this( new JsonValue(JsonValue.ValueType.object) );
} }
public String toString() { public String toString() {
return data.toString(); return data.toString();
} }
private Bundle( JSONObject data ) { private Bundle( JsonValue data ) {
this.data = data; this.data = data;
} }
@@ -70,31 +66,35 @@ public class Bundle {
} }
public boolean contains( String key ) { public boolean contains( String key ) {
return !data.isNull( key ); return data.has(key) && !data.get(key).isNull();
} }
public Set<String> getKeys(){ public ArrayList<String> getKeys(){
return data.keySet(); ArrayList<String> keys = new ArrayList<>();
for (JsonValue child : data){
keys.add(child.name());
}
return keys;
} }
public boolean getBoolean( String key ) { public boolean getBoolean( String key ) {
return data.optBoolean( key ); return data.getBoolean( key, false );
} }
public int getInt( String key ) { public int getInt( String key ) {
return data.optInt( key ); return data.getInt( key, 0 );
} }
public long getLong( String key ) { public long getLong( String key ) {
return data.optLong( key ); return data.getLong( key, 0 );
} }
public float getFloat( String key ) { public float getFloat( String key ) {
return (float)data.optDouble( key, 0.0 ); return data.getFloat( key, 0f );
} }
public String getString( String key ) { public String getString( String key ) {
return data.optString( key ); return data.getString( key, "" );
} }
public Class getClass( String key ) { public Class getClass( String key ) {
@@ -110,7 +110,7 @@ public class Bundle {
} }
public Bundle getBundle( String key ) { public Bundle getBundle( String key ) {
return new Bundle( data.optJSONObject( key ) ); return new Bundle( data.get(key) );
} }
private Bundlable get() { private Bundlable get() {
@@ -141,11 +141,8 @@ public class Bundle {
public <E extends Enum<E>> E getEnum( String key, Class<E> enumClass ) { public <E extends Enum<E>> E getEnum( String key, Class<E> enumClass ) {
try { try {
return Enum.valueOf( enumClass, data.getString( key ) ); return Enum.valueOf( enumClass, getString( key ) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e);
return enumClass.getEnumConstants()[0];
} catch (IllegalArgumentException e) {
Game.reportException(e); Game.reportException(e);
return enumClass.getEnumConstants()[0]; return enumClass.getEnumConstants()[0];
} }
@@ -153,14 +150,8 @@ public class Bundle {
public int[] getIntArray( String key ) { public int[] getIntArray( String key ) {
try { try {
JSONArray array = data.getJSONArray( key ); return data.get( key ).asIntArray();
int length = array.length(); } catch (Exception e) {
int[] result = new int[length];
for (int i=0; i < length; i++) {
result[i] = array.getInt( i );
}
return result;
} catch (JSONException e) {
Game.reportException(e); Game.reportException(e);
return null; return null;
} }
@@ -168,14 +159,8 @@ public class Bundle {
public float[] getFloatArray( String key ) { public float[] getFloatArray( String key ) {
try { try {
JSONArray array = data.getJSONArray( key ); return data.get( key ).asFloatArray();
int length = array.length(); } catch (Exception e) {
float[] result = new float[length];
for (int i=0; i < length; i++) {
result[i] = (float)array.optDouble( i, 0.0 );
}
return result;
} catch (JSONException e) {
Game.reportException(e); Game.reportException(e);
return null; return null;
} }
@@ -183,14 +168,8 @@ public class Bundle {
public boolean[] getBooleanArray( String key ) { public boolean[] getBooleanArray( String key ) {
try { try {
JSONArray array = data.getJSONArray( key ); return data.get( key ).asBooleanArray();
int length = array.length(); } catch (Exception e) {
boolean[] result = new boolean[length];
for (int i=0; i < length; i++) {
result[i] = array.getBoolean( i );
}
return result;
} catch (JSONException e) {
Game.reportException(e); Game.reportException(e);
return null; return null;
} }
@@ -198,14 +177,8 @@ public class Bundle {
public String[] getStringArray( String key ) { public String[] getStringArray( String key ) {
try { try {
JSONArray array = data.getJSONArray( key ); return data.get( key ).asStringArray();
int length = array.length(); } catch (Exception e) {
String[] result = new String[length];
for (int i=0; i < length; i++) {
result[i] = array.getString( i );
}
return result;
} catch (JSONException e) {
Game.reportException(e); Game.reportException(e);
return null; return null;
} }
@@ -213,11 +186,10 @@ public class Bundle {
public Class[] getClassArray( String key ) { public Class[] getClassArray( String key ) {
try { try {
JSONArray array = data.getJSONArray( key ); String[] clNames = data.get( key ).asStringArray();
int length = array.length(); Class[] result = new Class[clNames.length];
Class[] result = new Class[length]; for (int i=0; i < clNames.length; i++) {
for (int i=0; i < length; i++) { String clName = clNames[i].replace("class ", "");
String clName = array.getString( i ).replace("class ", "");
if (aliases.containsKey( clName )) { if (aliases.containsKey( clName )) {
clName = aliases.get( clName ); clName = aliases.get( clName );
} }
@@ -225,7 +197,7 @@ public class Bundle {
result[i] = cl; result[i] = cl;
} }
return result; return result;
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
return null; return null;
} }
@@ -237,14 +209,14 @@ public class Bundle {
public Bundle[] getBundleArray( String key ){ public Bundle[] getBundleArray( String key ){
try { try {
JSONArray array = data.getJSONArray( key ); JsonValue array = data.get( key );
int length = array.length(); int length = array.size;
Bundle[] result = new Bundle[length]; Bundle[] result = new Bundle[length];
for (int i=0; i < length; i++) { for (int i=0; i < length; i++) {
result[i] = new Bundle( array.getJSONObject( i ) ); result[i] = new Bundle( array.get( i ) );
} }
return result; return result;
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
return null; return null;
} }
@@ -255,12 +227,12 @@ public class Bundle {
ArrayList<Bundlable> list = new ArrayList<>(); ArrayList<Bundlable> list = new ArrayList<>();
try { try {
JSONArray array = data.getJSONArray( key ); JsonValue array = data.get( key );
for (int i=0; i < array.length(); i++) { for (JsonValue element : array) {
Bundlable O = new Bundle( array.getJSONObject( i ) ).get(); Bundlable O = new Bundle( element ).get();
if (O != null) list.add( O ); if (O != null) list.add( O );
} }
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
@@ -269,56 +241,56 @@ public class Bundle {
public void put( String key, boolean value ) { public void put( String key, boolean value ) {
try { try {
data.put( key, value ); data.addChild( key, new JsonValue(value) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, int value ) { public void put( String key, int value ) {
try { try {
data.put( key, value ); data.addChild( key, new JsonValue(value) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, long value ) { public void put( String key, long value ) {
try { try {
data.put( key, value ); data.addChild( key, new JsonValue(value) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, float value ) { public void put( String key, float value ) {
try { try {
data.put( key, value ); data.addChild( key, new JsonValue(value) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, String value ) { public void put( String key, String value ) {
try { try {
data.put( key, value ); data.addChild( key, new JsonValue(value) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, Class value ){ public void put( String key, Class value ){
try { try {
data.put( key, value ); data.addChild( key, new JsonValue(value.toString()) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, Bundle bundle ) { public void put( String key, Bundle bundle ) {
try { try {
data.put( key, bundle.data ); data.addChild( key, bundle.data);
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
@@ -329,8 +301,8 @@ public class Bundle {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.put( CLASS_NAME, object.getClass().getName() ); bundle.put( CLASS_NAME, object.getClass().getName() );
object.storeInBundle( bundle ); object.storeInBundle( bundle );
data.put( key, bundle.data ); data.addChild( key, bundle.data);
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
@@ -339,8 +311,8 @@ public class Bundle {
public void put( String key, Enum<?> value ) { public void put( String key, Enum<?> value ) {
if (value != null) { if (value != null) {
try { try {
data.put( key, value.name() ); data.addChild( key, new JsonValue(value.name()) );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
@@ -348,66 +320,66 @@ public class Bundle {
public void put( String key, int[] array ) { public void put( String key, int[] array ) {
try { try {
JSONArray jsonArray = new JSONArray(); JsonValue JSON = new JsonValue(JsonValue.ValueType.array);
for (int i=0; i < array.length; i++) { for (int val : array) {
jsonArray.put( i, array[i] ); JSON.addChild(new JsonValue(val));
} }
data.put( key, jsonArray ); data.addChild( key, JSON );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, float[] array ) { public void put( String key, float[] array ) {
try { try {
JSONArray jsonArray = new JSONArray(); JsonValue JSON = new JsonValue(JsonValue.ValueType.array);
for (int i=0; i < array.length; i++) { for (float val : array) {
jsonArray.put( i, array[i] ); JSON.addChild(new JsonValue(val));
} }
data.put( key, jsonArray ); data.addChild( key, JSON );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, boolean[] array ) { public void put( String key, boolean[] array ) {
try { try {
JSONArray jsonArray = new JSONArray(); JsonValue JSON = new JsonValue(JsonValue.ValueType.array);
for (int i=0; i < array.length; i++) { for (boolean val : array) {
jsonArray.put( i, array[i] ); JSON.addChild(new JsonValue(val));
} }
data.put( key, jsonArray ); data.addChild( key, JSON );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, String[] array ) { public void put( String key, String[] array ) {
try { try {
JSONArray jsonArray = new JSONArray(); JsonValue JSON = new JsonValue(JsonValue.ValueType.array);
for (int i=0; i < array.length; i++) { for (String val : array) {
jsonArray.put( i, array[i] ); JSON.addChild(new JsonValue(val));
} }
data.put( key, jsonArray ); data.addChild( key, JSON );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, Class[] array ){ public void put( String key, Class[] array ){
try { try {
JSONArray jsonArray = new JSONArray(); JsonValue JSON = new JsonValue(JsonValue.ValueType.array);
for (int i=0; i < array.length; i++) { for (Class val : array) {
jsonArray.put( i, array[i].getName() ); JSON.addChild(new JsonValue(val.getName()));
} }
data.put( key, jsonArray ); data.addChild( key, JSON );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
public void put( String key, Collection<? extends Bundlable> collection ) { public void put( String key, Collection<? extends Bundlable> collection ) {
JSONArray array = new JSONArray(); JsonValue JSON = new JsonValue(JsonValue.ValueType.array);
for (Bundlable object : collection) { for (Bundlable object : collection) {
//Skip none-static inner classes as they can't be instantiated through bundle restoring //Skip none-static inner classes as they can't be instantiated through bundle restoring
//Classes which make use of none-static inner classes must manage instantiation manually //Classes which make use of none-static inner classes must manage instantiation manually
@@ -417,13 +389,13 @@ public class Bundle {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.put(CLASS_NAME, cl.getName()); bundle.put(CLASS_NAME, cl.getName());
object.storeInBundle(bundle); object.storeInBundle(bundle);
array.put(bundle.data); JSON.addChild(bundle.data);
} }
} }
} }
try { try {
data.put( key, array ); data.addChild( key, JSON );
} catch (JSONException e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
} }
} }
@@ -453,15 +425,17 @@ public class Bundle {
//cannot just tokenize the stream directly as that constructor doesn't exist on Android //cannot just tokenize the stream directly as that constructor doesn't exist on Android
BufferedReader reader = new BufferedReader( new InputStreamReader( stream )); BufferedReader reader = new BufferedReader( new InputStreamReader( stream ));
Object json = new JSONTokener( reader.readLine() ).nextValue(); JsonValue json = new JsonReader().parse(reader);
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
if (json instanceof JSONArray){ if (json.isArray()){
json = new JSONObject().put( DEFAULT_KEY, json ); JsonValue result = new JsonValue( JsonValue.ValueType.object );
result.addChild( DEFAULT_KEY, json );
return new Bundle(result);
} else {
return new Bundle(json);
} }
return new Bundle( (JSONObject) json );
} catch (Exception e) { } catch (Exception e) {
Game.reportException(e); Game.reportException(e);
throw new IOException(); throw new IOException();
@@ -478,7 +452,7 @@ public class Bundle {
if (compressed) writer = new BufferedWriter( new OutputStreamWriter( new GZIPOutputStream(stream, GZIP_BUFFER ) ) ); if (compressed) writer = new BufferedWriter( new OutputStreamWriter( new GZIPOutputStream(stream, GZIP_BUFFER ) ) );
else writer = new BufferedWriter( new OutputStreamWriter( stream ) ); else writer = new BufferedWriter( new OutputStreamWriter( stream ) );
writer.write( bundle.data.toString() ); writer.write( bundle.toString() );
writer.close(); writer.close();
return true; return true;