From: Ole Streicher <olebole@debian.org>
Date: Mon, 19 Dec 2016 15:16:00 +0100
Subject: Use android-json

This one has only "String" and input parameter for JSONTokener
---
 src/main/uk/ac/starlink/ttools/build/JupyterCell.java | 14 ++++++++++----
 .../uk/ac/starlink/ttools/build/Plot2Example.java     |  5 +++++
 .../uk/ac/starlink/ttools/build/Plot2Notebook.java    |  3 +++
 .../uk/ac/starlink/ttools/cone/CdsUploadMatcher.java  |  6 +++++-
 .../starlink/ttools/example/GeojsonTableBuilder.java  | 19 ++++++++++++++++---
 .../uk/ac/starlink/ttools/server/PlotSession.java     | 18 +++++++++++++++---
 src/testcases/uk/ac/starlink/ttools/task/c2.geojson   |  2 +-
 7 files changed, 55 insertions(+), 12 deletions(-)

diff --git a/src/main/uk/ac/starlink/ttools/build/JupyterCell.java b/src/main/uk/ac/starlink/ttools/build/JupyterCell.java
index 4e0efee..c522578 100644
--- a/src/main/uk/ac/starlink/ttools/build/JupyterCell.java
+++ b/src/main/uk/ac/starlink/ttools/build/JupyterCell.java
@@ -8,6 +8,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 import org.json.JSONObject;
+import org.json.JSONException;
 
 /**
  * Manages export of a list of lines to the JSON format used for
@@ -50,7 +51,7 @@ public class JupyterCell {
      *
      * @return  JSON representation of this cell
      */
