Skip to content

Commit

Permalink
Version 2: Added support of Parsing Json String / List
Browse files Browse the repository at this point in the history
& added operations on JsonArray / JsonObject
  • Loading branch information
ColinTree committed Sep 2, 2018
1 parent 70e74e8 commit 3106997
Show file tree
Hide file tree
Showing 5 changed files with 559 additions and 50 deletions.
77 changes: 77 additions & 0 deletions JsonArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cn.colintree.aix.JsonUtils;

import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.util.YailList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
* A non visible container for JSONArray
*
*/
public final class JsonArray extends JsonType {

public static JsonArray parseJsonArray(YailList list) throws JSONException {
return new JsonArray(parseJSONArray(list));
}

public static JSONArray parseJSONArray(YailList list) throws JSONException {
if (JsonObject.canParseJSONObject(list)) {
throw new IllegalArgumentException();
}
int size = list.size();
JSONArray array = new JSONArray();
Object item;
for (int i = 0; i < size; i++) {
item = list.getObject(i);
array.put(item instanceof YailList
? JsonUtils.List2Json((YailList) item)
: item);
}
return array;
}

private final JSONArray array;

JsonArray(ComponentContainer container) {
this(new JSONArray());
}
JsonArray(JSONArray array) {
super(null);
if (array == null) {
array = new JSONArray();
}
this.array = array;
}

@Override
public String toString() {
return array.toString();
}

public JSONArray getArray() {
return array;
}

@Override
public YailList toList() {
return toList(array);
}

public static YailList toList(JSONArray array) {
int length = array.length();
Object[] objs = new Object[length];
for (int i = 0; i < length; i++) {
objs[i] = array.opt(i);
if (objs[i] instanceof JSONArray) {
objs[i] = JsonArray.toList((JSONArray) objs[i]);
} else if (objs[i] instanceof JSONObject) {
objs[i] = JsonObject.toList((JSONObject) objs[i]);
}
}
return YailList.makeList(objs);
}

}
101 changes: 101 additions & 0 deletions JsonObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cn.colintree.aix.JsonUtils;

import java.util.Iterator;

import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.util.YailList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
* A non visible container for JSONObject
*/
public final class JsonObject extends JsonType {

public static JsonObject parseJsonObject(YailList list) throws JSONException {
return new JsonObject(parseJSONObject(list));
}

public static JSONObject parseJSONObject(YailList list) throws JSONException {
if (!canParseJSONObject(list)) {
throw new IllegalArgumentException();
}
int size = list.size();
JSONObject object = new JSONObject();
YailList itemList;
for (int i = 0; i < size; i++) {
itemList = (YailList) list.getObject(i);
object.put(
(String) itemList.getObject(0),
itemList.getObject(1) instanceof YailList
? JsonUtils.List2Json((YailList) itemList.getObject(1))
: itemList.getObject(1)
);
}
return object;
}

public static boolean canParseJSONObject(YailList list) {
int size = list.size();
Object item;
for (int i = 0; i < size; i++) {
item = list.getObject(i);
if (item instanceof YailList && ((YailList)item).size() == 2
&& ((YailList)item).getObject(0) instanceof String) {
// this item is a valid object item
} else {
return false;
}
}
return true;
}


private final JSONObject object;

JsonObject(ComponentContainer container) {
this(new JSONObject());
}
JsonObject(JSONObject object) {
super(null);
this.object = object;
}

@Override
public String toString() {
return object.toString();
}

public JSONObject getObject() {
return object;
}

@Override
public YailList toList() {
return toList(object);
}

public static YailList toList(JSONObject object) {
int length = object.length();
Object[] objs = new Object[length];
int i = 0;
String key;
Object value;
Iterator<String> iterator = object.keys();
while (iterator.hasNext()) {
key = iterator.next();
value = object.opt(key);
if (value instanceof JSONArray) {
value = JsonArray.toList((JSONArray) value);
} else if (value instanceof JSONObject) {
value = JsonObject.toList((JSONObject) value);
}
objs[i] = YailList.makeList(new Object[]{ key, value });
i++;
}
return YailList.makeList(objs);
}

}
18 changes: 18 additions & 0 deletions JsonType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cn.colintree.aix.JsonUtils;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.util.YailList;

public abstract class JsonType extends AndroidNonvisibleComponent {

public JsonType(ComponentContainer container) {
super(null);
}

@Override
public abstract String toString();

public abstract YailList toList();

}
Loading

0 comments on commit 3106997

Please sign in to comment.