Android Open Source - Android-SDK Json Structure






From Project

Back to project page Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project Android-SDK 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.baasbox.android.json;
/*  www . j a v  a 2s.c  o  m*/
import java.io.IOException;
import java.io.StringReader;

/**
 * Represent JSON structured data, either a {@link com.baasbox.android.json.JsonObject}
 * or a {@link com.baasbox.android.json.JsonArray}.
 * <p/>
 * Created by Andrea Tortorella on 01/01/14.
 */
public abstract class JsonStructure {
// ------------------------------ FIELDS ------------------------------

    public static final int ABSENT = 0;
    public static final int OBJECT = 1;
    public static final int ARRAY = 2;
    public static final int STRING = 3;
    public static final int BOOLEAN = 4;
    public static final int NUMBER = 5;
    public static final int NULL = 6;

// --------------------------- CONSTRUCTORS ---------------------------
    JsonStructure() {
    }

// -------------------------- STATIC METHODS --------------------------

    /**
     * Decodes a string into a {@link JsonStructure}
     *
     * @param text the string to decode cannot be null
     * @return a JsonStructure representation of the string
     * @throws com.baasbox.android.json.JsonException if the text cannot be parsed as json.
     */
    public static JsonStructure decode(String text) {
        if (text == null) throw new IllegalArgumentException("text cannot be null");
        StringReader sr = new StringReader(text);
        JsonReader r = new JsonReader(sr);
        return decodeFully(r);
    }

    static JsonStructure decodeFully(JsonReader jr) {
        try {
            JsonToken t = jr.peek();
            switch (t) {
                case BEGIN_OBJECT:
                    return JsonObject.decodeFully(jr);
                case BEGIN_ARRAY:
                    return JsonArray.decodeFully(jr);
                default:
                    throw new JsonException("invalid json");
            }
        } catch (IOException e) {
            throw new JsonException("incalid json", e);
        }
    }

// -------------------------- OTHER METHODS --------------------------

    /**
     * Casts this structure to a {@link JsonArray}
     *
     * @return this as a {@link JsonArray}
     * @throws JsonException if this structure is not a JsonArray
     */
    public JsonArray asArray() {
        try {
            return (JsonArray) this;
        } catch (ClassCastException e) {
            throw new JsonException(e);
        }
    }

    /**
     * Casts this structure to a {@link JsonObject}
     *
     * @return this as a {@link JsonObject}
     * @throws JsonException if this structure is not a JsonObject
     */
    public JsonObject asObject() {
        try {
            return (JsonObject) this;
        } catch (ClassCastException e) {
            throw new JsonException(e);
        }
    }

    /**
     * Creates a deeep copy of this structure.
     *
     * @return a new JsonStructure with the same content
     */
    public abstract JsonStructure copy();

    /**
     * Encodes this structure to it's json representation
     *
     * @return a string representation of this structure
     */
    public abstract String encode();

    /**
     * Checks if this structure is a {@link JsonArray}
     *
     * @return true if this is a {@link JsonArray} false otherwise
     */
    public final boolean isArray() {
        return this instanceof JsonArray;
    }

    /**
     * Checks if this structure is {@link JsonObject}
     *
     * @return true if this is {@link JsonObject} false otherwise
     */
    public final boolean isObject() {
        return this instanceof JsonObject;
    }

    /**
     * Returns the values contained in this structure as a {@link JsonArray}
     *
     * @return the values of this structure
     */
    public abstract JsonArray values();

    public abstract JsonStructure clear();
}




Java Source Code List