-    public JSONObject toJson() {
+    public JSONObject toJson() throws JSONException {
         JSONObject json = new JSONObject();
         json.put( "cell_type", "code" );
         json.put( "execution_count", JSONObject.NULL );
@@ -69,7 +70,8 @@ public class JupyterCell {
      * @param   cells  list of cells
      * @return   JSON representation of notebook
      */
-    public static JSONObject toNotebook( List<JupyterCell> cells ) {
+    public static JSONObject toNotebook( List<JupyterCell> cells )
+    throws JSONException {
         JSONObject cmode = new JSONObject();
         cmode.put( "name", "ipython" );
         cmode.put( "version", 3 );
@@ -95,8 +97,12 @@ public class JupyterCell {
         JSONObject json = new JSONObject();
         json.put( "cells",
                   cells.stream()
-                       .map( JupyterCell::toJson )
-                       .collect( Collectors.toList() ) );
+		  .map( o -> { try {
+			      return ((JupyterCell)o).toJson();
+			  } catch (JSONException e) {
+			      return JSONObject.NULL;
+			  }})
+		  .collect( Collectors.toList() ) );
         json.put( "metadata", meta );
         json.put( "nbformat", 4 );
         json.put( "nbformat_minor", 2 );
diff --git a/src/main/uk/ac/starlink/ttools/build/Plot2Example.java b/src/main/uk/ac/starlink/ttools/build/Plot2Example.java
index e55ad73..fcf6e0d 100644
--- a/src/main/uk/ac/starlink/ttools/build/Plot2Example.java
+++ b/src/main/uk/ac/starlink/ttools/build/Plot2Example.java
@@ -31,6 +31,7 @@ import javax.swing.JComponent;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
 import javax.swing.KeyStroke;
+import org.json.JSONException;
 import uk.ac.starlink.table.StarTable;
 import uk.ac.starlink.table.StarTableFactory;
 import uk.ac.starlink.table.TableScheme;
@@ -864,8 +865,10 @@ public class Plot2Example {
                     lines.add( "])" );
                     cells.add( new JupyterCell( lines ) );
                 }
+		try {
                 System.out.println( JupyterCell.toNotebook( cells )
                                                .toString( 1 ) );
+		} catch (JSONException e) {}
             }
         },
 
@@ -906,8 +909,10 @@ public class Plot2Example {
                     lines.add( "])" );
                     cells.add( new JupyterCell( lines ) );
                 }
+		try {
                 System.out.println( JupyterCell.toNotebook( cells )
                                                .toString( 1 ) );
+		} catch (JSONException e) {}
             }
         };
 
diff --git a/src/main/uk/ac/starlink/ttools/build/Plot2Notebook.java b/src/main/uk/ac/starlink/ttools/build/Plot2Notebook.java
index ae0021d..1d817a9 100644
--- a/src/main/uk/ac/starlink/ttools/build/Plot2Notebook.java
+++ b/src/main/uk/ac/starlink/ttools/build/Plot2Notebook.java
@@ -8,6 +8,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import org.json.JSONException;
 import uk.ac.starlink.ttools.server.PlotServlet;
 
 /**
@@ -109,6 +110,8 @@ public class Plot2Notebook {
             "gridcolor_g=hotpink",
             "labelpos_g=none",
         } ) );
+	try {
         System.out.println( JupyterCell.toNotebook( cells ).toString( 1 ) );
+	} catch (JSONException e) {}
     }
 }
diff --git a/src/main/uk/ac/starlink/ttools/cone/CdsUploadMatcher.java b/src/main/uk/ac/starlink/ttools/cone/CdsUploadMatcher.java
index d334691..ca72c66 100644
--- a/src/main/uk/ac/starlink/ttools/cone/CdsUploadMatcher.java
+++ b/src/main/uk/ac/starlink/ttools/cone/CdsUploadMatcher.java
@@ -14,6 +14,8 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
+import org.apache.commons.io.IOUtils;
+import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONTokener;
 import org.xml.sax.SAXException;
@@ -258,7 +260,7 @@ public class CdsUploadMatcher implements UploadMatcher {
             new BufferedInputStream( ContentCoding.NONE.openStream( url ) );
         JSONObject infoObj;
         try {
-            JSONTokener jt = new JSONTokener( in );
+            JSONTokener jt = new JSONTokener( IOUtils.toString( in ) );
             Object next = jt.nextValue();
             if ( next instanceof JSONObject ) {
                 return new VizierMeta( (JSONObject) next );
@@ -266,6 +268,8 @@ public class CdsUploadMatcher implements UploadMatcher {
             else {
                 throw new IOException( "Unexpected JSON object from " + url );
             }
+        } catch (JSONException e) {
+               throw new IOException(e);
         }
         finally {
             in.close();
diff --git a/src/main/uk/ac/starlink/ttools/example/GeojsonTableBuilder.java b/src/main/uk/ac/starlink/ttools/example/GeojsonTableBuilder.java
index e0f9bc1..9bada0d 100644
--- a/src/main/uk/ac/starlink/ttools/example/GeojsonTableBuilder.java
+++ b/src/main/uk/ac/starlink/ttools/example/GeojsonTableBuilder.java
@@ -5,7 +5,9 @@ import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.LinkedHashMap;
+import java.util.Iterator;
 import java.util.Map;
+import org.apache.commons.io.IOUtils;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -104,7 +106,7 @@ public class GeojsonTableBuilder implements TableBuilder {
     private StarTable createStarTable( InputStream in ) throws IOException {
         JSONObject top;
         try {
-            top = new JSONObject( new JSONTokener( in ) );
+            top = new JSONObject( new JSONTokener( IOUtils.toString( in ) ) );
         }
         catch ( JSONException e ) {
             throw new TableFormatException( "Not JSON", e );
@@ -112,6 +114,7 @@ public class GeojsonTableBuilder implements TableBuilder {
         finally {
             in.close();
         }
+        try {
         if ( "FeatureCollection".equals( top.get( "type" ) ) ) {
             JSONArray features = (JSONArray) top.get( "features" );
             return createStarTable( features );
@@ -120,6 +123,7 @@ public class GeojsonTableBuilder implements TableBuilder {
             throw new TableFormatException( "No FeatureCollection "
                                           + " in top-level JSON object" );
         }
+        } catch ( JSONException e ) { throw new TableFormatException( "Not JSON", e ); }
     }
 
     /**
@@ -137,16 +141,19 @@ public class GeojsonTableBuilder implements TableBuilder {
          * that occurs at least once as a table heading. */
         Map<String,Object> propMap = new LinkedHashMap<>();
         for ( int i = 0; i < nf; i++ ) {
+            try {
             JSONObject jobj = features.getJSONObject( i );
             if ( "Feature".equals( jobj.get( "type" ) ) ) {
                 JSONObject props = jobj.getJSONObject( "properties" );
                 if ( props != null ) {
-                    for ( String key : props.keySet() ) {
+                    for ( Iterator<String> it = props.keys(); it.hasNext(); ) {
+                        String key = it.next();
                         Object value = props.get( key );
                         propMap.put( key, value );
                     }
                 }
             }
+            } catch ( JSONException e ) { }
         }
 
         /* Adapt the features array as a StarTable. */
@@ -166,12 +173,14 @@ public class GeojsonTableBuilder implements TableBuilder {
         geomInfo.setXtype( "stc-s" );
         table.addColumn( new ColumnData( geomInfo ) {
             public Object readValue( long irow ) {
+                try {
                 JSONObject geom = features.getJSONObject( (int) irow )
                                           .getJSONObject( "geometry" );
                 String type = geom.getString( "type" );
                 JSONArray coords = geom.getJSONArray( "coordinates" );
                 return toStcs( geom.getString( "type" ),
                                geom.getJSONArray( "coordinates" ) );
+                } catch ( JSONException e ) { return null; }
             }
         } );
         return table;
@@ -194,6 +203,7 @@ public class GeojsonTableBuilder implements TableBuilder {
         if ( exampleValue instanceof Number ) {
             return new ColumnData( new ColumnInfo( key, Double.class, null ) ) {
                 public Object readValue( long irow ) {
+                    try {
                     Object value = features
                                   .getJSONObject( (int) irow )
                                   .getJSONObject( "properties" )
@@ -207,6 +217,7 @@ public class GeojsonTableBuilder implements TableBuilder {
                     else {
                         return null;
                     }
+                    } catch (JSONException e) { return null; }
                 }
             };
         }
@@ -214,11 +225,13 @@ public class GeojsonTableBuilder implements TableBuilder {
                   exampleValue instanceof Boolean ) {
             return new ColumnData( new ColumnInfo( key, clazz, null ) ) {
                 public Object readValue( long irow ) {
+                    try {
                     Object value = features
                                   .getJSONObject( (int) irow )
                                   .getJSONObject( "properties" )
                                   .get( key );
                     return clazz.isInstance( value ) ? value : null;
+                    } catch (JSONException e) { return null; }
                 }
             };
         }
@@ -239,7 +252,7 @@ public class GeojsonTableBuilder implements TableBuilder {
      * @return  STC-S string approximating GeoJSON intent if possible,
      *          or null if not
      */
-    private String toStcs( String type, JSONArray jsonCoords ) {
+    private String toStcs( String type, JSONArray jsonCoords ) throws JSONException {
         if ( "MultiPolygon".equals( type ) ) {
             int npoly = jsonCoords.length();
             StringBuffer sbuf = new StringBuffer();
diff --git a/src/main/uk/ac/starlink/ttools/server/PlotSession.java b/src/main/uk/ac/starlink/ttools/server/PlotSession.java
index 7a97e44..d473bb7 100644
--- a/src/main/uk/ac/starlink/ttools/server/PlotSession.java
+++ b/src/main/uk/ac/starlink/ttools/server/PlotSession.java
@@ -38,8 +38,8 @@ import javax.servlet.http.HttpServletResponse;
 import org.jfree.graphics2d.svg.SVGGraphics2D;
 import org.jfree.graphics2d.svg.SVGUnits;
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
-import org.json.JSONWriter;
 import uk.ac.starlink.table.RowSequence;
 import uk.ac.starlink.table.StarTable;
 import uk.ac.starlink.ttools.DocUtils;
@@ -782,7 +782,7 @@ public class PlotSession<P,A> {
     private static void writeJsonQuotedString( OutputStream out, String txt )
             throws IOException {
         OutputStreamWriter writer = new OutputStreamWriter( out, "UTF-8" );
-        JSONObject.quote( txt, writer );
+        writer.write(JSONObject.quote( txt ));
         writer.flush();
     }
 
@@ -1019,6 +1019,7 @@ public class PlotSession<P,A> {
                                         HttpServletResponse response )
                     throws IOException {
 
+		try {
                 /* Write image data. */
                 ImageWriter imwriter =
                     session.getImageWriter( request, session.exporter_ );
@@ -1088,9 +1089,18 @@ public class PlotSession<P,A> {
                         out.write( ',' );
                     }
                     writeAscii( out, "\"" + BOUNDS_KEY + "\": " );
-                    writeAscii( out, new JSONArray( dbounds ).toString() );
+		    JSONArray dbArray =  new JSONArray();
+		    for (int i = 0; i < dbounds.length; i++) {
+			JSONArray db0Array =  new JSONArray();
+			for (int j = 0; j < dbounds.length; j++) {
+			    db0Array.put(dbounds[i][j]);
+			}
+			dbArray.put(db0Array);
+		    }
+                    writeAscii( out, dbArray.toString() );
                 }
                 out.write( '}' );
+		} catch (JSONException e) { throw new IOException(e); }
             }
         };
     }
@@ -1134,6 +1144,7 @@ public class PlotSession<P,A> {
                                         HttpServletResponse response )
                     throws IOException {
                 final String msg;
+		try {
                 Map<String,String> paramMap = getSingleParameterMap( request );
                 Point gpos = parseXY( paramMap.get( "pos" ) );
                 JSONObject json = new JSONObject();
@@ -1174,6 +1185,7 @@ public class PlotSession<P,A> {
                 response.setStatus( 200 );
                 response.setContentType( "application/json" );
                 response.getOutputStream().println( json.toString() );
+		} catch (JSONException e) { throw new IOException(e); }
             }
         };
     }
diff --git a/src/testcases/uk/ac/starlink/ttools/task/c2.geojson b/src/testcases/uk/ac/starlink/ttools/task/c2.geojson
index cc8e7ef..bd280e0 100644
--- a/src/testcases/uk/ac/starlink/ttools/task/c2.geojson
+++ b/src/testcases/uk/ac/starlink/ttools/task/c2.geojson
@@ -1,4 +1,4 @@
 {"type":"FeatureCollection","features":[
 {"type":"Feature","id":"DEU","properties":{"name":"Germany"},"geometry":{"type":"Polygon","coordinates":[[[9.921906,54.983104],[9.93958,54.596642],[10.950112,54.363607],[10.939467,54.008693],[11.956252,54.196486],[12.51844,54.470371],[13.647467,54.075511],[14.119686,53.757029],[14.353315,53.248171],[14.074521,52.981263],[14.4376,52.62485],[14.685026,52.089947],[14.607098,51.745188],[15.016996,51.106674],[14.570718,51.002339],[14.307013,51.117268],[14.056228,50.926918],[13.338132,50.733234],[12.966837,50.484076],[12.240111,50.266338],[12.415191,49.969121],[12.521024,49.547415],[13.031329,49.307068],[13.595946,48.877172],[13.243357,48.416115],[12.884103,48.289146],[13.025851,47.637584],[12.932627,47.467646],[12.62076,47.672388],[12.141357,47.703083],[11.426414,47.523766],[10.544504,47.566399],[10.402084,47.302488],[9.896068,47.580197],[9.594226,47.525058],[8.522612,47.830828],[8.317301,47.61358],[7.466759,47.620582],[7.593676,48.333019],[8.099279,49.017784],[6.65823,49.201958],[6.18632,49.463803],[6.242751,49.902226],[6.043073,50.128052],[6.156658,50.803721],[5.988658,51.851616],[6.589397,51.852029],[6.84287,52.22844],[7.092053,53.144043],[6.90514,53.482162],[7.100425,53.693932],[7.936239,53.748296],[8.121706,53.527792],[8.800734,54.020786],[8.572118,54.395646],[8.526229,54.962744],[9.282049,54.830865],[9.921906,54.983104]]]}},
-{"type":"Feature","id":"GBR","properties":{"name":"United Kingdom"},"geometry":{"type":"MultiPolygon","coordinates":[[[[-5.661949,54.554603],[-6.197885,53.867565],[-6.95373,54.073702],[-7.572168,54.059956],[-7.366031,54.595841],[-7.572168,55.131622],[-6.733847,55.17286],[-5.661949,54.554603]]],[[[-3.005005,58.635],[-4.073828,57.553025],[-3.055002,57.690019],[-1.959281,57.6848],[-2.219988,56.870017],[-3.119003,55.973793],[-2.085009,55.909998],[-2.005676,55.804903],[-1.114991,54.624986],[-0.430485,54.464376],[0.184981,53.325014],[0.469977,52.929999],[1.681531,52.73952],[1.559988,52.099998],[1.050562,51.806761],[1.449865,51.289428],[0.550334,50.765739],[-0.787517,50.774989],[-2.489998,50.500019],[-2.956274,50.69688],[-3.617448,50.228356],[-4.542508,50.341837],[-5.245023,49.96],[-5.776567,50.159678],[-4.30999,51.210001],[-3.414851,51.426009],[-3.422719,51.426848],[-4.984367,51.593466],[-5.267296,51.9914],[-4.222347,52.301356],[-4.770013,52.840005],[-4.579999,53.495004],[-3.093831,53.404547],[-3.09208,53.404441],[-2.945009,53.985],[-3.614701,54.600937],[-3.630005,54.615013],[-4.844169,54.790971],[-5.082527,55.061601],[-4.719112,55.508473],[-5.047981,55.783986],[-5.586398,55.311146],[-5.644999,56.275015],[-6.149981,56.78501],[-5.786825,57.818848],[-5.009999,58.630013],[-4.211495,58.550845],[-3.005005,58.635]]]]}},
+{"type":"Feature","id":"GBR","properties":{"name":"United Kingdom"},"geometry":{"type":"MultiPolygon","coordinates":[[[[-5.661949,54.554603],[-6.197885,53.867565],[-6.95373,54.073702],[-7.572168,54.059956],[-7.366031,54.595841],[-7.572168,55.131622],[-6.733847,55.17286],[-5.661949,54.554603]]],[[[-3.005005,58.635],[-4.073828,57.553025],[-3.055002,57.690019],[-1.959281,57.6848],[-2.219988,56.870017],[-3.119003,55.973793],[-2.085009,55.909998],[-2.005676,55.804903],[-1.114991,54.624986],[-0.430485,54.464376],[0.184981,53.325014],[0.469977,52.929999],[1.681531,52.73952],[1.559988,52.099998],[1.050562,51.806761],[1.449865,51.289428],[0.550334,50.765739],[-0.787517,50.774989],[-2.489998,50.500019],[-2.956274,50.69688],[-3.617448,50.228356],[-4.542508,50.341837],[-5.245023,49.96],[-5.776567,50.159678],[-4.30999,51.210001],[-3.414851,51.426009],[-3.422719,51.426848],[-4.984367,51.593466],[-5.267296,51.9914],[-4.222347,52.301356],[-4.770013,52.840005],[-4.579999,53.495004],[-3.093831,53.404547],[-3.09208,53.404441],[-2.945009,53.985],[-3.614701,54.600937],[-3.630005,54.615013],[-4.844169,54.790971],[-5.082527,55.061601],[-4.719112,55.508473],[-5.047981,55.783986],[-5.586398,55.311146],[-5.644999,56.275015],[-6.149981,56.78501],[-5.786825,57.818848],[-5.009999,58.630013],[-4.211495,58.550845],[-3.005005,58.635]]]]}}
 ]}
