1616
1717package uk .co .hassieswift621 .libraries .jsonio ;
1818
19+ import org .json .JSONArray ;
1920import org .json .JSONObject ;
21+ import uk .co .hassieswift621 .libraries .jsonio .exceptions .JsonIOException ;
2022
2123import java .io .*;
2224
@@ -30,9 +32,9 @@ public class JsonIO {
3032 *
3133 * @param inputStream The input stream.
3234 * @return A UTF-8 encoded JSON object.
33- * @throws IOException If the conversion failed.
35+ * @throws JsonIOException If the conversion failed.
3436 */
35- public static JSONObject toJSON (InputStream inputStream ) throws IOException {
37+ public static JSONObject toJson (InputStream inputStream ) throws JsonIOException {
3638
3739 try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
3840
@@ -48,6 +50,9 @@ public static JSONObject toJSON(InputStream inputStream) throws IOException {
4850
4951 // Convert output stream to JSON.
5052 return new JSONObject (outputStream .toString ("UTF-8" ));
53+
54+ } catch (IOException e ) {
55+ throw new JsonIOException ("Failed to convert input stream to JSON" , e );
5156 }
5257 }
5358
@@ -56,13 +61,16 @@ public static JSONObject toJSON(InputStream inputStream) throws IOException {
5661 *
5762 * @param file The file to convert.
5863 * @return A UTF-8 encoded JSON object.
59- * @throws IOException If the conversion failed.
64+ * @throws JsonIOException If the conversion failed.
6065 */
61- public static JSONObject toJSON (File file ) throws IOException {
66+ public static JSONObject toJson (File file ) throws JsonIOException {
6267
6368 try (FileInputStream fileInputStream = new FileInputStream (file )) {
6469
65- return toJSON (fileInputStream );
70+ return toJson (fileInputStream );
71+
72+ } catch (IOException e ) {
73+ throw new JsonIOException ("Failed to convert file to JSON" , e );
6674 }
6775 }
6876
@@ -71,20 +79,23 @@ public static JSONObject toJSON(File file) throws IOException {
7179 *
7280 * @param string The JSON as a string.
7381 * @return A UTF-8 encoded JSON object.
74- * @throws IOException If the conversion failed.
82+ * @throws JsonIOException If the conversion failed.
7583 */
76- public static JSONObject toJSON (String string ) throws IOException {
84+ public static JSONObject toJson (String string ) throws JsonIOException {
7785
7886 // Get raw JSON.
79- byte [] rawJSON = string .getBytes ();
87+ byte [] rawJson = string .getBytes ();
8088
8189 try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
8290
8391 // Convert bytes to output stream.
84- outputStream .write (rawJSON );
92+ outputStream .write (rawJson );
8593
8694 // Convert output stream to JSON.
8795 return new JSONObject (outputStream .toString ("UTF-8" ));
96+
97+ } catch (IOException e ) {
98+ throw new JsonIOException ("Failed to convert string to JSON" , e );
8899 }
89100 }
90101
@@ -93,9 +104,9 @@ public static JSONObject toJSON(String string) throws IOException {
93104 *
94105 * @param json The JSON object to output.
95106 * @param file The file to create.
96- * @throws IOException If the output failed.
107+ * @throws JsonIOException If the output failed.
97108 */
98- public static void toFile (JSONObject json , File file ) throws IOException {
109+ public static void toFile (JSONObject json , File file ) throws JsonIOException {
99110
100111 toFile (json , file , false );
101112 }
@@ -106,17 +117,20 @@ public static void toFile(JSONObject json, File file) throws IOException {
106117 * @param json The JSON object to output.
107118 * @param file The file to create or to append to.
108119 * @param append Whether to append the JSON contents to a file.
109- * @throws IOException If the output failed.
120+ * @throws JsonIOException If the output failed.
110121 */
111- public static void toFile (JSONObject json , File file , boolean append ) throws IOException {
112-
113- // Get raw JSON.
114- byte [] rawJSON = json .toString ().getBytes ("UTF-8" );
122+ public static void toFile (JSONObject json , File file , boolean append ) throws JsonIOException {
115123
116124 try (FileOutputStream fileOutputStream = new FileOutputStream (file , append )) {
117125
126+ // Get raw JSON.
127+ byte [] rawJson = json .toString ().getBytes ("UTF-8" );
128+
118129 // Write the raw JSON to file.
119- fileOutputStream .write (rawJSON );
130+ fileOutputStream .write (rawJson );
131+
132+ } catch (IOException e ) {
133+ throw new JsonIOException ("Failed to convert JSON to file" , e );
120134 }
121135 }
122136
@@ -125,18 +139,218 @@ public static void toFile(JSONObject json, File file, boolean append) throws IOE
125139 *
126140 * @param json The JSON object to convert.
127141 * @return A UTF-8 encoded string.
142+ * @throws JsonIOException If the conversion failed.
143+ */
144+ public static String toString (JSONObject json ) throws JsonIOException {
145+
146+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
147+
148+ // Get raw JSON.
149+ byte [] rawJson = json .toString ().getBytes ();
150+
151+ outputStream .write (rawJson );
152+
153+ // Convert output stream to string.
154+ return outputStream .toString ("UTF-8" );
155+
156+ } catch (IOException e ) {
157+ throw new JsonIOException ("Failed to convert JSON to string" , e );
158+ }
159+ }
160+
161+ /**
162+ * Converts an input stream to an UTF-8 encoded JSON array object.
163+ *
164+ * @param inputStream The input stream.
165+ * @return A UTF-8 encoded JSON array object.
166+ * @throws JsonIOException If the conversion failed.
128167 */
129- public static String toString (JSONObject json ) throws IOException {
168+ public static JSONArray toJsonArray (InputStream inputStream ) throws JsonIOException {
169+
170+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
171+
172+ // Create buffer.
173+ byte [] buffer = new byte [1024 ];
174+
175+ // Convert input stream to byte array output stream.
176+ int length = inputStream .read (buffer );
177+ while (length != -1 ) {
178+ outputStream .write (buffer , 0 , length );
179+ length = inputStream .read (buffer );
180+ }
181+
182+ // Convert output stream to JSON.
183+ return new JSONArray (outputStream .toString ("UTF-8" ));
184+
185+ } catch (IOException e ) {
186+ throw new JsonIOException ("Failed to convert input stream to JSON array" , e );
187+ }
188+ }
189+
190+ /**
191+ * Converts a file to a UTF-8 encoded JSON array object.
192+ *
193+ * @param file The file to convert.
194+ * @return A UTF-8 encoded JSON array object.
195+ * @throws JsonIOException If the conversion failed.
196+ */
197+ public static JSONArray toJsonArray (File file ) throws JsonIOException {
198+
199+ try (FileInputStream fileInputStream = new FileInputStream (file )) {
200+
201+ return toJsonArray (fileInputStream );
202+
203+ } catch (IOException e ) {
204+ throw new JsonIOException ("Failed to convert file to JSON array" , e );
205+ }
206+ }
207+
208+ /**
209+ * Converts a JSON string to a UTF-8 encoded JSON array object.
210+ *
211+ * @param string The JSON as a string.
212+ * @return A UTF-8 encoded JSON array object.
213+ * @throws JsonIOException If the conversion failed.
214+ */
215+ public static JSONArray toJsonArray (String string ) throws JsonIOException {
130216
131217 // Get raw JSON.
132- byte [] rawJSON = json . toString () .getBytes ();
218+ byte [] rawJson = string .getBytes ();
133219
134220 try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
135221
136- outputStream .write (rawJSON );
222+ // Convert bytes to output stream.
223+ outputStream .write (rawJson );
224+
225+ // Convert output stream to JSON.
226+ return new JSONArray (outputStream .toString ("UTF-8" ));
227+
228+ } catch (IOException e ) {
229+ throw new JsonIOException ("Failed to convert string to JSON array" , e );
230+ }
231+ }
232+
233+ /**
234+ * Outputs a JSON array object to a UTF-8 encoded file.
235+ *
236+ * @param json The JSON array object to output.
237+ * @param file The file to create.
238+ * @throws JsonIOException If the output failed.
239+ */
240+ public static void toFile (JSONArray json , File file ) throws JsonIOException {
241+
242+ toFile (json , file , false );
243+ }
244+
245+ /**
246+ * Outputs a JSON array object to a UTF-8 encoded file.
247+ *
248+ * @param json The JSON array object to output.
249+ * @param file The file to create or to append to.
250+ * @param append Whether to append the JSON array contents to a file.
251+ * @throws JsonIOException If the output failed.
252+ */
253+ public static void toFile (JSONArray json , File file , boolean append ) throws JsonIOException {
254+
255+ try (FileOutputStream fileOutputStream = new FileOutputStream (file , append )) {
256+
257+ // Get raw JSON.
258+ byte [] rawJson = json .toString ().getBytes ("UTF-8" );
259+
260+ // Write the raw JSON to file.
261+ fileOutputStream .write (rawJson );
262+
263+ } catch (IOException e ) {
264+ throw new JsonIOException ("Failed to convert JSON array to file" , e );
265+ }
266+ }
267+
268+ /**
269+ * Converts a JSON array object to a UTF-8 encoded string.
270+ *
271+ * @param json The JSON array object to convert.
272+ * @return A UTF-8 encoded string.
273+ */
274+ public static String toString (JSONArray json ) throws JsonIOException {
275+
276+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
277+
278+ // Get raw JSON.
279+ byte [] rawJson = json .toString ().getBytes ();
280+
281+ outputStream .write (rawJson );
137282
138283 // Convert output stream to string.
139284 return outputStream .toString ("UTF-8" );
285+
286+ } catch (IOException e ) {
287+ throw new JsonIOException ("Failed to convert JSON array to string" , e );
288+ }
289+ }
290+
291+ /**
292+ * Converts an input stream to an UTF-8 encoded JSON object.
293+ *
294+ * @param inputStream The input stream.
295+ * @return A UTF-8 encoded JSON object.
296+ * @throws IOException If the conversion failed.
297+ */
298+ @ Deprecated
299+ public static JSONObject toJSON (InputStream inputStream ) throws IOException {
300+
301+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
302+
303+ // Create buffer.
304+ byte [] buffer = new byte [1024 ];
305+
306+ // Convert input stream to byte array output stream.
307+ int length = inputStream .read (buffer );
308+ while (length != -1 ) {
309+ outputStream .write (buffer , 0 , length );
310+ length = inputStream .read (buffer );
311+ }
312+
313+ // Convert output stream to JSON.
314+ return new JSONObject (outputStream .toString ("UTF-8" ));
315+ }
316+ }
317+
318+ /**
319+ * Converts a file to a UTF-8 encoded JSON object.
320+ *
321+ * @param file The file to convert.
322+ * @return A UTF-8 encoded JSON object.
323+ * @throws IOException If the conversion failed.
324+ */
325+ @ Deprecated
326+ public static JSONObject toJSON (File file ) throws IOException {
327+
328+ try (FileInputStream fileInputStream = new FileInputStream (file )) {
329+
330+ return toJSON (fileInputStream );
331+ }
332+ }
333+
334+ /**
335+ * Converts a JSON string to a UTF-8 encoded JSON object.
336+ *
337+ * @param string The JSON as a string.
338+ * @return A UTF-8 encoded JSON object.
339+ * @throws IOException If the conversion failed.
340+ */
341+ @ Deprecated
342+ public static JSONObject toJSON (String string ) throws IOException {
343+
344+ // Get raw JSON.
345+ byte [] rawJSON = string .getBytes ();
346+
347+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
348+
349+ // Convert bytes to output stream.
350+ outputStream .write (rawJSON );
351+
352+ // Convert output stream to JSON.
353+ return new JSONObject (outputStream .toString ("UTF-8" ));
140354 }
141355 }
142356
0 commit comments