com.baasbox.android.AsyncStream.java
com.baasbox.android.BaasACL.java
com.baasbox.android.BaasApiException.java
com.baasbox.android.BaasAssetId.java
com.baasbox.android.BaasAsset.java
com.baasbox.android.BaasBox.java
com.baasbox.android.BaasCancellationException.java
com.baasbox.android.BaasClientException.java
com.baasbox.android.BaasCloudMessagingService.java
com.baasbox.android.BaasCredentialManager.java
com.baasbox.android.BaasDocument.java
com.baasbox.android.BaasException.java
com.baasbox.android.BaasFile.java
com.baasbox.android.BaasHandler.java
com.baasbox.android.BaasIOException.java
com.baasbox.android.BaasInvalidSessionException.java
com.baasbox.android.BaasLink.java
com.baasbox.android.BaasObject.java
com.baasbox.android.BaasQuery.java
com.baasbox.android.BaasResult.java
com.baasbox.android.BaasRuntimeException.java
com.baasbox.android.BaasServerException.java
com.baasbox.android.BaasStream.java
com.baasbox.android.BaasUser.java
com.baasbox.android.Cache.java
com.baasbox.android.DataStreamHandler.java
com.baasbox.android.ExceptionHandler.java
com.baasbox.android.FixedByteArrayOutputStream.java
com.baasbox.android.Grant.java
com.baasbox.android.HttpUrlConnectionClient.java
com.baasbox.android.JsonWrapper.java
com.baasbox.android.NetworkTask.java
com.baasbox.android.Plugin.java
com.baasbox.android.RequestFactory.java
com.baasbox.android.RequestOptions.java
com.baasbox.android.RequestToken.java
com.baasbox.android.RestImpl.java
com.baasbox.android.Rest.java
com.baasbox.android.Role.java
com.baasbox.android.SaveMode.java
com.baasbox.android.StreamBody.java
com.baasbox.android.StreamRequest.java
com.baasbox.android.impl.Base64.java
com.baasbox.android.impl.Constants.java
com.baasbox.android.impl.DiskLruCache.java
com.baasbox.android.impl.Dispatcher.java
com.baasbox.android.impl.GCMWrapper.java
com.baasbox.android.impl.ImmediateDispatcher.java
com.baasbox.android.impl.Logger.java
com.baasbox.android.impl.StrictLineReader.java
com.baasbox.android.impl.Task.java
com.baasbox.android.impl.Util.java
com.baasbox.android.impl.package-info.java
com.baasbox.android.json.JsonArray.java
com.baasbox.android.json.JsonException.java
com.baasbox.android.json.JsonObject.java
com.baasbox.android.json.JsonReader.java
com.baasbox.android.json.JsonScope.java
com.baasbox.android.json.JsonStructure.java
com.baasbox.android.json.JsonToken.java
com.baasbox.android.json.JsonWriter.java
com.baasbox.android.json.StringPool.java
com.baasbox.android.json.package-info.java
com.baasbox.android.net.HttpRequest.java
com.baasbox.android.net.OkClient.java
com.baasbox.android.net.RestClient.java
com.baasbox.android.plugins.glide.BaasAssetModelLoader.java
com.baasbox.android.plugins.glide.BaasFileModelLoader.java
com.baasbox.android.plugins.glide.BaasImageFetcher.java
com.baasbox.android.plugins.glide.GlidePlugin.java
com.baasbox.android.samples.aloa.Aloa.java
com.baasbox.android.samples.aloa.ApplicationTest.java
com.baasbox.android.samples.aloa.Configuration.java
com.baasbox.android.samples.aloa.Data.java
com.baasbox.android.samples.aloa.activities.CreateChannelFragment.java
com.baasbox.android.samples.aloa.activities.LoginActivity.java
com.baasbox.android.samples.aloa.activities.MainActivity.java
com.baasbox.android.samples.aloa.activities.OnTargetSelectedListener.java
com.baasbox.android.samples.aloa.activities.SendMessageActivity.java
com.baasbox.android.samples.aloa.activities.SendTargetFragment.java
com.baasbox.android.samples.aloa.activities.UserStatusFragment.java
com.baasbox.android.samples.aloa.activities.loaders.BaasUsersLoader.java
com.baasbox.android.samples.aloa.activities.loaders.ChannelsLoader.java
com.baasbox.android.samples.aloa.receive.NewMessageNotification.java
com.baasbox.android.samples.aloa.receive.PushReceiver.java
com.baasbox.android.samples.aloa.utils.BaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseActivity.java
com.baasbox.android.samples.aloa.utils.BaseBaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseFragment.java
com.baasbox.android.samples.phrasebook.Configuration.java
com.baasbox.android.samples.phrasebook.Phrasebook.java
com.baasbox.android.samples.phrasebook.ServerContract.java
com.baasbox.android.samples.phrasebook.common.BaseActivity.java
com.baasbox.android.samples.phrasebook.common.BaseFragment.java
com.baasbox.android.samples.phrasebook.loaders.BaasDocumentsLoader.java
com.baasbox.android.samples.phrasebook.ui.HomeActivity.java
com.baasbox.android.samples.phrasebook.ui.PhraseOfTheDay.java
com.baasbox.android.samples.phrasebook.utils.ThreadLocalRandom.java
com.baasbox.android.package-info.java