Android Open Source - Android-NetPowerctrl-Shared J S O N Helper






From Project

Back to project page Android-NetPowerctrl-Shared.

License

The source code is released under:

Copyright (c) 2014, David Gr?ff All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *...

If you think the Android project Android-NetPowerctrl-Shared 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 oly.netpowerctrl.device_base.data;
/*from  ww  w.j a va 2  s  .  c  o  m*/
import android.util.JsonReader;
import android.util.JsonWriter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

/**
 * Some helper methods to read and write json to and from strings.
 */
public class JSONHelper {
    private ByteArrayOutputStream output_stream;
    private JsonWriter writer;

    public static JsonReader getReader(byte[] bytes) {
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        return new JsonReader(new InputStreamReader(stream));
    }

    public static JsonReader getReader(String text) {
        if (text == null)
            return null;

        // Get JsonReader from String
        byte[] bytes;
        try {
            bytes = text.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ignored) {
            return null;
        }
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        return new JsonReader(new InputStreamReader(stream));
    }

    public static JsonWriter createWriter(OutputStream outputStream) {
        try {
            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;
        }
    }

    public String getString() throws IOException {
        writer.close();
        return output_stream.toString();
    }
}




Java Source Code List

oly.netpowerctrl.device_base.ApplicationTest.java
oly.netpowerctrl.device_base.data.JSONHelper.java
oly.netpowerctrl.device_base.data.StorableInterface.java
oly.netpowerctrl.device_base.device.DeviceConnectionAPI.java
oly.netpowerctrl.device_base.device.DeviceConnectionFabric.java
oly.netpowerctrl.device_base.device.DeviceConnectionHTTP.java
oly.netpowerctrl.device_base.device.DeviceConnectionUDP.java
oly.netpowerctrl.device_base.device.DeviceConnection.java
oly.netpowerctrl.device_base.device.DeviceFeatureFabric.java
oly.netpowerctrl.device_base.device.DeviceFeatureInterface.java
oly.netpowerctrl.device_base.device.DeviceFeatureTemperature.java
oly.netpowerctrl.device_base.device.DevicePort.java
oly.netpowerctrl.device_base.device.Device.java
oly.netpowerctrl.device_base.executables.ExecutableReachability.java
oly.netpowerctrl.device_base.executables.ExecutableType.java
oly.netpowerctrl.device_base.executables.Executable.java
oly.netpowerctrl.plugin.App2PluginCommunication.java
oly.netpowerctrl.plugin.App2PluginService.java
oly.netpowerctrl.plugin.DeviceConnectionStateInterface.java
oly.netpowerctrl.plugin.onDeviceStateChange.java
oly.netpowerctrl.plugin.onServiceStateChange.java