package com.electronwill.json;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.List;
import java.util.Map;

/**
 * Utility class for reading and writing JSON data. This class internally uses {@link JsonReader} and {@link JsonWriter}
 * .
 *
 * @author TheElectronWill
 */
public final class Json {
	
	/**
	 * Reads a json array from a Reader.
	 */
	public static List<Object> readArray(Reader reader) throws IOException {
		String data = readToString(reader);
		JsonReader jr = new JsonReader(data);
		return jr.readArray();
	}
	
	/**
	 * Reads a json array from a String.
	 */
	public static List<Object> readArray(String data) throws IOException {
		JsonReader jr = new JsonReader(data);
		return jr.readArray();
	}
	
	/**
	 * Reads a json object from a Reader.
	 */
	public static Map<String, Object> readObject(Reader reader) throws IOException {
		String data = readToString(reader);
		JsonReader jr = new JsonReader(data);
		return jr.readObject();
	}
	
	/**
	 * Reads a json object from a String.
	 */
	public static Map<String, Object> readObject(String data) throws IOException {
		JsonReader jr = new JsonReader(data);
		return jr.readObject();
	}
	
	/**
	 * Reads a json object or a json array, depending on the first non space (and non newline) character read.
	 */
	public static Object readObjectOrArray(Reader reader) throws IOException {
		String data = readToString(reader);
		JsonReader jr = new JsonReader(data);
		return jr.readObjectOrArray();
	}
	
	/**
	 * Reads a json object or a json array, depending on the first non space (and non newline) character read.
	 */
	public static Object readObjectOrArray(String data) throws IOException {
		JsonReader jr = new JsonReader(data);
		return jr.readObjectOrArray();
	}
	
	private static String readToString(Reader reader) throws IOException {
		StringBuilder sb = new StringBuilder(4096);
		char[] buf = new char[4096];
		int read;
		while ((read = reader.read(buf)) != -1) {
			sb.append(buf, 0, read);
		}
		return sb.toString();
	}
	
	/**
	 * Writes a json array to a Writer. This method is equivalent to {@code write(jsonArray, writer, false)}, so it
	 * isn't human friendly.
	 */
	public static void write(List<Object> jsonArray, Writer writer) throws IOException {
		write(jsonArray, writer, false);
	}
	
	/**
	 * Writes a json array to a Writer.
	 * 
	 * @param true to indent and space the output, false to compact everything
	 */
	public static void write(List<Object> jsonArray, Writer writer, boolean humanFriendly) throws IOException {
		JsonWriter jw = new JsonWriter(writer, humanFriendly);
		jw.writeArray(jsonArray);
	}
	
	/**
	 * Writes a json object to a Writer. This method is equivalent to {@code write(jsonObject, writer, false)}, so it
	 * isn't human friendly.
	 */
	public static void write(Map<String, Object> jsonObject, Writer writer) throws IOException {
		write(jsonObject, writer, false);
	}
	
	/**
	 * Writes a json object to a Writer.
	 * 
	 * @param true to indent and space the output, false to compact everything
	 */
	public static void write(Map<String, Object> jsonObject, Writer writer, boolean humanFriendly) throws IOException {
		JsonWriter jw = new JsonWriter(writer, humanFriendly);
		jw.writeObject(jsonObject);
	}
	
	/**
	 * Writes a json array to a String. This method is equivalent to {@code write(jsonArray, false)}, so it isn't human
	 * friendly.
	 */
	public static void writeToString(List<Object> jsonArray) throws IOException {
		writeToString(jsonArray, false);
	}
	
	/**
	 * Writes a json array to a String.
	 * 
	 * @param true to indent and space the output, false to compact everything
	 */
	public static void writeToString(List<Object> jsonArray, boolean humanFriendly) throws IOException {
		FastStringWriter stringWriter = new FastStringWriter();
		write(jsonArray, stringWriter, humanFriendly);
	}
	
	/**
	 * Writes a json object to a Writer. This method is equivalent to {@code write(jsonObject, writer, false)}, so it
	 * isn't human friendly.
	 */
	public static void writeToString(Map<String, Object> jsonObject) throws IOException {
		writeToString(jsonObject, false);
	}
	
	/**
	 * Writes a json object to a String.
	 * 
	 * @param true to indent and space the output, false to compact everything
	 */
	public static void writeToString(Map<String, Object> jsonObject, boolean humanFriendly) throws IOException {
		FastStringWriter stringWriter = new FastStringWriter();
		write(jsonObject, stringWriter, humanFriendly);
	}
	
	private Json() {}
	
}

@@ -3344,32 +3344,40 @@
  to %7B@code write
+ToString
 (jsonArray, fals
@@ -3386,16 +3386,20 @@
 %7D, so it
+%0A%09 *
  isn't h
@@ -3402,20 +3402,16 @@
 't human
-%0A%09 *
  friendl
@@ -3425,36 +3425,38 @@
 %0A%09public static 
-void
+String
  writeToString(L
@@ -3493,32 +3493,39 @@
 IOException %7B%0A%09%09
+return 
 writeToString(js
@@ -3681,36 +3681,38 @@
 %0A%09public static 
-void
+String
  writeToString(L
@@ -3881,24 +3881,58 @@
 nFriendly);%0A
+%09%09return stringWriter.toString();%0A
 %09%7D%0A%09%0A%09/**%0A%09 
@@ -4000,32 +4000,40 @@
  to %7B@code write
+ToString
 (jsonObject, wri
@@ -4024,32 +4024,24 @@
 (jsonObject,
- writer,
  false)%7D, so
@@ -4082,36 +4082,38 @@
 %0A%09public static 
-void
+String
  writeToString(M
@@ -4162,24 +4162,31 @@
 ception %7B%0A%09%09
+return 
 writeToStrin
@@ -4352,28 +4352,30 @@
 blic static 
-void
+String
  writeToStri
@@ -4557,24 +4557,58 @@
 nFriendly);%0A
+%09%09return stringWriter.toString();%0A
 %09%7D%0A%09%0A%09privat

Fix return type of writeToString()