com.richtodd.android.repository.JSONUtility.java Source code

Java tutorial

Introduction

Here is the source code for com.richtodd.android.repository.JSONUtility.java

Source

/* Copyright (c) 2013 Richard G. Todd.
 * Licensed under the terms of the GNU General Public License (GPL) Version 3.0.
 */

package com.richtodd.android.repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.ContentResolver;
import android.net.Uri;

public class JSONUtility {

    public static JSONObject loadJSONObject(InputStream in) throws RepositoryException {
        try {
            StringBuilder sb = new StringBuilder();

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            try {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } finally {
                reader.close();
            }

            String jsonString = sb.toString();

            JSONObject jsonObject = (JSONObject) new JSONTokener(jsonString).nextValue();

            return jsonObject;
        } catch (FileNotFoundException ex) {
            throw new RepositoryException(ex);
        } catch (IOException ex) {
            throw new RepositoryException(ex);
        } catch (JSONException ex) {
            throw new RepositoryException(ex);
        }
    }

    public static JSONObject loadJSONObject(File file) throws RepositoryException {
        try {
            InputStream in = new FileInputStream(file);
            try {
                return loadJSONObject(in);
            } finally {
                in.close();
            }
        } catch (FileNotFoundException ex) {
            throw new RepositoryException(ex);
        } catch (IOException ex) {
            throw new RepositoryException(ex);
        }
    }

    public static JSONObject loadJSONObject(ContentResolver contentResolver, Uri uri) throws RepositoryException {
        try {
            InputStream in = contentResolver.openInputStream(uri);
            try {
                return loadJSONObject(in);
            } finally {
                in.close();
            }
        } catch (FileNotFoundException ex) {
            throw new RepositoryException(ex);
        } catch (IOException ex) {
            throw new RepositoryException(ex);
        }
    }

    public static void saveJSONObject(File file, JSONObject jsonObject) throws RepositoryException {
        try {
            String jsonString = jsonObject.toString();

            OutputStream out = new FileOutputStream(file);
            try {

                Writer writer = new OutputStreamWriter(out);
                try {
                    writer.write(jsonString);
                } finally {
                    writer.close();
                }
            } finally {
                out.close();
            }
        } catch (FileNotFoundException ex) {
            throw new RepositoryException(ex);
        } catch (IOException ex) {
            throw new RepositoryException(ex);
        }
    }
}