Uses an internal JSON output stream. - Android File Input Output

Android examples for File Input Output:Json

Description

Uses an internal JSON output stream.

Demo Code


//package com.java2s;

import android.util.JsonWriter;

import java.io.ByteArrayOutputStream;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class Main {
    private ByteArrayOutputStream output_stream;
    private JsonWriter writer;

    public static JsonWriter createWriter(OutputStream outputStream) {
        try {//  w w w.  ja va2s  .  c om
            return new JsonWriter(new OutputStreamWriter(outputStream,
                    "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * Uses an internal output stream. Call getString() after this method. Example:
     * JSONHelper h = new JSONHelper();
     * toJSON(h.createWriter());
     * h.getString();
     *
     * @return Return a json writer
     */
    public JsonWriter createWriter() {
        output_stream = new ByteArrayOutputStream();
        try {
            writer = new JsonWriter(new OutputStreamWriter(output_stream,
                    "UTF-8"));
            return writer;
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
}

Related Tutorials