Android Open Source - dropbox-android-core-sdk-mirror Json Map






From Project

Back to project page dropbox-android-core-sdk-mirror.

License

The source code is released under:

Copyright (c) 2009-2011 Dropbox Inc., http://www.dropbox.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "So...

If you think the Android project dropbox-android-core-sdk-mirror 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

package com.dropbox.client2.jsonextract;
/*from w  ww .  j  av a 2s.c  om*/
import java.util.Iterator;
import java.util.Map;

/**
 * (Internal class for extracting JSON.)
 *
 * A JSON "object" (a mapping of string keys to arbitrary JSON values).
 */
public final class JsonMap extends JsonBase<Map<String,Object>> implements Iterable<Map.Entry<String,JsonThing>> {

    public JsonMap(Map<String, Object> internal, String path) {
        super(internal, path);
    }

    public JsonMap(Map<String, Object> internal) {
        super(internal);
    }

    private static boolean isIdentLike(String s) {
        if (s.length() == 0) return false;
        if (!isEnglishLetter(s.charAt(0))) return false;
        for (int i = 1; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!isEnglishLetter(c) && !isEnglishDigit(c)) return false;
        }
        return true;
    }

    private static boolean isEnglishLetter(char c) {
        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
    }

    private static boolean isEnglishDigit(char c) {
        return c >= '0' && c <= '9';
    }

    private static String pathConcatField(String path, String fieldName) {
        String suffix = fieldName;
        if (!isIdentLike(fieldName)) {
            suffix = '"' + fieldName + '"'; // TODO: Proper JSON quoting.
        }
        return JsonThing.pathConcat(path, suffix);
    }

    public JsonThing get(String fieldName) throws JsonExtractionException {
        if (!internal.containsKey(fieldName)) {
            throw error("expecting object to have field \"" + fieldName + "\", but it does not");
        }
        return new JsonThing(internal.get(fieldName), pathConcatField(path, fieldName));
    }

    public JsonThing getOrNull(String fieldName) {
        if (!internal.containsKey(fieldName)) {
            return null;
        }
        return new JsonThing(internal.get(fieldName), pathConcatField(path, fieldName));
    }

    /**
     * A key+value iterator that automatically wraps every value in a JsonThing.
     */
    private static final class WrapperIterator implements Iterator<Map.Entry<String,JsonThing>> {
        private final String path;
        private final Iterator<Map.Entry<String,Object>> internal;

        private WrapperIterator(String path, Iterator<Map.Entry<String,Object>> internal) {
            this.path = path;
            this.internal = internal;
        }

        public boolean hasNext() { return internal.hasNext(); }
        public Map.Entry<String,JsonThing> next() {
            return new WrappedEntry(path, internal.next());
        }
        public void remove() { throw new UnsupportedOperationException("can't remove"); }
    }

    private static final class WrappedEntry implements Map.Entry<String,JsonThing> {
        private final String key;
        private final JsonThing value;

        private WrappedEntry(String path, Map.Entry<String,Object> original) {
            this.key = original.getKey();
            this.value = new JsonThing(original.getValue(), pathConcatField(path, key));
        }

        public String getKey() { return key; }
        public JsonThing getValue() { return value; }
        public JsonThing setValue(JsonThing jsonThing) { throw new UnsupportedOperationException(); }
    }

    public Iterator<Map.Entry<String,JsonThing>> iterator() {
        return new WrapperIterator(path, internal.entrySet().iterator());
    }
}




Java Source Code List

.CopyBetweenAccounts.java
.SearchCache.java
com.dropbox.android.sample.DBRoulette.java
com.dropbox.android.sample.DownloadRandomPicture.java
com.dropbox.android.sample.UploadPicture.java
com.dropbox.client2.DropboxAPI.java
com.dropbox.client2.ProgressListener.java
com.dropbox.client2.RESTUtility.java
com.dropbox.client2.SdkVersion.java
com.dropbox.client2.android.AndroidAuthSession.java
com.dropbox.client2.android.AuthActivity.java
com.dropbox.client2.exception.DropboxException.java
com.dropbox.client2.exception.DropboxFileSizeException.java
com.dropbox.client2.exception.DropboxIOException.java
com.dropbox.client2.exception.DropboxLocalStorageFullException.java
com.dropbox.client2.exception.DropboxParseException.java
com.dropbox.client2.exception.DropboxPartialFileException.java
com.dropbox.client2.exception.DropboxSSLException.java
com.dropbox.client2.exception.DropboxServerException.java
com.dropbox.client2.exception.DropboxUnlinkedException.java
com.dropbox.client2.jsonextract.JsonBase.java
com.dropbox.client2.jsonextract.JsonExtractionException.java
com.dropbox.client2.jsonextract.JsonExtractor.java
com.dropbox.client2.jsonextract.JsonList.java
com.dropbox.client2.jsonextract.JsonMap.java
com.dropbox.client2.jsonextract.JsonThing.java
com.dropbox.client2.session.AbstractSession.java
com.dropbox.client2.session.AccessTokenPair.java
com.dropbox.client2.session.AppKeyPair.java
com.dropbox.client2.session.RequestTokenPair.java
com.dropbox.client2.session.Session.java
com.dropbox.client2.session.TokenPair.java
com.dropbox.client2.session.WebAuthSession.java