Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@unimodules/core] Add JSONObjectArguments
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...modules/core/android/src/main/java/org/unimodules/core/arguments/JSONObjectArguments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters