6 package com.google.appinventor.components.runtime.util;
8 import android.content.Context;
9 import android.util.Base64;
10 import android.util.Log;
15 import gnu.lists.FString;
17 import gnu.math.IntFraction;
20 import java.io.FileOutputStream;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map.Entry;
30 import java.util.TreeSet;
32 import org.json.JSONArray;
33 import org.json.JSONException;
34 import org.json.JSONObject;
35 import org.json.JSONTokener;
44 private static final String BINFILE_DIR =
"/AppInventorBinaries";
45 private static final String LOG_TAG =
"JsonUtil";
67 List<String> returnList =
new ArrayList<String>();
68 for (
int i = 0; i < jArray.length(); i++) {
69 String val = jArray.getString(i);
90 throws JSONException {
91 List<Object> returnList =
new ArrayList<Object>();
92 for (
int i = 0; i < jsonArray.length(); i++) {
113 List<Object> returnList =
new ArrayList<Object>();
114 Iterator<String> keys = jObject.keys();
116 List<String> keysList =
new ArrayList<String>();
117 while (keys.hasNext()) {
118 keysList.add(keys.next());
120 Collections.sort(keysList);
122 for (String key : keysList) {
123 List<Object> nestedList =
new ArrayList<Object>();
126 returnList.add(nestedList);
145 throws JSONException {
149 TreeSet<String> keys =
new TreeSet<String>();
150 Iterator<String> it = jsonObject.keys();
151 while (it.hasNext()) {
156 for (String key : keys) {
208 public static Object
convertJsonItem(Object o,
boolean useDicts)
throws JSONException {
213 if (o instanceof JSONObject) {
221 if (o instanceof JSONArray) {
230 if (o.equals(Boolean.FALSE) || (o instanceof String &&
231 ((String) o).equalsIgnoreCase(
"false"))) {
235 if (o.equals(Boolean.TRUE) || (o instanceof String && ((String) o).equalsIgnoreCase(
"true"))) {
239 if (o instanceof Number) {
247 if (value ==
null || value.equals(
null)) {
250 if (value instanceof FString) {
251 return JSONObject.quote(value.toString());
254 return ((
YailList) value).toJSONString();
263 if (value instanceof IntFraction) {
264 return JSONObject.numberToString((Number) ((IntFraction)value).doubleValue());
266 if (value instanceof Number) {
267 return JSONObject.numberToString((Number) value);
269 if (value instanceof Boolean) {
270 return value.toString();
272 if (value instanceof List) {
273 value = ((List)value).toArray();
276 StringBuilder sb =
new StringBuilder();
280 for (Entry<Object, Object> entry : (Set<Entry<Object, Object>>) dict.entrySet()) {
282 sb.append(JSONObject.quote(entry.getKey().toString()));
290 if (value.getClass().isArray()) {
291 StringBuilder sb =
new StringBuilder();
293 String separator =
"";
294 for (Object o: (Object[]) value) {
299 return sb.toString();
301 return JSONObject.quote(value.toString());
332 public static Object
getObjectFromJson(String jsonString,
boolean useDicts)
throws JSONException {
333 if ((jsonString ==
null) || jsonString.equals(
"")) {
340 final Object value = (
new JSONTokener(jsonString)).nextValue();
342 if (value ==
null || value.equals(JSONObject.NULL)) {
344 }
else if ((value instanceof String) ||
345 (value instanceof Number) ||
346 (value instanceof Boolean)) {
348 }
else if (value instanceof JSONArray) {
350 }
else if (value instanceof JSONObject) {
357 throw new JSONException(
"Invalid JSON string.");
377 Log.w(LOG_TAG,
"Calling deprecated function getJsonRepresentationIfValueFileName",
378 new IllegalAccessException());
405 List<String> valueList;
406 if (value instanceof String) {
407 JSONArray valueJsonList =
new JSONArray((String)value);
409 }
else if (value instanceof List) {
410 valueList = (List<String>) value;
412 throw new YailRuntimeError(
"getJsonRepresentationIfValueFileName called on unknown type",
413 value.getClass().getName());
415 if (valueList.size() == 2) {
416 if (valueList.get(0).startsWith(
".")) {
417 String filename = writeFile(context, valueList.get(1), valueList.get(0).substring(1));
418 System.out.println(
"Filename Written: " + filename);
419 filename = filename.replace(
"file:/",
"file:///");
427 }
catch(JSONException e) {
428 Log.e(LOG_TAG,
"JSONException", e);
445 private static String writeFile(Context context, String input, String fileExtension) {
446 FileOutputStream outStream =
null;
448 if (fileExtension.length() != 3 && fileExtension.length() != 4) {
449 throw new YailRuntimeError(
"File Extension must be three or four characters",
"Write Error");
451 byte [] content = Base64.decode(input, Base64.DEFAULT);
453 File destDirectory =
new File(fullDirName);
454 destDirectory.mkdirs();
455 File dest =
File.createTempFile(
"BinFile",
"." + fileExtension, destDirectory);
456 outStream =
new FileOutputStream(dest);
457 outStream.write(content);
458 String retval = dest.toURI().toASCIIString();
459 trimDirectory(20, destDirectory);
461 }
catch (Exception e) {
462 throw new YailRuntimeError(e.getMessage(),
"Write");
464 IOUtils.closeQuietly(LOG_TAG, outStream);
470 private static void trimDirectory(
int maxSavedFiles, File directory) {
472 File [] files = directory.listFiles();
474 Arrays.sort(files,
new Comparator<File>(){
475 public int compare(File f1, File f2)
477 return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
480 int excess = files.length - maxSavedFiles;
481 for (
int i = 0; i < excess; i++) {
496 }
catch (JSONException e) {
497 throw new IllegalArgumentException(
"jsonObject is not a legal JSON object");