|
17 | 17 | import com.fasterxml.jackson.jr.ob.impl.*;
|
18 | 18 |
|
19 | 19 | /**
|
20 |
| - * Main entry point for functionality. |
| 20 | + * Main entry point for functionality for reading and writing JSON |
| 21 | + * and configuring details of reading and writing. |
21 | 22 | *<p>
|
22 | 23 | * Note that instances are fully immutable, and thereby thread-safe.
|
| 24 | + *<p> |
| 25 | + * Note on source types: source to read is declared as {@link java.lang.Object} |
| 26 | + * but covers following types: |
| 27 | + *<ul> |
| 28 | + * <li>{@link InputStream}</li> |
| 29 | + * <li>{@link Reader}</li> |
| 30 | + * <li>{@code byte[]}</li> |
| 31 | + * <li>{@code char[]}</li> |
| 32 | + * <li>{@link String}/{@link CharSequence}</li> |
| 33 | + * <li>{@link URL}</li> |
| 34 | + * <li>{@link File}</li> |
| 35 | + * </ul> |
| 36 | + * |
23 | 37 | */
|
24 | 38 | @SuppressWarnings("resource")
|
25 | 39 | public class JSON implements Versioned
|
26 | 40 | {
|
27 | 41 | /**
|
28 |
| - * Simple on/off (enabled/disabled) features for {@link JSON}; used for simple configuration |
29 |
| - * aspects. |
| 42 | + * Simple on/off (enabled/disabled) features for {@link JSON}; used for simple |
| 43 | + * configuration aspects. |
30 | 44 | */
|
31 | 45 | public enum Feature
|
32 | 46 | {
|
@@ -651,6 +665,34 @@ public final boolean isEnabled(Feature f) {
|
651 | 665 | return (f.mask() & _features) != 0;
|
652 | 666 | }
|
653 | 667 |
|
| 668 | + /* |
| 669 | + /********************************************************************** |
| 670 | + /* Public factory methods for parsers, generators |
| 671 | + /********************************************************************** |
| 672 | + */ |
| 673 | + |
| 674 | + /** |
| 675 | + * Factory method for opening a {@link JsonParser} to read content from one of |
| 676 | + * following supported sources |
| 677 | + *<ul> |
| 678 | + * <li>{@link InputStream}</li> |
| 679 | + * <li>{@link Reader}</li> |
| 680 | + * <li>{@code byte[]}</li> |
| 681 | + * <li>{@code char[]}</li> |
| 682 | + * <li>{@link String}/{@link CharSequence}</li> |
| 683 | + * <li>{@link URL}</li> |
| 684 | + * <li>{@link File}</li> |
| 685 | + * </ul> |
| 686 | + *<p> |
| 687 | + * Rules regarding closing of the underlying source follow rules |
| 688 | + * that {@link JsonFactory} has for its {@code createParser} method. |
| 689 | + * |
| 690 | + * @since 2.10 |
| 691 | + */ |
| 692 | + public JsonParser createParser(Object source) throws IOException, JSONObjectException { |
| 693 | + return _parser(source); |
| 694 | + } |
| 695 | + |
654 | 696 | /*
|
655 | 697 | /**********************************************************************
|
656 | 698 | /* API: writing Simple objects as JSON
|
@@ -971,6 +1013,60 @@ public <T extends TreeNode> TreeNode treeFrom(Object source)
|
971 | 1013 | }
|
972 | 1014 | }
|
973 | 1015 |
|
| 1016 | + /* |
| 1017 | + /********************************************************************** |
| 1018 | + /* API: reading sequence of JSON values (LD-JSON and like) |
| 1019 | + /********************************************************************** |
| 1020 | + */ |
| 1021 | + |
| 1022 | + /** |
| 1023 | + * Method for creating {@link ValueIterator} for reading |
| 1024 | + * <a href="https://en.wikipedia.org/wiki/JSON_streaming">streaming JSON</a> |
| 1025 | + * content (specifically line-delimited and concatenated variants); |
| 1026 | + * individual values are bound to specific Bean (POJO) type. |
| 1027 | + * |
| 1028 | + * @since 2.10 |
| 1029 | + */ |
| 1030 | + public <T> ValueIterator<T> beanSequenceFrom(Class<T> type, Object source) |
| 1031 | + throws IOException, JSONObjectException |
| 1032 | + { |
| 1033 | + JsonParser p; |
| 1034 | + final boolean managed = !(source instanceof JsonParser); |
| 1035 | + |
| 1036 | + if (managed) { |
| 1037 | + p = _parser(source); |
| 1038 | + } else { |
| 1039 | + p = (JsonParser) source; |
| 1040 | + } |
| 1041 | + p = _initForReading(_config(p)); |
| 1042 | + JSONReader reader = _readerForOperation(p); |
| 1043 | + return new ValueIterator<T>(ValueIterator.MODE_BEAN, type, p, reader, managed); |
| 1044 | + } |
| 1045 | + |
| 1046 | + /** |
| 1047 | + * Method for creating {@link ValueIterator} for reading |
| 1048 | + * <a href="https://en.wikipedia.org/wiki/JSON_streaming">streaming JSON</a> |
| 1049 | + * content (specifically line-delimited and concatenated variants); |
| 1050 | + * individual values are bound as "Any" type: {@link java.util.Map}, |
| 1051 | + * {@link java.util.List}, {@link String}, {@link Number} or {@link Boolean}. |
| 1052 | + * |
| 1053 | + * @since 2.10 |
| 1054 | + */ |
| 1055 | + public ValueIterator<Object> anySequenceFrom(Object source) throws IOException |
| 1056 | + { |
| 1057 | + JsonParser p; |
| 1058 | + final boolean managed = !(source instanceof JsonParser); |
| 1059 | + |
| 1060 | + if (managed) { |
| 1061 | + p = _parser(source); |
| 1062 | + } else { |
| 1063 | + p = (JsonParser) source; |
| 1064 | + } |
| 1065 | + p = _initForReading(_config(p)); |
| 1066 | + JSONReader reader = _readerForOperation(p); |
| 1067 | + return new ValueIterator<Object>(ValueIterator.MODE_ANY, Object.class, p, reader, managed); |
| 1068 | + } |
| 1069 | + |
974 | 1070 | /*
|
975 | 1071 | /**********************************************************************
|
976 | 1072 | /* API: TreeNode construction
|
|
0 commit comments