If you think the Android project BagOfPix listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/**
* Copyright 2010-present Facebook.//www.java2s.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.facebook.model;
import android.annotation.SuppressLint;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
class JsonUtil {
staticvoid jsonObjectClear(JSONObject jsonObject) {
@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
keys.next();
keys.remove();
}
}
staticboolean jsonObjectContainsValue(JSONObject jsonObject, Object value) {
@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
Object thisValue = jsonObject.opt(keys.next());
if (thisValue != null && thisValue.equals(value)) {
return true;
}
}
return false;
}
privatefinalstaticclass JSONObjectEntry implements Map.Entry<String, Object> {
privatefinal String key;
privatefinal Object value;
JSONObjectEntry(String key, Object value) {
this.key = key;
this.value = value;
}
@SuppressLint("FieldGetter")
@Override
public String getKey() {
return this.key;
}
@Override
public Object getValue() {
return this.value;
}
@Override
public Object setValue(Object object) {
thrownew UnsupportedOperationException("JSONObjectEntry is immutable");
}
}
static Set<Map.Entry<String, Object>> jsonObjectEntrySet(JSONObject jsonObject) {
HashSet<Map.Entry<String, Object>> result = new HashSet<Map.Entry<String, Object>>();
@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = jsonObject.opt(key);
result.add(new JSONObjectEntry(key, value));
}
return result;
}
static Set<String> jsonObjectKeySet(JSONObject jsonObject) {
HashSet<String> result = new HashSet<String>();
@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
result.add(keys.next());
}
return result;
}
staticvoid jsonObjectPutAll(JSONObject jsonObject, Map<String, Object> map) {
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
try {
jsonObject.putOpt(entry.getKey(), entry.getValue());
} catch (JSONException e) {
thrownew IllegalArgumentException(e);
}
}
}
static Collection<Object> jsonObjectValues(JSONObject jsonObject) {
ArrayList<Object> result = new ArrayList<Object>();
@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
result.add(jsonObject.opt(keys.next()));
}
return result;
}
}