get
and
40 | * opt
methods for accessing the values by index, and
41 | * put
methods for adding or replacing values. The values can be
42 | * any of these types:
43 | * Boolean
,
44 | * JSONArray
,
45 | * JSONObject
,
46 | * Number
,
47 | * String
, or the
48 | * JSONObject.NULL object
.
49 | *
50 | * The constructor can convert a JSON text into a Java object. The
51 | * toString
method converts to JSON text.
52 | *
53 | * A
54 | * get
method returns a value if one can be found, and throws an
55 | * exception if one cannot be found. An
56 | * opt
method returns a default value instead of throwing an
57 | * exception, and so is useful for obtaining optional values.
58 | *
59 | * The generic
60 | * get()
and
61 | * opt()
methods return an object which you can cast or query for
62 | * type. There are also typed
63 | * get
and
64 | * opt
methods that do type checking and type coercion for you.
65 | *
66 | * The texts produced by the
67 | * toString
methods strictly conform to JSON syntax rules. The
68 | * constructors are more forgiving in the texts they will accept:
69 | *
,
(comma) may appear just before the closing
72 | * bracket.null
value will be inserted when there is
75 | * ,
(comma) elision.'
(single quote).{ } [ ] / \ : , = ; #
and if they do not look like numbers and
82 | * if they are not the reserved words
83 | * true
,
84 | * false
, or
85 | * null
.;
(semicolon) as well as by
88 | * ,
(comma).[
(left
150 | * bracket)
151 | * and ends with ]
(right bracket).
152 | * @throws JSONException If there is a syntax error.
153 | */
154 | public JSONArray(String source) throws JSONException {
155 | this(new JSONTokener(source));
156 | }
157 |
158 | /**
159 | * Construct a JSONArray from a Collection.
160 | *
161 | * @param collection A Collection.
162 | */
163 | public JSONArray(Collection collection) {
164 | this.myArrayList = new ArrayList();
165 | if (collection != null) {
166 | Iterator iter = collection.iterator();
167 | while (iter.hasNext()) {
168 | this.myArrayList.add(JSONObject.wrap(iter.next()));
169 | }
170 | }
171 | }
172 |
173 | /**
174 | * Construct a JSONArray from an array
175 | *
176 | * @throws JSONException If not an array.
177 | */
178 | @SuppressWarnings("OverridableMethodCallInConstructor")
179 | public JSONArray(Object array) throws JSONException {
180 | this();
181 | if (array.getClass().isArray()) {
182 | int length = Array.getLength(array);
183 | for (int i = 0; i < length; i += 1) {
184 | this.put(JSONObject.wrap(Array.get(array, i)));
185 | }
186 | } else {
187 | throw new JSONException(
188 | "JSONArray initial value should be a string or collection or array.");
189 | }
190 | }
191 |
192 | /**
193 | * Get the object value associated with an index.
194 | *
195 | * @param index The index must be between 0 and length() - 1.
196 | * @return An object value.
197 | * @throws JSONException If there is no value for the index.
198 | */
199 | public Object get(int index) throws JSONException {
200 | Object object = this.opt(index);
201 | if (object == null) {
202 | throw new JSONException("JSONArray[" + index + "] not found.");
203 | }
204 | return object;
205 | }
206 |
207 | /**
208 | * Get the boolean value associated with an index. The string values "true"
209 | * and "false" are converted to boolean.
210 | *
211 | * @param index The index must be between 0 and length() - 1.
212 | * @return The truth.
213 | * @throws JSONException If there is no value for the index or if the value
214 | * is not convertible to boolean.
215 | */
216 | public boolean getBoolean(int index) throws JSONException {
217 | Object object = this.get(index);
218 | if (object.equals(Boolean.FALSE)
219 | || (object instanceof String
220 | && ((String) object).equalsIgnoreCase("false"))) {
221 | return false;
222 | } else if (object.equals(Boolean.TRUE)
223 | || (object instanceof String
224 | && ((String) object).equalsIgnoreCase("true"))) {
225 | return true;
226 | }
227 | throw new JSONException("JSONArray[" + index + "] is not a boolean.");
228 | }
229 |
230 | /**
231 | * Get the double value associated with an index.
232 | *
233 | * @param index The index must be between 0 and length() - 1.
234 | * @return The value.
235 | * @throws JSONException If the key is not found or if the value cannot be
236 | * converted to a number.
237 | */
238 | public double getDouble(int index) throws JSONException {
239 | Object object = this.get(index);
240 | try {
241 | return object instanceof Number
242 | ? ((Number) object).doubleValue()
243 | : Double.parseDouble((String) object);
244 | } catch (Exception e) {
245 | throw new JSONException("JSONArray[" + index
246 | + "] is not a number.");
247 | }
248 | }
249 |
250 | /**
251 | * Get the int value associated with an index.
252 | *
253 | * @param index The index must be between 0 and length() - 1.
254 | * @return The value.
255 | * @throws JSONException If the key is not found or if the value is not a
256 | * number.
257 | */
258 | public int getInt(int index) throws JSONException {
259 | Object object = this.get(index);
260 | try {
261 | return object instanceof Number
262 | ? ((Number) object).intValue()
263 | : Integer.parseInt((String) object);
264 | } catch (Exception e) {
265 | throw new JSONException("JSONArray[" + index
266 | + "] is not a number.");
267 | }
268 | }
269 |
270 | /**
271 | * Get the JSONArray associated with an index.
272 | *
273 | * @param index The index must be between 0 and length() - 1.
274 | * @return A JSONArray value.
275 | * @throws JSONException If there is no value for the index. or if the value
276 | * is not a JSONArray
277 | */
278 | public JSONArray getJSONArray(int index) throws JSONException {
279 | Object object = this.get(index);
280 | if (object instanceof JSONArray) {
281 | return (JSONArray) object;
282 | }
283 | throw new JSONException("JSONArray[" + index
284 | + "] is not a JSONArray.");
285 | }
286 |
287 | /**
288 | * Get the JSONObject associated with an index.
289 | *
290 | * @param index subscript
291 | * @return A JSONObject value.
292 | * @throws JSONException If there is no value for the index or if the value
293 | * is not a JSONObject
294 | */
295 | public JSONObject getJSONObject(int index) throws JSONException {
296 | Object object = this.get(index);
297 | if (object instanceof JSONObject) {
298 | return (JSONObject) object;
299 | }
300 | throw new JSONException("JSONArray[" + index
301 | + "] is not a JSONObject.");
302 | }
303 |
304 | /**
305 | * Get the long value associated with an index.
306 | *
307 | * @param index The index must be between 0 and length() - 1.
308 | * @return The value.
309 | * @throws JSONException If the key is not found or if the value cannot be
310 | * converted to a number.
311 | */
312 | public long getLong(int index) throws JSONException {
313 | Object object = this.get(index);
314 | try {
315 | return object instanceof Number
316 | ? ((Number) object).longValue()
317 | : Long.parseLong((String) object);
318 | } catch (Exception e) {
319 | throw new JSONException("JSONArray[" + index
320 | + "] is not a number.");
321 | }
322 | }
323 |
324 | /**
325 | * Get the string associated with an index.
326 | *
327 | * @param index The index must be between 0 and length() - 1.
328 | * @return A string value.
329 | * @throws JSONException If there is no string value for the index.
330 | */
331 | public String getString(int index) throws JSONException {
332 | Object object = this.get(index);
333 | if (object instanceof String) {
334 | return (String) object;
335 | }
336 | throw new JSONException("JSONArray[" + index + "] not a string.");
337 | }
338 |
339 | /**
340 | * Determine if the value is null.
341 | *
342 | * @param index The index must be between 0 and length() - 1.
343 | * @return true if the value at the index is null, or if there is no value.
344 | */
345 | public boolean isNull(int index) {
346 | return JSONObject.NULL.equals(this.opt(index));
347 | }
348 |
349 | /**
350 | * Make a string from the contents of this JSONArray. The
351 | * separator
string is inserted between each element. Warning:
352 | * This method assumes that the data structure is acyclical.
353 | *
354 | * @param separator A string that will be inserted between the elements.
355 | * @return a string.
356 | * @throws JSONException If the array contains an invalid number.
357 | */
358 | public String join(String separator) throws JSONException {
359 | int len = this.length();
360 | StringBuilder sb = new StringBuilder();
361 |
362 | for (int i = 0; i < len; i += 1) {
363 | if (i > 0) {
364 | sb.append(separator);
365 | }
366 | sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
367 | }
368 | return sb.toString();
369 | }
370 |
371 | /**
372 | * Get the number of elements in the JSONArray, included nulls.
373 | *
374 | * @return The length (or size).
375 | */
376 | public int length() {
377 | return this.myArrayList.size();
378 | }
379 |
380 | /**
381 | * Get the optional object value associated with an index.
382 | *
383 | * @param index The index must be between 0 and length() - 1.
384 | * @return An object value, or null if there is no object at that index.
385 | */
386 | public Object opt(int index) {
387 | return (index < 0 || index >= this.length())
388 | ? null
389 | : this.myArrayList.get(index);
390 | }
391 |
392 | /**
393 | * Get the optional boolean value associated with an index. It returns false
394 | * if there is no value at that index, or if the value is not Boolean.TRUE
395 | * or the String "true".
396 | *
397 | * @param index The index must be between 0 and length() - 1.
398 | * @return The truth.
399 | */
400 | public boolean optBoolean(int index) {
401 | return this.optBoolean(index, false);
402 | }
403 |
404 | /**
405 | * Get the optional boolean value associated with an index. It returns the
406 | * defaultValue if there is no value at that index or if it is not a Boolean
407 | * or the String "true" or "false" (case insensitive).
408 | *
409 | * @param index The index must be between 0 and length() - 1.
410 | * @param defaultValue A boolean default.
411 | * @return The truth.
412 | */
413 | public boolean optBoolean(int index, boolean defaultValue) {
414 | try {
415 | return this.getBoolean(index);
416 | } catch (Exception e) {
417 | return defaultValue;
418 | }
419 | }
420 |
421 | /**
422 | * Get the optional double value associated with an index. NaN is returned
423 | * if there is no value for the index, or if the value is not a number and
424 | * cannot be converted to a number.
425 | *
426 | * @param index The index must be between 0 and length() - 1.
427 | * @return The value.
428 | */
429 | public double optDouble(int index) {
430 | return this.optDouble(index, Double.NaN);
431 | }
432 |
433 | /**
434 | * Get the optional double value associated with an index. The defaultValue
435 | * is returned if there is no value for the index, or if the value is not a
436 | * number and cannot be converted to a number.
437 | *
438 | * @param index subscript
439 | * @param defaultValue The default value.
440 | * @return The value.
441 | */
442 | public double optDouble(int index, double defaultValue) {
443 | try {
444 | return this.getDouble(index);
445 | } catch (Exception e) {
446 | return defaultValue;
447 | }
448 | }
449 |
450 | /**
451 | * Get the optional int value associated with an index. Zero is returned if
452 | * there is no value for the index, or if the value is not a number and
453 | * cannot be converted to a number.
454 | *
455 | * @param index The index must be between 0 and length() - 1.
456 | * @return The value.
457 | */
458 | public int optInt(int index) {
459 | return this.optInt(index, 0);
460 | }
461 |
462 | /**
463 | * Get the optional int value associated with an index. The defaultValue is
464 | * returned if there is no value for the index, or if the value is not a
465 | * number and cannot be converted to a number.
466 | *
467 | * @param index The index must be between 0 and length() - 1.
468 | * @param defaultValue The default value.
469 | * @return The value.
470 | */
471 | public int optInt(int index, int defaultValue) {
472 | try {
473 | return this.getInt(index);
474 | } catch (Exception e) {
475 | return defaultValue;
476 | }
477 | }
478 |
479 | /**
480 | * Get the optional JSONArray associated with an index.
481 | *
482 | * @param index subscript
483 | * @return A JSONArray value, or null if the index has no value, or if the
484 | * value is not a JSONArray.
485 | */
486 | public JSONArray optJSONArray(int index) {
487 | Object o = this.opt(index);
488 | return o instanceof JSONArray ? (JSONArray) o : null;
489 | }
490 |
491 | /**
492 | * Get the optional JSONObject associated with an index. Null is returned if
493 | * the key is not found, or null if the index has no value, or if the value
494 | * is not a JSONObject.
495 | *
496 | * @param index The index must be between 0 and length() - 1.
497 | * @return A JSONObject value.
498 | */
499 | public JSONObject optJSONObject(int index) {
500 | Object o = this.opt(index);
501 | return o instanceof JSONObject ? (JSONObject) o : null;
502 | }
503 |
504 | /**
505 | * Get the optional long value associated with an index. Zero is returned if
506 | * there is no value for the index, or if the value is not a number and
507 | * cannot be converted to a number.
508 | *
509 | * @param index The index must be between 0 and length() - 1.
510 | * @return The value.
511 | */
512 | public long optLong(int index) {
513 | return this.optLong(index, 0);
514 | }
515 |
516 | /**
517 | * Get the optional long value associated with an index. The defaultValue is
518 | * returned if there is no value for the index, or if the value is not a
519 | * number and cannot be converted to a number.
520 | *
521 | * @param index The index must be between 0 and length() - 1.
522 | * @param defaultValue The default value.
523 | * @return The value.
524 | */
525 | public long optLong(int index, long defaultValue) {
526 | try {
527 | return this.getLong(index);
528 | } catch (Exception e) {
529 | return defaultValue;
530 | }
531 | }
532 |
533 | /**
534 | * Get the optional string value associated with an index. It returns an
535 | * empty string if there is no value at that index. If the value is not a
536 | * string and is not null, then it is coverted to a string.
537 | *
538 | * @param index The index must be between 0 and length() - 1.
539 | * @return A String value.
540 | */
541 | public String optString(int index) {
542 | return this.optString(index, "");
543 | }
544 |
545 | /**
546 | * Get the optional string associated with an index. The defaultValue is
547 | * returned if the key is not found.
548 | *
549 | * @param index The index must be between 0 and length() - 1.
550 | * @param defaultValue The default value.
551 | * @return A String value.
552 | */
553 | public String optString(int index, String defaultValue) {
554 | Object object = this.opt(index);
555 | return JSONObject.NULL.equals(object)
556 | ? defaultValue
557 | : object.toString();
558 | }
559 |
560 | /**
561 | * Append a boolean value. This increases the array's length by one.
562 | *
563 | * @param value A boolean value.
564 | * @return this.
565 | */
566 | public JSONArray put(boolean value) {
567 | this.put(value ? Boolean.TRUE : Boolean.FALSE);
568 | return this;
569 | }
570 |
571 | /**
572 | * Put a value in the JSONArray, where the value will be a JSONArray which
573 | * is produced from a Collection.
574 | *
575 | * @param value A Collection value.
576 | * @return this.
577 | */
578 | public JSONArray put(Collection value) {
579 | this.put(new JSONArray(value));
580 | return this;
581 | }
582 |
583 | /**
584 | * Append a double value. This increases the array's length by one.
585 | *
586 | * @param value A double value.
587 | * @throws JSONException if the value is not finite.
588 | * @return this.
589 | */
590 | public JSONArray put(double value) throws JSONException {
591 | Double d = Double.valueOf(value);
592 | JSONObject.testValidity(d);
593 | this.put(d);
594 | return this;
595 | }
596 |
597 | /**
598 | * Append an int value. This increases the array's length by one.
599 | *
600 | * @param value An int value.
601 | * @return this.
602 | */
603 | public JSONArray put(int value) {
604 | this.put(Integer.valueOf(value));
605 | return this;
606 | }
607 |
608 | /**
609 | * Append an long value. This increases the array's length by one.
610 | *
611 | * @param value A long value.
612 | * @return this.
613 | */
614 | public JSONArray put(long value) {
615 | this.put(Long.valueOf(value));
616 | return this;
617 | }
618 |
619 | /**
620 | * Put a value in the JSONArray, where the value will be a JSONObject which
621 | * is produced from a Map.
622 | *
623 | * @param value A Map value.
624 | * @return this.
625 | */
626 | public JSONArray put(Map value) {
627 | this.put(new JSONObject(value));
628 | return this;
629 | }
630 |
631 | /**
632 | * Append an object value. This increases the array's length by one.
633 | *
634 | * @param value An object value. The value should be a Boolean, Double,
635 | * Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL
636 | * object.
637 | * @return this.
638 | */
639 | public JSONArray put(Object value) {
640 | this.myArrayList.add(value);
641 | return this;
642 | }
643 |
644 | /**
645 | * Put or replace a boolean value in the JSONArray. If the index is greater
646 | * than the length of the JSONArray, then null elements will be added as
647 | * necessary to pad it out.
648 | *
649 | * @param index The subscript.
650 | * @param value A boolean value.
651 | * @return this.
652 | * @throws JSONException If the index is negative.
653 | */
654 | public JSONArray put(int index, boolean value) throws JSONException {
655 | this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
656 | return this;
657 | }
658 |
659 | /**
660 | * Put a value in the JSONArray, where the value will be a JSONArray which
661 | * is produced from a Collection.
662 | *
663 | * @param index The subscript.
664 | * @param value A Collection value.
665 | * @return this.
666 | * @throws JSONException If the index is negative or if the value is not
667 | * finite.
668 | */
669 | public JSONArray put(int index, Collection value) throws JSONException {
670 | this.put(index, new JSONArray(value));
671 | return this;
672 | }
673 |
674 | /**
675 | * Put or replace a double value. If the index is greater than the length of
676 | * the JSONArray, then null elements will be added as necessary to pad it
677 | * out.
678 | *
679 | * @param index The subscript.
680 | * @param value A double value.
681 | * @return this.
682 | * @throws JSONException If the index is negative or if the value is not
683 | * finite.
684 | */
685 | public JSONArray put(int index, double value) throws JSONException {
686 | this.put(index, Double.valueOf(value));
687 | return this;
688 | }
689 |
690 | /**
691 | * Put or replace an int value. If the index is greater than the length of
692 | * the JSONArray, then null elements will be added as necessary to pad it
693 | * out.
694 | *
695 | * @param index The subscript.
696 | * @param value An int value.
697 | * @return this.
698 | * @throws JSONException If the index is negative.
699 | */
700 | public JSONArray put(int index, int value) throws JSONException {
701 | this.put(index, Integer.valueOf(value));
702 | return this;
703 | }
704 |
705 | /**
706 | * Put or replace a long value. If the index is greater than the length of
707 | * the JSONArray, then null elements will be added as necessary to pad it
708 | * out.
709 | *
710 | * @param index The subscript.
711 | * @param value A long value.
712 | * @return this.
713 | * @throws JSONException If the index is negative.
714 | */
715 | public JSONArray put(int index, long value) throws JSONException {
716 | this.put(index, Long.valueOf(value));
717 | return this;
718 | }
719 |
720 | /**
721 | * Put a value in the JSONArray, where the value will be a JSONObject that
722 | * is produced from a Map.
723 | *
724 | * @param index The subscript.
725 | * @param value The Map value.
726 | * @return this.
727 | * @throws JSONException If the index is negative or if the the value is an
728 | * invalid number.
729 | */
730 | public JSONArray put(int index, Map value) throws JSONException {
731 | this.put(index, new JSONObject(value));
732 | return this;
733 | }
734 |
735 | /**
736 | * Put or replace an object value in the JSONArray. If the index is greater
737 | * than the length of the JSONArray, then null elements will be added as
738 | * necessary to pad it out.
739 | *
740 | * @param index The subscript.
741 | * @param value The value to put into the array. The value should be a
742 | * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
743 | * JSONObject.NULL object.
744 | * @return this.
745 | * @throws JSONException If the index is negative or if the the value is an
746 | * invalid number.
747 | */
748 | public JSONArray put(int index, Object value) throws JSONException {
749 | JSONObject.testValidity(value);
750 | if (index < 0) {
751 | throw new JSONException("JSONArray[" + index + "] not found.");
752 | }
753 | if (index < this.length()) {
754 | this.myArrayList.set(index, value);
755 | } else {
756 | while (index != this.length()) {
757 | this.put(JSONObject.NULL);
758 | }
759 | this.put(value);
760 | }
761 | return this;
762 | }
763 |
764 | /**
765 | * Remove an index and close the hole.
766 | *
767 | * @param index The index of the element to be removed.
768 | * @return The value that was associated with the index, or null if there
769 | * was no value.
770 | */
771 | public Object remove(int index) {
772 | Object o = this.opt(index);
773 | this.myArrayList.remove(index);
774 | return o;
775 | }
776 |
777 | /**
778 | * Produce a JSONObject by combining a JSONArray of names with the values of
779 | * this JSONArray.
780 | *
781 | * @param names A JSONArray containing a list of key strings. These will be
782 | * paired with the values.
783 | * @return A JSONObject, or null if there are no names or if this JSONArray
784 | * has no values.
785 | * @throws JSONException If any of the names are null.
786 | */
787 | public JSONObject toJSONObject(JSONArray names) throws JSONException {
788 | if (names == null || names.length() == 0 || this.length() == 0) {
789 | return null;
790 | }
791 | JSONObject jo = new JSONObject();
792 | for (int i = 0; i < names.length(); i += 1) {
793 | jo.put(names.getString(i), this.opt(i));
794 | }
795 | return jo;
796 | }
797 |
798 | /**
799 | * Make a JSON text of this JSONArray. For compactness, no unnecessary
800 | * whitespace is added. If it is not possible to produce a syntactically
801 | * correct JSON text then null will be returned instead. This could occur if
802 | * the array contains an invalid number.
803 | *
804 | * Warning: This method assumes that the data structure is acyclical.
805 | *
806 | * @return a printable, displayable, transmittable representation of the
807 | * array.
808 | */
809 | @Override
810 | public String toString() {
811 | try {
812 | return this.toString(0);
813 | } catch (Exception e) {
814 | return null;
815 | }
816 | }
817 |
818 | /**
819 | * Make a prettyprinted JSON text of this JSONArray. Warning: This method
820 | * assumes that the data structure is acyclical.
821 | *
822 | * @param indentFactor The number of spaces to add to each level of
823 | * indentation.
824 | * @return a printable, displayable, transmittable representation of the
825 | * object, beginning with [
(left bracket)
826 | * and ending with ]
(right bracket).
827 | * @throws JSONException
828 | */
829 | public String toString(int indentFactor) throws JSONException {
830 | StringWriter sw = new StringWriter();
831 | synchronized (sw.getBuffer()) {
832 | return this.write(sw, indentFactor, 0).toString();
833 | }
834 | }
835 |
836 | /**
837 | * Write the contents of the JSONArray as JSON text to a writer. For
838 | * compactness, no whitespace is added.
839 | *
840 | * Warning: This method assumes that the data structure is acyclical. 841 | * 842 | * @return The writer. 843 | * @throws JSONException 844 | */ 845 | public Writer write(Writer writer) throws JSONException { 846 | return this.write(writer, 0, 0); 847 | } 848 | 849 | /** 850 | * Write the contents of the JSONArray as JSON text to a writer. For 851 | * compactness, no whitespace is added. 852 | *
853 | * Warning: This method assumes that the data structure is acyclical.
854 | *
855 | * @param indentFactor The number of spaces to add to each level of
856 | * indentation.
857 | * @param indent The indention of the top level.
858 | * @return The writer.
859 | * @throws JSONException
860 | */
861 | Writer write(Writer writer, int indentFactor, int indent)
862 | throws JSONException {
863 | try {
864 | boolean commanate = false;
865 | int length = this.length();
866 | writer.write('[');
867 |
868 | if (length == 1) {
869 | JSONObject.writeValue(writer, this.myArrayList.get(0),
870 | indentFactor, indent);
871 | } else if (length != 0) {
872 | final int newindent = indent + indentFactor;
873 |
874 | for (int i = 0; i < length; i += 1) {
875 | if (commanate) {
876 | writer.write(',');
877 | }
878 | if (indentFactor > 0) {
879 | writer.write('\n');
880 | }
881 | JSONObject.indent(writer, newindent);
882 | JSONObject.writeValue(writer, this.myArrayList.get(i),
883 | indentFactor, newindent);
884 | commanate = true;
885 | }
886 | if (indentFactor > 0) {
887 | writer.write('\n');
888 | }
889 | JSONObject.indent(writer, indent);
890 | }
891 | writer.write(']');
892 | return writer;
893 | } catch (IOException e) {
894 | throw new JSONException(e);
895 | }
896 | }
897 | }
898 |
--------------------------------------------------------------------------------
/SteamTrade-Java/src/main/java/bundled/steamtrade/org/json/JSONException.java:
--------------------------------------------------------------------------------
1 | package bundled.steamtrade.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 2010-12-24
8 | */
9 | public class JSONException extends Exception {
10 |
11 | private static final long serialVersionUID = 0;
12 | private Throwable cause;
13 |
14 | /**
15 | * Constructs a JSONException with an explanatory message.
16 | *
17 | * @param message Detail about the reason for the exception.
18 | */
19 | public JSONException(String message) {
20 | super(message);
21 | }
22 |
23 | public JSONException(Throwable cause) {
24 | super(cause.getMessage());
25 | this.cause = cause;
26 | }
27 |
28 | @Override
29 | public Throwable getCause() {
30 | return this.cause;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/SteamTrade-Java/src/main/java/bundled/steamtrade/org/json/JSONString.java:
--------------------------------------------------------------------------------
1 | package bundled.steamtrade.org.json;
2 |
3 | /**
4 | * The
5 | * JSONString
interface allows a
6 | * toJSONString()
method so that a class can change the behavior of
7 | * JSONObject.toString()
,
8 | * JSONArray.toString()
, and
9 | * JSONWriter.value(
Object)
. The
10 | * toJSONString
method will be used instead of the default behavior
11 | * of using the Object's
12 | * toString()
method and quoting the result.
13 | */
14 | public interface JSONString {
15 |
16 | /**
17 | * The
18 | * toJSONString
method allows a class to produce its own JSON
19 | * serialization.
20 | *
21 | * @return A strictly syntactically correct JSON text.
22 | */
23 | public String toJSONString();
24 | }
25 |
--------------------------------------------------------------------------------
/SteamTrade-Java/src/main/java/bundled/steamtrade/org/json/JSONStringer.java:
--------------------------------------------------------------------------------
1 | package bundled.steamtrade.org.json;
2 |
3 | /*
4 | * Copyright (c) 2006 JSON.org
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * The Software shall be used for Good, not Evil.
17 | *
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 | * SOFTWARE.
25 | */
26 | import java.io.StringWriter;
27 |
28 | /**
29 | * JSONStringer provides a quick and convenient way of producing JSON text. The
30 | * texts produced strictly conform to JSON syntax rules. No whitespace is added,
31 | * so the results are ready for transmission or storage. Each instance of
32 | * JSONStringer can produce one JSON text.
33 | *
34 | * A JSONStringer instance provides a
35 | * value
method for appending values to the text, and a
36 | * key
method for adding keys before values in objects. There are
37 | * array
and
38 | * endArray
methods that make and bound array values, and
39 | * object
and
40 | * endObject
methods which make and bound object values. All of
41 | * these methods return the JSONWriter instance, permitting cascade style. For
42 | * example,
43 | *
44 | * myString = new JSONStringer() 45 | * .object() 46 | * .key("JSON") 47 | * .value("Hello, World!") 48 | * .endObject() 49 | * .toString();which produces the string 50 | *
51 | * {"JSON":"Hello, World!"}52 | *
53 | * The first method called must be
54 | * array
or
55 | * object
. There are no methods for adding commas or colons.
56 | * JSONStringer adds them for you. Objects and arrays can be nested up to 20
57 | * levels deep.
58 | *
59 | * This can sometimes be easier than using a JSONObject to build a string.
60 | *
61 | * @author JSON.org
62 | * @version 2008-09-18
63 | */
64 | public class JSONStringer extends JSONWriter {
65 |
66 | /**
67 | * Make a fresh JSONStringer. It can be used to build one JSON text.
68 | */
69 | public JSONStringer() {
70 | super(new StringWriter());
71 | }
72 |
73 | /**
74 | * Return the JSON text. This method is used to obtain the product of the
75 | * JSONStringer instance. It will return
76 | * null
if there was a problem in the construction of the JSON
77 | * text (such as the calls to
78 | * array
were not properly balanced with calls to
79 | * endArray
).
80 | *
81 | * @return The JSON text.
82 | */
83 | @Override
84 | public String toString() {
85 | return this.mode == 'd' ? this.writer.toString() : null;
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/SteamTrade-Java/src/main/java/bundled/steamtrade/org/json/JSONTokener.java:
--------------------------------------------------------------------------------
1 | package bundled.steamtrade.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
21 | * all 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 | * A JSONTokener takes a source string and extracts characters and tokens from
35 | * it. It is used by the JSONObject and JSONArray constructors to parse JSON
36 | * source strings.
37 | *
38 | * @author JSON.org
39 | * @version 2012-02-16
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 | * Construct a JSONTokener from a Reader.
53 | *
54 | * @param reader A reader.
55 | */
56 | public JSONTokener(Reader reader) {
57 | this.reader = reader.markSupported()
58 | ? reader
59 | : new BufferedReader(reader);
60 | this.eof = false;
61 | this.usePrevious = false;
62 | this.previous = 0;
63 | this.index = 0;
64 | this.character = 1;
65 | this.line = 1;
66 | }
67 |
68 | /**
69 | * Construct a JSONTokener from an InputStream.
70 | */
71 | public JSONTokener(InputStream inputStream) throws JSONException {
72 | this(new InputStreamReader(inputStream));
73 | }
74 |
75 | /**
76 | * Construct a JSONTokener from a string.
77 | *
78 | * @param s A source string.
79 | */
80 | public JSONTokener(String s) {
81 | this(new StringReader(s));
82 | }
83 |
84 | /**
85 | * Back up one character. This provides a sort of lookahead capability, so
86 | * that you can test for a digit or letter before attempting to parse the
87 | * next number or identifier.
88 | */
89 | public void back() throws JSONException {
90 | if (this.usePrevious || this.index <= 0) {
91 | throw new JSONException("Stepping back two steps is not supported");
92 | }
93 | this.index -= 1;
94 | this.character -= 1;
95 | this.usePrevious = true;
96 | this.eof = false;
97 | }
98 |
99 | /**
100 | * Get the hex value of a character (base16).
101 | *
102 | * @param c A character between '0' and '9' or between 'A' and 'F' or
103 | * between 'a' and 'f'.
104 | * @return An int between 0 and 15, or -1 if c was not a hex digit.
105 | */
106 | public static int dehexchar(char c) {
107 | if (c >= '0' && c <= '9') {
108 | return c - '0';
109 | }
110 | if (c >= 'A' && c <= 'F') {
111 | return c - ('A' - 10);
112 | }
113 | if (c >= 'a' && c <= 'f') {
114 | return c - ('a' - 10);
115 | }
116 | return -1;
117 | }
118 |
119 | public boolean end() {
120 | return this.eof && !this.usePrevious;
121 | }
122 |
123 | /**
124 | * Determine if the source string still contains characters that next() can
125 | * consume.
126 | *
127 | * @return true if not yet at the end of the source.
128 | */
129 | public boolean more() throws JSONException {
130 | this.next();
131 | if (this.end()) {
132 | return false;
133 | }
134 | this.back();
135 | return true;
136 | }
137 |
138 | /**
139 | * Get the next character in the source string.
140 | *
141 | * @return The next character, or 0 if past the end of the source string.
142 | */
143 | public char next() throws JSONException {
144 | int c;
145 | if (this.usePrevious) {
146 | this.usePrevious = false;
147 | c = this.previous;
148 | } else {
149 | try {
150 | c = this.reader.read();
151 | } catch (IOException exception) {
152 | throw new JSONException(exception);
153 | }
154 |
155 | if (c <= 0) { // End of stream
156 | this.eof = true;
157 | c = 0;
158 | }
159 | }
160 | this.index += 1;
161 | if (this.previous == '\r') {
162 | this.line += 1;
163 | this.character = c == '\n' ? 0 : 1;
164 | } else if (c == '\n') {
165 | this.line += 1;
166 | this.character = 0;
167 | } else {
168 | this.character += 1;
169 | }
170 | this.previous = (char) c;
171 | return this.previous;
172 | }
173 |
174 | /**
175 | * Consume the next character, and check that it matches a specified
176 | * character.
177 | *
178 | * @param c The character to match.
179 | * @return The character.
180 | * @throws JSONException if the character does not match.
181 | */
182 | public char next(char c) throws JSONException {
183 | char n = this.next();
184 | if (n != c) {
185 | throw this.syntaxError("Expected '" + c + "' and instead saw '"
186 | + n + "'");
187 | }
188 | return n;
189 | }
190 |
191 | /**
192 | * Get the next n characters.
193 | *
194 | * @param n The number of characters to take.
195 | * @return A string of n characters.
196 | * @throws JSONException Substring bounds error if there are not n
197 | * characters remaining in the source string.
198 | */
199 | public String next(int n) throws JSONException {
200 | if (n == 0) {
201 | return "";
202 | }
203 |
204 | char[] chars = new char[n];
205 | int pos = 0;
206 |
207 | while (pos < n) {
208 | chars[pos] = this.next();
209 | if (this.end()) {
210 | throw this.syntaxError("Substring bounds error");
211 | }
212 | pos += 1;
213 | }
214 | return new String(chars);
215 | }
216 |
217 | /**
218 | * Get the next char in the string, skipping whitespace.
219 | *
220 | * @throws JSONException
221 | * @return A character, or 0 if there are no more characters.
222 | */
223 | public char nextClean() throws JSONException {
224 | for (;;) {
225 | char c = this.next();
226 | if (c == 0 || c > ' ') {
227 | return c;
228 | }
229 | }
230 | }
231 |
232 | /**
233 | * Return the characters up to the next close quote character. Backslash
234 | * processing is done. The formal JSON format does not allow strings in
235 | * single quotes, but an implementation is allowed to accept them.
236 | *
237 | * @param quote The quoting character, either
238 | * "
(double quote) or
239 | * '
(single quote).
240 | * @return A String.
241 | * @throws JSONException Unterminated string.
242 | */
243 | public String nextString(char quote) throws JSONException {
244 | char c;
245 | StringBuilder sb = new StringBuilder();
246 | for (;;) {
247 | c = this.next();
248 | switch (c) {
249 | case 0:
250 | case '\n':
251 | case '\r':
252 | throw this.syntaxError("Unterminated string");
253 | case '\\':
254 | c = this.next();
255 | switch (c) {
256 | case 'b':
257 | sb.append('\b');
258 | break;
259 | case 't':
260 | sb.append('\t');
261 | break;
262 | case 'n':
263 | sb.append('\n');
264 | break;
265 | case 'f':
266 | sb.append('\f');
267 | break;
268 | case 'r':
269 | sb.append('\r');
270 | break;
271 | case 'u':
272 | sb.append((char) Integer.parseInt(this.next(4), 16));
273 | break;
274 | case '"':
275 | case '\'':
276 | case '\\':
277 | case '/':
278 | sb.append(c);
279 | break;
280 | default:
281 | throw this.syntaxError("Illegal escape.");
282 | }
283 | break;
284 | default:
285 | if (c == quote) {
286 | return sb.toString();
287 | }
288 | sb.append(c);
289 | }
290 | }
291 | }
292 |
293 | /**
294 | * Get the text up but not including the specified character or the end of
295 | * line, whichever comes first.
296 | *
297 | * @param delimiter A delimiter character.
298 | * @return A string.
299 | */
300 | public String nextTo(char delimiter) throws JSONException {
301 | StringBuilder sb = new StringBuilder();
302 | for (;;) {
303 | char c = this.next();
304 | if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
305 | if (c != 0) {
306 | this.back();
307 | }
308 | return sb.toString().trim();
309 | }
310 | sb.append(c);
311 | }
312 | }
313 |
314 | /**
315 | * Get the text up but not including one of the specified delimiter
316 | * characters or the end of line, whichever comes first.
317 | *
318 | * @param delimiters A set of delimiter characters.
319 | * @return A string, trimmed.
320 | */
321 | public String nextTo(String delimiters) throws JSONException {
322 | char c;
323 | StringBuilder sb = new StringBuilder();
324 | for (;;) {
325 | c = this.next();
326 | if (delimiters.indexOf(c) >= 0 || c == 0
327 | || c == '\n' || c == '\r') {
328 | if (c != 0) {
329 | this.back();
330 | }
331 | return sb.toString().trim();
332 | }
333 | sb.append(c);
334 | }
335 | }
336 |
337 | /**
338 | * Get the next value. The value can be a Boolean, Double, Integer,
339 | * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
340 | *
341 | * @throws JSONException If syntax error.
342 | *
343 | * @return An object.
344 | */
345 | public Object nextValue() throws JSONException {
346 | char c = this.nextClean();
347 | String string;
348 |
349 | switch (c) {
350 | case '"':
351 | case '\'':
352 | return this.nextString(c);
353 | case '{':
354 | this.back();
355 | return new JSONObject(this);
356 | case '[':
357 | this.back();
358 | return new JSONArray(this);
359 | }
360 |
361 | /*
362 | * Handle unquoted text. This could be the values true, false, or null,
363 | * or it can be a number. An implementation (such as this one) is
364 | * allowed to also accept non-standard forms.
365 | *
366 | * Accumulate characters until we reach the end of the text or a
367 | * formatting character.
368 | */
369 |
370 | StringBuilder sb = new StringBuilder();
371 | while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
372 | sb.append(c);
373 | c = this.next();
374 | }
375 | this.back();
376 |
377 | string = sb.toString().trim();
378 | if ("".equals(string)) {
379 | throw this.syntaxError("Missing value");
380 | }
381 | return JSONObject.stringToValue(string);
382 | }
383 |
384 | /**
385 | * Skip characters until the next character is the requested character. If
386 | * the requested character is not found, no characters are skipped.
387 | *
388 | * @param to A character to skip to.
389 | * @return The requested character, or zero if the requested character is
390 | * not found.
391 | */
392 | public char skipTo(char to) throws JSONException {
393 | char c;
394 | try {
395 | long startIndex = this.index;
396 | long startCharacter = this.character;
397 | long startLine = this.line;
398 | this.reader.mark(1000000);
399 | do {
400 | c = this.next();
401 | if (c == 0) {
402 | this.reader.reset();
403 | this.index = startIndex;
404 | this.character = startCharacter;
405 | this.line = startLine;
406 | return c;
407 | }
408 | } while (c != to);
409 | } catch (IOException exc) {
410 | throw new JSONException(exc);
411 | }
412 |
413 | this.back();
414 | return c;
415 | }
416 |
417 | /**
418 | * Make a JSONException to signal a syntax error.
419 | *
420 | * @param message The error message.
421 | * @return A JSONException object, suitable for throwing
422 | */
423 | public JSONException syntaxError(String message) {
424 | return new JSONException(message + this.toString());
425 | }
426 |
427 | /**
428 | * Make a printable string of this JSONTokener.
429 | *
430 | * @return " at {index} [character {character} line {line}]"
431 | */
432 | @Override
433 | public String toString() {
434 | return " at " + this.index + " [character " + this.character + " line "
435 | + this.line + "]";
436 | }
437 | }
438 |
--------------------------------------------------------------------------------
/SteamTrade-Java/src/main/java/bundled/steamtrade/org/json/JSONWriter.java:
--------------------------------------------------------------------------------
1 | package bundled.steamtrade.org.json;
2 |
3 | import java.io.IOException;
4 | import java.io.Writer;
5 |
6 | /*
7 | * Copyright (c) 2006 JSON.org
8 | *
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy
10 | * of this software and associated documentation files (the "Software"), to deal
11 | * in the Software without restriction, including without limitation the rights
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | * copies of the Software, and to permit persons to whom the Software is
14 | * furnished to do so, subject to the following conditions:
15 | *
16 | * The above copyright notice and this permission notice shall be included in
17 | * all copies or substantial portions of the Software.
18 | *
19 | * The Software shall be used for Good, not Evil.
20 | *
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 | * SOFTWARE.
28 | */
29 | /**
30 | * JSONWriter provides a quick and convenient way of producing JSON text. The
31 | * texts produced strictly conform to JSON syntax rules. No whitespace is added,
32 | * so the results are ready for transmission or storage. Each instance of
33 | * JSONWriter can produce one JSON text.
34 | *
35 | * A JSONWriter instance provides a
36 | * value
method for appending values to the text, and a
37 | * key
method for adding keys before values in objects. There are
38 | * array
and
39 | * endArray
methods that make and bound array values, and
40 | * object
and
41 | * endObject
methods which make and bound object values. All of
42 | * these methods return the JSONWriter instance, permitting a cascade style. For
43 | * example,
44 | *
45 | * new JSONWriter(myWriter) 46 | * .object() 47 | * .key("JSON") 48 | * .value("Hello, World!") 49 | * .endObject();which writes 50 | *
51 | * {"JSON":"Hello, World!"}52 | *
53 | * The first method called must be
54 | * array
or
55 | * object
. There are no methods for adding commas or colons.
56 | * JSONWriter adds them for you. Objects and arrays can be nested up to 20
57 | * levels deep.
58 | *
59 | * This can sometimes be easier than using a JSONObject to build a string.
60 | *
61 | * @author JSON.org
62 | * @version 2011-11-24
63 | */
64 | public class JSONWriter {
65 |
66 | private static final int maxdepth = 200;
67 | /**
68 | * The comma flag determines if a comma should be output before the next
69 | * value.
70 | */
71 | private boolean comma;
72 | /**
73 | * The current mode. Values: 'a' (array), 'd' (done), 'i' (initial), 'k'
74 | * (key), 'o' (object).
75 | */
76 | protected char mode;
77 | /**
78 | * The object/array stack.
79 | */
80 | private final JSONObject stack[];
81 | /**
82 | * The stack top index. A value of 0 indicates that the stack is empty.
83 | */
84 | private int top;
85 | /**
86 | * The writer that will receive the output.
87 | */
88 | protected Writer writer;
89 |
90 | /**
91 | * Make a fresh JSONWriter. It can be used to build one JSON text.
92 | */
93 | public JSONWriter(Writer w) {
94 | this.comma = false;
95 | this.mode = 'i';
96 | this.stack = new JSONObject[maxdepth];
97 | this.top = 0;
98 | this.writer = w;
99 | }
100 |
101 | /**
102 | * Append a value.
103 | *
104 | * @param string A string value.
105 | * @return this
106 | * @throws JSONException If the value is out of sequence.
107 | */
108 | private JSONWriter append(String string) throws JSONException {
109 | if (string == null) {
110 | throw new JSONException("Null pointer");
111 | }
112 | if (this.mode == 'o' || this.mode == 'a') {
113 | try {
114 | if (this.comma && this.mode == 'a') {
115 | this.writer.write(',');
116 | }
117 | this.writer.write(string);
118 | } catch (IOException e) {
119 | throw new JSONException(e);
120 | }
121 | if (this.mode == 'o') {
122 | this.mode = 'k';
123 | }
124 | this.comma = true;
125 | return this;
126 | }
127 | throw new JSONException("Value out of sequence.");
128 | }
129 |
130 | /**
131 | * Begin appending a new array. All values until the balancing
132 | * endArray
will be appended to this array. The
133 | * endArray
method must be called to mark the array's end.
134 | *
135 | * @return this
136 | * @throws JSONException If the nesting is too deep, or if the object is
137 | * started in the wrong place (for example as a key or after the end of the
138 | * outermost array or object).
139 | */
140 | public JSONWriter array() throws JSONException {
141 | if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
142 | this.push(null);
143 | this.append("[");
144 | this.comma = false;
145 | return this;
146 | }
147 | throw new JSONException("Misplaced array.");
148 | }
149 |
150 | /**
151 | * End something.
152 | *
153 | * @param mode Mode
154 | * @param c Closing character
155 | * @return this
156 | * @throws JSONException If unbalanced.
157 | */
158 | private JSONWriter end(char mode, char c) throws JSONException {
159 | if (this.mode != mode) {
160 | throw new JSONException(mode == 'a'
161 | ? "Misplaced endArray."
162 | : "Misplaced endObject.");
163 | }
164 | this.pop(mode);
165 | try {
166 | this.writer.write(c);
167 | } catch (IOException e) {
168 | throw new JSONException(e);
169 | }
170 | this.comma = true;
171 | return this;
172 | }
173 |
174 | /**
175 | * End an array. This method most be called to balance calls to
176 | * array
.
177 | *
178 | * @return this
179 | * @throws JSONException If incorrectly nested.
180 | */
181 | public JSONWriter endArray() throws JSONException {
182 | return this.end('a', ']');
183 | }
184 |
185 | /**
186 | * End an object. This method most be called to balance calls to
187 | * object
.
188 | *
189 | * @return this
190 | * @throws JSONException If incorrectly nested.
191 | */
192 | public JSONWriter endObject() throws JSONException {
193 | return this.end('k', '}');
194 | }
195 |
196 | /**
197 | * Append a key. The key will be associated with the next value. In an
198 | * object, every value must be preceded by a key.
199 | *
200 | * @param string A key string.
201 | * @return this
202 | * @throws JSONException If the key is out of place. For example, keys do
203 | * not belong in arrays or if the key is null.
204 | */
205 | public JSONWriter key(String string) throws JSONException {
206 | if (string == null) {
207 | throw new JSONException("Null key.");
208 | }
209 | if (this.mode == 'k') {
210 | try {
211 | this.stack[this.top - 1].putOnce(string, Boolean.TRUE);
212 | if (this.comma) {
213 | this.writer.write(',');
214 | }
215 | this.writer.write(JSONObject.quote(string));
216 | this.writer.write(':');
217 | this.comma = false;
218 | this.mode = 'o';
219 | return this;
220 | } catch (IOException e) {
221 | throw new JSONException(e);
222 | }
223 | }
224 | throw new JSONException("Misplaced key.");
225 | }
226 |
227 | /**
228 | * Begin appending a new object. All keys and values until the balancing
229 | * endObject
will be appended to this object. The
230 | * endObject
method must be called to mark the object's end.
231 | *
232 | * @return this
233 | * @throws JSONException If the nesting is too deep, or if the object is
234 | * started in the wrong place (for example as a key or after the end of the
235 | * outermost array or object).
236 | */
237 | public JSONWriter object() throws JSONException {
238 | if (this.mode == 'i') {
239 | this.mode = 'o';
240 | }
241 | if (this.mode == 'o' || this.mode == 'a') {
242 | this.append("{");
243 | this.push(new JSONObject());
244 | this.comma = false;
245 | return this;
246 | }
247 | throw new JSONException("Misplaced object.");
248 |
249 | }
250 |
251 | /**
252 | * Pop an array or object scope.
253 | *
254 | * @param c The scope to close.
255 | * @throws JSONException If nesting is wrong.
256 | */
257 | private void pop(char c) throws JSONException {
258 | if (this.top <= 0) {
259 | throw new JSONException("Nesting error.");
260 | }
261 | char m = this.stack[this.top - 1] == null ? 'a' : 'k';
262 | if (m != c) {
263 | throw new JSONException("Nesting error.");
264 | }
265 | this.top -= 1;
266 | this.mode = this.top == 0
267 | ? 'd'
268 | : this.stack[this.top - 1] == null
269 | ? 'a'
270 | : 'k';
271 | }
272 |
273 | /**
274 | * Push an array or object scope.
275 | *
276 | * @param c The scope to open.
277 | * @throws JSONException If nesting is too deep.
278 | */
279 | private void push(JSONObject jo) throws JSONException {
280 | if (this.top >= maxdepth) {
281 | throw new JSONException("Nesting too deep.");
282 | }
283 | this.stack[this.top] = jo;
284 | this.mode = jo == null ? 'a' : 'k';
285 | this.top += 1;
286 | }
287 |
288 | /**
289 | * Append either the value
290 | * true
or the value
291 | * false
.
292 | *
293 | * @param b A boolean.
294 | * @return this
295 | * @throws JSONException
296 | */
297 | public JSONWriter value(boolean b) throws JSONException {
298 | return this.append(b ? "true" : "false");
299 | }
300 |
301 | /**
302 | * Append a double value.
303 | *
304 | * @param d A double.
305 | * @return this
306 | * @throws JSONException If the number is not finite.
307 | */
308 | public JSONWriter value(double d) throws JSONException {
309 | return this.value(Double.valueOf(d));
310 | }
311 |
312 | /**
313 | * Append a long value.
314 | *
315 | * @param l A long.
316 | * @return this
317 | * @throws JSONException
318 | */
319 | public JSONWriter value(long l) throws JSONException {
320 | return this.append(Long.toString(l));
321 | }
322 |
323 | /**
324 | * Append an object value.
325 | *
326 | * @param object The object to append. It can be null, or a Boolean, Number,
327 | * String, JSONObject, or JSONArray, or an object that implements
328 | * JSONString.
329 | * @return this
330 | * @throws JSONException If the value is out of sequence.
331 | */
332 | public JSONWriter value(Object object) throws JSONException {
333 | return this.append(JSONObject.valueToString(object));
334 | }
335 | }
336 |
--------------------------------------------------------------------------------
/SteamTrade-Java/src/main/java/com/nosoop/steamtrade/TradeListener.java:
--------------------------------------------------------------------------------
1 | package com.nosoop.steamtrade;
2 |
3 | import com.nosoop.steamtrade.inventory.TradeInternalAsset;
4 | import com.nosoop.steamtrade.status.TradeEvent;
5 |
6 | /**
7 | * Receives trade events from the TradeSession that it should be attached to.
8 | *
9 | * @author nosoop < nosoop at users.noreply.github.com >
10 | */
11 | public abstract class TradeListener {
12 | public TradeSession trade;
13 |
14 | /**
15 | * Defines trade status codes to be interpreted by the onError() method.
16 | */
17 | public static class TradeStatusCodes {
18 | public final static int //
19 | /**
20 | * Non-error statuses. Everything is okay according to Steam.
21 | * Something weird is going on if onError() is called with these
22 | * values.
23 | */
24 | // We are polling for updates.
25 | STATUS_OK = 0,
26 | // Both users have decided to make the trade.
27 | TRADE_COMPLETED = 1,
28 | /**
29 | * Steam web errors. Something funky happened on Steam's side.
30 | * The error codes are defined by Steam.
31 | */
32 | // Why this would happen, I don't know.
33 | TRADE_NOT_FOUND = 2,
34 | // One user cancelled.
35 | TRADE_CANCELLED = 3,
36 | // The other user timed out.
37 | PARTNER_TIMED_OUT = 4,
38 | // The trade failed in general. (?????)
39 | TRADE_FAILED = 5,
40 | /**
41 | * SteamTrade-Java errors. Something in this library bugged out.
42 | * The following error values are defined and used within the
43 | * library.
44 | */
45 | // There was a JSONException caught when parsing the status.
46 | STATUS_PARSE_ERROR = 1001,
47 | // The trade session was unable to fetch your inventories.
48 | BACKPACK_SCRAPE_ERROR = 1002,
49 | // Unknown status -- message provided by the Status instance.
50 | STATUS_ERRORMESSAGE = 1003,
51 | // Something happened with the foreign inventory loading.
52 | FOREIGN_INVENTORY_LOAD_ERROR = 1004,
53 | // Something happened with our own inventory loading.
54 | OWN_INVENTORY_LOAD_ERROR = 1005,
55 | // The item specified could not be found in the inventory.
56 | USER_ITEM_NOT_FOUND = 1006,
57 | // The event action code is missing.
58 | TRADEEVENT_ACTION_MISSING = 1007;
59 | public final static String EMPTY_MESSAGE = "";
60 | }
61 |
62 | /**
63 | * Called when an error occurs during the trade such that the trade is
64 | * closed.
65 | *
66 | * @param errorCode The error code. Known values are defined in
67 | * TradeListener.TradeErrorCodes.
68 | * @param errorMessage An error message, if available. If not available,
69 | * will default to TradeStatusCodes.EMPTY_MESSAGE.
70 | */
71 | public abstract void onError(int errorCode, String errorMessage);
72 |
73 | // TODO implement onException, create TradeException?
74 | /**
75 | * Called when the client polls the trade. If you want to warn the other
76 | * person for taking too long, implement this method and add a cancel.
77 | * Otherwise, just do nothing.
78 | *
79 | * @param secondsSinceAction Number of seconds since the trading partner has
80 | * done something.
81 | * @param secondsSinceTrade Number of seconds since the trade has started.
82 | */
83 | public abstract void onTimer(int secondsSinceAction, int secondsSinceTrade);
84 |
85 | /**
86 | * Called when the listener is connected to a trade. Things that depend on
87 | * trade session information can be assigned here. Or say a welcome message.
88 | */
89 | public abstract void onWelcome();
90 |
91 | /**
92 | * Called after backpacks are loaded and trading can start. If you need to
93 | * store inventories in your own listener, handle that here.
94 | */
95 | public abstract void onAfterInit();
96 |
97 | /**
98 | * Called when the other person adds an item. If this is an item from a new
99 | * inventory, that inventory is loaded before this event is called.
100 | *
101 | * @param inventoryItem The item added to the trade.
102 | */
103 | public abstract void onUserAddItem(TradeInternalAsset inventoryItem);
104 |
105 | /**
106 | * Called when the other person removes an item.
107 | *
108 | * @param inventoryItem The item removed from the trade.
109 | */
110 | public abstract void onUserRemoveItem(TradeInternalAsset inventoryItem);
111 |
112 | /**
113 | * Called when the other client send a message through Steam Trade.
114 | *
115 | * @param msg The received message.
116 | */
117 | public abstract void onMessage(String msg);
118 |
119 | /*
120 | * Called when the trading partner checks / unchecks the 'ready' box.
121 | */
122 | public abstract void onUserSetReadyState(boolean ready);
123 |
124 | /**
125 | * Called once the trading partner has accepted the trade and is waiting for
126 | * us to accept.
127 | */
128 | public abstract void onUserAccept();
129 |
130 | /**
131 | * Called when something has happened in the trade.
132 | */
133 | public abstract void onNewVersion();
134 |
135 | /**
136 | * Called once a trade has been made.
137 | */
138 | public abstract void onTradeSuccess();
139 |
140 | /**
141 | * Called once the trade has been closed for the client to begin cleaning
142 | * up. Called immediately after a successful trade or trade error.
143 | */
144 | public abstract void onTradeClosed();
145 |
146 | /**
147 | * Called when the client receives a TradeEvent that it has no idea how to
148 | * handle. In this case, a subclass of TradeListener can override this
149 | * method to handle the event a bit without having to recompile the library.
150 | *
151 | * @param event A trade event to be handled manually.
152 | */
153 | public void onUnknownAction(TradeEvent event) {
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/SteamTrade-Java/src/main/java/com/nosoop/steamtrade/TradeSession.java:
--------------------------------------------------------------------------------
1 | package com.nosoop.steamtrade;
2 |
3 | import bundled.steamtrade.org.json.JSONObject;
4 | import bundled.steamtrade.org.json.JSONException;
5 | import com.nosoop.steamtrade.status.*;
6 | import com.nosoop.steamtrade.TradeListener.TradeStatusCodes;
7 | import com.nosoop.steamtrade.inventory.*;
8 | import com.nosoop.steamtrade.status.TradeEvent.TradeAction;
9 | import java.io.UnsupportedEncodingException;
10 | import java.io.BufferedReader;
11 | import java.io.IOException;
12 | import java.io.InputStreamReader;
13 | import java.io.OutputStreamWriter;
14 | import java.io.StringReader;
15 | import java.net.HttpURLConnection;
16 | import java.net.URL;
17 | import java.net.URLEncoder;
18 | import java.net.URLDecoder;
19 | import java.util.HashMap;
20 | import java.util.HashSet;
21 | import java.util.Map;
22 | import java.util.Set;
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | /**
27 | * Represents a session of a trade.
28 | *
29 | * @author Top-Cat, nosoop
30 | */
31 | public class TradeSession implements Runnable {
32 | /**
33 | * Static URL properties.
34 | */
35 | public final static String STEAM_COMMUNITY_DOMAIN = "steamcommunity.com",
36 | STEAM_TRADE_URL = "http://steamcommunity.com/trade/%s/";
37 | /**
38 | * Object to lock while polling and handling updates.
39 | */
40 | protected final Object POLL_LOCK = new Object();
41 | /**
42 | * Object representation of the users in the trade.
43 | */
44 | private final TradeUser TRADE_USER_SELF, TRADE_USER_PARTNER;
45 | /**
46 | * List of app-context pairs for the active client's inventory. (A list of
47 | * the inventories we have, basically.)
48 | */
49 | public Listnew AppContextPair(440, 2);
129 | *
130 | * @return An AppContextPair "key" representing this instance.
131 | */
132 | public AppContextPair getAppContextPair() {
133 | return appContext;
134 | }
135 |
136 | /**
137 | * Gets the user's available trading inventory.
138 | *
139 | * @return A List containing all the available TradeInternalItem instances.
140 | */
141 | public List