Codebase list jackson-core / upstream/2.12.0
New upstream version 2.12.0 Mechtilde 3 years ago
66 changed file(s) with 2019 addition(s) and 479 deletion(s). Raw diff Collapse all Expand all
1111 before_install: "git clone -b travis `git config --get remote.origin.url` target/travis"
1212 script: "[ ${TRAVIS_PULL_REQUEST} = 'false' ] && mvn clean deploy --settings target/travis/settings.xml || mvn clean verify --settings target/travis/settings.xml"
1313
14 # whitelist
15 branches:
16 only:
17 - "2.11"
18
1914 env:
2015 global:
2116 - secure: "YW0hrdsHvH41pb5uPJ2DGzXrBgOVT7nEyag/bAQoDcSlOQ/g55tnY6rIGkqE/aYD47IroTEhW4yLyM3tZpbrqwagX4dUX90ukjcUwUvFE1ePTSEfdBtuHVwl8f6HmLIIw2yK0dQ1gOJ21T+3g+wddvK+6sWBJJ+s3O1FePDh6X0="
0 This copy of Jackson JSON processor annotations is licensed under the
1 Apache (Software) License, version 2.0 ("the License").
2 See the License for details about distribution rights, and the
3 specific rights regarding derivate works.
4
5 You may obtain a copy of the License at:
6
7 http://www.apache.org/licenses/LICENSE-2.0
0
1 Apache License
2 Version 2.0, January 2004
3 http://www.apache.org/licenses/
4
5 TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
7 1. Definitions.
8
9 "License" shall mean the terms and conditions for use, reproduction,
10 and distribution as defined by Sections 1 through 9 of this document.
11
12 "Licensor" shall mean the copyright owner or entity authorized by
13 the copyright owner that is granting the License.
14
15 "Legal Entity" shall mean the union of the acting entity and all
16 other entities that control, are controlled by, or are under common
17 control with that entity. For the purposes of this definition,
18 "control" means (i) the power, direct or indirect, to cause the
19 direction or management of such entity, whether by contract or
20 otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 outstanding shares, or (iii) beneficial ownership of such entity.
22
23 "You" (or "Your") shall mean an individual or Legal Entity
24 exercising permissions granted by this License.
25
26 "Source" form shall mean the preferred form for making modifications,
27 including but not limited to software source code, documentation
28 source, and configuration files.
29
30 "Object" form shall mean any form resulting from mechanical
31 transformation or translation of a Source form, including but
32 not limited to compiled object code, generated documentation,
33 and conversions to other media types.
34
35 "Work" shall mean the work of authorship, whether in Source or
36 Object form, made available under the License, as indicated by a
37 copyright notice that is included in or attached to the work
38 (an example is provided in the Appendix below).
39
40 "Derivative Works" shall mean any work, whether in Source or Object
41 form, that is based on (or derived from) the Work and for which the
42 editorial revisions, annotations, elaborations, or other modifications
43 represent, as a whole, an original work of authorship. For the purposes
44 of this License, Derivative Works shall not include works that remain
45 separable from, or merely link (or bind by name) to the interfaces of,
46 the Work and Derivative Works thereof.
47
48 "Contribution" shall mean any work of authorship, including
49 the original version of the Work and any modifications or additions
50 to that Work or Derivative Works thereof, that is intentionally
51 submitted to Licensor for inclusion in the Work by the copyright owner
52 or by an individual or Legal Entity authorized to submit on behalf of
53 the copyright owner. For the purposes of this definition, "submitted"
54 means any form of electronic, verbal, or written communication sent
55 to the Licensor or its representatives, including but not limited to
56 communication on electronic mailing lists, source code control systems,
57 and issue tracking systems that are managed by, or on behalf of, the
58 Licensor for the purpose of discussing and improving the Work, but
59 excluding communication that is conspicuously marked or otherwise
60 designated in writing by the copyright owner as "Not a Contribution."
61
62 "Contributor" shall mean Licensor and any individual or Legal Entity
63 on behalf of whom a Contribution has been received by Licensor and
64 subsequently incorporated within the Work.
65
66 2. Grant of Copyright License. Subject to the terms and conditions of
67 this License, each Contributor hereby grants to You a perpetual,
68 worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 copyright license to reproduce, prepare Derivative Works of,
70 publicly display, publicly perform, sublicense, and distribute the
71 Work and such Derivative Works in Source or Object form.
72
73 3. Grant of Patent License. Subject to the terms and conditions of
74 this License, each Contributor hereby grants to You a perpetual,
75 worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 (except as stated in this section) patent license to make, have made,
77 use, offer to sell, sell, import, and otherwise transfer the Work,
78 where such license applies only to those patent claims licensable
79 by such Contributor that are necessarily infringed by their
80 Contribution(s) alone or by combination of their Contribution(s)
81 with the Work to which such Contribution(s) was submitted. If You
82 institute patent litigation against any entity (including a
83 cross-claim or counterclaim in a lawsuit) alleging that the Work
84 or a Contribution incorporated within the Work constitutes direct
85 or contributory patent infringement, then any patent licenses
86 granted to You under this License for that Work shall terminate
87 as of the date such litigation is filed.
88
89 4. Redistribution. You may reproduce and distribute copies of the
90 Work or Derivative Works thereof in any medium, with or without
91 modifications, and in Source or Object form, provided that You
92 meet the following conditions:
93
94 (a) You must give any other recipients of the Work or
95 Derivative Works a copy of this License; and
96
97 (b) You must cause any modified files to carry prominent notices
98 stating that You changed the files; and
99
100 (c) You must retain, in the Source form of any Derivative Works
101 that You distribute, all copyright, patent, trademark, and
102 attribution notices from the Source form of the Work,
103 excluding those notices that do not pertain to any part of
104 the Derivative Works; and
105
106 (d) If the Work includes a "NOTICE" text file as part of its
107 distribution, then any Derivative Works that You distribute must
108 include a readable copy of the attribution notices contained
109 within such NOTICE file, excluding those notices that do not
110 pertain to any part of the Derivative Works, in at least one
111 of the following places: within a NOTICE text file distributed
112 as part of the Derivative Works; within the Source form or
113 documentation, if provided along with the Derivative Works; or,
114 within a display generated by the Derivative Works, if and
115 wherever such third-party notices normally appear. The contents
116 of the NOTICE file are for informational purposes only and
117 do not modify the License. You may add Your own attribution
118 notices within Derivative Works that You distribute, alongside
119 or as an addendum to the NOTICE text from the Work, provided
120 that such additional attribution notices cannot be construed
121 as modifying the License.
122
123 You may add Your own copyright statement to Your modifications and
124 may provide additional or different license terms and conditions
125 for use, reproduction, or distribution of Your modifications, or
126 for any such Derivative Works as a whole, provided Your use,
127 reproduction, and distribution of the Work otherwise complies with
128 the conditions stated in this License.
129
130 5. Submission of Contributions. Unless You explicitly state otherwise,
131 any Contribution intentionally submitted for inclusion in the Work
132 by You to the Licensor shall be under the terms and conditions of
133 this License, without any additional terms or conditions.
134 Notwithstanding the above, nothing herein shall supersede or modify
135 the terms of any separate license agreement you may have executed
136 with Licensor regarding such Contributions.
137
138 6. Trademarks. This License does not grant permission to use the trade
139 names, trademarks, service marks, or product names of the Licensor,
140 except as required for reasonable and customary use in describing the
141 origin of the Work and reproducing the content of the NOTICE file.
142
143 7. Disclaimer of Warranty. Unless required by applicable law or
144 agreed to in writing, Licensor provides the Work (and each
145 Contributor provides its Contributions) on an "AS IS" BASIS,
146 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 implied, including, without limitation, any warranties or conditions
148 of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 PARTICULAR PURPOSE. You are solely responsible for determining the
150 appropriateness of using or redistributing the Work and assume any
151 risks associated with Your exercise of permissions under this License.
152
153 8. Limitation of Liability. In no event and under no legal theory,
154 whether in tort (including negligence), contract, or otherwise,
155 unless required by applicable law (such as deliberate and grossly
156 negligent acts) or agreed to in writing, shall any Contributor be
157 liable to You for damages, including any direct, indirect, special,
158 incidental, or consequential damages of any character arising as a
159 result of this License or out of the use or inability to use the
160 Work (including but not limited to damages for loss of goodwill,
161 work stoppage, computer failure or malfunction, or any and all
162 other commercial damages or losses), even if such Contributor
163 has been advised of the possibility of such damages.
164
165 9. Accepting Warranty or Additional Liability. While redistributing
166 the Work or Derivative Works thereof, You may choose to offer,
167 and charge a fee for, acceptance of support, warranty, indemnity,
168 or other liability obligations and/or rights consistent with this
169 License. However, in accepting such obligations, You may act only
170 on Your own behalf and on Your sole responsibility, not on behalf
171 of any other Contributor, and only if You agree to indemnify,
172 defend, and hold each Contributor harmless for any liability
173 incurred by, or claims asserted against, such Contributor by reason
174 of your accepting any such warranty or additional liability.
175
176 END OF TERMS AND CONDITIONS
177
178 APPENDIX: How to apply the Apache License to your work.
179
180 To apply the Apache License to your work, attach the following
181 boilerplate notice, with the fields enclosed by brackets "[]"
182 replaced with your own identifying information. (Don't include
183 the brackets!) The text should be enclosed in the appropriate
184 comment syntax for the file format. We also recommend that a
185 file or class name and description of purpose be included on the
186 same "printed page" as the copyright notice for easier
187 identification within third-party archives.
188
189 Copyright [yyyy] [name of copyright owner]
190
191 Licensed under the Apache License, Version 2.0 (the "License");
192 you may not use this file except in compliance with the License.
193 You may obtain a copy of the License at
194
195 http://www.apache.org/licenses/LICENSE-2.0
196
197 Unless required by applicable law or agreed to in writing, software
198 distributed under the License is distributed on an "AS IS" BASIS,
199 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 See the License for the specific language governing permissions and
201 limitations under the License.
00 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1 <!-- This module was also published with a richer model, Gradle metadata, -->
2 <!-- which should be used instead. Do not delete the following line which -->
3 <!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
4 <!-- that they should prefer consuming it instead. -->
5 <!-- do_not_remove: published-with-gradle-metadata -->
16 <modelVersion>4.0.0</modelVersion>
27 <parent>
38 <groupId>com.fasterxml.jackson</groupId>
49 <artifactId>jackson-base</artifactId>
5 <version>2.11.3</version>
10 <version>2.12.0</version>
611 </parent>
712
813 <groupId>com.fasterxml.jackson.core</groupId>
914 <artifactId>jackson-core</artifactId>
1015 <name>Jackson-core</name>
11 <version>2.11.3</version>
16 <version>2.12.0</version>
1217 <packaging>bundle</packaging>
1318 <description>Core Jackson processing abstractions (aka Streaming API), implementation for JSON</description>
1419 <licenses>
2530 <connection>scm:git:git@github.com:FasterXML/jackson-core.git</connection>
2631 <developerConnection>scm:git:git@github.com:FasterXML/jackson-core.git</developerConnection>
2732 <url>http://github.com/FasterXML/jackson-core</url>
28 <tag>jackson-core-2.11.3</tag>
33 <tag>jackson-core-2.12.0</tag>
2934 </scm>
3035
3136 <properties>
6671 <plugin>
6772 <groupId>org.jacoco</groupId>
6873 <artifactId>jacoco-maven-plugin</artifactId>
69 <version>0.8.4</version>
7074 <executions>
7175 <execution>
7276 <goals>
121125 <artifactId>replacer</artifactId>
122126 </plugin>
123127
124 <!-- 04-Mar-2019, tatu: Add rudimentary JDK9+ module info. To build with JDK 8
128 <!-- 04-Mar-2019, tatu: Add rudimentary JDK9+ module info. To build with JDK 8
125129 will have to use `moduleInfoFile` as anything else requires JDK 9+
126130 -->
127131 <plugin>
128132 <groupId>org.moditect</groupId>
129133 <artifactId>moditect-maven-plugin</artifactId>
130134 </plugin>
135 <!-- 03-Nov-2020, tatu: Add LICENSE from main level -->
136 <plugin>
137 <groupId>org.codehaus.mojo</groupId>
138 <artifactId>build-helper-maven-plugin</artifactId>
139 </plugin>
140
141 <plugin>
142 <groupId>de.jjohannes</groupId>
143 <artifactId>gradle-module-metadata-maven-plugin</artifactId>
144 </plugin>
131145 </plugins>
132146 </build>
133147
239239 James Agnew (jamesagnew@github)
240240 * Contributed implementation of #611: Optionally allow leading decimal in float tokens
241241 (2.11.0)
242
243 Pavel Krutikhin (pakru@github)
244 * Contributed fix for #647: Fix NPE in `writeNumber(String)` method of `UTF8JsonGenerator`,
245 `WriterBasedJsonGenerator`
246 (2.11.4)
247
248 Pavan Kalyan (pavan-kalyan@github)
249 * Contributed #500: Allow "optional-padding" for `Base64Variant`
250 (2.12.0)
251
252 Jendrik Johannes (jjohannes@github)
253 * Contributed #618: Publish Gradle Module Metadata
254 (2.12.0)
255
256 Jonathan Haber (jhaber@github)
257 * Contributed #573: More customizable TokenFilter inclusion (using `Tokenfilter.Inclusion`)
258 (2.12.0)
1313 === Releases ===
1414 ------------------------------------------------------------------------
1515
16 2.12.0 (not yet released)
16 2.12.0 (29-Nov-2020)
17
18 #500: Allow "optional-padding" for `Base64Variant`
19 (contributed by Pavan K)
20 #573: More customizable TokenFilter inclusion (using `Tokenfilter.Inclusion`)
21 (contributed by Jonathan H)
22 #618: Publish Gradle Module Metadata
23 (contributed by Jendrik J)
24 #619: Add `StreamReadCapability` for further format-based/format-agnostic
25 handling improvements
26 #627: Add `JsonParser.isExpectedNumberIntToken()` convenience method
27 #630: Add `StreamWriteCapability` for further format-based/format-agnostic
28 handling improvements
29 #631: Add `JsonParser.getNumberValueExact()` to allow precision-retaining buffering
30 #639: Limit initial allocated block size by `ByteArrayBuilder` to max block size
31 #640: Add `JacksonException` as parent class of `JsonProcessingException`
32 #653: Make `JsonWriteContext.reset()` and `JsonReadContext.reset()` methods public
33 - Deprecate `JsonParser.getCurrentTokenId()` (use `#currentTokenId()` instead)
34 - Full "LICENSE" included in jar for easier access by compliancy tools
35
36 2.11.4 (not yet released)
37
38 #647: Fix NPE in `writeNumber(String)` method of `UTF8JsonGenerator`,
39 `WriterBasedJsonGenerator`
40 (contributed by Pavel K)
1741
1842 2.11.3 (02-Oct-2020)
1943 2.11.2 (02-Aug-2020)
1818 public final class Base64Variant
1919 implements java.io.Serializable
2020 {
21 /**
22 * Defines how the Base64Variant deals with Padding while reading
23 *
24 * @since 2.12
25 */
26 public enum PaddingReadBehaviour {
27 /**
28 * Padding is not allowed in Base64 content being read (finding something
29 * that looks like padding at the end of content results in an exception)
30 */
31 PADDING_FORBIDDEN,
32
33 /**
34 * Padding is required in Base64 content being read
35 * (missing padding for incomplete ending quartet results in an exception)
36 */
37 PADDING_REQUIRED,
38
39 /**
40 * Padding is allowed but not required in Base64 content being read: no
41 * exception thrown based on existence or absence, as long as proper
42 * padding characters are used.
43 */
44 PADDING_ALLOWED
45 ;
46 }
47
2148 private final static int INT_SPACE = 0x20;
2249
2350 // We'll only serialize name
2754 * Placeholder used by "no padding" variant, to be used when a character
2855 * value is needed.
2956 */
30 final static char PADDING_CHAR_NONE = '\0';
57 protected final static char PADDING_CHAR_NONE = '\0';
3158
3259 /**
3360 * Marker used to denote ascii characters that do not correspond
82109 final String _name;
83110
84111 /**
85 * Whether this variant uses padding or not.
86 */
87 private final transient boolean _usesPadding;
88
89 /**
90112 * Character used for padding, if any ({@link #PADDING_CHAR_NONE} if not).
91113 */
92 private final transient char _paddingChar;
93
114 private final char _paddingChar;
115
94116 /**
95117 * Maximum number of encoded base64 characters to output during encoding
96118 * before adding a linefeed, if line length is to be limited
99121 * Note: for some output modes (when writing attributes) linefeeds may
100122 * need to be avoided, and this value ignored.
101123 */
102 private final transient int _maxLineLength;
124 private final int _maxLineLength;
125
126 /**
127 * Whether this variant uses padding when writing out content or not.
128 *
129 * @since 2.12
130 */
131 private final boolean _writePadding;
132
133 /**
134 * Whether padding characters should be required or not while decoding
135 *
136 * @since 2.12
137 */
138 private final PaddingReadBehaviour _paddingReadBehaviour;
103139
104140 /*
105141 /**********************************************************
107143 /**********************************************************
108144 */
109145
110 public Base64Variant(String name, String base64Alphabet, boolean usesPadding, char paddingChar, int maxLineLength)
146 public Base64Variant(String name, String base64Alphabet, boolean writePadding, char paddingChar, int maxLineLength)
111147 {
112148 _name = name;
113 _usesPadding = usesPadding;
149 _writePadding = writePadding;
114150 _paddingChar = paddingChar;
115151 _maxLineLength = maxLineLength;
116152
132168 }
133169
134170 // Plus if we use padding, add that in too
135 if (usesPadding) {
171 if (writePadding) {
136172 _asciiToBase64[(int) paddingChar] = BASE64_VALUE_PADDING;
137173 }
174
175 // By default, require padding on input if written on output; do not
176 // accept if padding not written
177 _paddingReadBehaviour = writePadding
178 ? PaddingReadBehaviour.PADDING_REQUIRED
179 : PaddingReadBehaviour.PADDING_FORBIDDEN;
138180 }
139181
140182 /**
142184 * to one used by another variant except for the maximum line length
143185 * (and obviously, name).
144186 */
145 public Base64Variant(Base64Variant base, String name, int maxLineLength)
146 {
147 this(base, name, base._usesPadding, base._paddingChar, maxLineLength);
187 public Base64Variant(Base64Variant base,
188 String name, int maxLineLength)
189 {
190 this(base, name, base._writePadding, base._paddingChar, maxLineLength);
148191 }
149192
150193 /**
152195 * to one used by another variant, but other details (padding, maximum
153196 * line length) differ
154197 */
155 public Base64Variant(Base64Variant base, String name, boolean usesPadding, char paddingChar, int maxLineLength)
198 public Base64Variant(Base64Variant base,
199 String name, boolean writePadding, char paddingChar, int maxLineLength)
200 {
201 this(base, name, writePadding, paddingChar, base._paddingReadBehaviour, maxLineLength);
202 }
203
204 private Base64Variant(Base64Variant base,
205 String name, boolean writePadding, char paddingChar, PaddingReadBehaviour paddingReadBehaviour, int maxLineLength)
156206 {
157207 _name = name;
158208 byte[] srcB = base._base64ToAsciiB;
162212 int[] srcV = base._asciiToBase64;
163213 System.arraycopy(srcV, 0, this._asciiToBase64, 0, srcV.length);
164214
165 _usesPadding = usesPadding;
215 _writePadding = writePadding;
166216 _paddingChar = paddingChar;
167217 _maxLineLength = maxLineLength;
218 _paddingReadBehaviour = paddingReadBehaviour;
219 }
220
221 private Base64Variant(Base64Variant base, PaddingReadBehaviour paddingReadBehaviour) {
222 this(base, base._name, base._writePadding, base._paddingChar, paddingReadBehaviour, base._maxLineLength);
223 }
224
225 /**
226 * @return Base64Variant which does not require padding on read
227 *
228 * @since 2.12
229 */
230 public Base64Variant withPaddingAllowed() {
231 return withReadPadding(PaddingReadBehaviour.PADDING_ALLOWED);
232 }
233
234 /**
235 * @return Base64Variant which requires padding on read
236 * @since 2.12
237 */
238 public Base64Variant withPaddingRequired() {
239 return withReadPadding(PaddingReadBehaviour.PADDING_REQUIRED);
240 }
241
242 /**
243 * @return Base64Variant which does not accept padding on read
244 * @since 2.12
245 */
246 public Base64Variant withPaddingForbidden() {
247 return withReadPadding(PaddingReadBehaviour.PADDING_FORBIDDEN);
248 }
249
250 /**
251 * @param readPadding Padding read behavior desired
252 *
253 * @return Instance with desired padding read behavior setting (this
254 * if already has setting; new instance otherwise)
255 *
256 * @since 2.12
257 */
258 public Base64Variant withReadPadding(PaddingReadBehaviour readPadding) {
259 return (readPadding == _paddingReadBehaviour) ? this
260 : new Base64Variant(this, readPadding);
261 }
262
263 /**
264 * @param writePadding Determines if padding is output on write or not
265 *
266 * @return Base64Variant which writes padding or not depending on writePadding
267 *
268 * @since 2.12
269 */
270 public Base64Variant withWritePadding(boolean writePadding) {
271 return (writePadding == _writePadding) ? this
272 : new Base64Variant(this, _name, writePadding, _paddingChar, _maxLineLength);
168273 }
169274
170275 /*
173278 /**********************************************************
174279 */
175280
176 /**
177 * Method used to "demote" deserialized instances back to
178 * canonical ones
179 */
281 // 26-Oct-2020, tatu: Much more complicated with 2.12 as it is
282 // possible to create differently configured instances.
283 // Need to start with name to regenerate tables etc but then
284 // handle overrides
180285 protected Object readResolve() {
181 return Base64Variants.valueOf(_name);
182 }
183
286 Base64Variant base = Base64Variants.valueOf(_name);
287 if ((_writePadding != base._writePadding)
288 || (_paddingChar != base._paddingChar)
289 || (_paddingReadBehaviour != base._paddingReadBehaviour)
290 || (_maxLineLength != base._maxLineLength)
291 || (_writePadding != base._writePadding)
292 ) {
293 return new Base64Variant(base,
294 _name, _writePadding, _paddingChar, _paddingReadBehaviour, _maxLineLength);
295 }
296 return base;
297 }
298
184299 /*
185300 /**********************************************************
186301 /* Public accessors
189304
190305 public String getName() { return _name; }
191306
192 public boolean usesPadding() { return _usesPadding; }
307 /**
308 * @return True if this Base64 encoding will <b>write</b> padding on output
309 * (note: before Jackson 2.12 also dictated whether padding was accepted on read)
310 */
311 public boolean usesPadding() { return _writePadding; }
312
313 /**
314 * @since 2.12
315 */
316 public boolean requiresPaddingOnRead() {
317 return _paddingReadBehaviour == PaddingReadBehaviour.PADDING_REQUIRED;
318 }
319
320 /**
321 * @since 2.12
322 */
323 public boolean acceptsPaddingOnRead() {
324 return _paddingReadBehaviour != PaddingReadBehaviour.PADDING_FORBIDDEN;
325 }
326
193327 public boolean usesPaddingChar(char c) { return c == _paddingChar; }
194328 public boolean usesPaddingChar(int ch) { return ch == (int) _paddingChar; }
329
330 /**
331 * @return Indicator on how this Base64 encoding will handle possible padding
332 * in content when reading.
333 */
334 public PaddingReadBehaviour paddingReadBehaviour() { return _paddingReadBehaviour; }
335
195336 public char getPaddingChar() { return _paddingChar; }
196337 public byte getPaddingByte() { return (byte)_paddingChar; }
197338
235376
236377 public char encodeBase64BitsAsChar(int value)
237378 {
238 /* Let's assume caller has done necessary checks; this
239 * method must be fast and inlinable
240 */
379 // Let's assume caller has done necessary checks; this
380 // method must be fast and inlinable
241381 return _base64ToAsciiC[value];
242382 }
243383
274414 {
275415 buffer[outPtr++] = _base64ToAsciiC[(bits >> 18) & 0x3F];
276416 buffer[outPtr++] = _base64ToAsciiC[(bits >> 12) & 0x3F];
277 if (_usesPadding) {
417 if (usesPadding()) {
278418 buffer[outPtr++] = (outputBytes == 2) ?
279419 _base64ToAsciiC[(bits >> 6) & 0x3F] : _paddingChar;
280420 buffer[outPtr++] = _paddingChar;
290430 {
291431 sb.append(_base64ToAsciiC[(bits >> 18) & 0x3F]);
292432 sb.append(_base64ToAsciiC[(bits >> 12) & 0x3F]);
293 if (_usesPadding) {
433 if (usesPadding()) {
294434 sb.append((outputBytes == 2) ?
295435 _base64ToAsciiC[(bits >> 6) & 0x3F] : _paddingChar);
296436 sb.append(_paddingChar);
332472 {
333473 buffer[outPtr++] = _base64ToAsciiB[(bits >> 18) & 0x3F];
334474 buffer[outPtr++] = _base64ToAsciiB[(bits >> 12) & 0x3F];
335 if (_usesPadding) {
475 if (usesPadding()) {
336476 byte pb = (byte) _paddingChar;
337477 buffer[outPtr++] = (outputBytes == 2) ?
338478 _base64ToAsciiB[(bits >> 6) & 0x3F] : pb;
528668 decodedData = (decodedData << 6) | bits;
529669 // third base64 char; can be padding, but not ws
530670 if (ptr >= len) {
531 // but as per [JACKSON-631] can be end-of-input, iff not using padding
532 if (!usesPadding()) {
671 // but as per [JACKSON-631] can be end-of-input, iff padding is not required
672 if (!requiresPaddingOnRead()) {
533673 decodedData >>= 4;
534674 builder.append(decodedData);
535675 break;
543683 if (bits < 0) {
544684 if (bits != Base64Variant.BASE64_VALUE_PADDING) {
545685 _reportInvalidBase64(ch, 2, null);
686 }
687 if (!acceptsPaddingOnRead()) {
688 _reportBase64UnexpectedPadding();
546689 }
547690 // Ok, must get padding
548691 if (ptr >= len) {
561704 decodedData = (decodedData << 6) | bits;
562705 // fourth and last base64 char; can be padding, but not ws
563706 if (ptr >= len) {
564 // but as per [JACKSON-631] can be end-of-input, iff not using padding
565 if (!usesPadding()) {
707 // but as per [JACKSON-631] can be end-of-input, iff padding on read is not required
708 if (!requiresPaddingOnRead()) {
566709 decodedData >>= 2;
567710 builder.appendTwoBytes(decodedData);
568711 break;
574717 if (bits < 0) {
575718 if (bits != Base64Variant.BASE64_VALUE_PADDING) {
576719 _reportInvalidBase64(ch, 3, null);
720 }
721 if (!acceptsPaddingOnRead()) {
722 _reportBase64UnexpectedPadding();
577723 }
578724 decodedData >>= 2;
579725 builder.appendTwoBytes(decodedData);
593739
594740 @Override
595741 public String toString() { return _name; }
596
742
597743 @Override
598744 public boolean equals(Object o) {
599 // identity comparison should be dine
600 return (o == this);
745 // identity comparison should be fine
746 // 26-Oct-2020, tatu: ... not any more with 2.12
747 if (o == this) return true;
748 if (o == null || o.getClass() != getClass()) return false;
749
750 Base64Variant other = (Base64Variant) o;
751 return (other._paddingChar == _paddingChar)
752 && (other._maxLineLength == _maxLineLength)
753 && (other._writePadding == _writePadding)
754 && (other._paddingReadBehaviour == _paddingReadBehaviour)
755 && (_name.equals(other._name))
756 ;
601757 }
602758
603759 @Override
604760 public int hashCode() {
605761 return _name.hashCode();
606762 }
607
763
608764 /*
609765 /**********************************************************
610766 /* Internal helper methods
611767 /**********************************************************
612768 */
613
769
614770 /**
615771 * @param bindex Relative index within base64 character unit; between 0
616772 * and 3 (as unit has exactly 4 characters)
639795 throw new IllegalArgumentException(missingPaddingMessage());
640796 }
641797
798 protected void _reportBase64UnexpectedPadding() throws IllegalArgumentException {
799 throw new IllegalArgumentException(unexpectedPaddingMessage());
800 }
801
802 /**
803 * Helper method that will construct a message to use in exceptions for cases where input ends
804 * prematurely in place where padding is not expected.
805 *
806 * @since 2.12
807 */
808 protected String unexpectedPaddingMessage() {
809 return String.format("Unexpected end of base64-encoded String: base64 variant '%s' expects no padding at the end while decoding. This Base64Variant might have been incorrectly configured",
810 getName());
811 }
812
642813 /**
643814 * Helper method that will construct a message to use in exceptions for cases where input ends
644815 * prematurely in place where padding would be expected.
645816 *
646817 * @since 2.10
647818 */
648 public String missingPaddingMessage() {
649 return String.format("Unexpected end of base64-encoded String: base64 variant '%s' expects padding (one or more '%c' characters) at the end",
819 public String missingPaddingMessage() { // !!! TODO: why is this 'public'?
820 return String.format("Unexpected end of base64-encoded String: base64 variant '%s' expects padding (one or more '%c' characters) at the end. This Base64Variant might have been incorrectly configured",
650821 getName(), getPaddingChar());
651822 }
652
653823 }
654
1111 * <li> {@link #PEM}
1212 * <li> {@link #MODIFIED_FOR_URL}
1313 * </ul>
14 *
14 * See entries for full description of differences.
15 *<p>
16 * Note that for default {@link Base64Variant} instances listed above, configuration
17 * is such that if padding is written on output, it will also be required on
18 * reading. This behavior may be changed by using methods:
19 *<ul>
20 * <li>{@link Base64Variant#withPaddingAllowed()}
21 * </li>
22 * <li>{@link Base64Variant#withPaddingForbidden()}
23 * </li>
24 * <li>{@link Base64Variant#withPaddingRequired()}
25 * </li>
26 * <li>{@link Base64Variant#withWritePadding(boolean)}
27 * </li>
28 *</ul>
29 *
1530 * @author Tatu Saloranta
1631 */
1732 public final class Base64Variants
2641 *<p>
2742 * Note that although this can be thought of as the standard variant,
2843 * it is <b>not</b> the default for Jackson: no-linefeeds alternative
29 * is because of JSON requirement of escaping all linefeeds.
44 * is instead used because of JSON requirement of escaping all linefeeds.
45 *<p>
46 * Writes padding on output; requires padding when reading (may change later with a call to {@link Base64Variant#withWritePadding})
3047 */
3148 public final static Base64Variant MIME;
3249 static {
3855 * use linefeeds (max line length set to infinite). Useful when linefeeds
3956 * wouldn't work well (possibly in attributes), or for minor space savings
4057 * (save 1 linefeed per 76 data chars, ie. ~1.4% savings).
58 *<p>
59 * Writes padding on output; requires padding when reading (may change later with a call to {@link Base64Variant#withWritePadding})
4160 */
4261 public final static Base64Variant MIME_NO_LINEFEEDS;
4362 static {
4766 /**
4867 * This variant is the one that predates {@link #MIME}: it is otherwise
4968 * identical, except that it mandates shorter line length.
69 *<p>
70 * Writes padding on output; requires padding when reading (may change later with a call to {@link Base64Variant#withWritePadding})
5071 */
5172 public final static Base64Variant PEM = new Base64Variant(MIME, "PEM", true, '=', 64);
5273
6081 * line length set to infinite). And finally, two characters (plus and
6182 * slash) that would need quoting in URLs are replaced with more
6283 * optimal alternatives (hyphen and underscore, respectively).
84 *<p>
85 * Does not write padding on output; does not accept padding when reading (may change later with a call to {@link Base64Variant#withWritePadding})
6386 */
6487 public final static Base64Variant MODIFIED_FOR_URL;
6588 static {
7295 }
7396
7497 /**
75 * Method used to get the default variant ("MIME_NO_LINEFEEDS") for cases
98 * Method used to get the default variant -- {@link #MIME_NO_LINEFEEDS} -- for cases
7699 * where caller does not explicitly specify the variant.
77100 * We will prefer no-linefeed version because linefeeds in JSON values
78101 * must be escaped, making linefeed-containing variants sub-optimal.
82105 }
83106
84107 /**
108 * Lookup method for finding one of standard variants by name.
109 * If name does not match any of standard variant names,
110 * a {@link IllegalArgumentException} is thrown.
111 *
85112 * @since 2.1
86113 */
87114 public static Base64Variant valueOf(String name) throws IllegalArgumentException
00 package com.fasterxml.jackson.core;
1
2 import com.fasterxml.jackson.core.util.JacksonFeature;
13
24 /**
35 * Marker interface that is to be implemented by data format - specific features.
57 * they can implement interfaces; and as such we may be able to use limited
68 * amount of generic functionality.
79 *<p>
8 * Note that this type is only implemented by non-JSON formats:
9 * types {@link JsonParser.Feature} and {@link JsonGenerator.Feature} do NOT
10 * implement it. This is to make it easier to avoid ambiguity with method
11 * calls.
10 * Since 2.12 this is more of an extra marker feature, as its core API is now
11 * defined in more general {@link JacksonFeature}.
1212 *
13 * @since 2.6 (to be fully used in 2.7 and beyond)
13 * @since 2.6
1414 */
1515 public interface FormatFeature
16 extends JacksonFeature // since 2.12
1617 {
1718 /**
1819 * Accessor for checking whether this feature is enabled by default.
1920 */
21 @Override
2022 public boolean enabledByDefault();
2123
2224 /**
2325 * Returns bit mask for this feature instance; must be a single bit,
2426 * that is of form <code>(1 &lt;&lt; N)</code>
2527 */
28 @Override
2629 public int getMask();
2730
2831 /**
2932 * Convenience method for checking whether feature is enabled in given bitmask
3033 */
34 @Override
3135 public boolean enabledIn(int flags);
3236 }
0 package com.fasterxml.jackson.core;
1
2 /**
3 * Base class for all Jackson-produced checked exceptions.
4 *<p>
5 * For Jackson 2.x this base type is not widely used (instead, its main subtype
6 * {@link JsonProcessingException} is): it is provided more for forwards-compatibility
7 * purposes as 3.x will base all other types directly on it and deprecate
8 * {@link JsonProcessingException} (as well as chance its type to unchecked).
9 *
10 * @since 2.12
11 */
12 public abstract class JacksonException extends java.io.IOException
13 {
14 private final static long serialVersionUID = 123; // eclipse complains otherwise
15
16 protected JacksonException(String msg) {
17 super(msg);
18 }
19
20 protected JacksonException(Throwable t) {
21 super(t);
22 }
23
24 protected JacksonException(String msg, Throwable rootCause) {
25 super(msg, rootCause);
26 // 23-Sep-2020, tatu: before 2.12, had null checks for some reason...
27 // But I don't think that is actually required; Javadocs for
28 // `java.lang.Throwable` constructor claim {@code null} is fine.
29 /* if (rootCause != null) {
30 initCause(rootCause);
31 }*/
32 }
33
34 /*
35 /**********************************************************
36 /* Extended API
37 /**********************************************************
38 */
39
40 /**
41 * Accessor for location information related to position within input
42 * or output (depending on operation), if available; if not available
43 * may return {@code null}.
44 *<p>
45 * Accuracy of location information depends on backend (format) as well
46 * as (in some cases) operation being performed.
47 */
48 public abstract JsonLocation getLocation();
49
50 /**
51 * Method that allows accessing the original "message" argument,
52 * without additional decorations (like location information)
53 * that overridden {@link #getMessage} adds.
54 */
55 public abstract String getOriginalMessage();
56
57 /**
58 * Method that allows accessing underlying processor that triggered
59 * this exception; typically either {@link JsonParser} or {@link JsonGenerator}
60 * for exceptions that originate from streaming API, but other abstractions
61 * when thrown by databinding.
62 *<p>
63 * Note that it is possible that `null` may be returned if code throwing
64 * exception either has no access to processor; or has not been retrofitted
65 * to set it; this means that caller needs to take care to check for nulls.
66 * Subtypes override this method with co-variant return type, for more
67 * type-safe access.
68 *
69 * @return Originating processor, if available; null if not.
70 */
71 public abstract Object getProcessor();
72 }
1717 import com.fasterxml.jackson.core.util.BufferRecycler;
1818 import com.fasterxml.jackson.core.util.BufferRecyclers;
1919 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
20 import com.fasterxml.jackson.core.util.JacksonFeature;
2021
2122 /**
2223 * The main factory class of Jackson package, used to configure and
5657 * Enumeration that defines all on/off features that can only be
5758 * changed for {@link JsonFactory}.
5859 */
59 public enum Feature {
60
60 public enum Feature
61 implements JacksonFeature // since 2.12
62 {
6163 // // // Symbol handling (interning etc)
6264
6365 /**
111113 * but may not make sense on platforms where {@link SoftReference} handling
112114 * is broken (like Android), or if there are retention issues due to
113115 * {@link ThreadLocal} (see
114 * <a href="https://github.com/FasterXML/jackson-core/issues/189">Issue #189</a>
116 * <a href="https://github.com/FasterXML/jackson-core/issues/189">jackson-core#189</a>
115117 * for a possible case)
116118 *<p>
117119 * This setting is enabled by default.
140142 }
141143
142144 private Feature(boolean defaultState) { _defaultState = defaultState; }
143
145
146 @Override
144147 public boolean enabledByDefault() { return _defaultState; }
148 @Override
145149 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
150 @Override
146151 public int getMask() { return (1 << ordinal()); }
147152 }
148153
1212 public class JsonGenerationException
1313 extends JsonProcessingException
1414 {
15 private final static long serialVersionUID = 123; // Stupid eclipse...
15 private final static long serialVersionUID = 123; // eclipse complains otherwise
1616
1717 // transient since 2.7.4
1818 protected transient JsonGenerator _processor;
1414 import com.fasterxml.jackson.core.io.CharacterEscapes;
1515 import com.fasterxml.jackson.core.type.WritableTypeId;
1616 import com.fasterxml.jackson.core.type.WritableTypeId.Inclusion;
17 import com.fasterxml.jackson.core.util.JacksonFeatureSet;
1718 import com.fasterxml.jackson.core.util.VersionUtil;
1819
1920 import static com.fasterxml.jackson.core.JsonTokenId.*;
2829 public abstract class JsonGenerator
2930 implements Closeable, Flushable, Versioned
3031 {
32 /**
33 * Default set of {@link StreamReadCapability}ies that may be used as
34 * basis for format-specific readers (or as bogus instance if non-null
35 * set needs to be passed).
36 *
37 * @since 2.12
38 */
39 protected final static JacksonFeatureSet<StreamWriteCapability> DEFAULT_WRITE_CAPABILITIES
40 = JacksonFeatureSet.fromDefaults(StreamWriteCapability.values());
41
42 /**
43 * Default set of {@link StreamReadCapability}ies for typical textual formats,
44 * to use either as-is, or as a base with possible differences.
45 *
46 * @since 2.12
47 */
48 protected final static JacksonFeatureSet<StreamWriteCapability> DEFAULT_TEXTUAL_WRITE_CAPABILITIES
49 = DEFAULT_WRITE_CAPABILITIES.with(StreamWriteCapability.CAN_WRITE_FORMATTED_NUMBERS);
50
51 /**
52 * Default set of {@link StreamReadCapability}ies for typical binary formats,
53 * to use either as-is, or as a base with possible differences.
54 *
55 * @since 2.12
56 */
57 protected final static JacksonFeatureSet<StreamWriteCapability> DEFAULT_BINARY_WRITE_CAPABILITIES
58 = DEFAULT_WRITE_CAPABILITIES.with(StreamWriteCapability.CAN_WRITE_BINARY_NATIVELY);
59
3160 /**
3261 * Enumeration that defines all togglable features for generators.
3362 */
633662 ctxt.setCurrentValue(v);
634663 }
635664 }
636
665
637666 /*
638667 /**********************************************************
639668 /* Public API, capability introspection methods
718747 */
719748 public boolean canWriteFormattedNumbers() { return false; }
720749
750 /**
751 * Accessor for getting metadata on capabilities of this parser, based on
752 * underlying data format being read (directly or indirectly).
753 *
754 * @return Set of read capabilities for content to read via this parser
755 *
756 * @since 2.12
757 */
758 public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
759 return DEFAULT_WRITE_CAPABILITIES;
760 }
761
721762 /*
722763 /**********************************************************
723764 /* Public API, write methods, structural
735776 */
736777 public abstract void writeStartArray() throws IOException;
737778
738 // TODO: deprecate in 2.11 (remove from 3.0)
739779 /**
740780 * Method for writing start marker of an Array value, similar
741781 * to {@link #writeStartArray()}, but also specifying how many
750790 * this is the case.
751791 *
752792 * @since 2.4
753 */
793 *
794 * @deprecated Since 2.12 Use {@link #writeStartArray(Object, int)} instead
795 */
796 @Deprecated
754797 public void writeStartArray(int size) throws IOException {
755798 writeStartArray();
756799 }
1212 import com.fasterxml.jackson.core.async.NonBlockingInputFeeder;
1313 import com.fasterxml.jackson.core.exc.InputCoercionException;
1414 import com.fasterxml.jackson.core.type.TypeReference;
15 import com.fasterxml.jackson.core.util.JacksonFeatureSet;
1516 import com.fasterxml.jackson.core.util.RequestPayload;
1617
1718 /**
3839 public enum NumberType {
3940 INT, LONG, BIG_INTEGER, FLOAT, DOUBLE, BIG_DECIMAL
4041 };
42
43 /**
44 * Default set of {@link StreamReadCapability}ies that may be used as
45 * basis for format-specific readers (or as bogus instance if non-null
46 * set needs to be passed).
47 *
48 * @since 2.12
49 */
50 protected final static JacksonFeatureSet<StreamReadCapability> DEFAULT_READ_CAPABILITIES
51 = JacksonFeatureSet.fromDefaults(StreamReadCapability.values());
4152
4253 /**
4354 * Enumeration that defines all on/off features for parsers.
542553 * Access mode is determined by earlier calls via {@link JsonFactory};
543554 * it may not be changed after construction.
544555 *<p>
545 * If non-blocking decoding is u (@code true}, it is possible to call
556 * If non-blocking decoding is (@code true}, it is possible to call
546557 * {@link #getNonBlockingInputFeeder()} to obtain object to use
547558 * for feeding input; otherwise (<code>false</code> returned)
548559 * input is read by blocking
560571 */
561572 public NonBlockingInputFeeder getNonBlockingInputFeeder() {
562573 return null;
574 }
575
576 /**
577 * Accessor for getting metadata on capabilities of this parser, based on
578 * underlying data format being read (directly or indirectly).
579 *
580 * @return Set of read capabilities for content to read via this parser
581 *
582 * @since 2.12
583 */
584 public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
585 return DEFAULT_READ_CAPABILITIES;
563586 }
564587
565588 /*
10081031 }
10091032
10101033 /**
1011 * Alias for {@link #currentToken()}, will be deprecated in Jackson 2.9
1034 * Alias for {@link #currentToken()}, may be deprecated sometime after
1035 * Jackson 2.12 (will be removed from 3.0).
10121036 */
10131037 public abstract JsonToken getCurrentToken();
10141038
10151039 /**
1016 * Alias for {@link #currentTokenId()}, will be deprecated in Jackson 2.9
1017 */
1040 * Alias for {@link #currentTokenId()}.
1041 *
1042 * @deprecated Since 2.12 use {@link #currentTokenId} instead
1043 */
1044 @Deprecated
10181045 public abstract int getCurrentTokenId();
1019
1046
10201047 /**
10211048 * Method for checking whether parser currently points to
10221049 * a token (and data for that token is available).
10891116 */
10901117 public boolean isExpectedStartObjectToken() { return currentToken() == JsonToken.START_OBJECT; }
10911118
1119 /**
1120 * Similar to {@link #isExpectedStartArrayToken()}, but checks whether stream
1121 * currently points to {@link JsonToken#VALUE_NUMBER_INT}.
1122 *<p>
1123 * The initial use case is for XML backend to efficiently (attempt to) coerce
1124 * textual content into numbers.
1125 *
1126 * @since 2.12
1127 */
1128 public boolean isExpectedNumberIntToken() { return currentToken() == JsonToken.VALUE_NUMBER_INT; }
1129
10921130 /**
10931131 * Access for checking whether current token is a numeric value token, but
10941132 * one that is of "not-a-number" (NaN) variety (including both "NaN" AND
12761314 * all kinds of numeric values. It will return the optimal
12771315 * (simplest/smallest possible) wrapper object that can
12781316 * express the numeric value just parsed.
1317 *
1318 * @return Numeric value of the current token in its most optimal
1319 * representation
1320 *
1321 * @throws IOException Problem with access: {@link JsonParseException} if
1322 * the current token is not numeric, or if decoding of the value fails
1323 * (invalid format for numbers); plain {@link IOException} if underlying
1324 * content read fails (possible if values are extracted lazily)
12791325 */
12801326 public abstract Number getNumberValue() throws IOException;
1327
1328 /**
1329 * Method similar to {@link #getNumberValue} with the difference that
1330 * for floating-point numbers value returned may be {@link BigDecimal}
1331 * if the underlying format does not store floating-point numbers using
1332 * native representation: for example, textual formats represent numbers
1333 * as Strings (which are 10-based), and conversion to {@link java.lang.Double}
1334 * is potentially lossy operation.
1335 *<p>
1336 * Default implementation simply returns {@link #getNumberValue()}
1337 *
1338 * @return Numeric value of the current token using most accurate representation
1339 *
1340 * @throws IOException Problem with access: {@link JsonParseException} if
1341 * the current token is not numeric, or if decoding of the value fails
1342 * (invalid format for numbers); plain {@link IOException} if underlying
1343 * content read fails (possible if values are extracted lazily)
1344 *
1345 * @since 2.12
1346 */
1347 public Number getNumberValueExact() throws IOException {
1348 return getNumberValue();
1349 }
12811350
12821351 /**
12831352 * If current token is of type
12971366 * exception.
12981367 *<p>
12991368 * Note: if the resulting integer value falls outside range of
1300 * Java byte, a {@link JsonParseException}
1369 * Java byte, a {@link InputCoercionException}
13011370 * will be thrown to indicate numeric overflow/underflow.
13021371 */
13031372 public byte getByteValue() throws IOException {
13231392 * exception.
13241393 *<p>
13251394 * Note: if the resulting integer value falls outside range of
1326 * Java short, a {@link JsonParseException}
1395 * Java short, a {@link InputCoercionException}
13271396 * will be thrown to indicate numeric overflow/underflow.
13281397 */
13291398 public short getShortValue() throws IOException
13471416 * exception.
13481417 *<p>
13491418 * Note: if the resulting integer value falls outside range of
1350 * Java int, a {@link JsonParseException}
1419 * Java int, a {@link InputCoercionException}
13511420 * may be thrown to indicate numeric overflow/underflow.
13521421 */
13531422 public abstract int getIntValue() throws IOException;
13621431 * exception.
13631432 *<p>
13641433 * Note: if the token is an integer, but its value falls
1365 * outside of range of Java long, a {@link JsonParseException}
1434 * outside of range of Java long, a {@link InputCoercionException}
13661435 * may be thrown to indicate numeric overflow/underflow.
13671436 */
13681437 public abstract long getLongValue() throws IOException;
13881457 * exception.
13891458 *<p>
13901459 * Note: if the value falls
1391 * outside of range of Java float, a {@link JsonParseException}
1460 * outside of range of Java float, a {@link InputCoercionException}
13921461 * will be thrown to indicate numeric overflow/underflow.
13931462 */
13941463 public abstract float getFloatValue() throws IOException;
14031472 * exception.
14041473 *<p>
14051474 * Note: if the value falls
1406 * outside of range of Java double, a {@link JsonParseException}
1475 * outside of range of Java double, a {@link InputCoercionException}
14071476 * will be thrown to indicate numeric overflow/underflow.
14081477 */
14091478 public abstract double getDoubleValue() throws IOException;
14891558 * Convenience alternative to {@link #getBinaryValue(Base64Variant)}
14901559 * that defaults to using
14911560 * {@link Base64Variants#getDefaultVariant} as the default encoding.
1561 *
1562 * @return Decoded binary data
14921563 */
14931564 public byte[] getBinaryValue() throws IOException {
14941565 return getBinaryValue(Base64Variants.getDefaultVariant());
1010 * that are not pure I/O problems.
1111 * Regular {@link java.io.IOException}s will be passed through as is.
1212 * Sub-class of {@link java.io.IOException} for convenience.
13 *<p>
14 * Since Jackson 2.12 extends intermediate {@link JacksonException} type
15 * instead of directly extending {@link java.io.IOException}
1316 */
14 public class JsonProcessingException extends java.io.IOException
17 public class JsonProcessingException extends JacksonException
1518 {
16 final static long serialVersionUID = 123; // Stupid eclipse...
17
19 private final static long serialVersionUID = 123; // eclipse complains otherwise
20
1821 protected JsonLocation _location;
1922
2023 protected JsonProcessingException(String msg, JsonLocation loc, Throwable rootCause) {
21 super(msg);
22 if (rootCause != null) {
23 initCause(rootCause);
24 }
24 super(msg, rootCause);
2525 _location = loc;
2626 }
2727
4747 /**********************************************************
4848 */
4949
50 @Override
5051 public JsonLocation getLocation() { return _location; }
51
52
5253 /**
5354 * Method that allows to remove context information from this exception's message.
5455 * Useful when you are parsing security-sensitive data and don't want original data excerpts
6566 *
6667 * @since 2.1
6768 */
69 @Override
6870 public String getOriginalMessage() { return super.getMessage(); }
6971
7072 /**
8183 *
8284 * @since 2.7
8385 */
86 @Override
8487 public Object getProcessor() { return null; }
8588
8689 /*
1919 public abstract class JsonStreamContext
2020 {
2121 // // // Type constants used internally
22
23 protected final static int TYPE_ROOT = 0;
24 protected final static int TYPE_ARRAY = 1;
25 protected final static int TYPE_OBJECT = 2;
26
22 // // // (but exposed publicly as of 2.12 as possibly needed)
23
24 /**
25 * Indicator for "Root Value" context (has not parent)
26 */
27 public final static int TYPE_ROOT = 0;
28
29 /**
30 * Indicator for "Array" context.
31 */
32 public final static int TYPE_ARRAY = 1;
33
34 /**
35 * Indicator for "Object" context.
36 */
37 public final static int TYPE_OBJECT = 2;
38
39 /**
40 * Indicates logical type of context as one of {@code TYPE_xxx} consants.
41 */
2742 protected int _type;
2843
2944 /**
0 package com.fasterxml.jackson.core;
1
2 import com.fasterxml.jackson.core.util.JacksonFeature;
3
4 /**
5 * Set of on/off capabilities that a {@link JsonParser} for given format
6 * (or in case of buffering, original format) has.
7 * Used in some cases to adjust aspects of things like content conversions,
8 * coercions and validation by format-agnostic functionality.
9 * Specific or expected usage documented by individual capability entry
10 * Javadocs.
11 *
12 * @since 2.12
13 */
14 public enum StreamReadCapability
15 implements JacksonFeature
16 {
17 /**
18 * Capability that indicates that data format can expose multiple properties
19 * with same name ("duplicates") within one Object context.
20 * This is usually not enabled, except for formats like {@code xml} that
21 * have content model that does not map cleanly to JSON-based token stream.
22 *<p>
23 * Capability may be used for allowing secondary mapping of such duplicates
24 * in case of using Tree Model (see {@link TreeNode}), or "untyped" databinding
25 * (mapping content as generic {@link java.lang.Object}).
26 *<p>
27 * Capability is currently only enabled for XML format backend.
28 */
29 DUPLICATE_PROPERTIES(false),
30
31 /**
32 * Capability that indicates that data format may in some cases expose Scalar values
33 * (whether typed or untyped) as Object values. There are additional access methods
34 * at databind level: this capability may be used to decide whether to attempt to
35 * use such methods especially in potentially ambiguous cases.
36 *<p>
37 * Capability is currently only enabled for XML format backend.
38 */
39 SCALARS_AS_OBJECTS(false),
40
41 /**
42 * Capability that indicates that data format only exposed "untyped" scalars: that is,
43 * instead of Number, Boolean and String types all scalar values are reported as
44 * text ({@link JsonToken#VALUE_STRING})
45 * unless some sort of coercion is implied by caller.
46 *<p>
47 * This capability is true for many textual formats like CSV, Properties and XML.
48 */
49 UNTYPED_SCALARS(false),
50 ;
51
52 /**
53 * Whether feature is enabled or disabled by default.
54 */
55 private final boolean _defaultState;
56
57 private final int _mask;
58
59 private StreamReadCapability(boolean defaultState) {
60 _defaultState = defaultState;
61 _mask = (1 << ordinal());
62 }
63
64 @Override
65 public boolean enabledByDefault() { return _defaultState; }
66 @Override
67 public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
68 @Override
69 public int getMask() { return _mask; }
70 }
11
22 import java.io.InputStream;
33 import java.io.Reader;
4
5 import com.fasterxml.jackson.core.util.JacksonFeature;
46
57 /**
68 * Token reader (parser) features not-specific to any particular format backend.
911 * @since 2.10
1012 */
1113 public enum StreamReadFeature
14 implements JacksonFeature // since 2.12
1215 {
1316 // // // Low-level I/O handling features:
1417
124127 return flags;
125128 }
126129
130 @Override
127131 public boolean enabledByDefault() { return _defaultState; }
132 @Override
128133 public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
134 @Override
129135 public int getMask() { return _mask; }
130136
131137 public JsonParser.Feature mappedFeature() { return _mappedFeature; }
0 package com.fasterxml.jackson.core;
1
2 import com.fasterxml.jackson.core.util.JacksonFeature;
3
4 /**
5 * Set of on/off capabilities that a {@link JsonGenerator} for given format
6 * (or in case of buffering, original format) has.
7 * Used in some cases to adjust aspects of things like content conversions and
8 * coercions by format-agnostic functionality.
9 * Specific or expected usage documented by individual capability entry Javadocs.
10 *
11 * @since 2.12
12 */
13 public enum StreamWriteCapability
14 implements JacksonFeature
15 {
16 /**
17 * Capability that indicates that the data format is able to express binary
18 * data natively, without using textual encoding like Base64.
19 *<p>
20 * Capability is currently enabled for all binary formats and none of textual
21 * formats.
22 */
23 CAN_WRITE_BINARY_NATIVELY(false),
24
25 /**
26 * Capability that indicates that the data format is able to write
27 * "formatted numbers": that is, output of numbers is done as Strings
28 * and caller is allowed to pass in logical number values as Strings.
29 *<p>
30 * Capability is currently enabled for most textual formats and none of binary
31 * formats.
32 */
33 CAN_WRITE_FORMATTED_NUMBERS(false)
34 ;
35
36 /**
37 * Whether feature is enabled or disabled by default.
38 */
39 private final boolean _defaultState;
40
41 private final int _mask;
42
43 private StreamWriteCapability(boolean defaultState) {
44 _defaultState = defaultState;
45 _mask = (1 << ordinal());
46 }
47
48 @Override
49 public boolean enabledByDefault() { return _defaultState; }
50 @Override
51 public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
52 @Override
53 public int getMask() { return _mask; }
54 }
22 import java.io.OutputStream;
33 import java.io.Writer;
44 import java.math.BigDecimal;
5
6 import com.fasterxml.jackson.core.util.JacksonFeature;
57
68 /**
79 * Token writer (generator) features not-specific to any particular format backend.
1012 * @since 2.10
1113 */
1214 public enum StreamWriteFeature
15 implements JacksonFeature // since 2.12
1316 {
1417 // // Low-level I/O / content features
1518
143146 return flags;
144147 }
145148
149 @Override
146150 public boolean enabledByDefault() { return _defaultState; }
151 @Override
147152 public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
153 @Override
148154 public int getMask() { return _mask; }
149155
150156 public JsonGenerator.Feature mappedFeature() { return _mappedFeature; }
7171 /**
7272 * Flag set to indicate that implicit conversion from number
7373 * to JSON String is needed (as per
74 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#WRITE_NUMBERS_AS_STRINGS}).
74 * {@link com.fasterxml.jackson.core.json.JsonWriteFeature#WRITE_NUMBERS_AS_STRINGS}).
7575 */
7676 protected boolean _cfgNumbersAsStrings;
7777
55 import java.util.Arrays;
66
77 import com.fasterxml.jackson.core.*;
8 import com.fasterxml.jackson.core.JsonParser.Feature;
98 import com.fasterxml.jackson.core.io.IOContext;
109 import com.fasterxml.jackson.core.io.NumberInput;
1110 import com.fasterxml.jackson.core.json.DupDetector;
1211 import com.fasterxml.jackson.core.json.JsonReadContext;
1312 import com.fasterxml.jackson.core.json.PackageVersion;
1413 import com.fasterxml.jackson.core.util.ByteArrayBuilder;
14 import com.fasterxml.jackson.core.util.JacksonFeatureSet;
1515 import com.fasterxml.jackson.core.util.TextBuffer;
1616
1717 /**
2121 */
2222 public abstract class ParserBase extends ParserMinimalBase
2323 {
24 // JSON capabilities are the same as defaults
25 // @since 2.12
26 protected final static JacksonFeatureSet<StreamReadCapability> JSON_READ_CAPABILITIES
27 = DEFAULT_READ_CAPABILITIES;
28
2429 /*
2530 /**********************************************************
2631 /* Generic I/O state
349354 if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
350355 ctxt = ctxt.getParent();
351356 }
352 /* 24-Sep-2013, tatu: Unfortunate, but since we did not expose exceptions,
353 * need to wrap this here
354 */
357 // 24-Sep-2013, tatu: Unfortunate, but since we did not expose exceptions,
358 // need to wrap this here
355359 try {
356360 ctxt.setCurrentName(name);
357361 } catch (IOException e) {
575579 /* Numeric accessors of public API
576580 /**********************************************************
577581 */
578
582
579583 @Override
580584 public Number getNumberValue() throws IOException
581585 {
593597 if ((_numTypesValid & NR_BIGINT) != 0) {
594598 return _numberBigInt;
595599 }
596 // Shouldn't get this far but if we do
597 return _numberBigDecimal;
598 }
599
600 /* And then floating point types. But here optimal type
601 * needs to be big decimal, to avoid losing any data?
602 */
600 _throwInternal();
601 }
602
603 // And then floating point types. But here optimal type
604 // needs to be big decimal, to avoid losing any data?
603605 if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
604606 return _numberBigDecimal;
605607 }
608610 }
609611 return _numberDouble;
610612 }
611
613
614 // NOTE: mostly copied from above
615 @Override
616 public Number getNumberValueExact() throws IOException
617 {
618 if (_currToken == JsonToken.VALUE_NUMBER_INT) {
619 if (_numTypesValid == NR_UNKNOWN) {
620 _parseNumericValue(NR_UNKNOWN);
621 }
622 if ((_numTypesValid & NR_INT) != 0) {
623 return _numberInt;
624 }
625 if ((_numTypesValid & NR_LONG) != 0) {
626 return _numberLong;
627 }
628 if ((_numTypesValid & NR_BIGINT) != 0) {
629 return _numberBigInt;
630 }
631 _throwInternal();
632 }
633 // 09-Jul-2020, tatu: [databind#2644] requires we will retain accuracy, so:
634 if (_numTypesValid == NR_UNKNOWN) {
635 _parseNumericValue(NR_BIGDECIMAL);
636 }
637 if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
638 return _numberBigDecimal;
639 }
640 if ((_numTypesValid & NR_DOUBLE) == 0) { // sanity check
641 _throwInternal();
642 }
643 return _numberDouble;
644 }
645
612646 @Override
613647 public NumberType getNumberType() throws IOException
614648 {
727761 /* Conversion from textual to numeric representation
728762 /**********************************************************
729763 */
730
764
731765 /**
732766 * Method that will parse actual numeric value out of a syntactically
733767 * valid number value. Type it will parse into depends on whether
739773 */
740774 protected void _parseNumericValue(int expType) throws IOException
741775 {
776 // 12-Jun-2020, tatu: Sanity check to prevent more cryptic error for this case.
777 // (note: could alternatively see if TextBuffer has aggregated contents, avoid
778 // exception -- but that might be more confusing)
779 if (_closed) {
780 _reportError("Internal error: _parseNumericValue called when parser instance closed");
781 }
782
742783 // Int or float?
743784 if (_currToken == JsonToken.VALUE_NUMBER_INT) {
744 int len = _intLength;
785 final int len = _intLength;
745786 // First: optimization for simple int
746 if (len <= 9) {
787 if (len <= 9) {
747788 int i = _textBuffer.contentsAsInt(_numberNegative);
748789 _numberInt = i;
749790 _numTypesValid = NR_INT;
786827 */
787828 protected int _parseIntValue() throws IOException
788829 {
830 // 12-Jun-2020, tatu: Sanity check to prevent more cryptic error for this case.
831 // (note: could alternatively see if TextBuffer has aggregated contents, avoid
832 // exception -- but that might be more confusing)
833 if (_closed) {
834 _reportError("Internal error: _parseNumericValue called when parser instance closed");
835 }
789836 // Inlined variant of: _parseNumericValue(NR_INT)
790837 if (_currToken == JsonToken.VALUE_NUMBER_INT) {
791838 if (_intLength <= 9) {
10341081 /**
10351082 * Method called to report a problem with unquoted control character.
10361083 * Note: it is possible to suppress some instances of
1037 * exception by enabling {@link Feature#ALLOW_UNQUOTED_CONTROL_CHARS}.
1084 * exception by enabling
1085 * {@link com.fasterxml.jackson.core.json.JsonReadFeature#ALLOW_UNESCAPED_CONTROL_CHARS}.
10381086 */
10391087 @SuppressWarnings("deprecation")
10401088 protected void _throwUnquotedSpace(int i, String ctxtDesc) throws JsonParseException {
187187 final JsonToken t = _currToken;
188188 return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
189189 }
190
190
191191 @Override public JsonToken getCurrentToken() { return _currToken; }
192
193 @Deprecated
192194 @Override public int getCurrentTokenId() {
193195 final JsonToken t = _currToken;
194196 return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
209211
210212 @Override public boolean isExpectedStartArrayToken() { return _currToken == JsonToken.START_ARRAY; }
211213 @Override public boolean isExpectedStartObjectToken() { return _currToken == JsonToken.START_OBJECT; }
212
214 @Override public boolean isExpectedNumberIntToken() { return _currToken == JsonToken.VALUE_NUMBER_INT; }
215
213216 @Override
214217 public JsonToken nextValue() throws IOException {
215218 // Implementation should be as trivial as follows; only needs to change if
547550
548551 /**
549552 * Method called to throw an exception for integral (not floating point) input
550 * token with value outside of Java signed 32-bit range when requested as {@link int}.
553 * token with value outside of Java signed 32-bit range when requested as {@code int}.
551554 * Result will be {@link InputCoercionException} being thrown.
552555 */
553556 protected void reportOverflowInt() throws IOException {
568571
569572 /**
570573 * Method called to throw an exception for integral (not floating point) input
571 * token with value outside of Java signed 64-bit range when requested as {@link long}.
574 * token with value outside of Java signed 64-bit range when requested as {@code long}.
572575 * Result will be {@link InputCoercionException} being thrown.
573576 */
574577 protected void reportOverflowLong() throws IOException {
66 import java.math.BigInteger;
77
88 import com.fasterxml.jackson.core.*;
9 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
910 import com.fasterxml.jackson.core.util.JsonGeneratorDelegate;
1011
1112 /**
1718 */
1819 public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
1920 {
21
2022 /*
2123 /**********************************************************
2224 /* Configuration
4446 * done and only explicitly included entries are output; if `true` then
4547 * path from main level down to match is also included as necessary.
4648 */
47 protected boolean _includePath;
48
49 /* NOTE: this feature is included in the first version (2.6), but
50 * there is no public API to enable it, yet, since there isn't an
51 * actual use case. But it seemed possible need could arise, which
52 * is feature has not yet been removed. If no use is found within
53 * first version or two, just remove.
54 *
55 * Marked as deprecated since its status is uncertain.
56 */
57 @Deprecated
58 protected boolean _includeImmediateParent;
49 protected TokenFilter.Inclusion _inclusion;
5950
6051 /*
6152 /**********************************************************
8980 /**********************************************************
9081 */
9182
83 /**
84 * @deprecated since 2.12 Use the constructor that takes {@link TokenFilter.Inclusion}
85 * argument instead.
86 */
87 @Deprecated
9288 public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f,
9389 boolean includePath, boolean allowMultipleMatches)
90 {
91 this(d, f, includePath ? Inclusion.INCLUDE_ALL_AND_PATH : Inclusion.ONLY_INCLUDE_ALL, allowMultipleMatches);
92 }
93
94 /**
95 * @since 2.12
96 */
97 public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f,
98 TokenFilter.Inclusion inclusion, boolean allowMultipleMatches)
9499 {
95100 // By default, do NOT delegate copy methods
96101 super(d, false);
98103 // and this is the currently active filter for root values
99104 _itemFilter = f;
100105 _filterContext = TokenFilterContext.createRootContext(f);
101 _includePath = includePath;
106 _inclusion = inclusion;
102107 _allowMultipleMatches = allowMultipleMatches;
103108 }
104109
169174 _checkParentPath();
170175 _filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
171176 delegate.writeStartArray();
177 } else if (_itemFilter != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
178 _checkParentPath(false /* isMatch */);
179 _filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
180 delegate.writeStartArray();
172181 } else {
173182 _filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
174183 }
175184 }
176185
186 @SuppressWarnings("deprecation")
177187 @Override
178188 public void writeStartArray(int size) throws IOException
179189 {
196206 }
197207 if (_itemFilter == TokenFilter.INCLUDE_ALL) {
198208 _checkParentPath();
209 _filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
210 delegate.writeStartArray(size);
211 } else if (_itemFilter != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
212 _checkParentPath(false /* isMatch */);
199213 _filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
200214 delegate.writeStartArray(size);
201215 } else {
296310 _checkParentPath();
297311 _filterContext = _filterContext.createChildObjectContext(f, true);
298312 delegate.writeStartObject();
313 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
314 _checkParentPath(false /* isMatch */);
315 _filterContext = _filterContext.createChildObjectContext(f, true);
316 delegate.writeStartObject();
299317 } else { // filter out
300318 _filterContext = _filterContext.createChildObjectContext(f, false);
301319 }
324342 }
325343 if (f == TokenFilter.INCLUDE_ALL) {
326344 _checkParentPath();
345 _filterContext = _filterContext.createChildObjectContext(f, true);
346 delegate.writeStartObject(forValue);
347 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
348 _checkParentPath(false /* isMatch */);
327349 _filterContext = _filterContext.createChildObjectContext(f, true);
328350 delegate.writeStartObject(forValue);
329351 } else { // filter out
439461 }
440462 }
441463 _checkParentPath();
442 }
464 }
443465 delegate.writeString(value);
444466 }
445467
461483 }
462484 }
463485 _checkParentPath();
464 }
486 }
465487 delegate.writeString(text, offset, len);
466488 }
467489
482504 }
483505 }
484506 _checkParentPath();
485 }
507 }
486508 delegate.writeString(value);
487509 }
488510
635657 }
636658 }
637659 _checkParentPath();
638 }
660 }
639661 delegate.writeNumber(v);
640662 }
641663
656678 }
657679 }
658680 _checkParentPath();
659 }
681 }
660682 delegate.writeNumber(v);
661683 }
662684
677699 }
678700 }
679701 _checkParentPath();
680 }
702 }
681703 delegate.writeNumber(v);
682704 }
683705
698720 }
699721 }
700722 _checkParentPath();
701 }
723 }
702724 delegate.writeNumber(v);
703725 }
704726
719741 }
720742 }
721743 _checkParentPath();
722 }
744 }
723745 delegate.writeNumber(v);
724746 }
725747
740762 }
741763 }
742764 _checkParentPath();
743 }
765 }
744766 delegate.writeNumber(v);
745767 }
746768
761783 }
762784 }
763785 _checkParentPath();
764 }
786 }
765787 delegate.writeNumber(v);
766788 }
767789
824846 }
825847 }
826848 _checkParentPath();
827 }
849 }
828850 delegate.writeBoolean(v);
829851 }
830852
845867 }
846868 }
847869 _checkParentPath();
848 }
870 }
849871 delegate.writeNull();
850872 }
851873
968990
969991 protected void _checkParentPath() throws IOException
970992 {
993 _checkParentPath(true);
994 }
995
996 protected void _checkParentPath(boolean isMatch) throws IOException
997 {
998 if (isMatch) {
999 ++_matchCount;
1000 }
1001 // only need to construct path if parent wasn't written
1002 if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
1003 _filterContext.writePath(delegate);
1004 } else if (_inclusion == Inclusion.INCLUDE_NON_NULL) {
1005 // path has already been written, except for maybe field name
1006 _filterContext.ensureFieldNameWritten(delegate);
1007 }
1008 // also: if no multiple matches desired, short-cut checks
1009 if (isMatch && !_allowMultipleMatches) {
1010 // Mark parents as "skip" so that further check calls are not made
1011 _filterContext.skipParentChecks();
1012 }
1013 }
1014
1015 /**
1016 * Specialized variant of {@link #_checkParentPath} used when checking
1017 * parent for a property name to be included with value: rules are slightly
1018 * different.
1019 */
1020 protected void _checkPropertyParentPath() throws IOException
1021 {
9711022 ++_matchCount;
972 // only need to construct path if parent wasn't written
973 if (_includePath) {
1023 if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
9741024 _filterContext.writePath(delegate);
975 }
1025 } else if (_inclusion == Inclusion.INCLUDE_NON_NULL) {
1026 // path has already been written, except for maybe field name
1027 _filterContext.ensureFieldNameWritten(delegate);
1028 }
1029
9761030 // also: if no multiple matches desired, short-cut checks
9771031 if (!_allowMultipleMatches) {
9781032 // Mark parents as "skip" so that further check calls are not made
9791033 _filterContext.skipParentChecks();
9801034 }
9811035 }
982
983 /**
984 * Specialized variant of {@link #_checkParentPath} used when checking
985 * parent for a property name to be included with value: rules are slightly
986 * different.
987 */
988 protected void _checkPropertyParentPath() throws IOException
989 {
990 ++_matchCount;
991 if (_includePath) {
992 _filterContext.writePath(delegate);
993 } else if (_includeImmediateParent) {
994 // 21-Apr-2015, tatu: Note that there is no API to enable this currently...
995 // retained for speculative future use
996 _filterContext.writeImmediatePath(delegate);
997 }
998
999 // also: if no multiple matches desired, short-cut checks
1000 if (!_allowMultipleMatches) {
1001 // Mark parents as "skip" so that further check calls are not made
1002 _filterContext.skipParentChecks();
1003 }
1004 }
10051036
10061037 protected boolean _checkBinaryWrite() throws IOException
10071038 {
55 import java.math.BigInteger;
66
77 import com.fasterxml.jackson.core.*;
8 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
89 import com.fasterxml.jackson.core.util.JsonParserDelegate;
910
1011 import static com.fasterxml.jackson.core.JsonTokenId.*;
4546 * done and only explicitly included entries are output; if `true` then
4647 * path from main level down to match is also included as necessary.
4748 */
48 protected boolean _includePath;
49
50 /* NOTE: this feature is included in the first version (2.6), but
51 * there is no public API to enable it, yet, since there isn't an
52 * actual use case. But it seemed possible need could arise, which
53 * is feature has not yet been removed. If no use is found within
54 * first version or two, just remove.
55 *
56 * Marked as deprecated since its status is uncertain.
57 */
58 @Deprecated
59 protected boolean _includeImmediateParent;
60
49 protected TokenFilter.Inclusion _inclusion;
50
6151 /*
6252 /**********************************************************
6353 /* State
110100 /**********************************************************
111101 */
112102
103 @Deprecated
113104 public FilteringParserDelegate(JsonParser p, TokenFilter f,
114105 boolean includePath, boolean allowMultipleMatches)
106 {
107 this(p, f, includePath ? Inclusion.INCLUDE_ALL_AND_PATH : Inclusion.ONLY_INCLUDE_ALL, allowMultipleMatches);
108 }
109
110 public FilteringParserDelegate(JsonParser p, TokenFilter f,
111 TokenFilter.Inclusion inclusion, boolean allowMultipleMatches)
115112 {
116113 super(p);
117114 rootFilter = f;
118115 // and this is the currently active filter for root values
119116 _itemFilter = f;
120117 _headContext = TokenFilterContext.createRootContext(f);
121 _includePath = includePath;
118 _inclusion = inclusion;
122119 _allowMultipleMatches = allowMultipleMatches;
123120 }
124121
147144 @Override public JsonToken getCurrentToken() { return _currToken; }
148145 @Override public JsonToken currentToken() { return _currToken; }
149146
147 @Deprecated // since 2.12
150148 @Override public final int getCurrentTokenId() {
151 final JsonToken t = _currToken;
152 return (t == null) ? JsonTokenId.ID_NO_TOKEN : t.id();
149 return currentTokenId();
153150 }
154151 @Override public final int currentTokenId() {
155152 final JsonToken t = _currToken;
234231 // If all the conditions matches then check for scalar / non-scalar property
235232
236233 if (!_allowMultipleMatches && (_currToken != null) && (_exposedContext == null)) {
237 // if scalar, and scalar not present in obj/array and !includePath and INCLUDE_ALL
238 // matched once, return null
239 if (_currToken.isScalarValue() && !_headContext.isStartHandled() && !_includePath
234 // if scalar, and scalar not present in obj/array and _inclusion == ONLY_INCLUDE_ALL
235 // and INCLUDE_ALL matched once, return null
236 if (_currToken.isScalarValue() && !_headContext.isStartHandled()
237 && _inclusion == Inclusion.ONLY_INCLUDE_ALL
240238 && (_itemFilter == TokenFilter.INCLUDE_ALL)) {
241239 return (_currToken = null);
242240 }
317315 if (f == TokenFilter.INCLUDE_ALL) {
318316 _headContext = _headContext.createChildArrayContext(f, true);
319317 return (_currToken = t);
318 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
319 // TODO don't count as match?
320 _headContext = _headContext.createChildArrayContext(f, true);
321 return (_currToken = t);
320322 }
321323 _headContext = _headContext.createChildArrayContext(f, false);
322
324
323325 // Also: only need buffering if parent path to be included
324 if (_includePath) {
326 if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
325327 t = _nextTokenWithBuffering(_headContext);
326328 if (t != null) {
327329 _currToken = t;
353355 if (f == TokenFilter.INCLUDE_ALL) {
354356 _headContext = _headContext.createChildObjectContext(f, true);
355357 return (_currToken = t);
358 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
359 // TODO don't count as match?
360 _headContext = _headContext.createChildObjectContext(f, true);
361 return (_currToken = t);
356362 }
357363 _headContext = _headContext.createChildObjectContext(f, false);
358364 // Also: only need buffering if parent path to be included
359 if (_includePath) {
365 if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
360366 t = _nextTokenWithBuffering(_headContext);
361367 if (t != null) {
362368 _currToken = t;
390396 f = _headContext.setFieldName(name);
391397 if (f == TokenFilter.INCLUDE_ALL) {
392398 _itemFilter = f;
393 if (!_includePath) {
394 // Minor twist here: if parent NOT included, may need to induce output of
395 // surrounding START_OBJECT/END_OBJECT
396 if (_includeImmediateParent && !_headContext.isStartHandled()) {
397 t = _headContext.nextTokenToRead(); // returns START_OBJECT but also marks it handled
398 _exposedContext = _headContext;
399 }
400 }
401399 return (_currToken = t);
402400 }
403401 if (f == null) {
414412 _itemFilter = f;
415413 if (f == TokenFilter.INCLUDE_ALL) {
416414 if (_verifyAllowedMatches()) {
417 if (_includePath) {
415 if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
418416 return (_currToken = t);
419417 }
420418 } else {
422420 delegate.skipChildren();
423421 }
424422 }
425 if (_includePath) {
423 if (_inclusion != Inclusion.ONLY_INCLUDE_ALL) {
426424 t = _nextTokenWithBuffering(_headContext);
427425 if (t != null) {
428426 _currToken = t;
495493 if (f == TokenFilter.INCLUDE_ALL) {
496494 _headContext = _headContext.createChildArrayContext(f, true);
497495 return (_currToken = t);
496 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
497 _headContext = _headContext.createChildArrayContext(f, true);
498 return (_currToken = t);
498499 }
499500 _headContext = _headContext.createChildArrayContext(f, false);
500501 // but if we didn't figure it out yet, need to buffer possible events
501 if (_includePath) {
502 if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
502503 t = _nextTokenWithBuffering(_headContext);
503504 if (t != null) {
504505 _currToken = t;
530531 if (f == TokenFilter.INCLUDE_ALL) {
531532 _headContext = _headContext.createChildObjectContext(f, true);
532533 return (_currToken = t);
534 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
535 _headContext = _headContext.createChildObjectContext(f, true);
536 return (_currToken = t);
533537 }
534538 _headContext = _headContext.createChildObjectContext(f, false);
535 if (_includePath) {
539 if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
536540 t = _nextTokenWithBuffering(_headContext);
537541 if (t != null) {
538542 _currToken = t;
578582 }
579583 _itemFilter = f;
580584 if (f == TokenFilter.INCLUDE_ALL) {
581 if (_verifyAllowedMatches() && _includePath) {
585 if (_verifyAllowedMatches() && _inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
582586 return (_currToken = t);
583587 }
584 // if (_includeImmediateParent) { ...
585588 continue main_loop;
586589 }
587 if (_includePath) {
590 if (_inclusion != Inclusion.ONLY_INCLUDE_ALL) {
588591 t = _nextTokenWithBuffering(_headContext);
589592 if (t != null) {
590593 _currToken = t;
646649 if (f == TokenFilter.INCLUDE_ALL) {
647650 _headContext = _headContext.createChildArrayContext(f, true);
648651 return _nextBuffered(buffRoot);
652 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
653 // TODO don't count as match?
654 _headContext = _headContext.createChildArrayContext(f, true);
655 return _nextBuffered(buffRoot);
649656 }
650657 _headContext = _headContext.createChildArrayContext(f, false);
651658 continue main_loop;
672679 _itemFilter = f;
673680 if (f == TokenFilter.INCLUDE_ALL) {
674681 _headContext = _headContext.createChildObjectContext(f, true);
682 return _nextBuffered(buffRoot);
683 } else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
684 // TODO don't count as match?
685 _headContext = _headContext.createChildArrayContext(f, true);
675686 return _nextBuffered(buffRoot);
676687 }
677688 _headContext = _headContext.createChildObjectContext(f, false);
1515 public class TokenFilter
1616 {
1717
18 /**
19 * Enumeration that controls how TokenFilter return values are interpreted.
20 *
21 * @since 2.12
22 */
23 public enum Inclusion {
24 /**
25 * Tokens will only be included if the filter returns TokenFilter.INCLUDE_ALL
26 */
27 ONLY_INCLUDE_ALL,
28 /**
29 * When TokenFilter.INCLUDE_ALL is returned, the corresponding token will
30 * be included as well as enclosing tokens up to the root
31 */
32 INCLUDE_ALL_AND_PATH,
33 /**
34 * Tokens will be included if any non-null filter is returned.
35 * The exception is if a field name returns a non-null filter,
36 * but the field value returns a null filter. In this case the
37 * field name and value will both be omitted.
38 */
39 INCLUDE_NON_NULL
40 }
41
1842 // // Marker values
1943
2044 /**
147147 }
148148
149149 /**
150 * Method called to ensure that field name, if present, has been written
151 */
152 public void ensureFieldNameWritten(JsonGenerator gen) throws IOException
153 {
154 if (_needToHandleName) {
155 _needToHandleName = false;
156 gen.writeFieldName(_currentName);
157 }
158 }
159
160 /**
150161 * Method called to ensure that parent path from root is written up to
151162 * and including this node.
152163 */
168179 if (_type == TYPE_OBJECT) {
169180 gen.writeStartObject();
170181 gen.writeFieldName(_currentName); // we know name must be written
171 } else if (_type == TYPE_ARRAY) {
172 gen.writeStartArray();
173 }
174 }
175 }
176
177 /**
178 * Variant of {@link #writePath(JsonGenerator)} called when all we
179 * need is immediately surrounding Object. Method typically called
180 * when including a single property but not including full path
181 * to root.
182 */
183 public void writeImmediatePath(JsonGenerator gen) throws IOException
184 {
185 if ((_filter == null) || (_filter == TokenFilter.INCLUDE_ALL)) {
186 return;
187 }
188 if (_startHandled) {
189 // even if Object started, need to start leaf-level name
190 if (_needToHandleName) {
191 gen.writeFieldName(_currentName);
192 }
193 } else {
194 _startHandled = true;
195 if (_type == TYPE_OBJECT) {
196 gen.writeStartObject();
197 if (_needToHandleName) {
198 gen.writeFieldName(_currentName);
199 }
200182 } else if (_type == TYPE_ARRAY) {
201183 gen.writeStartArray();
202184 }
33
44 public final class CharTypes
55 {
6 private final static char[] HC = "0123456789ABCDEF".toCharArray();
7 private final static byte[] HB;
6 protected final static char[] HC = "0123456789ABCDEF".toCharArray();
7 protected final static byte[] HB;
88 static {
99 int len = HC.length;
1010 HB = new byte[len];
1717 * Lookup table used for determining which input characters
1818 * need special handling when contained in text segment.
1919 */
20 private final static int[] sInputCodes;
20 protected final static int[] sInputCodes;
2121 static {
2222 /* 96 would do for most cases (backslash is ASCII 94)
2323 * but if we want to do lookups by raw bytes it's better
3838 * Additionally we can combine UTF-8 decoding info into similar
3939 * data table.
4040 */
41 private final static int[] sInputCodesUTF8;
41 protected final static int[] sInputCodesUTF8;
4242 static {
4343 final int[] table = new int[sInputCodes.length];
4444 System.arraycopy(sInputCodes, 0, table, 0, table.length);
6868 * Basically this is list of 8-bit ASCII characters that are legal
6969 * as part of Javascript identifier
7070 */
71 private final static int[] sInputCodesJsNames;
71 protected final static int[] sInputCodesJsNames;
7272 static {
7373 final int[] table = new int[256];
7474 // Default is "not a name char", mark ones that are
9595 * code as ok. They will be validated at a later point, when decoding
9696 * name
9797 */
98 private final static int[] sInputCodesUtf8JsNames;
98 protected final static int[] sInputCodesUtf8JsNames;
9999 static {
100100 final int[] table = new int[256];
101101 // start with 8-bit JS names
108108 * Decoding table used to quickly determine characters that are
109109 * relevant within comment content.
110110 */
111 private final static int[] sInputCodesComment;
111 protected final static int[] sInputCodesComment;
112112 static {
113113 final int[] buf = new int[256];
114114 // but first: let's start with UTF-8 multi-byte markers:
128128 *
129129 * @since 2.3
130130 */
131 private final static int[] sInputCodesWS;
131 protected final static int[] sInputCodesWS;
132132 static {
133133 // but first: let's start with UTF-8 multi-byte markers:
134134 final int[] buf = new int[256];
151151 * Lookup table used for determining which output characters in
152152 * 7-bit ASCII range need to be quoted.
153153 */
154 private final static int[] sOutputEscapes128;
154 protected final static int[] sOutputEscapes128;
155155 static {
156156 int[] table = new int[128];
157157 // Control chars need generic escape sequence
178178 *<p>
179179 * NOTE: before 2.10.1, was of size 128, extended for simpler handling
180180 */
181 private final static int[] sHexValues = new int[256];
181 protected final static int[] sHexValues = new int[256];
182182 static {
183183 Arrays.fill(sHexValues, -1);
184184 for (int i = 0; i < 10; ++i) {
77 import com.fasterxml.jackson.core.io.CharacterEscapes;
88 import com.fasterxml.jackson.core.io.IOContext;
99 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
10 import com.fasterxml.jackson.core.util.JacksonFeatureSet;
1011 import com.fasterxml.jackson.core.util.VersionUtil;
1112
1213 /**
2930 */
3031 protected final static int[] sOutputEscapes = CharTypes.get7BitOutputEscapes();
3132
33 /**
34 * Default capabilities for JSON generator implementations which do not
35 * different from "general textual" defaults
36 *
37 * @since 2.12
38 */
39 protected final static JacksonFeatureSet<StreamWriteCapability> JSON_WRITE_CAPABILITIES
40 = DEFAULT_TEXTUAL_WRITE_CAPABILITIES;
41
3242 /*
3343 /**********************************************************
3444 /* Configuration, basic I/O
191201 return this;
192202 }
193203
204 @Override
205 public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
206 return JSON_WRITE_CAPABILITIES;
207 }
208
194209 /*
195210 /**********************************************************
196211 /* Shared helper methods
1414 * Parent context for this context; null for root context.
1515 */
1616 protected final JsonReadContext _parent;
17
17
1818 // // // Optional duplicate detection
1919
2020 protected DupDetector _dups;
2121
2222 /*
2323 /**********************************************************
24 /* Simple instance reuse slots; speeds up things
25 /* a bit (10-15%) for docs with lots of small
26 /* arrays/objects (for which allocation was
27 /* visible in profile stack frames)
24 /* Simple instance reuse slots; speeds up things a bit (10-15%)
25 /* for docs with lots of small arrays/objects (for which
26 /* allocation was visible in profile stack frames)
2827 /**********************************************************
2928 */
3029
6261 _index = -1;
6362 }
6463
65 protected void reset(int type, int lineNr, int colNr) {
64 /**
65 * Internal method to allow instance reuse: DO NOT USE unless you absolutely
66 * know what you are doing.
67 * Clears up state (including "current value"), changes type to one specified;
68 * resets current duplicate-detection state (if any).
69 * Parent link left as-is since it is {@code final}.
70 *<p>
71 * NOTE: Public since 2.12.
72 */
73 public void reset(int type, int lineNr, int colNr) {
6674 _type = type;
6775 _index = -1;
6876 _lineNr = lineNr;
8383 _currentValue = currValue;
8484 }
8585
86 protected JsonWriteContext reset(int type) {
86 /**
87 * Internal method to allow instance reuse: DO NOT USE unless you absolutely
88 * know what you are doing.
89 * Clears up state (including "current value"), changes type to one specified;
90 * resets current duplicate-detection state (if any).
91 * Parent link left as-is since it is {@code final}.
92 *<p>
93 * NOTE: Public since 2.12.
94 */
95 public JsonWriteContext reset(int type) {
8796 _type = type;
8897 _index = -1;
8998 _currentName = null;
93102 return this;
94103 }
95104
96 /* @since 2.10 */
97 protected JsonWriteContext reset(int type, Object currValue) {
105 /**
106 * Internal method to allow instance reuse: DO NOT USE unless you absolutely
107 * know what you are doing.
108 * Clears up state, changes type to one specified, assigns "current value";
109 * resets current duplicate-detection state (if any).
110 * Parent link left as-is since it is {@code final}.
111 *<p>
112 * NOTE: Public since 2.12.
113 *
114 * @since 2.10
115 */
116 public JsonWriteContext reset(int type, Object currValue) {
98117 _type = type;
99118 _index = -1;
100119 _currentName = null;
1515 * based on a {@link java.io.Reader} to handle low-level character
1616 * conversion tasks.
1717 */
18 public class ReaderBasedJsonParser // final in 2.3, earlier
18 public class ReaderBasedJsonParser
1919 extends ParserBase
2020 {
2121 @SuppressWarnings("deprecation")
166166
167167 @Override public ObjectCodec getCodec() { return _objectCodec; }
168168 @Override public void setCodec(ObjectCodec c) { _objectCodec = c; }
169
170 @Override // @since 2.12
171 public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
172 return JSON_READ_CAPABILITIES;
173 }
169174
170175 @Override
171176 public int releaseBuffered(Writer w) throws IOException {
139139 @Override
140140 public void setCodec(ObjectCodec c) {
141141 _objectCodec = c;
142 }
143
144 @Override // @since 2.12
145 public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
146 return JSON_READ_CAPABILITIES;
142147 }
143148
144149 /*
324324 }
325325 }
326326
327 @Override // since 2.10
328 public void writeStartArray(int size) throws IOException
327 @Override // since 2.12
328 public final void writeStartArray(Object currentValue) throws IOException
329329 {
330330 _verifyValueWrite("start an array");
331 _writeContext = _writeContext.createChildArrayContext();
331 _writeContext = _writeContext.createChildArrayContext(currentValue);
332 if (_cfgPrettyPrinter != null) {
333 _cfgPrettyPrinter.writeStartArray(this);
334 } else {
335 if (_outputTail >= _outputEnd) {
336 _flushBuffer();
337 }
338 _outputBuffer[_outputTail++] = BYTE_LBRACKET;
339 }
340 }
341
342 @Override // since 2.12
343 public void writeStartArray(Object currentValue, int size) throws IOException
344 {
345 _verifyValueWrite("start an array");
346 _writeContext = _writeContext.createChildArrayContext(currentValue);
332347 if (_cfgPrettyPrinter != null) {
333348 _cfgPrettyPrinter.writeStartArray(this);
334349 } else {
10531068 public void writeNumber(String encodedValue) throws IOException
10541069 {
10551070 _verifyValueWrite(WRITE_NUMBER);
1056 if (_cfgNumbersAsStrings) {
1071 if (encodedValue == null) {
1072 _writeNull();
1073 } else if (_cfgNumbersAsStrings) {
10571074 _writeQuotedRaw(encodedValue);
10581075 } else {
10591076 writeRaw(encodedValue);
175175 @Override
176176 public void setCodec(ObjectCodec c) {
177177 _objectCodec = c;
178 }
179
180 @Override // @since 2.12
181 public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
182 return JSON_READ_CAPABILITIES;
178183 }
179184
180185 /*
257257 }
258258 }
259259
260 @Override // since 2.10
261 public void writeStartArray(int size) throws IOException
260 @Override // since 2.12
261 public void writeStartArray(Object currentValue) throws IOException
262262 {
263263 _verifyValueWrite("start an array");
264 _writeContext = _writeContext.createChildArrayContext();
264 _writeContext = _writeContext.createChildArrayContext(currentValue);
265265 if (_cfgPrettyPrinter != null) {
266266 _cfgPrettyPrinter.writeStartArray(this);
267267 } else {
271271 _outputBuffer[_outputTail++] = '[';
272272 }
273273 }
274
274
275 @Override // since 2.12
276 public void writeStartArray(Object currentValue, int size) throws IOException
277 {
278 _verifyValueWrite("start an array");
279 _writeContext = _writeContext.createChildArrayContext(currentValue);
280 if (_cfgPrettyPrinter != null) {
281 _cfgPrettyPrinter.writeStartArray(this);
282 } else {
283 if (_outputTail >= _outputEnd) {
284 _flushBuffer();
285 }
286 _outputBuffer[_outputTail++] = '[';
287 }
288 }
289
275290 @Override
276291 public void writeEndArray() throws IOException
277292 {
828843 public void writeNumber(String encodedValue) throws IOException
829844 {
830845 _verifyValueWrite(WRITE_NUMBER);
831 if (_cfgNumbersAsStrings) {
846 if (encodedValue == null) {
847 _writeNull();
848 } else if (_cfgNumbersAsStrings) {
832849 _writeQuotedRaw(encodedValue);
833850 } else {
834851 writeRaw(encodedValue);
77 import com.fasterxml.jackson.core.json.JsonReadContext;
88 import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
99 import com.fasterxml.jackson.core.util.ByteArrayBuilder;
10 import com.fasterxml.jackson.core.util.JacksonFeatureSet;
1011
1112 import static com.fasterxml.jackson.core.JsonTokenId.*;
1213
265266 throw new UnsupportedOperationException("Can not use ObjectMapper with non-blocking parser");
266267 }
267268
268 /**
269 * @since 2.9
270 */
271 @Override
269 @Override // since 2.9
272270 public boolean canParseAsync() { return true; }
271
272 @Override // @since 2.12
273 public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
274 return JSON_READ_CAPABILITIES;
275 }
273276
274277 /*
275278 /**********************************************************
6666 * Reference to the root symbol table, for child tables, so
6767 * that they can merge table information back as necessary.
6868 */
69 final private ByteQuadsCanonicalizer _parent;
69 final protected ByteQuadsCanonicalizer _parent;
7070
7171 /**
7272 * Member that is only used by the root table instance: root
7474 * may return new state if they add entries to the table.
7575 * Child tables do NOT use the reference.
7676 */
77 final private AtomicReference<TableInfo> _tableInfo;
77 final protected AtomicReference<TableInfo> _tableInfo;
7878
7979 /**
8080 * Seed value we use as the base to make hash codes non-static between
8383 * This is done for security reasons, to avoid potential DoS attack via
8484 * hash collisions.
8585 */
86 final private int _seed;
86 final protected int _seed;
8787
8888 /*
8989 /**********************************************************
9898 * NOTE: non-final to allow disabling intern()ing in case of excessive
9999 * collisions.
100100 */
101 private boolean _intern;
101 protected boolean _intern;
102102
103103 /**
104104 * Flag that indicates whether we should throw an exception if enough
106106 *
107107 * @since 2.4
108108 */
109 private final boolean _failOnDoS;
109 protected final boolean _failOnDoS;
110110
111111 /*
112112 /**********************************************************
120120 * structure (details of which may be tweaked depending on expected rates
121121 * of collisions).
122122 */
123 private int[] _hashArea;
123 protected int[] _hashArea;
124124
125125 /**
126126 * Number of slots for primary entries within {@link #_hashArea}; which is
128128 * primary covers only half of the area; plus, additional area for longer
129129 * symbols after hash area).
130130 */
131 private int _hashSize;
131 protected int _hashSize;
132132
133133 /**
134134 * Offset within {@link #_hashArea} where secondary entries start
135135 */
136 private int _secondaryStart;
136 protected int _secondaryStart;
137137
138138 /**
139139 * Offset within {@link #_hashArea} where tertiary entries start
140140 */
141 private int _tertiaryStart;
141 protected int _tertiaryStart;
142142
143143 /**
144144 * Constant that determines size of buckets for tertiary entries:
149149 * Default value is 2, for buckets of 4 slots; grows bigger with
150150 * bigger table sizes.
151151 */
152 private int _tertiaryShift;
152 protected int _tertiaryShift;
153153
154154 /**
155155 * Total number of Strings in the symbol table; only used for child tables.
156156 */
157 private int _count;
157 protected int _count;
158158
159159 /**
160160 * Array that contains <code>String</code> instances matching
162162 * Contains nulls for unused entries. Note that this size is twice
163163 * that of {@link #_hashArea}
164164 */
165 private String[] _names;
165 protected String[] _names;
166166
167167 /*
168168 /**********************************************************
175175 * for more spilled over entries (if any).
176176 * Spill over area is within fixed-size portion of {@link #_hashArea}.
177177 */
178 private int _spilloverEnd;
178 protected int _spilloverEnd;
179179
180180 /**
181181 * Offset within {@link #_hashArea} that follows main slots and contains
185185 * Note that long name area follows immediately after the fixed-size
186186 * main hash area ({@link #_hashArea}).
187187 */
188 private int _longNameOffset;
188 protected int _longNameOffset;
189189
190190 /*
191191 /**********************************************************
206206 * and when adding new collision list queues (i.e. creating a new
207207 * collision list head entry)
208208 */
209 private boolean _hashShared;
209 protected boolean _hashShared;
210210
211211 /*
212212 /**********************************************************
100100 * defined, and child instance is released (call to <code>release</code>),
101101 * parent's shared tables may be updated from the child instance.
102102 */
103 final private CharsToNameCanonicalizer _parent;
103 final protected CharsToNameCanonicalizer _parent;
104104
105105 /**
106106 * Member that is only used by the root table instance: root
108108 * may return new state if they add entries to the table.
109109 * Child tables do NOT use the reference.
110110 */
111 final private AtomicReference<TableInfo> _tableInfo;
111 final protected AtomicReference<TableInfo> _tableInfo;
112112
113113 /**
114114 * Seed value we use as the base to make hash codes non-static between
119119 *
120120 * @since 2.1
121121 */
122 final private int _seed;
123
124 final private int _flags;
122 final protected int _seed;
123
124 final protected int _flags;
125125
126126 /**
127127 * Whether any canonicalization should be attempted (whether using
129129 *<p>
130130 * NOTE: non-final since we may need to disable this with overflow.
131131 */
132 private boolean _canonicalize;
132 protected boolean _canonicalize;
133133
134134 /*
135135 /**********************************************************
141141 * Primary matching symbols; it's expected most match occur from
142142 * here.
143143 */
144 private String[] _symbols;
144 protected String[] _symbols;
145145
146146 /**
147147 * Overflow buckets; if primary doesn't match, lookup is done
150150 * Note: Number of buckets is half of number of symbol entries, on
151151 * assumption there's less need for buckets.
152152 */
153 private Bucket[] _buckets;
153 protected Bucket[] _buckets;
154154
155155 /**
156156 * Current size (number of entries); needed to know if and when
157157 * rehash.
158158 */
159 private int _size;
159 protected int _size;
160160
161161 /**
162162 * Limit that indicates maximum size this instance can hold before
163163 * it needs to be expanded and rehashed. Calculated using fill
164164 * factor passed in to constructor.
165165 */
166 private int _sizeThreshold;
166 protected int _sizeThreshold;
167167
168168 /**
169169 * Mask used to get index from hash values; equal to
170170 * <code>_buckets.length - 1</code>, when _buckets.length is
171171 * a power of two.
172172 */
173 private int _indexMask;
173 protected int _indexMask;
174174
175175 /**
176176 * We need to keep track of the longest collision list; this is needed
179179 *
180180 * @since 2.1
181181 */
182 private int _longestCollisionList;
182 protected int _longestCollisionList;
183183
184184 /*
185185 /**********************************************************
198198 * and when adding new collision list queues (i.e. creating a new
199199 * collision list head entry)
200200 */
201 private boolean _hashShared;
201 protected boolean _hashShared;
202202
203203 /*
204204 /**********************************************************
214214 *
215215 * @since 2.4
216216 */
217 private BitSet _overflows;
217 protected BitSet _overflows;
218218
219219 /*
220220 /**********************************************************
5353
5454 public ByteArrayBuilder(BufferRecycler br, int firstBlockSize) {
5555 _bufferRecycler = br;
56 // 04-Sep-2020, tatu: Let's make this bit more robust and refuse to allocate
57 // humongous blocks even if requested
58 if (firstBlockSize > MAX_BLOCK_SIZE) {
59 firstBlockSize = MAX_BLOCK_SIZE;
60 }
5661 _currBlock = (br == null) ? new byte[firstBlockSize] : br.allocByteBuffer(BufferRecycler.BYTE_WRITE_CONCAT_BUFFER);
5762 }
5863
0 package com.fasterxml.jackson.core.util;
1
2 /**
3 * Basic API implemented by Enums used for simple Jackson "features": on/off
4 * settings and capabilities exposed as something that can be internally
5 * represented as bit sets.
6 * Designed to be used with {@link JacksonFeatureSet}.
7 *
8 * @since 2.12
9 */
10 public interface JacksonFeature
11 {
12 /**
13 * Accessor for checking whether this feature is enabled by default.
14 */
15 public boolean enabledByDefault();
16
17 /**
18 * Returns bit mask for this feature instance; must be a single bit,
19 * that is of form {@code 1 << N}
20 */
21 public int getMask();
22
23 /**
24 * Convenience method for checking whether feature is enabled in given bitmask
25 */
26 public boolean enabledIn(int flags);
27 }
0 package com.fasterxml.jackson.core.util;
1
2 /**
3 * Container similar to {@link java.util.EnumSet} meant for storing sets of
4 * {@link JacksonFeature}s (usually {@link java.lang.Enum}s): main
5 * difference being that these sets are immutable. Also only supports relatively
6 * small sets of features: specifically, up to 31 features.
7 *
8 * @since 2.12
9 */
10 public final class JacksonFeatureSet<F extends JacksonFeature>
11 {
12 protected int _enabled;
13
14 /**
15 * Constructor for creating instance with specific bitmask, wherein
16 * {@code 1} bit means matching {@link JacksonFeature} is enabled and
17 * {@code 0} disabled.
18 */
19 protected JacksonFeatureSet(int bitmask) {
20 _enabled = bitmask;
21 }
22
23 /**
24 * "Default" factory which will calculate settings based on default-enabled
25 * status of all features.
26 *
27 * @param allFeatures Set of all features (enabled or disabled): usually from {@code Enum.values()}
28 */
29 public static <F extends JacksonFeature> JacksonFeatureSet<F> fromDefaults(F[] allFeatures) {
30 // first sanity check
31 if (allFeatures.length > 31) {
32 final String desc = allFeatures[0].getClass().getName();
33 throw new IllegalArgumentException(String.format(
34 "Can not use type `%s` with JacksonFeatureSet: too many entries (%d > 31)",
35 desc, allFeatures.length));
36 }
37
38 int flags = 0;
39 for (F f : allFeatures) {
40 if (f.enabledByDefault()) {
41 flags |= f.getMask();
42 }
43 }
44 return new JacksonFeatureSet<F>(flags);
45 }
46
47 public static <F extends JacksonFeature> JacksonFeatureSet<F> fromBitmask(int bitmask) {
48 return new JacksonFeatureSet<F>(bitmask);
49 }
50
51 /**
52 * Mutant factory for getting a set in which specified feature is enabled:
53 * will either return this instance (if no change), or newly created set (if there
54 * is change).
55 *
56 * @param feature Feature to enable in set returned
57 *
58 * @return Newly created set of state of feature changed; {@code this} if not
59 */
60 public JacksonFeatureSet<F> with(F feature) {
61 int newMask = _enabled | feature.getMask();
62 return (newMask == _enabled) ? this : new JacksonFeatureSet<F>(newMask);
63 }
64
65 /**
66 * Mutant factory for getting a set in which specified feature is disabled:
67 * will either return this instance (if no change), or newly created set (if there
68 * is change).
69 *
70 * @param feature Feature to disable in set returned
71 *
72 * @return Newly created set of state of feature changed; {@code this} if not
73 */
74 public JacksonFeatureSet<F> without(F feature) {
75 int newMask = _enabled & ~feature.getMask();
76 return (newMask == _enabled) ? this : new JacksonFeatureSet<F>(newMask);
77 }
78
79 /**
80 * Main accessor for checking whether given feature is enabled in this feature set.
81 *
82 * @param feature Feature to check
83 *
84 * @return True if feature is enabled in this set; false otherwise
85 */
86 public boolean isEnabled(F feature) {
87 return (feature.getMask() & _enabled) != 0;
88 }
89
90 /**
91 * Accessor for underlying bitmask
92 *
93 * @return Bitmask of enabled features
94 */
95 public int asBitmask() {
96 return _enabled;
97 }
98 }
9595 @Override
9696 public boolean canWriteFormattedNumbers() { return delegate.canWriteFormattedNumbers(); }
9797
98 @Override
99 public JacksonFeatureSet<StreamWriteCapability> getWriteCapabilities() {
100 return delegate.getWriteCapabilities();
101 }
102
98103 /*
99104 /**********************************************************
100105 /* Public API, configuration
187192 @Override
188193 public void writeStartArray() throws IOException { delegate.writeStartArray(); }
189194
195 @SuppressWarnings("deprecation")
190196 @Override
191197 public void writeStartArray(int size) throws IOException { delegate.writeStartArray(size); }
192198
9292
9393 @Override public boolean requiresCustomCodec() { return delegate.requiresCustomCodec(); }
9494
95 @Override public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() { return delegate.getReadCapabilities(); }
96
9597 /*
9698 /**********************************************************
9799 /* Closeable impl
111113 @Override public int currentTokenId() { return delegate.currentTokenId(); }
112114
113115 @Override public JsonToken getCurrentToken() { return delegate.getCurrentToken(); }
116
117 @Deprecated // since 2.12
114118 @Override public int getCurrentTokenId() { return delegate.getCurrentTokenId(); }
119
115120 @Override public boolean hasCurrentToken() { return delegate.hasCurrentToken(); }
116121 @Override public boolean hasTokenId(int id) { return delegate.hasTokenId(id); }
117122 @Override public boolean hasToken(JsonToken t) { return delegate.hasToken(t); }
121126 @Override public JsonStreamContext getParsingContext() { return delegate.getParsingContext(); }
122127 @Override public boolean isExpectedStartArrayToken() { return delegate.isExpectedStartArrayToken(); }
123128 @Override public boolean isExpectedStartObjectToken() { return delegate.isExpectedStartObjectToken(); }
129 @Override public boolean isExpectedNumberIntToken() { return delegate.isExpectedNumberIntToken(); }
130
124131 @Override public boolean isNaN() throws IOException { return delegate.isNaN(); }
125132
126133 /*
185192 @Override
186193 public Number getNumberValue() throws IOException { return delegate.getNumberValue(); }
187194
195 @Override
196 public Number getNumberValueExact() throws IOException { return delegate.getNumberValueExact(); }
197
188198 /*
189199 /**********************************************************
190200 /* Public API, access to token information, coercion/conversion
191201 /**********************************************************
192202 */
193
203
194204 @Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); }
195205 @Override public int getValueAsInt(int defaultValue) throws IOException { return delegate.getValueAsInt(defaultValue); }
196206 @Override public long getValueAsLong() throws IOException { return delegate.getValueAsLong(); }
201211 @Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); }
202212 @Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); }
203213 @Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); }
204
214
205215 /*
206216 /**********************************************************
207217 /* Public API, access to token values, other
+0
-8
src/main/resources/META-INF/LICENSE less more
0 This copy of Jackson JSON processor streaming parser/generator is licensed under the
1 Apache (Software) License, version 2.0 ("the License").
2 See the License for details about distribution rights, and the
3 specific rights regarding derivate works.
4
5 You may obtain a copy of the License at:
6
7 http://www.apache.org/licenses/LICENSE-2.0
22 Jackson is a high-performance, Free/Open Source JSON processing library.
33 It was originally written by Tatu Saloranta (tatu.saloranta@iki.fi), and has
44 been in development since 2007.
5 It is currently developed by a community of developers, as well as supported
6 commercially by FasterXML.com.
5 It is currently developed by a community of developers.
76
87 ## Licensing
98
10 Jackson core and extension components may licensed under different licenses.
9 Jackson 2.x core and extension components are licensed under Apache License 2.0
1110 To find the details that apply to this artifact see the accompanying LICENSE file.
12 For more information, including possible other licensing options, contact
13 FasterXML.com (http://fasterxml.com).
1411
1512 ## Credits
1613
17 A list of contributors may be found from CREDITS file, which is included
14 A list of contributors may be found from CREDITS(-2.x) file, which is included
1815 in some artifacts (usually source distributions); but is always available
1916 from the source code management (SCM) system project uses.
2626
2727 public void testBase64Variant() throws Exception
2828 {
29 Base64Variant orig = Base64Variants.PEM;
30 byte[] stuff = jdkSerialize(orig);
31 Base64Variant back = jdkDeserialize(stuff);
32 assertSame(orig, back);
29 {
30 Base64Variant orig = Base64Variants.PEM;
31 final String exp = _encodeBase64(orig);
32 byte[] stuff = jdkSerialize(orig);
33 Base64Variant back = jdkDeserialize(stuff);
34 assertSame(orig, back);
35 assertEquals(exp, _encodeBase64(back));
36 }
37
38 // and then with a twist
39 {
40 Base64Variant orig = Base64Variants.MODIFIED_FOR_URL;
41 assertFalse(orig.usesPadding());
42 Base64Variant mod = orig.withWritePadding(true);
43 assertTrue(mod.usesPadding());
44 assertNotSame(orig, mod);
45 assertFalse(orig.equals(mod));
46 assertFalse(mod.equals(orig));
47
48 final String exp = _encodeBase64(mod);
49 byte[] stuff = jdkSerialize(mod);
50 Base64Variant back = jdkDeserialize(stuff);
51 assertTrue(back.usesPadding());
52 assertNotSame(mod, back);
53 assertEquals(mod, back);
54 assertEquals(exp, _encodeBase64(back));
55 }
3356 }
3457
3558 public void testPrettyPrinter() throws Exception
146169 p.close();
147170 g.close();
148171 }
172
173 protected String _encodeBase64(Base64Variant b64) throws IOException
174 {
175 // something with padding...
176 return b64.encode("abcde".getBytes("UTF-8"));
177 }
149178 }
1414
1515 protected final static String UNICODE_SEGMENT = "["+UNICODE_2BYTES+"/"+UNICODE_3BYTES+"]";
1616
17 protected static AsyncReaderWrapper asyncForBytes(JsonFactory f,
17 public static AsyncReaderWrapper asyncForBytes(JsonFactory f,
1818 int bytesPerRead,
1919 byte[] bytes, int padding) throws IOException
2020 {
5151
5252 assertEquals(Base64Variant.BASE64_VALUE_INVALID, std.decodeBase64Byte((byte) '?'));
5353 assertEquals(Base64Variant.BASE64_VALUE_INVALID, std.decodeBase64Byte((byte) 0xA0));
54
54
5555 assertEquals(0, std.decodeBase64Char('A'));
5656 assertEquals(1, std.decodeBase64Char((int) 'B'));
5757 assertEquals(2, std.decodeBase64Char((byte)'C'));
5959 assertEquals(0, std.decodeBase64Byte((byte) 'A'));
6060 assertEquals(1, std.decodeBase64Byte((byte) 'B'));
6161 assertEquals(2, std.decodeBase64Byte((byte)'C'));
62
62
6363 assertEquals('/', std.encodeBase64BitsAsChar(63));
6464 assertEquals((byte) 'b', std.encodeBase64BitsAsByte(27));
6565
8181
8282 byte[] input = new byte[] { 1, 2, 34, 127, -1 };
8383 String encoded = std.encode(input, false);
84 byte[] decoded = std.decode(encoded);
84 byte[] decoded = std.decode(encoded);
8585 Assert.assertArrayEquals(input, decoded);
8686
8787 assertEquals(quote(encoded), std.encode(input, true));
114114 }
115115 sb.append("AQ==");
116116 final String exp = sb.toString();
117
117
118118 // first, JSON standard
119119 assertEquals(exp.replace("##", "\\n"), std.encode(data, false));
120120
147147 verifyException(iae, "Illegal character");
148148 }
149149 }
150
151 public void testPaddingReadBehaviour() throws Exception {
152
153 for (Base64Variant variant: Arrays.asList(Base64Variants.MIME, Base64Variants.MIME_NO_LINEFEEDS, Base64Variants.PEM)) {
154
155 final String BASE64_HELLO = "aGVsbG8=";
156 try {
157 variant.withPaddingForbidden().decode(BASE64_HELLO);
158 fail("Should not pass");
159 } catch (IllegalArgumentException iae) {
160 verifyException(iae, "no padding");
161 }
162
163 variant.withPaddingAllowed().decode(BASE64_HELLO);
164 variant.withPaddingRequired().decode(BASE64_HELLO);
165
166 final String BASE64_HELLO_WITHOUT_PADDING = "aGVsbG8";
167 try {
168 variant.withPaddingRequired().decode(BASE64_HELLO_WITHOUT_PADDING);
169 fail("Should not pass");
170 } catch (IllegalArgumentException iae) {
171 verifyException(iae, "expects padding");
172 }
173 variant.withPaddingAllowed().decode(BASE64_HELLO_WITHOUT_PADDING);
174 variant.withPaddingForbidden().decode(BASE64_HELLO_WITHOUT_PADDING);
175 }
176
177 //testing for MODIFIED_FOR_URL
178
179 final String BASE64_HELLO = "aGVsbG8=";
180 try {
181 Base64Variants.MODIFIED_FOR_URL.withPaddingForbidden().decode(BASE64_HELLO);
182 fail("Should not pass");
183 } catch (IllegalArgumentException iae) {
184 verifyException(iae, "illegal character");
185 }
186
187 try {
188 Base64Variants.MODIFIED_FOR_URL.withPaddingAllowed().decode(BASE64_HELLO);
189 fail("Should not pass");
190 } catch (IllegalArgumentException iae) {
191 verifyException(iae, "illegal character");
192 }
193
194 try {
195 Base64Variants.MODIFIED_FOR_URL.withPaddingRequired().decode(BASE64_HELLO);
196 fail("Should not pass");
197 } catch (IllegalArgumentException iae) {
198 verifyException(iae, "illegal character");
199 }
200
201 final String BASE64_HELLO_WITHOUT_PADDING = "aGVsbG8";
202 try {
203 Base64Variants.MODIFIED_FOR_URL.withPaddingRequired().decode(BASE64_HELLO_WITHOUT_PADDING);
204 fail("Should not pass");
205 } catch (IllegalArgumentException iae) {
206 verifyException(iae, "expects padding");
207 }
208
209 Base64Variants.MODIFIED_FOR_URL.withPaddingAllowed().decode(BASE64_HELLO_WITHOUT_PADDING);
210 Base64Variants.MODIFIED_FOR_URL.withPaddingForbidden().decode(BASE64_HELLO_WITHOUT_PADDING);
211
212 }
150213 }
55 import java.util.*;
66
77 import com.fasterxml.jackson.core.*;
8 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
89 import com.fasterxml.jackson.core.io.SerializedString;
910
1011 /**
6667 }
6768 }
6869
70 static class StrictNameMatchFilter extends TokenFilter
71 {
72 private final Set<String> _names;
73
74 public StrictNameMatchFilter(String... names) {
75 _names = new HashSet<String>(Arrays.asList(names));
76 }
77
78 @Override
79 public TokenFilter includeProperty(String name) {
80 if (_names.contains(name)) {
81 return TokenFilter.INCLUDE_ALL;
82 }
83 return null;
84 }
85 }
86
6987 static class IndexMatchFilter extends TokenFilter
7088 {
7189 private final BitSet _indices;
94112 protected boolean _includeScalar() { return false; }
95113 }
96114
115 static class NoArraysFilter extends TokenFilter
116 {
117 @Override
118 public TokenFilter filterStartArray() {
119 return null;
120 }
121 }
122
123 static class NoObjectsFilter extends TokenFilter
124 {
125 @Override
126 public TokenFilter filterStartObject() {
127 return null;
128 }
129 }
130
97131 /*
98132 /**********************************************************
99133 /* Test methods
119153 StringWriter w = new StringWriter();
120154 JsonGenerator gen = new FilteringGeneratorDelegate(_createGenerator(w),
121155 new NameMatchFilter("value"),
122 false, // includePath
156 Inclusion.ONLY_INCLUDE_ALL,
123157 false // multipleMatches
124158 );
125159 final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
140174 NameMatchFilter filter = new NameMatchFilter("value");
141175 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(origGen,
142176 filter,
143 true, // includePath
177 Inclusion.INCLUDE_ALL_AND_PATH,
144178 false // multipleMatches
145179 );
146180
162196 NameMatchFilter filter = new NameMatchFilter("value");
163197 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(origGen,
164198 filter,
165 true, // includePath
199 Inclusion.INCLUDE_ALL_AND_PATH,
166200 false // multipleMatches
167201 );
168202
191225 : new NameMatchFilter("value");
192226 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
193227 tf,
194 true, // includePath
228 Inclusion.INCLUDE_ALL_AND_PATH,
195229 true // multipleMatches
196230 );
197231 //final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':[3],'value2':'foo'},'b':true}";
238272 StringWriter w = new StringWriter();
239273 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
240274 new NameMatchFilter("array"),
241 true, // includePath
275 Inclusion.INCLUDE_ALL_AND_PATH,
242276 false // multipleMatches
243277 );
244278 //final String JSON = "{'header':['ENCODED',raw],'array':['base64stuff',1,2,3,4,5,6.25,7.5],'extra':[1,2,3,4,5,6.25,7.5]}";
289323 StringWriter w = new StringWriter();
290324 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
291325 new NameMatchFilter("value0", "value2"),
292 true, /* includePath */ true /* multipleMatches */ );
326 Inclusion.INCLUDE_ALL_AND_PATH, true /* multipleMatches */ );
293327 final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
294328 writeJsonDoc(JSON_F, JSON, gen);
295329 assertEquals(aposToQuotes("{'ob':{'value0':2,'value2':4}}"), w.toString());
299333
300334 w = new StringWriter();
301335 gen = new FilteringGeneratorDelegate(_createGenerator(w),
302 new NameExcludeFilter(true, "ob"), true, true);
336 new NameExcludeFilter(true, "ob"), Inclusion.INCLUDE_ALL_AND_PATH, true);
303337 writeJsonDoc(JSON_F, JSON, gen);
304338 assertEquals(aposToQuotes("{'a':123,'array':[1,2],'b':true}"), w.toString());
305339
306340 // then excluding them
307341 w = new StringWriter();
308342 gen = new FilteringGeneratorDelegate(_createGenerator(w),
309 new NameExcludeFilter(false, "ob"), true, true);
343 new NameExcludeFilter(false, "ob"), Inclusion.INCLUDE_ALL_AND_PATH, true);
310344 writeJsonDoc(JSON_F, JSON, gen);
311345 assertEquals(aposToQuotes("{'a':123,'b':true}"), w.toString());
312346 }
317351
318352 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
319353 new NameMatchFilter("array", "b", "value"),
320 true, true);
354 Inclusion.INCLUDE_ALL_AND_PATH, true);
321355 final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
322356 writeJsonDoc(JSON_F, JSON, gen);
323357 assertEquals(aposToQuotes("{'array':[1,2],'ob':{'value':3},'b':true}"), w.toString());
330364
331365 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
332366 new NameMatchFilter("value"),
333 true, true);
367 Inclusion.INCLUDE_ALL_AND_PATH, true);
334368 final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':'abc'}},'b0':false}";
335369 writeJsonDoc(JSON_F, JSON, gen);
336370 assertEquals(aposToQuotes("{'root':{'a':{'value':3},'b':{'value':'abc'}}}"), w.toString());
337371 assertEquals(2, gen.getMatchCount());
338372 }
339373
374 public void testNoMatchFiltering1() throws Exception
375 {
376 StringWriter w = new StringWriter();
377
378 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
379 new NameMatchFilter("invalid"),
380 Inclusion.INCLUDE_NON_NULL, true);
381 final String JSON = "{'root':{'a0':true,'b':{'value':4}},'b0':false}";
382 writeJsonDoc(JSON_F, JSON, gen);
383 assertEquals(aposToQuotes("{'root':{'b':{}}}"), w.toString());
384 assertEquals(0, gen.getMatchCount());
385 }
386
387 public void testNoMatchFiltering2() throws Exception
388 {
389 StringWriter w = new StringWriter();
390
391 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
392 new NameMatchFilter("invalid"),
393 Inclusion.INCLUDE_NON_NULL, true);
394 final String object = "{'root':{'a0':true,'b':{'value':4}},'b0':false}";
395 final String JSON = String.format("[%s,%s,%s]", object, object, object);
396 writeJsonDoc(JSON_F, JSON, gen);
397 assertEquals(aposToQuotes("[{'root':{'b':{}}},{'root':{'b':{}}},{'root':{'b':{}}}]"), w.toString());
398 assertEquals(0, gen.getMatchCount());
399 }
400
401 public void testNoMatchFiltering3() throws Exception
402 {
403 StringWriter w = new StringWriter();
404
405 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
406 new NameMatchFilter("invalid"),
407 Inclusion.INCLUDE_NON_NULL, true);
408 final String object = "{'root':{'a0':true,'b':{'value':4}},'b0':false}";
409 final String JSON = String.format("[[%s],[%s],[%s]]", object, object, object);
410 writeJsonDoc(JSON_F, JSON, gen);
411 assertEquals(aposToQuotes("[[{'root':{'b':{}}}],[{'root':{'b':{}}}],[{'root':{'b':{}}}]]"), w.toString());
412 assertEquals(0, gen.getMatchCount());
413 }
414
415 public void testNoMatchFiltering4() throws Exception
416 {
417 StringWriter w = new StringWriter();
418
419 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
420 new StrictNameMatchFilter("invalid"),
421 Inclusion.INCLUDE_NON_NULL, true);
422 final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':4}},'b0':false}";
423 writeJsonDoc(JSON_F, JSON, gen);
424 assertEquals(aposToQuotes("{}"), w.toString());
425 assertEquals(0, gen.getMatchCount());
426 }
427
428 public void testNoMatchFiltering5() throws Exception
429 {
430 StringWriter w = new StringWriter();
431
432 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
433 new StrictNameMatchFilter("invalid"),
434 Inclusion.INCLUDE_NON_NULL, true);
435 final String object = "{'root':{'a0':true,'b':{'value':4}},'b0':false}";
436 final String JSON = String.format("[%s,%s,%s]", object, object, object);
437 writeJsonDoc(JSON_F, JSON, gen);
438 assertEquals(aposToQuotes("[{},{},{}]"), w.toString());
439 assertEquals(0, gen.getMatchCount());
440 }
441
442 public void testNoMatchFiltering6() throws Exception
443 {
444 StringWriter w = new StringWriter();
445
446 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
447 new StrictNameMatchFilter("invalid"),
448 Inclusion.INCLUDE_NON_NULL, true);
449 final String object = "{'root':{'a0':true,'b':{'value':4}},'b0':false}";
450 final String JSON = String.format("[[%s],[%s],[%s]]", object, object, object);
451 writeJsonDoc(JSON_F, JSON, gen);
452 assertEquals(aposToQuotes("[[{}],[{}],[{}]]"), w.toString());
453 assertEquals(0, gen.getMatchCount());
454 }
455
456 public void testValueOmitsFieldName1() throws Exception
457 {
458 StringWriter w = new StringWriter();
459
460 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
461 new NoArraysFilter(),
462 Inclusion.INCLUDE_NON_NULL, true);
463 final String JSON = "{'root':['a'],'b0':false}";
464 writeJsonDoc(JSON_F, JSON, gen);
465 assertEquals(aposToQuotes("{'b0':false}"), w.toString());
466 assertEquals(1, gen.getMatchCount());
467 }
468
340469 public void testMultipleMatchFilteringWithPath4() throws Exception
341470 {
342471 StringWriter w = new StringWriter();
343472 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
344473 new NameMatchFilter("b0"),
345 true, true);
474 Inclusion.INCLUDE_ALL_AND_PATH, true);
346475 final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':'abc'}},'b0':false}";
347476 writeJsonDoc(JSON_F, JSON, gen);
348477 assertEquals(aposToQuotes("{'b0':false}"), w.toString());
349478 assertEquals(1, gen.getMatchCount());
350479 }
351480
481 public void testValueOmitsFieldName2() throws Exception
482 {
483 StringWriter w = new StringWriter();
484
485 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
486 new NoObjectsFilter(),
487 Inclusion.INCLUDE_NON_NULL, true);
488 final String JSON = "['a',{'root':{'b':{'value':4}},'b0':false}]";
489 writeJsonDoc(JSON_F, JSON, gen);
490 assertEquals(aposToQuotes("['a']"), w.toString());
491 assertEquals(1, gen.getMatchCount());
492 }
493
352494 public void testIndexMatchWithPath1() throws Exception
353495 {
354496 StringWriter w = new StringWriter();
355497 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
356498 new IndexMatchFilter(1),
357 true, true);
499 Inclusion.INCLUDE_ALL_AND_PATH, true);
358500 final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':'abc'},'b':true}";
359501 writeJsonDoc(JSON_F, JSON, gen);
360502 assertEquals(aposToQuotes("{'array':[2]}"), w.toString());
362504 w = new StringWriter();
363505 gen = new FilteringGeneratorDelegate(_createGenerator(w),
364506 new IndexMatchFilter(0),
365 true, true);
507 Inclusion.INCLUDE_ALL_AND_PATH, true);
366508 writeJsonDoc(JSON_F, JSON, gen);
367509 assertEquals(aposToQuotes("{'array':[1]}"), w.toString());
368510 assertEquals(1, gen.getMatchCount());
373515 StringWriter w = new StringWriter();
374516 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
375517 new IndexMatchFilter(0,1),
376 true, true);
518 Inclusion.INCLUDE_ALL_AND_PATH, true);
377519 String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
378520 writeJsonDoc(JSON_F, JSON, gen);
379521 assertEquals(aposToQuotes("{'array':[1,2]}"), w.toString());
383525 w = new StringWriter();
384526 gen = new FilteringGeneratorDelegate(_createGenerator(w),
385527 new IndexMatchFilter(1, 3, 5),
386 true, true);
528 Inclusion.INCLUDE_ALL_AND_PATH, true);
387529 JSON = "{'a':123,'misc':[1,2, null, true, false, 'abc', 123],'ob':null,'b':true}";
388530 writeJsonDoc(JSON_F, JSON, gen);
389531 assertEquals(aposToQuotes("{'misc':[2,true,'abc']}"), w.toString());
392534 w = new StringWriter();
393535 gen = new FilteringGeneratorDelegate(_createGenerator(w),
394536 new IndexMatchFilter(2,6),
395 true, true);
537 Inclusion.INCLUDE_ALL_AND_PATH, true);
396538 JSON = "{'misc':[1,2, null, 0.25, false, 'abc', 11234567890]}";
397539 writeJsonDoc(JSON_F, JSON, gen);
398540 assertEquals(aposToQuotes("{'misc':[null,11234567890]}"), w.toString());
401543 w = new StringWriter();
402544 gen = new FilteringGeneratorDelegate(_createGenerator(w),
403545 new IndexMatchFilter(1),
404 true, true);
546 Inclusion.INCLUDE_ALL_AND_PATH, true);
405547 JSON = "{'misc':[1,0.25,11234567890]}";
406548 writeJsonDoc(JSON_F, JSON, gen);
407549 assertEquals(aposToQuotes("{'misc':[0.25]}"), w.toString());
414556
415557 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
416558 TokenFilter.INCLUDE_ALL,
417 true, true);
559 Inclusion.INCLUDE_ALL_AND_PATH, true);
418560
419561 String value = "val";
420562
438580 {
439581 StringWriter w = new StringWriter();
440582 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
441 TokenFilter.INCLUDE_ALL, true, true);
583 TokenFilter.INCLUDE_ALL, Inclusion.INCLUDE_ALL_AND_PATH, true);
442584
443585 gen.writeStartArray();
444586 gen.writeRawValue(new char[] { '1'}, 0, 1);
458600 {
459601 StringWriter w = new StringWriter();
460602 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(_createGenerator(w),
461 TokenFilter.INCLUDE_ALL, true, true);
603 TokenFilter.INCLUDE_ALL, Inclusion.INCLUDE_ALL_AND_PATH, true);
462604
463605 gen.writeStartObject();
464606 gen.writeNumberField("f1", 1);
33 import java.util.*;
44
55 import com.fasterxml.jackson.core.*;
6 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
67
78 @SuppressWarnings("resource")
89 public class BasicParserFilteringTest extends BaseTest
3031
3132 @Override
3233 protected boolean _includeScalar() { return false; }
34 }
35
36 static class StrictNameMatchFilter extends TokenFilter
37 {
38 private final Set<String> _names;
39
40 public StrictNameMatchFilter(String... names) {
41 _names = new HashSet<String>(Arrays.asList(names));
42 }
43
44 @Override
45 public TokenFilter includeProperty(String name) {
46 if (_names.contains(name)) {
47 return TokenFilter.INCLUDE_ALL;
48 }
49 return null;
50 }
3351 }
3452
3553 static class IndexMatchFilter extends TokenFilter
6078 protected boolean _includeScalar() { return false; }
6179 }
6280
81 static class NoObjectsFilter extends TokenFilter
82 {
83 @Override
84 public TokenFilter filterStartObject() {
85 return null;
86 }
87 }
88
6389 /*
6490 /**********************************************************
6591 /* Test methods
83109 JsonParser p0 = JSON_F.createParser(SIMPLE);
84110 FilteringParserDelegate p = new FilteringParserDelegate(p0,
85111 new NameMatchFilter("value"),
86 false, // includePath
112 Inclusion.ONLY_INCLUDE_ALL,
87113 false // multipleMatches
88114 );
89115 String result = readAndWrite(JSON_F, p);
97123 JsonParser p0 = JSON_F.createParser(jsonString);
98124 FilteringParserDelegate p = new FilteringParserDelegate(p0,
99125 new NameMatchFilter("a"),
100 true, // includePath
126 Inclusion.INCLUDE_ALL_AND_PATH,
101127 false // multipleMatches
102128 );
103129 String result = readAndWrite(JSON_F, p);
111137 JsonParser p0 = JSON_F.createParser(jsonString);
112138 FilteringParserDelegate p = new FilteringParserDelegate(p0,
113139 new NameMatchFilter("value"),
114 true, // includePath
140 Inclusion.INCLUDE_ALL_AND_PATH,
115141 false // multipleMatches
116142 );
117143 String result = readAndWrite(JSON_F, p);
125151 JsonParser p0 = JSON_F.createParser(jsonString);
126152 FilteringParserDelegate p = new FilteringParserDelegate(p0,
127153 new NameMatchFilter("ob"),
128 true, // includePath
154 Inclusion.INCLUDE_ALL_AND_PATH,
129155 false // multipleMatches
130156 );
131157 String result = readAndWrite(JSON_F, p);
139165 JsonParser p0 = JSON_F.createParser(jsonString);
140166 FilteringParserDelegate p = new FilteringParserDelegate(p0,
141167 new NameMatchFilter("value"),
142 false, // includePath
168 Inclusion.ONLY_INCLUDE_ALL,
143169 false // multipleMatches -false
144170 );
145171 String result = readAndWrite(JSON_F, p);
153179 JsonParser p0 = JSON_F.createParser(jsonString);
154180 FilteringParserDelegate p = new FilteringParserDelegate(p0,
155181 new IndexMatchFilter(1),
156 false, // includePath
182 Inclusion.ONLY_INCLUDE_ALL,
157183 false // multipleMatches -false
158184 );
159185 String result = readAndWrite(JSON_F, p);
167193 JsonParser p0 = JSON_F.createParser(jsonString);
168194 FilteringParserDelegate p = new FilteringParserDelegate(p0,
169195 new IndexMatchFilter(1),
170 true, // includePath
196 Inclusion.INCLUDE_ALL_AND_PATH,
171197 false // multipleMatches -false
172198 );
173199 String result = readAndWrite(JSON_F, p);
182208 JsonParser p0 = JSON_F.createParser(jsonString);
183209 FilteringParserDelegate p = new FilteringParserDelegate(p0,
184210 new IndexMatchFilter(1),
185 true, // includePath
211 Inclusion.INCLUDE_ALL_AND_PATH,
186212 false // multipleMatches -false
187213 );
188214 String result = readAndWrite(JSON_F, p);
196222 JsonParser p0 = JSON_F.createParser(jsonString);
197223 FilteringParserDelegate p = new FilteringParserDelegate(p0,
198224 new NameMatchFilter("value"),
199 true, // includePath
225 Inclusion.INCLUDE_ALL_AND_PATH,
200226 false // multipleMatches -false
201227 );
202228 String result = readAndWrite(JSON_F, p);
210236 JsonParser p0 = JSON_F.createParser(jsonString);
211237 FilteringParserDelegate p = new FilteringParserDelegate(p0,
212238 new NameMatchFilter("ob"),
213 true, // includePath
239 Inclusion.INCLUDE_ALL_AND_PATH,
214240 false // multipleMatches -false
215241 );
216242 String result = readAndWrite(JSON_F, p);
224250 JsonParser p0 = JSON_F.createParser(jsonString);
225251 FilteringParserDelegate p = new FilteringParserDelegate(p0,
226252 new NameMatchFilter("value"),
227 false, // includePath
253 Inclusion.ONLY_INCLUDE_ALL,
228254 true // multipleMatches - true
229255 );
230256 String result = readAndWrite(JSON_F, p);
238264 JsonParser p0 = JSON_F.createParser(jsonString);
239265 FilteringParserDelegate p = new FilteringParserDelegate(p0,
240266 new NameMatchFilter("value"),
241 true, // includePath
267 Inclusion.INCLUDE_ALL_AND_PATH,
242268 true // multipleMatches - true
243269 );
244270 String result = readAndWrite(JSON_F, p);
252278 JsonParser p0 = JSON_F.createParser(jsonString);
253279 FilteringParserDelegate p = new FilteringParserDelegate(p0,
254280 new IndexMatchFilter(1),
255 true, // includePath
281 Inclusion.INCLUDE_ALL_AND_PATH,
256282 true // multipleMatches - true
257283 );
258284 String result = readAndWrite(JSON_F, p);
265291 JsonParser p0 = JSON_F.createParser(SIMPLE);
266292 FilteringParserDelegate p = new FilteringParserDelegate(p0,
267293 new NameMatchFilter("value0", "value2"),
268 true, /* includePath */ true /* multipleMatches */ );
294 Inclusion.INCLUDE_ALL_AND_PATH, true /* multipleMatches */ );
269295 String result = readAndWrite(JSON_F, p);
270296 assertEquals(aposToQuotes("{'ob':{'value0':2,'value2':0.25}}"), result);
271297 assertEquals(2, p.getMatchCount());
278304 JsonParser p0 = JSON_F.createParser(INPUT);
279305 FilteringParserDelegate p = new FilteringParserDelegate(p0,
280306 new NameMatchFilter("b", "value"),
281 true, true);
307 Inclusion.INCLUDE_ALL_AND_PATH, true);
282308
283309 String result = readAndWrite(JSON_F, p);
284310 assertEquals(aposToQuotes("{'ob':{'value':3},'b':true}"), result);
291317 JsonParser p0 = JSON_F.createParser(JSON);
292318 FilteringParserDelegate p = new FilteringParserDelegate(p0,
293319 new NameMatchFilter("value"),
294 true, true);
320 Inclusion.INCLUDE_ALL_AND_PATH, true);
295321 String result = readAndWrite(JSON_F, p);
296322 assertEquals(aposToQuotes("{'root':{'a':{'value':3},'b':{'value':\"foo\"}}}"), result);
297323 assertEquals(2, p.getMatchCount());
298324 }
299325
326 public void testNoMatchFiltering1() throws Exception
327 {
328 String jsonString = aposToQuotes("{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}");
329 JsonParser p0 = JSON_F.createParser(jsonString);
330 FilteringParserDelegate p = new FilteringParserDelegate(p0,
331 new NameMatchFilter("invalid"),
332 Inclusion.INCLUDE_NON_NULL,
333 true // multipleMatches
334 );
335 String result = readAndWrite(JSON_F, p);
336 assertEquals(aposToQuotes("{'array':[],'ob':{}}"), result);
337 assertEquals(0, p.getMatchCount());
338 }
339
340 public void testNoMatchFiltering2() throws Exception
341 {
342 String object = aposToQuotes("{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}");
343 String jsonString = String.format("[%s,%s,%s]", object, object, object);
344 JsonParser p0 = JSON_F.createParser(jsonString);
345 FilteringParserDelegate p = new FilteringParserDelegate(p0,
346 new NameMatchFilter("invalid"),
347 Inclusion.INCLUDE_NON_NULL,
348 true // multipleMatches
349 );
350 String result = readAndWrite(JSON_F, p);
351 assertEquals(aposToQuotes("[{'array':[],'ob':{}},{'array':[],'ob':{}},{'array':[],'ob':{}}]"), result);
352 assertEquals(0, p.getMatchCount());
353 }
354
355 public void testNoMatchFiltering3() throws Exception
356 {
357 String object = aposToQuotes("{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}");
358 String jsonString = String.format("[[%s],[%s],[%s]]", object, object, object);
359 JsonParser p0 = JSON_F.createParser(jsonString);
360 FilteringParserDelegate p = new FilteringParserDelegate(p0,
361 new NameMatchFilter("invalid"),
362 Inclusion.INCLUDE_NON_NULL,
363 true // multipleMatches
364 );
365 String result = readAndWrite(JSON_F, p);
366 assertEquals(aposToQuotes("[[{'array':[],'ob':{}}],[{'array':[],'ob':{}}],[{'array':[],'ob':{}}]]"), result);
367 assertEquals(0, p.getMatchCount());
368 }
369
370 public void testNoMatchFiltering4() throws Exception
371 {
372 String jsonString = aposToQuotes("{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}");
373 JsonParser p0 = JSON_F.createParser(jsonString);
374 FilteringParserDelegate p = new FilteringParserDelegate(p0,
375 new StrictNameMatchFilter("invalid"),
376 Inclusion.INCLUDE_NON_NULL,
377 true // multipleMatches
378 );
379 String result = readAndWrite(JSON_F, p);
380 assertEquals(aposToQuotes("{}"), result);
381 assertEquals(0, p.getMatchCount());
382 }
383
384 public void testNoMatchFiltering5() throws Exception
385 {
386 String object = aposToQuotes("{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}");
387 String jsonString = String.format("[%s,%s,%s]", object, object, object);
388 JsonParser p0 = JSON_F.createParser(jsonString);
389 FilteringParserDelegate p = new FilteringParserDelegate(p0,
390 new StrictNameMatchFilter("invalid"),
391 Inclusion.INCLUDE_NON_NULL,
392 true // multipleMatches
393 );
394 String result = readAndWrite(JSON_F, p);
395 assertEquals(aposToQuotes("[{},{},{}]"), result);
396 assertEquals(0, p.getMatchCount());
397 }
398
399 public void testNoMatchFiltering6() throws Exception
400 {
401 String object = aposToQuotes("{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}");
402 String jsonString = String.format("[[%s],[%s],[%s]]", object, object, object);
403 JsonParser p0 = JSON_F.createParser(jsonString);
404 FilteringParserDelegate p = new FilteringParserDelegate(p0,
405 new StrictNameMatchFilter("invalid"),
406 Inclusion.INCLUDE_NON_NULL,
407 true // multipleMatches
408 );
409 String result = readAndWrite(JSON_F, p);
410 assertEquals(aposToQuotes("[[{}],[{}],[{}]]"), result);
411 assertEquals(0, p.getMatchCount());
412 }
413
414 public void testValueOmitsFieldName2() throws Exception
415 {
416 String jsonString = aposToQuotes("['a',{'value0':3,'b':{'value':4}},123]");
417 JsonParser p0 = JSON_F.createParser(jsonString);
418 FilteringParserDelegate p = new FilteringParserDelegate(p0,
419 new NoObjectsFilter(),
420 Inclusion.INCLUDE_NON_NULL,
421 true // multipleMatches
422 );
423 String result = readAndWrite(JSON_F, p);
424 assertEquals(aposToQuotes("['a',123]"), result);
425 assertEquals(2, p.getMatchCount());
426 }
427
300428 public void testIndexMatchWithPath1() throws Exception
301429 {
302430 FilteringParserDelegate p = new FilteringParserDelegate(JSON_F.createParser(SIMPLE),
303 new IndexMatchFilter(1), true, true);
431 new IndexMatchFilter(1), Inclusion.INCLUDE_ALL_AND_PATH, true);
304432 String result = readAndWrite(JSON_F, p);
305433 assertEquals(aposToQuotes("{'array':[2]}"), result);
306434 assertEquals(1, p.getMatchCount());
307435
308436 p = new FilteringParserDelegate(JSON_F.createParser(SIMPLE),
309 new IndexMatchFilter(0), true, true);
437 new IndexMatchFilter(0), Inclusion.INCLUDE_ALL_AND_PATH, true);
310438 result = readAndWrite(JSON_F, p);
311439 assertEquals(aposToQuotes("{'array':[1]}"), result);
312440 assertEquals(1, p.getMatchCount());
315443 public void testIndexMatchWithPath2() throws Exception
316444 {
317445 FilteringParserDelegate p = new FilteringParserDelegate(JSON_F.createParser(SIMPLE),
318 new IndexMatchFilter(0, 1), true, true);
446 new IndexMatchFilter(0, 1), Inclusion.INCLUDE_ALL_AND_PATH, true);
319447 assertEquals(aposToQuotes("{'array':[1,2]}"), readAndWrite(JSON_F, p));
320448 assertEquals(2, p.getMatchCount());
321449
322450 String JSON = aposToQuotes("{'a':123,'array':[1,2,3,4,5],'b':[1,2,3]}");
323451 p = new FilteringParserDelegate(JSON_F.createParser(JSON),
324 new IndexMatchFilter(1, 3), true, true);
452 new IndexMatchFilter(1, 3), Inclusion.INCLUDE_ALL_AND_PATH, true);
325453 assertEquals(aposToQuotes("{'array':[2,4],'b':[2]}"), readAndWrite(JSON_F, p));
326454 assertEquals(3, p.getMatchCount());
327455 }
331459 JsonParser p0 = JSON_F.createParser(SIMPLE);
332460 JsonParser p = new FilteringParserDelegate(p0,
333461 new NameMatchFilter("value"),
334 true, // includePath
462 Inclusion.INCLUDE_ALL_AND_PATH,
335463 false // multipleMatches
336464 );
337465
345473 JsonParser p0 = JSON_F.createParser(SIMPLE);
346474 JsonParser p = new FilteringParserDelegate(p0,
347475 new NameMatchFilter("value"),
348 true, // includePath
476 Inclusion.INCLUDE_ALL_AND_PATH,
349477 false // multipleMatches
350478 );
351479
352480 assertFalse(p.hasCurrentToken());
353481 assertNull(p.getCurrentToken());
354 assertEquals(JsonTokenId.ID_NO_TOKEN, p.getCurrentTokenId());
482 assertEquals(JsonTokenId.ID_NO_TOKEN, p.currentTokenId());
355483 assertFalse(p.isExpectedStartObjectToken());
356484 assertFalse(p.isExpectedStartArrayToken());
357485
363491 assertEquals(JsonToken.START_OBJECT, p.getCurrentToken());
364492 assertEquals(JsonToken.START_OBJECT, p.currentToken());
365493 assertEquals(JsonTokenId.ID_START_OBJECT, p.currentTokenId());
366 assertEquals(JsonTokenId.ID_START_OBJECT, p.getCurrentTokenId());
494 assertEquals(JsonTokenId.ID_START_OBJECT, p.currentTokenId());
367495 assertTrue(p.isExpectedStartObjectToken());
368496 assertFalse(p.isExpectedStartArrayToken());
369497
422550 JsonParser p0 = JSON_F.createParser(SIMPLE);
423551 JsonParser p = new FilteringParserDelegate(p0,
424552 new NameMatchFilter("value"),
425 true, // includePath
553 Inclusion.INCLUDE_ALL_AND_PATH,
426554 false // multipleMatches
427555 );
428556
22 import java.io.*;
33
44 import com.fasterxml.jackson.core.*;
5 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
56 import com.fasterxml.jackson.core.util.JsonGeneratorDelegate;
67
78 // for [core#609]
5657 }
5758
5859 // for [core#609]: will pass in 2.10 for some cases
59 @SuppressWarnings("resource")
6060 public void testIssue609() throws Exception
6161 {
6262 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
6363 JsonGenerator g = createGenerator(outputStream);
6464 g = new FilteringGeneratorDelegate(
65 g, NullExcludingTokenFilter.INSTANCE, true, true);
65 g, NullExcludingTokenFilter.INSTANCE, Inclusion.INCLUDE_ALL_AND_PATH, true);
6666 int maxStringLength = 10;
6767 g = new StringTruncatingGeneratorDelegate(
6868 g, maxStringLength);
7171 g.writeString("1234567890!");
7272 g.writeEndObject();
7373 g.close();
74 outputStream.close();
7475
7576 String json = outputStream.toString("US-ASCII");
7677 assertEquals("{\"message\":\"1234567890\"}", json);
22 import java.io.*;
33
44 import com.fasterxml.jackson.core.*;
5 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
56
67 @SuppressWarnings("resource")
78 public class JsonPointerGeneratorFilteringTest extends com.fasterxml.jackson.core.BaseTest
1213
1314 public void testSimplePropertyWithPath() throws Exception
1415 {
15 _assert(SIMPLE_INPUT, "/c", true, "{'c':{'d':{'a':true}}}");
16 _assert(SIMPLE_INPUT, "/c/d", true, "{'c':{'d':{'a':true}}}");
17 _assert(SIMPLE_INPUT, "/c/d/a", true, "{'c':{'d':{'a':true}}}");
16 _assert(SIMPLE_INPUT, "/c", Inclusion.INCLUDE_ALL_AND_PATH, "{'c':{'d':{'a':true}}}");
17 _assert(SIMPLE_INPUT, "/c/d", Inclusion.INCLUDE_ALL_AND_PATH, "{'c':{'d':{'a':true}}}");
18 _assert(SIMPLE_INPUT, "/c/d/a", Inclusion.INCLUDE_ALL_AND_PATH, "{'c':{'d':{'a':true}}}");
1819
19 _assert(SIMPLE_INPUT, "/c/d/a", true, "{'c':{'d':{'a':true}}}");
20 _assert(SIMPLE_INPUT, "/c/d/a", Inclusion.INCLUDE_ALL_AND_PATH, "{'c':{'d':{'a':true}}}");
2021
21 _assert(SIMPLE_INPUT, "/a", true, "{'a':1}");
22 _assert(SIMPLE_INPUT, "/d", true, "{'d':null}");
22 _assert(SIMPLE_INPUT, "/a", Inclusion.INCLUDE_ALL_AND_PATH, "{'a':1}");
23 _assert(SIMPLE_INPUT, "/d", Inclusion.INCLUDE_ALL_AND_PATH, "{'d':null}");
2324
2425 // and then non-match
25 _assert(SIMPLE_INPUT, "/x", true, "");
26 _assert(SIMPLE_INPUT, "/x", Inclusion.INCLUDE_ALL_AND_PATH, "");
2627 }
2728
2829 public void testSimplePropertyWithoutPath() throws Exception
2930 {
30 _assert(SIMPLE_INPUT, "/c", false, "{'d':{'a':true}}");
31 _assert(SIMPLE_INPUT, "/c/d", false, "{'a':true}");
32 _assert(SIMPLE_INPUT, "/c/d/a", false, "true");
31 _assert(SIMPLE_INPUT, "/c", Inclusion.ONLY_INCLUDE_ALL, "{'d':{'a':true}}");
32 _assert(SIMPLE_INPUT, "/c/d", Inclusion.ONLY_INCLUDE_ALL, "{'a':true}");
33 _assert(SIMPLE_INPUT, "/c/d/a", Inclusion.ONLY_INCLUDE_ALL, "true");
3334
34 _assert(SIMPLE_INPUT, "/a", false, "1");
35 _assert(SIMPLE_INPUT, "/d", false, "null");
35 _assert(SIMPLE_INPUT, "/a", Inclusion.ONLY_INCLUDE_ALL, "1");
36 _assert(SIMPLE_INPUT, "/d", Inclusion.ONLY_INCLUDE_ALL, "null");
3637
3738 // and then non-match
38 _assert(SIMPLE_INPUT, "/x", false, "");
39 _assert(SIMPLE_INPUT, "/x", Inclusion.ONLY_INCLUDE_ALL, "");
3940 }
4041
4142 public void testArrayElementWithPath() throws Exception
4243 {
43 _assert(SIMPLE_INPUT, "/b", true, "{'b':[1,2,3]}");
44 _assert(SIMPLE_INPUT, "/b/1", true, "{'b':[2]}");
45 _assert(SIMPLE_INPUT, "/b/2", true, "{'b':[3]}");
44 _assert(SIMPLE_INPUT, "/b", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':[1,2,3]}");
45 _assert(SIMPLE_INPUT, "/b/1", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':[2]}");
46 _assert(SIMPLE_INPUT, "/b/2", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':[3]}");
4647
4748 // and then non-match
48 _assert(SIMPLE_INPUT, "/b/8", true, "");
49 _assert(SIMPLE_INPUT, "/b/8", Inclusion.INCLUDE_ALL_AND_PATH, "");
4950 }
5051
5152 public void testArrayNestedWithPath() throws Exception
5253 {
53 _assert("{'a':[true,{'b':3,'d':2},false]}", "/a/1/b", true, "{'a':[{'b':3}]}");
54 _assert("[true,[1]]", "/0", true, "[true]");
55 _assert("[true,[1]]", "/1", true, "[[1]]");
56 _assert("[true,[1,2,[true],3],0]", "/0", true, "[true]");
57 _assert("[true,[1,2,[true],3],0]", "/1", true, "[[1,2,[true],3]]");
54 _assert("{'a':[true,{'b':3,'d':2},false]}", "/a/1/b", Inclusion.INCLUDE_ALL_AND_PATH, "{'a':[{'b':3}]}");
55 _assert("[true,[1]]", "/0", Inclusion.INCLUDE_ALL_AND_PATH, "[true]");
56 _assert("[true,[1]]", "/1", Inclusion.INCLUDE_ALL_AND_PATH, "[[1]]");
57 _assert("[true,[1,2,[true],3],0]", "/0", Inclusion.INCLUDE_ALL_AND_PATH, "[true]");
58 _assert("[true,[1,2,[true],3],0]", "/1", Inclusion.INCLUDE_ALL_AND_PATH, "[[1,2,[true],3]]");
5859
59 _assert("[true,[1,2,[true],3],0]", "/1/2", true, "[[[true]]]");
60 _assert("[true,[1,2,[true],3],0]", "/1/2/0", true, "[[[true]]]");
61 _assert("[true,[1,2,[true],3],0]", "/1/3/0", true, "");
60 _assert("[true,[1,2,[true],3],0]", "/1/2", Inclusion.INCLUDE_ALL_AND_PATH, "[[[true]]]");
61 _assert("[true,[1,2,[true],3],0]", "/1/2/0", Inclusion.INCLUDE_ALL_AND_PATH, "[[[true]]]");
62 _assert("[true,[1,2,[true],3],0]", "/1/3/0", Inclusion.INCLUDE_ALL_AND_PATH, "");
6263 }
6364
6465 public void testArrayNestedWithoutPath() throws Exception
6566 {
66 _assert("{'a':[true,{'b':3,'d':2},false]}", "/a/1/b", false, "3");
67 _assert("[true,[1,2,[true],3],0]", "/0", false, "true");
68 _assert("[true,[1,2,[true],3],0]", "/1", false,
67 _assert("{'a':[true,{'b':3,'d':2},false]}", "/a/1/b", Inclusion.ONLY_INCLUDE_ALL, "3");
68 _assert("[true,[1,2,[true],3],0]", "/0", Inclusion.ONLY_INCLUDE_ALL, "true");
69 _assert("[true,[1,2,[true],3],0]", "/1", Inclusion.ONLY_INCLUDE_ALL,
6970 "[1,2,[true],3]");
7071
71 _assert("[true,[1,2,[true],3],0]", "/1/2", false, "[true]");
72 _assert("[true,[1,2,[true],3],0]", "/1/2/0", false, "true");
73 _assert("[true,[1,2,[true],3],0]", "/1/3/0", false, "");
72 _assert("[true,[1,2,[true],3],0]", "/1/2", Inclusion.ONLY_INCLUDE_ALL, "[true]");
73 _assert("[true,[1,2,[true],3],0]", "/1/2/0", Inclusion.ONLY_INCLUDE_ALL, "true");
74 _assert("[true,[1,2,[true],3],0]", "/1/3/0", Inclusion.ONLY_INCLUDE_ALL, "");
7475 }
7576
7677 // final String SIMPLE_INPUT = aposToQuotes("{'a':1,'b':[1,2,3],'c':{'d':{'a':true}},'d':null}");
7778
7879 public void testArrayElementWithoutPath() throws Exception
7980 {
80 _assert(SIMPLE_INPUT, "/b", false, "[1,2,3]");
81 _assert(SIMPLE_INPUT, "/b/1", false, "2");
82 _assert(SIMPLE_INPUT, "/b/2", false, "3");
81 _assert(SIMPLE_INPUT, "/b", Inclusion.ONLY_INCLUDE_ALL, "[1,2,3]");
82 _assert(SIMPLE_INPUT, "/b/1", Inclusion.ONLY_INCLUDE_ALL, "2");
83 _assert(SIMPLE_INPUT, "/b/2", Inclusion.ONLY_INCLUDE_ALL, "3");
8384
84 _assert(SIMPLE_INPUT, "/b/8", false, "");
85 _assert(SIMPLE_INPUT, "/b/8", Inclusion.ONLY_INCLUDE_ALL, "");
8586
8687 // and then non-match
87 _assert(SIMPLE_INPUT, "/x", false, "");
88 _assert(SIMPLE_INPUT, "/x", Inclusion.ONLY_INCLUDE_ALL, "");
8889 }
8990
90 private void _assert(String input, String pathExpr, boolean includeParent, String exp)
91 private void _assert(String input, String pathExpr, Inclusion tokenFilterInclusion, String exp)
9192 throws Exception
9293 {
9394 StringWriter w = new StringWriter();
9596 JsonGenerator g0 = JSON_F.createGenerator(w);
9697 FilteringGeneratorDelegate g = new FilteringGeneratorDelegate(g0,
9798 new JsonPointerBasedFilter(pathExpr),
98 includeParent, false);
99 tokenFilterInclusion, false);
99100
100101 try {
101102 writeJsonDoc(JSON_F, input, g);
129130 JsonGenerator jg = JSON_F.createGenerator(output);
130131
131132 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(jg,
132 new JsonPointerBasedFilter("/noMatch"), true, true);
133 new JsonPointerBasedFilter("/noMatch"), Inclusion.INCLUDE_ALL_AND_PATH, true);
133134 final String[] stuff = new String[] { "foo", "bar" };
134135
135136 switch (mode) {
22 import java.io.StringWriter;
33
44 import com.fasterxml.jackson.core.*;
5 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
56
67 public class JsonPointerParserFilteringTest extends com.fasterxml.jackson.core.BaseTest
78 {
1314
1415 public void testSimplestWithPath() throws Exception
1516 {
16 _assert(SIMPLEST_INPUT, "/a", true, "{'a':1}");
17 _assert(SIMPLEST_INPUT, "/b", true, "{'b':2}");
18 _assert(SIMPLEST_INPUT, "/c", true, "{'c':3}");
19 _assert(SIMPLEST_INPUT, "/c/0", true, "");
20 _assert(SIMPLEST_INPUT, "/d", true, "");
17 _assert(SIMPLEST_INPUT, "/a", Inclusion.INCLUDE_ALL_AND_PATH, "{'a':1}");
18 _assert(SIMPLEST_INPUT, "/b", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':2}");
19 _assert(SIMPLEST_INPUT, "/c", Inclusion.INCLUDE_ALL_AND_PATH, "{'c':3}");
20 _assert(SIMPLEST_INPUT, "/c/0", Inclusion.INCLUDE_ALL_AND_PATH, "");
21 _assert(SIMPLEST_INPUT, "/d", Inclusion.INCLUDE_ALL_AND_PATH, "");
2122 }
2223
2324 public void testSimplestNoPath() throws Exception
2425 {
25 _assert(SIMPLEST_INPUT, "/a", false, "1");
26 _assert(SIMPLEST_INPUT, "/b", false, "2");
27 _assert(SIMPLEST_INPUT, "/b/2", false, "");
28 _assert(SIMPLEST_INPUT, "/c", false, "3");
29 _assert(SIMPLEST_INPUT, "/d", false, "");
26 _assert(SIMPLEST_INPUT, "/a", Inclusion.ONLY_INCLUDE_ALL, "1");
27 _assert(SIMPLEST_INPUT, "/b", Inclusion.ONLY_INCLUDE_ALL, "2");
28 _assert(SIMPLEST_INPUT, "/b/2", Inclusion.ONLY_INCLUDE_ALL, "");
29 _assert(SIMPLEST_INPUT, "/c", Inclusion.ONLY_INCLUDE_ALL, "3");
30 _assert(SIMPLEST_INPUT, "/d", Inclusion.ONLY_INCLUDE_ALL, "");
3031 }
3132
3233 public void testSimpleWithPath() throws Exception
3334 {
34 _assert(SIMPLE_INPUT, "/c", true, "{'c':{'d':{'a':true}}}");
35 _assert(SIMPLE_INPUT, "/c/d", true, "{'c':{'d':{'a':true}}}");
36 _assert(SIMPLE_INPUT, "/a", true, "{'a':1}");
37 _assert(SIMPLE_INPUT, "/b", true, "{'b':[1,2,3]}");
38 _assert(SIMPLE_INPUT, "/b/0", true, "{'b':[1]}");
39 _assert(SIMPLE_INPUT, "/b/1", true, "{'b':[2]}");
40 _assert(SIMPLE_INPUT, "/b/2", true, "{'b':[3]}");
41 _assert(SIMPLE_INPUT, "/b/3", true, "");
35 _assert(SIMPLE_INPUT, "/c", Inclusion.INCLUDE_ALL_AND_PATH, "{'c':{'d':{'a':true}}}");
36 _assert(SIMPLE_INPUT, "/c/d", Inclusion.INCLUDE_ALL_AND_PATH, "{'c':{'d':{'a':true}}}");
37 _assert(SIMPLE_INPUT, "/a", Inclusion.INCLUDE_ALL_AND_PATH, "{'a':1}");
38 _assert(SIMPLE_INPUT, "/b", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':[1,2,3]}");
39 _assert(SIMPLE_INPUT, "/b/0", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':[1]}");
40 _assert(SIMPLE_INPUT, "/b/1", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':[2]}");
41 _assert(SIMPLE_INPUT, "/b/2", Inclusion.INCLUDE_ALL_AND_PATH, "{'b':[3]}");
42 _assert(SIMPLE_INPUT, "/b/3", Inclusion.INCLUDE_ALL_AND_PATH, "");
4243 }
4344
4445 public void testSimpleNoPath() throws Exception
4546 {
46 _assert(SIMPLE_INPUT, "/c", false, "{'d':{'a':true}}");
47 _assert(SIMPLE_INPUT, "/c", Inclusion.ONLY_INCLUDE_ALL, "{'d':{'a':true}}");
4748
48 _assert(SIMPLE_INPUT, "/c/d", false, "{'a':true}");
49 _assert(SIMPLE_INPUT, "/a", false, "1");
50 _assert(SIMPLE_INPUT, "/b", false, "[1,2,3]");
51 _assert(SIMPLE_INPUT, "/b/0", false, "1");
52 _assert(SIMPLE_INPUT, "/b/1", false, "2");
53 _assert(SIMPLE_INPUT, "/b/2", false, "3");
54 _assert(SIMPLE_INPUT, "/b/3", false, "");
49 _assert(SIMPLE_INPUT, "/c/d", Inclusion.ONLY_INCLUDE_ALL, "{'a':true}");
50 _assert(SIMPLE_INPUT, "/a", Inclusion.ONLY_INCLUDE_ALL, "1");
51 _assert(SIMPLE_INPUT, "/b", Inclusion.ONLY_INCLUDE_ALL, "[1,2,3]");
52 _assert(SIMPLE_INPUT, "/b/0", Inclusion.ONLY_INCLUDE_ALL, "1");
53 _assert(SIMPLE_INPUT, "/b/1", Inclusion.ONLY_INCLUDE_ALL, "2");
54 _assert(SIMPLE_INPUT, "/b/2", Inclusion.ONLY_INCLUDE_ALL, "3");
55 _assert(SIMPLE_INPUT, "/b/3", Inclusion.ONLY_INCLUDE_ALL, "");
5556 }
5657
5758 @SuppressWarnings("resource")
58 void _assert(String input, String pathExpr, boolean includeParent, String exp)
59 void _assert(String input, String pathExpr, TokenFilter.Inclusion inclusion, String exp)
5960 throws Exception
6061 {
6162 JsonParser p0 = JSON_F.createParser(input);
6263 FilteringParserDelegate p = new FilteringParserDelegate(p0,
6364 new JsonPointerBasedFilter(pathExpr),
64 includeParent, false);
65 inclusion, false);
6566 StringWriter w = new StringWriter();
6667 JsonGenerator g = JSON_F.createGenerator(w);
6768
9494 {
9595 JsonFactory f = new JsonFactory();
9696 // by default should output numbers as-is:
97 assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, false));
98 assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3]", _writeNumbers(f, true));
97 assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3,null,null,null]", _writeNumbers(f, false));
98 assertEquals("[1,2,3,1.25,2.25,3001,0.5,-1,12.3,null,null,null]", _writeNumbers(f, true));
9999
100100 // but if overridden, quotes as Strings
101101 f = JsonFactory.builder()
102102 .enable(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS)
103103 .build();
104 assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
104 assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\",null,null,null]",
105105 _writeNumbers(f, false));
106 assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\"]",
106 assertEquals("[\"1\",\"2\",\"3\",\"1.25\",\"2.25\",\"3001\",\"0.5\",\"-1\",\"12.3\",null,null,null]",
107107 _writeNumbers(f, true));
108108
109109
227227 g.writeNumber(BigDecimal.valueOf(0.5));
228228 g.writeNumber("-1");
229229 g.writeNumber(new char[]{'1', '2', '.', '3', '-'}, 0, 4);
230 g.writeNumber((String) null);
231 g.writeNumber((BigDecimal) null);
232 g.writeNumber((BigInteger) null);
230233 g.writeEndArray();
231234 g.close();
232235
99 public class JsonFactoryTest
1010 extends com.fasterxml.jackson.core.BaseTest
1111 {
12 private static class BogusCodec extends ObjectCodec {
12 static class BogusCodec extends ObjectCodec {
1313 @Override
1414 public Version version() { return null; }
1515
1111
1212 public class AsyncConcurrencyTest extends AsyncTestBase
1313 {
14 private final static JsonFactory JSON_F = new JsonFactory();
14 final static JsonFactory JSON_F = new JsonFactory();
1515 static {
1616 // To make it pass, try:
1717 // JSON_F.disable(JsonFactory.Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING);
2121 private final static String TEXT2 = "Some longer text";
2222 private final static String TEXT3 = "and yet more";
2323 private final static String TEXT4 = "... Longest yet although not superbly long still (see 'apos'?)";
24
25 private final static byte[] JSON_DOC = utf8Bytes(String.format(
24
25 final static byte[] JSON_DOC = utf8Bytes(String.format(
2626 "[\"%s\", \"%s\",\n\"%s\",\"%s\" ]", TEXT1, TEXT2, TEXT3, TEXT4));
2727
2828 static class WorkUnit {
29
3029 private int stage = 0;
3130
3231 private AsyncReaderWrapper parser;
55 import com.fasterxml.jackson.core.async.AsyncTestBase;
66 import com.fasterxml.jackson.core.filter.FilteringParserDelegate;
77 import com.fasterxml.jackson.core.filter.TokenFilter;
8 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
89
910 // [core#462], [core#463]
1011 public class AsyncTokenFilterTest extends AsyncTestBase
3233 {
3334 NonBlockingJsonParser nonBlockingParser = (NonBlockingJsonParser) JSON_F.createNonBlockingByteArrayParser();
3435 FilteringParserDelegate filteredParser = new FilteringParserDelegate(nonBlockingParser,
35 TOKEN_FILTER, true, true);
36 TOKEN_FILTER, Inclusion.INCLUDE_ALL_AND_PATH, true);
3637 nonBlockingParser.feedInput(INPUT_BYTES, 0, INPUT_BYTES.length);
3738 int expectedIdx = 0;
3839 while (expectedIdx < EXPECTED_TOKENS.length) {
5354 NonBlockingJsonParser nbParser = (NonBlockingJsonParser) JSON_F.createNonBlockingByteArrayParser();
5455 @SuppressWarnings("resource")
5556 FilteringParserDelegate filteredParser = new FilteringParserDelegate(nbParser,
56 TOKEN_FILTER, true, true);
57 TOKEN_FILTER, Inclusion.INCLUDE_ALL_AND_PATH, true);
5758 nbParser.feedInput(INPUT_BYTES, 0, 5);
5859
5960 assertToken(JsonToken.START_OBJECT, nbParser.nextToken());
8686 assertEquals(expected, actual);
8787 }
8888
89 private static class LocData
89 static class LocData
9090 {
9191 long lineNumber;
9292 long columnNumber;
8686 assertEquals(expected, actual);
8787 }
8888
89 private static class LocData
89 static class LocData
9090 {
9191 long lineNumber;
9292 long columnNumber;
6868 assertToken(JsonToken.START_ARRAY, p.nextToken());
6969 assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
7070 assertEquals(JsonParser.NumberType.INT, p.getNumberType());
71 assertTrue(p.isExpectedNumberIntToken());
7172 assertEquals(""+EXP_I, p.getText());
7273
7374 if (((short) EXP_I) == EXP_I) {
107108 DOC = String.valueOf(EXP_I);
108109 p = createParser(mode, DOC + " "); // DataInput requires separator
109110 assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
111 assertTrue(p.isExpectedNumberIntToken());
110112 assertEquals(DOC, p.getText());
111113
112114 int i = p.getIntValue();
121123 DOC = String.valueOf(EXP_I)+".0";
122124 p = createParser(mode, DOC + " ");
123125 assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
126 assertFalse(p.isExpectedNumberIntToken());
124127 assertEquals(EXP_I, p.getValueAsInt());
125128 p.close();
126129
264267 public void testLongBoundsChecks() throws Exception
265268 {
266269 String minLong = String.valueOf(Long.MIN_VALUE).substring(1);
270 String belowMinLong = BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)
271 .toString().substring(1);
267272 String maxLong = String.valueOf(Long.MAX_VALUE);
273 String aboveMaxLong = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE).toString();
268274 final String VALUE_491 = "1323372036854775807"; // is within range (JACKSON-491)
269275 final String OVERFLOW = "9999999999999999999"; // and this one is clearly out
270276
272278 assertTrue(NumberInput.inLongRange(maxLong, false));
273279 assertTrue(NumberInput.inLongRange(VALUE_491, true));
274280 assertTrue(NumberInput.inLongRange(VALUE_491, false));
281
275282 assertFalse(NumberInput.inLongRange(OVERFLOW, false));
276283 assertFalse(NumberInput.inLongRange(OVERFLOW, true));
284 assertFalse(NumberInput.inLongRange(belowMinLong, true));
285 assertFalse(NumberInput.inLongRange(aboveMaxLong, false));
277286
278287 char[] cbuf = minLong.toCharArray();
279288 assertTrue(NumberInput.inLongRange(cbuf, 0, cbuf.length, true));
282291 cbuf = VALUE_491.toCharArray();
283292 assertTrue(NumberInput.inLongRange(cbuf, 0, cbuf.length, true));
284293 assertTrue(NumberInput.inLongRange(cbuf, 0, cbuf.length, false));
294
285295 cbuf = OVERFLOW.toCharArray();
286296 assertFalse(NumberInput.inLongRange(cbuf, 0, cbuf.length, true));
297 assertFalse(NumberInput.inLongRange(cbuf, 0, cbuf.length, false));
298
299 cbuf = aboveMaxLong.toCharArray();
287300 assertFalse(NumberInput.inLongRange(cbuf, 0, cbuf.length, false));
288301 }
289302
00 package com.fasterxml.jackson.core.read;
1
2 import java.io.*;
31
42 import com.fasterxml.jackson.core.*;
53 import com.fasterxml.jackson.core.json.JsonReadFeature;
119 public class ParserFeaturesTest
1210 extends com.fasterxml.jackson.core.BaseTest
1311 {
12 private final JsonFactory JSON_F = sharedStreamFactory();
13
1414 public void testDefaultSettings() throws Exception
1515 {
16 JsonFactory f = new JsonFactory();
17 assertTrue(f.isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE));
16 assertTrue(JSON_F.isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE));
1817
19 JsonParser p = f.createParser(new StringReader("{}"));
20 _testDefaultSettings(p);
21 p.close();
22 p = f.createParser(new ByteArrayInputStream("{}".getBytes("UTF-8")));
23 _testDefaultSettings(p);
24 p.close();
18 _testDefaultSettings(createParser(JSON_F, MODE_INPUT_STREAM, "{}"));
19 _testDefaultSettings(createParser(JSON_F, MODE_READER, "{}"));
20 _testDefaultSettings(createParser(JSON_F, MODE_DATA_INPUT, "{}"));
2521 }
2622
2723 @SuppressWarnings("deprecation")
2824 public void testDeprecatedDefaultSettings() throws Exception
2925 {
30 JsonFactory f = sharedStreamFactory();
31 assertFalse(f.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
32 assertFalse(f.isEnabled(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS));
33 assertFalse(f.isEnabled(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES));
34 assertFalse(f.isEnabled(JsonParser.Feature.ALLOW_SINGLE_QUOTES));
26 assertFalse(JSON_F.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
27 assertFalse(JSON_F.isEnabled(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS));
28 assertFalse(JSON_F.isEnabled(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES));
29 assertFalse(JSON_F.isEnabled(JsonParser.Feature.ALLOW_SINGLE_QUOTES));
3530 }
3631
3732 public void testQuotesRequired() throws Exception
3934 _testQuotesRequired(false);
4035 _testQuotesRequired(true);
4136 }
42
43 // // Tests for [JACKSON-208], unquoted tabs:
4437
4538 public void testTabsDefault() throws Exception
4639 {
6053 /****************************************************************
6154 */
6255
63 private void _testDefaultSettings(JsonParser p) {
56 private void _testDefaultSettings(JsonParser p) throws Exception {
6457 assertFalse(p.canReadObjectId());
6558 assertFalse(p.canReadTypeId());
59
60 // [core#619]:
61 assertFalse(p.getReadCapabilities().isEnabled(StreamReadCapability.DUPLICATE_PROPERTIES));
62 assertFalse(p.getReadCapabilities().isEnabled(StreamReadCapability.SCALARS_AS_OBJECTS));
63 assertFalse(p.getReadCapabilities().isEnabled(StreamReadCapability.UNTYPED_SCALARS));
64
65 p.close();
6666 }
67
67
6868 private void _testQuotesRequired(boolean useStream) throws Exception
6969 {
7070 final String JSON = "{ test : 3 }";
2828 DefaultIndenter defaultIndenterTwo = defaultIndenter.withIndent("9Qh/6,~n");
2929 DefaultIndenter defaultIndenterThree = defaultIndenterTwo.withIndent("9Qh/6,~n");
3030
31 assertEquals("\n", defaultIndenterThree.getEol());
31 assertEquals(System.lineSeparator(), defaultIndenterThree.getEol());
3232 assertNotSame(defaultIndenterThree, defaultIndenter);
3333 assertSame(defaultIndenterThree, defaultIndenterTwo);
3434 }
99 * @see RequestPayload
1010 **/
1111 public class RequestPayloadTest {
12 @SuppressWarnings("unused")
1312 @Test(expected = IllegalArgumentException.class)
1413 public void testFailsToCreateTakingCharSequenceThrowsIllegalArgumentExceptionOne() {
1514 new RequestPayload(null);
1615 }
1716
18 @SuppressWarnings("unused")
1917 @Test(expected = IllegalArgumentException.class)
2018 public void testFailsToCreateTakingCharSequenceThrowsIllegalArgumentExceptionTwo() {
2119 new RequestPayload(null, "UTF-8");
230230 assertFalse(del.hasTokenId(JsonTokenId.ID_START_OBJECT));
231231 assertTrue(del.isExpectedStartArrayToken());
232232 assertFalse(del.isExpectedStartObjectToken());
233 assertFalse(del.isExpectedNumberIntToken());
233234 assertEquals("[", del.getText());
234235 assertNotNull(del.getParsingContext());
235236 assertSame(parser.getParsingContext(), del.getParsingContext());
250251 assertEquals((short)1, del.getShortValue());
251252 assertEquals(1f, del.getFloatValue());
252253 assertFalse(del.isNaN());
254 assertTrue(del.isExpectedNumberIntToken());
253255 assertEquals(NumberType.INT, del.getNumberType());
254256 assertEquals(Integer.valueOf(1), del.getNumberValue());
255257 assertNull(del.getEmbeddedObject());
22 import com.fasterxml.jackson.core.*;
33 import com.fasterxml.jackson.core.filter.FilteringGeneratorDelegate;
44 import com.fasterxml.jackson.core.filter.JsonPointerBasedFilter;
5 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
56 import com.fasterxml.jackson.core.io.IOContext;
67 import com.fasterxml.jackson.core.json.UTF8JsonGenerator;
78 import com.fasterxml.jackson.core.util.BufferRecycler;
7374
7475 FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(g,
7576 new JsonPointerBasedFilter("/escapes"),
76 true, // includePath
77 Inclusion.INCLUDE_ALL_AND_PATH,
7778 false // multipleMatches
7879 );
7980
0 package com.fasterxml.jackson.failing.filter;
1
2 import com.fasterxml.jackson.core.BaseTest;
3 import com.fasterxml.jackson.core.JsonFactory;
4 import com.fasterxml.jackson.core.JsonParser;
5 import com.fasterxml.jackson.core.filter.FilteringParserDelegate;
6 import com.fasterxml.jackson.core.filter.TokenFilter;
7 import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
8
9 public class BasicParserFilteringTest extends BaseTest {
10
11 private final JsonFactory JSON_F = new JsonFactory();
12
13 static class NoArraysFilter extends TokenFilter
14 {
15 @Override
16 public TokenFilter filterStartArray() {
17 return null;
18 }
19 }
20
21 // for [core#649]
22 public void testValueOmitsFieldName1() throws Exception
23 {
24 String jsonString = aposToQuotes("{'a':123,'array':[1,2]}");
25 JsonParser p0 = JSON_F.createParser(jsonString);
26 FilteringParserDelegate p = new FilteringParserDelegate(p0,
27 new NoArraysFilter(),
28 Inclusion.INCLUDE_NON_NULL,
29 true // multipleMatches
30 );
31 String result = readAndWrite(JSON_F, p);
32 assertEquals(aposToQuotes("{'a':123}"), result);
33 assertEquals(0, p.getMatchCount());
34 }
35 }