From: Ole Streicher <olebole@debian.org>
Date: Wed, 12 Oct 2022 16:33:09 +0200
Subject: Use android-json

---
 src/main/uk/ac/starlink/tfcat/Decoders.java | 18 +++++++++++++++---
 src/main/uk/ac/starlink/tfcat/JsonTool.java |  5 +++++
 src/main/uk/ac/starlink/tfcat/Validate.java |  3 ++-
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/main/uk/ac/starlink/tfcat/Decoders.java b/src/main/uk/ac/starlink/tfcat/Decoders.java
index 4634fb0..39bcbf9 100644
--- a/src/main/uk/ac/starlink/tfcat/Decoders.java
+++ b/src/main/uk/ac/starlink/tfcat/Decoders.java
@@ -4,10 +4,12 @@ import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
 
 /**
@@ -176,8 +178,10 @@ public abstract class Decoders {
         List<Field> fieldList = new ArrayList<>();
         JSONObject jobj = new JsonTool( reporter ).asJSONObject( json );
         if ( jobj != null ) {
-            for ( String key : jobj.keySet() ) {
+	    for ( Iterator<String> it = jobj.keys(); it.hasNext(); ) {
+		String key = it.next();
                 Reporter fieldReporter = reporter.createReporter( key );
+		try {
                 JSONObject fieldObj = new JsonTool( fieldReporter ) 
                                      .asJSONObject( jobj.get( key ) );
                 if ( fieldObj != null ) {
@@ -214,6 +218,7 @@ public abstract class Decoders {
                         }
                     } );
                 }
+		} catch (JSONException e) {}
             }
         }
         return fieldList.toArray( new Field[ 0 ] );
@@ -275,13 +280,15 @@ public abstract class Decoders {
             JSONArray jarray = new JsonTool( geomsReporter )
                               .asJSONArray( jobj.opt( "geometries" ) );
             if ( jarray != null ) {
-                for ( int ig = 0; ig < jarray.length(); ig++ ) { 
+                for ( int ig = 0; ig < jarray.length(); ig++ ) {
+		    try {
                     Geometry<?> geom =
                         GEOMETRY.decode( geomsReporter.createReporter( ig ),
                                          jarray.get( ig ), parent );
                     if ( geom != null ) {
                         geomList.add( geom );
                     }
+		    } catch (JSONException e) {}
                 }
             }
         }
@@ -604,13 +611,15 @@ public abstract class Decoders {
     private static void checkProperties( Reporter reporter,
                                          JSONObject properties,
                                          Map<String,Field> fields ) {
-        for ( String key : properties.keySet() ) {
+	for ( Iterator<String> it = properties.keys(); it.hasNext(); ) {
+	    String key = it.next();
             Field field = fields.get( key );
             Reporter propReporter = reporter.createReporter( key );
             if ( field == null ) {
                 propReporter.report( "no corresponding field for property" );
             }
             else {
+		try {
                 Object value = properties.get( key );
                 if ( !JsonTool.isNull( value ) &&
                      ( value instanceof Number || value instanceof String ) ) {
@@ -620,6 +629,7 @@ public abstract class Decoders {
                                            + " \"" + value + "\"" );
                     }
                 }
+		} catch (JSONException e) {}
             }
         }
     }
@@ -692,12 +702,14 @@ public abstract class Decoders {
             @SuppressWarnings("unchecked")
             T[] array = (T[]) Array.newInstance( scalarClazz, n );
             for ( int i = 0; i < n; i++ ) {
+		try {
                 T item = scalarDecoder.decode( reporter.createReporter( i ),
                                                jarray.get( i ), parent );
                 if ( item == null ) {
                     return null;
                 }
                 array[ i ] = item;
+		} catch (JSONException e) { return null; }
             }
             return array;
         };
diff --git a/src/main/uk/ac/starlink/tfcat/JsonTool.java b/src/main/uk/ac/starlink/tfcat/JsonTool.java
index bb93e01..4698dba 100644
--- a/src/main/uk/ac/starlink/tfcat/JsonTool.java
+++ b/src/main/uk/ac/starlink/tfcat/JsonTool.java
@@ -1,6 +1,7 @@
 package uk.ac.starlink.tfcat;
 
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
 
 /**
@@ -96,6 +97,7 @@ class JsonTool {
             }
             double[] darray = new double[ nd ];
             for ( int id = 0; id < nd; id++ ) {
+		try {
                 Object item = jarray.get( id );
                 final double dval;
                 if ( isNull( item ) && isNanPermitted ) {
@@ -110,6 +112,9 @@ class JsonTool {
                     return null;
                 }
                 darray[ id ] = dval;
+		} catch (JSONException e) {
+                    return null;
+		}
             }
             return darray;
         }
diff --git a/src/main/uk/ac/starlink/tfcat/Validate.java b/src/main/uk/ac/starlink/tfcat/Validate.java
index dabd869..5c30a96 100644
--- a/src/main/uk/ac/starlink/tfcat/Validate.java
+++ b/src/main/uk/ac/starlink/tfcat/Validate.java
@@ -7,6 +7,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
+import org.apache.commons.io.IOUtils;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONTokener;
@@ -51,7 +52,7 @@ public class Validate {
                 new BasicReporter( isDebug, TfcatUtil.getUcdChecker(),
                                    TfcatUtil.getUnitChecker() );
             try {
-                JSONObject json = new JSONObject( new JSONTokener( in ) );
+                JSONObject json = new JSONObject( new JSONTokener( IOUtils.toString( in ) ) );
                 TfcatObject tfcat =
                     Decoders.TFCAT.decode( reporter, json, null );
                 if ( tfcat != null ) {
