Back to project page Android-NetPowerctrl-Shared.
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.
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(); } }