Skip to content

Commit

Permalink
[@unimodules/core] Add JSONObjectArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
sjchmiela committed May 5, 2020
1 parent 2c6e5ce commit 9bf00c9
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
@@ -0,0 +1,104 @@
package org.unimodules.core.arguments;

import android.os.Bundle;

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JSONObjectArguments implements ReadableArguments {
private JSONObject mObject;

public JSONObjectArguments(JSONObject object) {
mObject = object;
}

@Override
public Collection<String> keys() {
Collection<String> keys = new ArrayList<>();
Iterator<String> iterator = mObject.keys();
while (iterator.hasNext()) {
keys.add(iterator.next());
}
return keys;
}

@Override
public boolean containsKey(String key) {
return mObject.has(key);
}

@Override
public Object get(String key) {
return mObject.opt(key);
}

@Override
public boolean getBoolean(String key, boolean defaultValue) {
return mObject.optBoolean(key, defaultValue);
}

@Override
public double getDouble(String key, double defaultValue) {
return mObject.optDouble(key, defaultValue);
}

@Override
public int getInt(String key, int defaultValue) {
return mObject.optInt(key, defaultValue);
}

@Override
public String getString(String key, String defaultValue) {
return mObject.optString(key, defaultValue);
}

@Override
@SuppressWarnings("unchecked")
public List getList(String key, List defaultValue) {
JSONArray jsonArray = mObject.optJSONArray(key);
if (jsonArray == null) {
return defaultValue;
}
List list = new ArrayList();
for (int i = 0; i < jsonArray.length(); i++) {
Object value = jsonArray.opt(i);
list.add(value);
}
return list;
}

@Override
@SuppressWarnings("unchecked")
public Map getMap(String key, Map defaultValue) {
JSONObject jsonObject = mObject.optJSONObject(key);
if (jsonObject == null) {
return defaultValue;
}
Bundle bundle = new JSONObjectArguments(jsonObject).toBundle();
Map map = new HashMap();
for (String subKey : bundle.keySet()) {
Object value = bundle.get(subKey);
if (value != null) {
map.put(subKey, value);
}
}
return map;
}

@Override
public boolean isEmpty() {
return mObject.length() > 0;
}

@Override
public int size() {
return mObject.length();
}
}
Expand Up @@ -2,6 +2,8 @@

import android.os.Bundle;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -55,6 +57,8 @@ default ReadableArguments getArguments(String key) {
Object value = get(key);
if (value instanceof Map) {
return new MapArguments((Map) value);
} else if (value instanceof JSONObject) {
return new JSONObjectArguments((JSONObject) value);
}
return null;
}
Expand Down Expand Up @@ -83,6 +87,9 @@ default Bundle toBundle() {
bundle.putBundle(key, new MapArguments((Map) value).toBundle());
} else if (value instanceof Bundle) {
bundle.putBundle(key, (Bundle) value);
} else if (value instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) value;
bundle.putBundle(key, new JSONObjectArguments(jsonObject).toBundle());
} else {
throw new UnsupportedOperationException("Could not put a value of " + value.getClass() + " to bundle.");
}
Expand Down

0 comments on commit 9bf00c9

Please sign in to comment.