1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
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 ) {
|