value) throws JSONException {
771 | this.put(index, new JSONObject(value));
772 | return this;
773 | }
774 |
775 | /**
776 | * Put or replace an object value in the JSONArray. If the index is greater
777 | * than the length of the JSONArray, then null elements will be added as
778 | * necessary to pad it out.
779 | *
780 | * @param index
781 | * The subscript.
782 | * @param value
783 | * The value to put into the array. The value should be a
784 | * Boolean, Double, Integer, JSONArray, JSONObject, Long, or
785 | * String, or the JSONObject.NULL object.
786 | * @return this.
787 | * @throws JSONException
788 | * If the index is negative or if the the value is an invalid
789 | * number.
790 | */
791 | public JSONArray put(int index, Object value) throws JSONException {
792 | JSONObject.testValidity(value);
793 | if (index < 0) {
794 | throw new JSONException("JSONArray[" + index + "] not found.");
795 | }
796 | if (index < this.length()) {
797 | this.myArrayList.set(index, value);
798 | } else {
799 | while (index != this.length()) {
800 | this.put(JSONObject.NULL);
801 | }
802 | this.put(value);
803 | }
804 | return this;
805 | }
806 |
807 | /**
808 | * Remove an index and close the hole.
809 | *
810 | * @param index
811 | * The index of the element to be removed.
812 | * @return The value that was associated with the index, or null if there
813 | * was no value.
814 | */
815 | public Object remove(int index) {
816 | return index >= 0 && index < this.length()
817 | ? this.myArrayList.remove(index)
818 | : null;
819 | }
820 |
821 | /**
822 | * Determine if two JSONArrays are similar.
823 | * They must contain similar sequences.
824 | *
825 | * @param other The other JSONArray
826 | * @return true if they are equal
827 | */
828 | public boolean similar(Object other) {
829 | if (!(other instanceof JSONArray)) {
830 | return false;
831 | }
832 | int len = this.length();
833 | if (len != ((JSONArray)other).length()) {
834 | return false;
835 | }
836 | for (int i = 0; i < len; i += 1) {
837 | Object valueThis = this.get(i);
838 | Object valueOther = ((JSONArray)other).get(i);
839 | if (valueThis instanceof JSONObject) {
840 | if (!((JSONObject)valueThis).similar(valueOther)) {
841 | return false;
842 | }
843 | } else if (valueThis instanceof JSONArray) {
844 | if (!((JSONArray)valueThis).similar(valueOther)) {
845 | return false;
846 | }
847 | } else if (!valueThis.equals(valueOther)) {
848 | return false;
849 | }
850 | }
851 | return true;
852 | }
853 |
854 | /**
855 | * Produce a JSONObject by combining a JSONArray of names with the values of
856 | * this JSONArray.
857 | *
858 | * @param names
859 | * A JSONArray containing a list of key strings. These will be
860 | * paired with the values.
861 | * @return A JSONObject, or null if there are no names or if this JSONArray
862 | * has no values.
863 | * @throws JSONException
864 | * If any of the names are null.
865 | */
866 | public JSONObject toJSONObject(JSONArray names) throws JSONException {
867 | if (names == null || names.length() == 0 || this.length() == 0) {
868 | return null;
869 | }
870 | JSONObject jo = new JSONObject();
871 | for (int i = 0; i < names.length(); i += 1) {
872 | jo.put(names.getString(i), this.opt(i));
873 | }
874 | return jo;
875 | }
876 |
877 | /**
878 | * Make a JSON text of this JSONArray. For compactness, no unnecessary
879 | * whitespace is added. If it is not possible to produce a syntactically
880 | * correct JSON text then null will be returned instead. This could occur if
881 | * the array contains an invalid number.
882 | *
883 | * Warning: This method assumes that the data structure is acyclical.
884 | *
885 | * @return a printable, displayable, transmittable representation of the
886 | * array.
887 | */
888 | public String toString() {
889 | try {
890 | return this.toString(0);
891 | } catch (Exception e) {
892 | return null;
893 | }
894 | }
895 |
896 | /**
897 | * Make a prettyprinted JSON text of this JSONArray. Warning: This method
898 | * assumes that the data structure is acyclical.
899 | *
900 | * @param indentFactor
901 | * The number of spaces to add to each level of indentation.
902 | * @return a printable, displayable, transmittable representation of the
903 | * object, beginning with [
(left
904 | * bracket) and ending with ]
905 | * (right bracket).
906 | * @throws JSONException
907 | */
908 | public String toString(int indentFactor) throws JSONException {
909 | StringWriter sw = new StringWriter();
910 | synchronized (sw.getBuffer()) {
911 | return this.write(sw, indentFactor, 0).toString();
912 | }
913 | }
914 |
915 | /**
916 | * Write the contents of the JSONArray as JSON text to a writer. For
917 | * compactness, no whitespace is added.
918 | *
919 | * Warning: This method assumes that the data structure is acyclical.
920 | *
921 | * @return The writer.
922 | * @throws JSONException
923 | */
924 | public Writer write(Writer writer) throws JSONException {
925 | return this.write(writer, 0, 0);
926 | }
927 |
928 | /**
929 | * Write the contents of the JSONArray as JSON text to a writer. For
930 | * compactness, no whitespace is added.
931 | *
932 | * Warning: This method assumes that the data structure is acyclical.
933 | *
934 | * @param indentFactor
935 | * The number of spaces to add to each level of indentation.
936 | * @param indent
937 | * The indention of the top level.
938 | * @return The writer.
939 | * @throws JSONException
940 | */
941 | Writer write(Writer writer, int indentFactor, int indent)
942 | throws JSONException {
943 | try {
944 | boolean commanate = false;
945 | int length = this.length();
946 | writer.write('[');
947 |
948 | if (length == 1) {
949 | JSONObject.writeValue(writer, this.myArrayList.get(0),
950 | indentFactor, indent);
951 | } else if (length != 0) {
952 | final int newindent = indent + indentFactor;
953 |
954 | for (int i = 0; i < length; i += 1) {
955 | if (commanate) {
956 | writer.write(',');
957 | }
958 | if (indentFactor > 0) {
959 | writer.write('\n');
960 | }
961 | JSONObject.indent(writer, newindent);
962 | JSONObject.writeValue(writer, this.myArrayList.get(i),
963 | indentFactor, newindent);
964 | commanate = true;
965 | }
966 | if (indentFactor > 0) {
967 | writer.write('\n');
968 | }
969 | JSONObject.indent(writer, indent);
970 | }
971 | writer.write(']');
972 | return writer;
973 | } catch (IOException e) {
974 | throw new JSONException(e);
975 | }
976 | }
977 | }
978 |
--------------------------------------------------------------------------------
/lib/src/main/java/org/json/JSONException.java:
--------------------------------------------------------------------------------
1 | package org.json;
2 |
3 | /**
4 | * The JSONException is thrown by the JSON.org classes when things are amiss.
5 | *
6 | * @author JSON.org
7 | * @version 2014-05-03
8 | */
9 | public class JSONException extends RuntimeException {
10 | private static final long serialVersionUID = 0;
11 | private Throwable cause;
12 |
13 | /**
14 | * Constructs a JSONException with an explanatory message.
15 | *
16 | * @param message
17 | * Detail about the reason for the exception.
18 | */
19 | public JSONException(String message) {
20 | super(message);
21 | }
22 |
23 | /**
24 | * Constructs a new JSONException with the specified cause.
25 | * @param cause The cause.
26 | */
27 | public JSONException(Throwable cause) {
28 | super(cause.getMessage());
29 | this.cause = cause;
30 | }
31 |
32 | /**
33 | * Returns the cause of this exception or null if the cause is nonexistent
34 | * or unknown.
35 | *
36 | * @return the cause of this exception or null if the cause is nonexistent
37 | * or unknown.
38 | */
39 | @Override
40 | public Throwable getCause() {
41 | return this.cause;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/lib/src/main/java/org/json/JSONString.java:
--------------------------------------------------------------------------------
1 | package org.json;
2 | /**
3 | * The JSONString
interface allows a toJSONString()
4 | * method so that a class can change the behavior of
5 | * JSONObject.toString()
, JSONArray.toString()
,
6 | * and JSONWriter.value(
Object)
. The
7 | * toJSONString
method will be used instead of the default behavior
8 | * of using the Object's toString()
method and quoting the result.
9 | */
10 | public interface JSONString {
11 | /**
12 | * The toJSONString
method allows a class to produce its own JSON
13 | * serialization.
14 | *
15 | * @return A strictly syntactically correct JSON text.
16 | */
17 | public String toJSONString();
18 | }
19 |
--------------------------------------------------------------------------------
/lib/src/main/java/org/json/JSONTokener.java:
--------------------------------------------------------------------------------
1 | package org.json;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.io.InputStreamReader;
7 | import java.io.Reader;
8 | import java.io.StringReader;
9 |
10 | /*
11 | Copyright (c) 2002 JSON.org
12 |
13 | Permission is hereby granted, free of charge, to any person obtaining a copy
14 | of this software and associated documentation files (the "Software"), to deal
15 | in the Software without restriction, including without limitation the rights
16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 | copies of the Software, and to permit persons to whom the Software is
18 | furnished to do so, subject to the following conditions:
19 |
20 | The above copyright notice and this permission notice shall be included in all
21 | copies or substantial portions of the Software.
22 |
23 | The Software shall be used for Good, not Evil.
24 |
25 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 | SOFTWARE.
32 | */
33 |
34 | /**
35 | * A JSONTokener takes a source string and extracts characters and tokens from
36 | * it. It is used by the JSONObject and JSONArray constructors to parse
37 | * JSON source strings.
38 | * @author JSON.org
39 | * @version 2014-05-03
40 | */
41 | public class JSONTokener {
42 |
43 | private long character;
44 | private boolean eof;
45 | private long index;
46 | private long line;
47 | private char previous;
48 | private Reader reader;
49 | private boolean usePrevious;
50 |
51 |
52 | /**
53 | * Construct a JSONTokener from a Reader.
54 | *
55 | * @param reader A reader.
56 | */
57 | public JSONTokener(Reader reader) {
58 | this.reader = reader.markSupported()
59 | ? reader
60 | : new BufferedReader(reader);
61 | this.eof = false;
62 | this.usePrevious = false;
63 | this.previous = 0;
64 | this.index = 0;
65 | this.character = 1;
66 | this.line = 1;
67 | }
68 |
69 |
70 | /**
71 | * Construct a JSONTokener from an InputStream.
72 | * @param inputStream The source.
73 | */
74 | public JSONTokener(InputStream inputStream) throws JSONException {
75 | this(new InputStreamReader(inputStream));
76 | }
77 |
78 |
79 | /**
80 | * Construct a JSONTokener from a string.
81 | *
82 | * @param s A source string.
83 | */
84 | public JSONTokener(String s) {
85 | this(new StringReader(s));
86 | }
87 |
88 |
89 | /**
90 | * Back up one character. This provides a sort of lookahead capability,
91 | * so that you can test for a digit or letter before attempting to parse
92 | * the next number or identifier.
93 | */
94 | public void back() throws JSONException {
95 | if (this.usePrevious || this.index <= 0) {
96 | throw new JSONException("Stepping back two steps is not supported");
97 | }
98 | this.index -= 1;
99 | this.character -= 1;
100 | this.usePrevious = true;
101 | this.eof = false;
102 | }
103 |
104 |
105 | /**
106 | * Get the hex value of a character (base16).
107 | * @param c A character between '0' and '9' or between 'A' and 'F' or
108 | * between 'a' and 'f'.
109 | * @return An int between 0 and 15, or -1 if c was not a hex digit.
110 | */
111 | public static int dehexchar(char c) {
112 | if (c >= '0' && c <= '9') {
113 | return c - '0';
114 | }
115 | if (c >= 'A' && c <= 'F') {
116 | return c - ('A' - 10);
117 | }
118 | if (c >= 'a' && c <= 'f') {
119 | return c - ('a' - 10);
120 | }
121 | return -1;
122 | }
123 |
124 | public boolean end() {
125 | return this.eof && !this.usePrevious;
126 | }
127 |
128 |
129 | /**
130 | * Determine if the source string still contains characters that next()
131 | * can consume.
132 | * @return true if not yet at the end of the source.
133 | */
134 | public boolean more() throws JSONException {
135 | this.next();
136 | if (this.end()) {
137 | return false;
138 | }
139 | this.back();
140 | return true;
141 | }
142 |
143 |
144 | /**
145 | * Get the next character in the source string.
146 | *
147 | * @return The next character, or 0 if past the end of the source string.
148 | */
149 | public char next() throws JSONException {
150 | int c;
151 | if (this.usePrevious) {
152 | this.usePrevious = false;
153 | c = this.previous;
154 | } else {
155 | try {
156 | c = this.reader.read();
157 | } catch (IOException exception) {
158 | throw new JSONException(exception);
159 | }
160 |
161 | if (c <= 0) { // End of stream
162 | this.eof = true;
163 | c = 0;
164 | }
165 | }
166 | this.index += 1;
167 | if (this.previous == '\r') {
168 | this.line += 1;
169 | this.character = c == '\n' ? 0 : 1;
170 | } else if (c == '\n') {
171 | this.line += 1;
172 | this.character = 0;
173 | } else {
174 | this.character += 1;
175 | }
176 | this.previous = (char) c;
177 | return this.previous;
178 | }
179 |
180 |
181 | /**
182 | * Consume the next character, and check that it matches a specified
183 | * character.
184 | * @param c The character to match.
185 | * @return The character.
186 | * @throws JSONException if the character does not match.
187 | */
188 | public char next(char c) throws JSONException {
189 | char n = this.next();
190 | if (n != c) {
191 | throw this.syntaxError("Expected '" + c + "' and instead saw '" +
192 | n + "'");
193 | }
194 | return n;
195 | }
196 |
197 |
198 | /**
199 | * Get the next n characters.
200 | *
201 | * @param n The number of characters to take.
202 | * @return A string of n characters.
203 | * @throws JSONException
204 | * Substring bounds error if there are not
205 | * n characters remaining in the source string.
206 | */
207 | public String next(int n) throws JSONException {
208 | if (n == 0) {
209 | return "";
210 | }
211 |
212 | char[] chars = new char[n];
213 | int pos = 0;
214 |
215 | while (pos < n) {
216 | chars[pos] = this.next();
217 | if (this.end()) {
218 | throw this.syntaxError("Substring bounds error");
219 | }
220 | pos += 1;
221 | }
222 | return new String(chars);
223 | }
224 |
225 |
226 | /**
227 | * Get the next char in the string, skipping whitespace.
228 | * @throws JSONException
229 | * @return A character, or 0 if there are no more characters.
230 | */
231 | public char nextClean() throws JSONException {
232 | for (;;) {
233 | char c = this.next();
234 | if (c == 0 || c > ' ') {
235 | return c;
236 | }
237 | }
238 | }
239 |
240 |
241 | /**
242 | * Return the characters up to the next close quote character.
243 | * Backslash processing is done. The formal JSON format does not
244 | * allow strings in single quotes, but an implementation is allowed to
245 | * accept them.
246 | * @param quote The quoting character, either
247 | * "
(double quote) or
248 | * '
(single quote).
249 | * @return A String.
250 | * @throws JSONException Unterminated string.
251 | */
252 | public String nextString(char quote) throws JSONException {
253 | char c;
254 | StringBuilder sb = new StringBuilder();
255 | for (;;) {
256 | c = this.next();
257 | switch (c) {
258 | case 0:
259 | case '\n':
260 | case '\r':
261 | throw this.syntaxError("Unterminated string");
262 | case '\\':
263 | c = this.next();
264 | switch (c) {
265 | case 'b':
266 | sb.append('\b');
267 | break;
268 | case 't':
269 | sb.append('\t');
270 | break;
271 | case 'n':
272 | sb.append('\n');
273 | break;
274 | case 'f':
275 | sb.append('\f');
276 | break;
277 | case 'r':
278 | sb.append('\r');
279 | break;
280 | case 'u':
281 | sb.append((char)Integer.parseInt(this.next(4), 16));
282 | break;
283 | case '"':
284 | case '\'':
285 | case '\\':
286 | case '/':
287 | sb.append(c);
288 | break;
289 | default:
290 | throw this.syntaxError("Illegal escape.");
291 | }
292 | break;
293 | default:
294 | if (c == quote) {
295 | return sb.toString();
296 | }
297 | sb.append(c);
298 | }
299 | }
300 | }
301 |
302 |
303 | /**
304 | * Get the text up but not including the specified character or the
305 | * end of line, whichever comes first.
306 | * @param delimiter A delimiter character.
307 | * @return A string.
308 | */
309 | public String nextTo(char delimiter) throws JSONException {
310 | StringBuilder sb = new StringBuilder();
311 | for (;;) {
312 | char c = this.next();
313 | if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
314 | if (c != 0) {
315 | this.back();
316 | }
317 | return sb.toString().trim();
318 | }
319 | sb.append(c);
320 | }
321 | }
322 |
323 |
324 | /**
325 | * Get the text up but not including one of the specified delimiter
326 | * characters or the end of line, whichever comes first.
327 | * @param delimiters A set of delimiter characters.
328 | * @return A string, trimmed.
329 | */
330 | public String nextTo(String delimiters) throws JSONException {
331 | char c;
332 | StringBuilder sb = new StringBuilder();
333 | for (;;) {
334 | c = this.next();
335 | if (delimiters.indexOf(c) >= 0 || c == 0 ||
336 | c == '\n' || c == '\r') {
337 | if (c != 0) {
338 | this.back();
339 | }
340 | return sb.toString().trim();
341 | }
342 | sb.append(c);
343 | }
344 | }
345 |
346 |
347 | /**
348 | * Get the next value. The value can be a Boolean, Double, Integer,
349 | * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
350 | * @throws JSONException If syntax error.
351 | *
352 | * @return An object.
353 | */
354 | public Object nextValue() throws JSONException {
355 | char c = this.nextClean();
356 | String string;
357 |
358 | switch (c) {
359 | case '"':
360 | case '\'':
361 | return this.nextString(c);
362 | case '{':
363 | this.back();
364 | return new org.json.JSONObject(this);
365 | case '[':
366 | this.back();
367 | return new org.json.JSONArray(this);
368 | }
369 |
370 | /*
371 | * Handle unquoted text. This could be the values true, false, or
372 | * null, or it can be a number. An implementation (such as this one)
373 | * is allowed to also accept non-standard forms.
374 | *
375 | * Accumulate characters until we reach the end of the text or a
376 | * formatting character.
377 | */
378 |
379 | StringBuilder sb = new StringBuilder();
380 | while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
381 | sb.append(c);
382 | c = this.next();
383 | }
384 | this.back();
385 |
386 | string = sb.toString().trim();
387 | if ("".equals(string)) {
388 | throw this.syntaxError("Missing value");
389 | }
390 | return JSONObject.stringToValue(string);
391 | }
392 |
393 |
394 | /**
395 | * Skip characters until the next character is the requested character.
396 | * If the requested character is not found, no characters are skipped.
397 | * @param to A character to skip to.
398 | * @return The requested character, or zero if the requested character
399 | * is not found.
400 | */
401 | public char skipTo(char to) throws JSONException {
402 | char c;
403 | try {
404 | long startIndex = this.index;
405 | long startCharacter = this.character;
406 | long startLine = this.line;
407 | this.reader.mark(1000000);
408 | do {
409 | c = this.next();
410 | if (c == 0) {
411 | this.reader.reset();
412 | this.index = startIndex;
413 | this.character = startCharacter;
414 | this.line = startLine;
415 | return c;
416 | }
417 | } while (c != to);
418 | } catch (IOException exception) {
419 | throw new JSONException(exception);
420 | }
421 | this.back();
422 | return c;
423 | }
424 |
425 |
426 | /**
427 | * Make a JSONException to signal a syntax error.
428 | *
429 | * @param message The error message.
430 | * @return A JSONException object, suitable for throwing
431 | */
432 | public JSONException syntaxError(String message) {
433 | return new JSONException(message + this.toString());
434 | }
435 |
436 |
437 | /**
438 | * Make a printable string of this JSONTokener.
439 | *
440 | * @return " at {index} [character {character} line {line}]"
441 | */
442 | public String toString() {
443 | return " at " + this.index + " [character " + this.character + " line " +
444 | this.line + "]";
445 | }
446 | }
447 |
--------------------------------------------------------------------------------
/lib/src/main/resources/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Main-Class: com.jsonformat.Main
3 |
4 |
--------------------------------------------------------------------------------
/lib/src/main/resources/META-INF/services/javax.annotation.processing.Processor:
--------------------------------------------------------------------------------
1 | kale.net.json.processor.Json2ModelProcessor
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':lib'
2 |
--------------------------------------------------------------------------------