All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.facebook.model.JsonUtil Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2010-present Facebook.
 *
 * 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 {
    static void jsonObjectClear(JSONObject jsonObject) {
        @SuppressWarnings("unchecked")
        Iterator keys = (Iterator) jsonObject.keys();
        while (keys.hasNext()) {
            keys.next();
            keys.remove();
        }
    }

    static boolean jsonObjectContainsValue(JSONObject jsonObject, Object value) {
        @SuppressWarnings("unchecked")
        Iterator keys = (Iterator) jsonObject.keys();
        while (keys.hasNext()) {
            Object thisValue = jsonObject.opt(keys.next());
            if (thisValue != null && thisValue.equals(value)) {
                return true;
            }
        }
        return false;
    }

    private final static class JSONObjectEntry implements Map.Entry {
        private final String key;
        private final 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) {
            throw new UnsupportedOperationException("JSONObjectEntry is immutable");
        }

    }

    static Set> jsonObjectEntrySet(JSONObject jsonObject) {
        HashSet> result = new HashSet>();

        @SuppressWarnings("unchecked")
        Iterator keys = (Iterator) jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            Object value = jsonObject.opt(key);
            result.add(new JSONObjectEntry(key, value));
        }

        return result;
    }

    static Set jsonObjectKeySet(JSONObject jsonObject) {
        HashSet result = new HashSet();

        @SuppressWarnings("unchecked")
        Iterator keys = (Iterator) jsonObject.keys();
        while (keys.hasNext()) {
            result.add(keys.next());
        }

        return result;
    }

    static void jsonObjectPutAll(JSONObject jsonObject, Map map) {
        Set> entrySet = map.entrySet();
        for (Map.Entry entry : entrySet) {
            try {
                jsonObject.putOpt(entry.getKey(), entry.getValue());
            } catch (JSONException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }

    static Collection jsonObjectValues(JSONObject jsonObject) {
        ArrayList result = new ArrayList();

        @SuppressWarnings("unchecked")
        Iterator keys = (Iterator) jsonObject.keys();
        while (keys.hasNext()) {
            result.add(jsonObject.opt(keys.next()));
        }

        return result;
    }
}