Project

General

Profile

« Previous | Next » 

Revision 9e8eb9e4

Added by Leszek Koltunski over 2 years ago

Correect JSON reading and writing: shape corners and centers are optional!

View differences:

build.gradle
26 26
    api project(':distorted-library')
27 27
    implementation 'com.google.firebase:firebase-crashlytics:18.2.3'
28 28
    implementation 'androidx.appcompat:appcompat:1.3.1'
29
    implementation project(path: ':distorted-puzzle-dmesh')
30
    implementation project(path: ':distorted-puzzle-jsons')
29 31
}
src/main/java/org/distorted/objectlib/json/JsonReader.java
233 233
        }
234 234

  
235 235
      ////// cornerPush ///////////////////////////////////////////////
236
      JSONArray jsonCornerPush= jsonShape.getJSONArray("cornerPush");
237
      int numCornerP = jsonCornerPush.length();
238
      corners[i] = new float[numCornerP][2];
236
      JSONArray jsonCornerPush= jsonShape.optJSONArray("cornerPush");
239 237

  
240
      for(int j=0; j<numCornerP; j++)
238
      if( jsonCornerPush!=null )
241 239
        {
242
        JSONObject jsonC = jsonCornerPush.getJSONObject(j);
243
        corners[i][j][0] = (float)jsonC.getDouble("strength");
244
        corners[i][j][1] = (float)jsonC.getDouble("radius");
240
        int numCornerP = jsonCornerPush.length();
241
        corners[i] = new float[numCornerP][2];
242

  
243
        for(int j=0; j<numCornerP; j++)
244
          {
245
          JSONObject jsonC = jsonCornerPush.getJSONObject(j);
246
          corners[i][j][0] = (float)jsonC.getDouble("strength");
247
          corners[i][j][1] = (float)jsonC.getDouble("radius");
248
          }
245 249
        }
246 250

  
247 251
      ////// centerPush ///////////////////////////////////////////////
248
      JSONArray jsonCenterPush= jsonShape.getJSONArray("centerPush");
249
      int numCenterP = jsonCenterPush.length();
250
      centers[i] = new float[numCenterP][3];
252
      JSONArray jsonCenterPush= jsonShape.optJSONArray("centerPush");
251 253

  
252
      for(int j=0; j<numCenterP; j++)
254
      if( jsonCenterPush!=null )
253 255
        {
254
        JSONObject jsonC = jsonCenterPush.getJSONObject(j);
255
        centers[i][j][0] = (float)jsonC.getDouble("x");
256
        centers[i][j][1] = (float)jsonC.getDouble("y");
257
        centers[i][j][2] = (float)jsonC.getDouble("z");
256
        int numCenterP = jsonCenterPush.length();
257
        centers[i] = new float[numCenterP][3];
258

  
259
        for(int j=0; j<numCenterP; j++)
260
          {
261
          JSONObject jsonC = jsonCenterPush.getJSONObject(j);
262
          centers[i][j][0] = (float)jsonC.getDouble("x");
263
          centers[i][j][1] = (float)jsonC.getDouble("y");
264
          centers[i][j][2] = (float)jsonC.getDouble("z");
265
          }
258 266
        }
259 267

  
260 268
      ////// convexity ///////////////////////////////////////////////
261
      try
269
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
270

  
271
      if( jsonConvexity!=null )
262 272
        {
263
        JSONObject jsonConvexity = jsonShape.getJSONObject("convexity");
264 273
        convexity[i] = new float[3];
265 274
        convexity[i][0] = (float)jsonConvexity.getDouble("x");
266 275
        convexity[i][1] = (float)jsonConvexity.getDouble("y");
267 276
        convexity[i][2] = (float)jsonConvexity.getDouble("z");
268 277
        }
269
      catch(JSONException ex)
270
        {
271
        // ignore
272
        }
273 278
      }
274 279

  
275 280
    for(int i=0; i<mNumCubitVariants; i++)
src/main/java/org/distorted/objectlib/json/JsonWriter.java
273 273
      shapeObj.put("faces", facesArr);
274 274
      JSONArray bandsArr = generateBands(bands);
275 275
      shapeObj.put("bands", bandsArr);
276
      JSONArray cornerArr = generateCorners(corners);
277
      shapeObj.put("cornerPush", cornerArr);
278
      JSONArray centerArr = generateCenters(centers);
279
      shapeObj.put("centerPush", centerArr);
276

  
277
      if( corners!=null )
278
        {
279
        JSONArray cornerArr = generateCorners(corners);
280
        shapeObj.put("cornerPush", cornerArr);
281
        }
282

  
283
      if( centers!=null )
284
        {
285
        JSONArray centerArr = generateCenters(centers);
286
        shapeObj.put("centerPush", centerArr);
287
        }
280 288

  
281 289
      shapes.put(shapeObj);
282 290
      }

Also available in: Unified diff