From: Ole Streicher <olebole@debian.org>
Date: Mon, 28 May 2018 14:26:48 -0700
Subject: Use android json

The original org.json code is not packaged for Debian because of its
license, and the android variant is a bit outdated now.

The libjson-java code is different and contains net.sf.json (which
however has classes of the same name...)
---
 src/main/uk/ac/starlink/topcat/TopcatCodec2.java   |  5 +++--
 .../uk/ac/starlink/topcat/activate/HipsSurvey.java | 26 +++++++++++++++++-----
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/src/main/uk/ac/starlink/topcat/TopcatCodec2.java b/src/main/uk/ac/starlink/topcat/TopcatCodec2.java
index 9abc756..8f50db0 100644
--- a/src/main/uk/ac/starlink/topcat/TopcatCodec2.java
+++ b/src/main/uk/ac/starlink/topcat/TopcatCodec2.java
@@ -810,7 +810,7 @@ public class TopcatCodec2 implements TopcatCodec {
             .append( "[" );
         for ( Map<String,String> map : list ) {
             buf.append( "\n  " )
-               .append( JSONObject.valueToString( map ) )
+               .append( (new JSONObject( map )).toString() )
                .append( "," );
         }
         buf.setLength( buf.length() - 1 );
@@ -837,7 +837,8 @@ public class TopcatCodec2 implements TopcatCodec {
                 if ( el instanceof JSONObject ) {
                     JSONObject jsonObj = (JSONObject) el;
                     Map<String,String> map = new LinkedHashMap<String,String>();
-                    for ( String key : jsonObj.keySet() ) {
+		     java.util.Iterator<String> iterator = jsonObj.keys();
+                    for ( String key : (Iterable<String>)() -> iterator ) {
                         Object val = jsonObj.get( key );
                         if ( val instanceof String ) {
                             map.put( key, (String) val );
diff --git a/src/main/uk/ac/starlink/topcat/activate/HipsSurvey.java b/src/main/uk/ac/starlink/topcat/activate/HipsSurvey.java
index ce7bafd..92ed057 100644
--- a/src/main/uk/ac/starlink/topcat/activate/HipsSurvey.java
+++ b/src/main/uk/ac/starlink/topcat/activate/HipsSurvey.java
@@ -17,6 +17,7 @@ import java.util.concurrent.Callable;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONTokener;
 import uk.ac.starlink.util.ContentCoding;
@@ -400,7 +401,13 @@ public class HipsSurvey {
         logger_.info( "Loading HiPS list from " + url );
         InputStream in = new BufferedInputStream( coding.openStream( url ) );
         try {
-            JSONTokener jt = new JSONTokener( in );
+            byte[] contents = new byte[1024];
+            int bytesRead = 0;
+            String strFileContents = "";
+            while((bytesRead = in.read(contents)) != -1) {
+                strFileContents += new String(contents, 0, bytesRead);
+            }
+            JSONTokener jt = new JSONTokener( strFileContents );
             Object next = jt.nextValue();
             if ( next instanceof JSONArray ) {
                 List<HipsSurvey> list = new ArrayList<HipsSurvey>();
@@ -420,6 +427,9 @@ public class HipsSurvey {
                 throw new IOException( "Unexpected JSON object from " + url );
             }
         }
+        catch (JSONException e) {
+            throw new IOException( "Unexpected JSON object from " + url );
+        }
         finally {
             in.close();
         }
@@ -434,10 +444,16 @@ public class HipsSurvey {
      */
     private static final Map<String,String> toMap( JSONObject jobj ) {
         Map<String,String> map = new LinkedHashMap<String,String>();
-        for ( String key : JSONObject.getNames( jobj ) ) {
-            Object val = jobj.get( key );
-            if ( val instanceof String ) {
-                map.put( key, (String) val ); 
+        JSONArray names = jobj.names();
+        for ( int i = 0; i < names.length(); i++ ) {
+            try {
+                String key = (String)names.get(i);
+                Object val = jobj.get( key );
+                if ( val instanceof String ) {
+                    map.put( key, (String) val );
+                }
+            }
+            catch (JSONException e) {
             }
         }
         return map;
