blob: 51f2783c2d47700d984c7983d15411c004b7488a [file] [log] [blame]
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001/* Jackson JSON-processor.
2 *
3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
4 *
5 * Licensed under the License specified in file LICENSE, included with
6 * the source code and binary code bundles.
7 * You may not use this file except in compliance with the License.
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package com.fasterxml.jackson.core;
16
17import java.io.*;
18import java.lang.ref.SoftReference;
19import java.net.URL;
20
21import com.fasterxml.jackson.core.format.InputAccessor;
22import com.fasterxml.jackson.core.format.MatchStrength;
23import com.fasterxml.jackson.core.io.*;
Tatu07351902012-01-19 13:12:13 -080024import com.fasterxml.jackson.core.json.*;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080025import com.fasterxml.jackson.core.sym.BytesToNameCanonicalizer;
26import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
27import com.fasterxml.jackson.core.util.BufferRecycler;
Tatu Salorantae6dfc692012-09-28 15:34:05 -070028import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080029
30/**
31 * The main factory class of Jackson package, used to configure and
32 * construct reader (aka parser, {@link JsonParser})
33 * and writer (aka generator, {@link JsonGenerator})
34 * instances.
35 *<p>
36 * Factory instances are thread-safe and reusable after configuration
37 * (if any). Typically applications and services use only a single
38 * globally shared factory instance, unless they need differently
39 * configured factories. Factory reuse is important if efficiency matters;
40 * most recycling of expensive construct is done on per-factory basis.
41 *<p>
42 * Creation of a factory instance is a light-weight operation,
43 * and since there is no need for pluggable alternative implementations
44 * (as there is no "standard" JSON processor API to implement),
45 * the default constructor is used for constructing factory
46 * instances.
47 *
48 * @author Tatu Saloranta
49 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -070050public class JsonFactory
51 implements Versioned,
52 java.io.Serializable // since 2.1 (for Android, mostly)
Tatu Salorantaf15531c2011-12-22 23:00:40 -080053{
54 /**
Tatu Saloranta95f76a42012-10-05 13:04:23 -070055 * Computed for Jackson 2.1.0 release
56 */
Tatu Saloranta101820d2012-10-05 13:42:49 -070057 private static final long serialVersionUID = 8726401676402117450L;
Tatu Saloranta95f76a42012-10-05 13:04:23 -070058
59 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -080060 * Name used to identify JSON format
61 * (and returned by {@link #getFormatName()}
62 */
63 public final static String FORMAT_NAME_JSON = "JSON";
64
65 /**
Tatu07351902012-01-19 13:12:13 -080066 * Bitfield (set of flags) of all factory features that are enabled by default.
67 */
68 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
69
70 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -080071 * Bitfield (set of flags) of all parser features that are enabled
72 * by default.
73 */
Tatu07351902012-01-19 13:12:13 -080074 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
75
Tatu Salorantaf15531c2011-12-22 23:00:40 -080076 /**
77 * Bitfield (set of flags) of all generator features that are enabled
78 * by default.
79 */
Tatu07351902012-01-19 13:12:13 -080080 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
Tatu Salorantaf15531c2011-12-22 23:00:40 -080081
Tatu Salorantae6dfc692012-09-28 15:34:05 -070082 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
83
Tatu07351902012-01-19 13:12:13 -080084 /**
85 * Enumeration that defines all on/off features that can only be
86 * changed for {@link JsonFactory}.
87 */
88 public enum Feature {
89
90 // // // Symbol handling (interning etc)
91
92 /**
93 * Feature that determines whether JSON object field names are
94 * to be canonicalized using {@link String#intern} or not:
95 * if enabled, all field names will be intern()ed (and caller
96 * can count on this being true for all such names); if disabled,
97 * no intern()ing is done. There may still be basic
98 * canonicalization (that is, same String will be used to represent
99 * all identical object property names for a single document).
100 *<p>
101 * Note: this setting only has effect if
102 * {@link #CANONICALIZE_FIELD_NAMES} is true -- otherwise no
103 * canonicalization of any sort is done.
104 *<p>
105 * This setting is enabled by default.
106 */
107 INTERN_FIELD_NAMES(true),
108
109 /**
110 * Feature that determines whether JSON object field names are
111 * to be canonicalized (details of how canonicalization is done
112 * then further specified by
113 * {@link #INTERN_FIELD_NAMES}).
114 *<p>
115 * This setting is enabled by default.
116 */
117 CANONICALIZE_FIELD_NAMES(true)
118
119 ;
120
121 /**
122 * Whether feature is enabled or disabled by default.
123 */
124 private final boolean _defaultState;
125
126 /**
127 * Method that calculates bit set (flags) of all features that
128 * are enabled by default.
129 */
130 public static int collectDefaults()
131 {
132 int flags = 0;
133 for (Feature f : values()) {
134 if (f.enabledByDefault()) {
135 flags |= f.getMask();
136 }
137 }
138 return flags;
139 }
140
141 private Feature(boolean defaultState)
142 {
143 _defaultState = defaultState;
144 }
145
146 public boolean enabledByDefault() { return _defaultState; }
147
148 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
149
150 public int getMask() { return (1 << ordinal()); }
151 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800152 /*
153 /**********************************************************
154 /* Buffer, symbol table management
155 /**********************************************************
156 */
157
158 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700159 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800160 * to a {@link BufferRecycler} used to provide a low-cost
161 * buffer recycling between reader and writer instances.
162 */
163 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
164 = new ThreadLocal<SoftReference<BufferRecycler>>();
165
166 /**
167 * Each factory comes equipped with a shared root symbol table.
168 * It should not be linked back to the original blueprint, to
169 * avoid contents from leaking between factories.
170 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700171 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800172
173 /**
174 * Alternative to the basic symbol table, some stream-based
175 * parsers use different name canonicalization method.
176 *<p>
177 * TODO: should clean up this; looks messy having 2 alternatives
178 * with not very clear differences.
179 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700180 protected final transient BytesToNameCanonicalizer _rootByteSymbols = BytesToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800181
182 /*
183 /**********************************************************
184 /* Configuration
185 /**********************************************************
186 */
187
188 /**
189 * Object that implements conversion functionality between
190 * Java objects and JSON content. For base JsonFactory implementation
191 * usually not set by default, but can be explicitly set.
192 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
193 * usually provide an implementation.
194 */
195 protected ObjectCodec _objectCodec;
196
197 /**
Tatu07351902012-01-19 13:12:13 -0800198 * Currently enabled factory features.
199 */
200 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
201
202 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800203 * Currently enabled parser features.
204 */
205 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
206
207 /**
208 * Currently enabled generator features.
209 */
210 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
211
212 /**
213 * Definition of custom character escapes to use for generators created
214 * by this factory, if any. If null, standard data format specific
215 * escapes are used.
216 */
217 protected CharacterEscapes _characterEscapes;
218
219 /**
220 * Optional helper object that may decorate input sources, to do
221 * additional processing on input during parsing.
222 */
223 protected InputDecorator _inputDecorator;
224
225 /**
226 * Optional helper object that may decorate output object, to do
227 * additional processing on output during content generation.
228 */
229 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700230
231 /**
232 * Separator used between root-level values, if any; null indicates
233 * "do not add separator".
234 * Default separator is a single space character.
235 *
236 * @since 2.1
237 */
238 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800239
240 /*
241 /**********************************************************
242 /* Construction
243 /**********************************************************
244 */
245
246 /**
247 * Default constructor used to create factory instances.
248 * Creation of a factory instance is a light-weight operation,
249 * but it is still a good idea to reuse limited number of
250 * factory instances (and quite often just a single instance):
251 * factories are used as context for storing some reused
252 * processing objects (such as symbol tables parsers use)
253 * and this reuse only works within context of a single
254 * factory instance.
255 */
256 public JsonFactory() { this(null); }
257
258 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
259
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700260 /**
261 * Method for constructing a new {@link JsonFactory} that has
262 * the same settings as this instance, but is otherwise
263 * independent (i.e. nothing is actually shared, symbol tables
264 * are separate).
265 * Note that {@link ObjectCodec} reference is not copied but is
266 * set to null; caller typically needs to set it after calling
267 * this method.
268 *
269 * @since 2.1
270 */
271 public JsonFactory copy()
272 {
273 _checkInvalidCopy(JsonFactory.class);
274 return new JsonFactory(null);
275 }
276
277 /**
278 * @since 2.1
279 * @param exp
280 */
281 protected void _checkInvalidCopy(Class<?> exp)
282 {
283 if (getClass() != exp) {
284 throw new IllegalStateException("Failed copy(): "+getClass().getName()
285 +" (version: "+version()+") does not override copy(); it has to");
286 }
287 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700288
289 /*
290 /**********************************************************
291 /* Serializable overrides
292 /**********************************************************
293 */
294
295 /**
296 * Method that we need to override to actually make restoration go
297 * through constructors etc.
298 * Also: must be overridden by sub-classes as well.
299 */
300 protected Object readResolve() {
301 return new JsonFactory(_objectCodec);
302 }
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700303
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800304 /*
305 /**********************************************************
306 /* Format detection functionality (since 1.8)
307 /**********************************************************
308 */
309
310 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700311 * Method that can be used to quickly check whether given schema
312 * is something that parsers and/or generators constructed by this
313 * factory could use. Note that this means possible use, at the level
314 * of data format (i.e. schema is for same data format as parsers and
315 * generators this factory constructs); individual schema instances
316 * may have further usage restrictions.
317 *
318 * @since 2.1
319 */
320 public boolean canUseSchema(FormatSchema schema) {
321 String ourFormat = getFormatName();
322 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
323 }
324
325 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800326 * Method that returns short textual id identifying format
327 * this factory supports.
328 *<p>
329 * Note: sub-classes should override this method; default
330 * implementation will return null for all sub-classes
331 */
332 public String getFormatName()
333 {
334 /* Somewhat nasty check: since we can't make this abstract
335 * (due to backwards compatibility concerns), need to prevent
336 * format name "leakage"
337 */
338 if (getClass() == JsonFactory.class) {
339 return FORMAT_NAME_JSON;
340 }
341 return null;
342 }
343
344 public MatchStrength hasFormat(InputAccessor acc) throws IOException
345 {
346 // since we can't keep this abstract, only implement for "vanilla" instance
347 if (getClass() == JsonFactory.class) {
348 return hasJSONFormat(acc);
349 }
350 return null;
351 }
352
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700353 /**
354 * Method that can be called to determine if a custom
355 * {@link ObjectCodec} is needed for binding data parsed
356 * using {@link JsonParser} constructed by this factory
357 * (which typically also implies the same for serialization
358 * with {@link JsonGenerator}).
359 *
360 * @return True if custom codec is needed with parsers and
361 * generators created by this factory; false if a general
362 * {@link ObjectCodec} is enough
363 *
364 * @since 2.1
365 */
366 public boolean requiresCustomCodec() {
367 return false;
368 }
369
370 /**
371 * Helper method that can be called to determine if content accessed
372 * using given accessor seems to be JSON content.
373 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800374 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
375 {
376 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700377 }
378
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800379 /*
380 /**********************************************************
381 /* Versioned
382 /**********************************************************
383 */
384
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800385 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800386 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800387 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800388 }
389
390 /*
391 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800392 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800393 /**********************************************************
394 */
395
396 /**
397 * Method for enabling or disabling specified parser feature
398 * (check {@link JsonParser.Feature} for list of features)
399 */
Tatu07351902012-01-19 13:12:13 -0800400 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
401 return state ? enable(f) : disable(f);
402 }
403
404 /**
405 * Method for enabling specified parser feature
406 * (check {@link JsonFactory.Feature} for list of features)
407 */
408 public JsonFactory enable(JsonFactory.Feature f) {
409 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800410 return this;
411 }
412
413 /**
Tatu07351902012-01-19 13:12:13 -0800414 * Method for disabling specified parser features
415 * (check {@link JsonFactory.Feature} for list of features)
416 */
417 public JsonFactory disable(JsonFactory.Feature f) {
418 _factoryFeatures &= ~f.getMask();
419 return this;
420 }
421
422 /**
423 * Checked whether specified parser feature is enabled.
424 */
425 public final boolean isEnabled(JsonFactory.Feature f) {
426 return (_factoryFeatures & f.getMask()) != 0;
427 }
428
429 /*
430 /**********************************************************
431 /* Configuration, parser configuration
432 /**********************************************************
433 */
434
435 /**
436 * Method for enabling or disabling specified parser feature
437 * (check {@link JsonParser.Feature} for list of features)
438 */
439 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
440 return state ? enable(f) : disable(f);
441 }
442
443 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800444 * Method for enabling specified parser feature
445 * (check {@link JsonParser.Feature} for list of features)
446 */
447 public JsonFactory enable(JsonParser.Feature f) {
448 _parserFeatures |= f.getMask();
449 return this;
450 }
451
452 /**
453 * Method for disabling specified parser features
454 * (check {@link JsonParser.Feature} for list of features)
455 */
456 public JsonFactory disable(JsonParser.Feature f) {
457 _parserFeatures &= ~f.getMask();
458 return this;
459 }
460
461 /**
462 * Checked whether specified parser feature is enabled.
463 */
464 public final boolean isEnabled(JsonParser.Feature f) {
465 return (_parserFeatures & f.getMask()) != 0;
466 }
467
468 /**
469 * Method for getting currently configured input decorator (if any;
470 * there is no default decorator).
471 */
472 public InputDecorator getInputDecorator() {
473 return _inputDecorator;
474 }
475
476 /**
477 * Method for overriding currently configured input decorator
478 */
479 public JsonFactory setInputDecorator(InputDecorator d) {
480 _inputDecorator = d;
481 return this;
482 }
483
484 /*
485 /**********************************************************
486 /* Configuration, generator settings
487 /**********************************************************
488 */
489
490 /**
491 * Method for enabling or disabling specified generator feature
492 * (check {@link JsonGenerator.Feature} for list of features)
493 */
494 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800495 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800496 }
497
498
499 /**
500 * Method for enabling specified generator features
501 * (check {@link JsonGenerator.Feature} for list of features)
502 */
503 public JsonFactory enable(JsonGenerator.Feature f) {
504 _generatorFeatures |= f.getMask();
505 return this;
506 }
507
508 /**
509 * Method for disabling specified generator feature
510 * (check {@link JsonGenerator.Feature} for list of features)
511 */
512 public JsonFactory disable(JsonGenerator.Feature f) {
513 _generatorFeatures &= ~f.getMask();
514 return this;
515 }
516
517 /**
518 * Check whether specified generator feature is enabled.
519 */
520 public final boolean isEnabled(JsonGenerator.Feature f) {
521 return (_generatorFeatures & f.getMask()) != 0;
522 }
523
524 /**
525 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
526 * it creates.
527 */
528 public CharacterEscapes getCharacterEscapes() {
529 return _characterEscapes;
530 }
531
532 /**
533 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
534 * it creates.
535 */
536 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
537 _characterEscapes = esc;
538 return this;
539 }
540
541 /**
542 * Method for getting currently configured output decorator (if any;
543 * there is no default decorator).
544 */
545 public OutputDecorator getOutputDecorator() {
546 return _outputDecorator;
547 }
548
549 /**
550 * Method for overriding currently configured output decorator
551 */
552 public JsonFactory setOutputDecorator(OutputDecorator d) {
553 _outputDecorator = d;
554 return this;
555 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700556
557 /**
558 * Method that allows overriding String used for separating root-level
559 * JSON values (default is single space character)
560 *
561 * @param sep Separator to use, if any; null means that no separator is
562 * automatically added
563 *
564 * @since 2.1
565 */
566 public JsonFactory setRootValueSeparator(String sep) {
567 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
568 return this;
569 }
570
571 /**
572 * @since 2.1
573 */
574 public String getRootValueSeparator() {
575 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
576 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800577
578 /*
579 /**********************************************************
580 /* Configuration, other
581 /**********************************************************
582 */
583
584 /**
585 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800586 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
587 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800588 * it constructs). This is needed to use data-binding methods
589 * of {@link JsonParser} and {@link JsonGenerator} instances.
590 */
591 public JsonFactory setCodec(ObjectCodec oc) {
592 _objectCodec = oc;
593 return this;
594 }
595
596 public ObjectCodec getCodec() { return _objectCodec; }
597
598 /*
599 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700600 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800601 /**********************************************************
602 */
603
604 /**
605 * Method for constructing JSON parser instance to parse
606 * contents of specified file. Encoding is auto-detected
607 * from contents according to JSON specification recommended
608 * mechanism.
609 *<p>
610 * Underlying input stream (needed for reading contents)
611 * will be <b>owned</b> (and managed, i.e. closed as need be) by
612 * the parser, since caller has no access to it.
613 *
614 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700615 *
616 * @since 2.1
617 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800618 @SuppressWarnings("resource")
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700619 public JsonParser createParser(File f)
620 throws IOException, JsonParseException
621 {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800622 // true, since we create InputStream from File
623 IOContext ctxt = _createContext(f, true);
624 InputStream in = new FileInputStream(f);
625 // [JACKSON-512]: allow wrapping with InputDecorator
626 if (_inputDecorator != null) {
627 in = _inputDecorator.decorate(ctxt, in);
628 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700629 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800630 }
631
632 /**
633 * Method for constructing JSON parser instance to parse
634 * contents of resource reference by given URL.
635 * Encoding is auto-detected
636 * from contents according to JSON specification recommended
637 * mechanism.
638 *<p>
639 * Underlying input stream (needed for reading contents)
640 * will be <b>owned</b> (and managed, i.e. closed as need be) by
641 * the parser, since caller has no access to it.
642 *
643 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800644 *
645 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800646 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800647 public JsonParser createParser(URL url)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800648 throws IOException, JsonParseException
649 {
650 // true, since we create InputStream from URL
651 IOContext ctxt = _createContext(url, true);
652 InputStream in = _optimizedStreamFromURL(url);
653 // [JACKSON-512]: allow wrapping with InputDecorator
654 if (_inputDecorator != null) {
655 in = _inputDecorator.decorate(ctxt, in);
656 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700657 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800658 }
659
660 /**
661 * Method for constructing JSON parser instance to parse
662 * the contents accessed via specified input stream.
663 *<p>
664 * The input stream will <b>not be owned</b> by
665 * the parser, it will still be managed (i.e. closed if
666 * end-of-stream is reacher, or parser close method called)
667 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
668 * is enabled.
669 *<p>
670 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700671 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800672 *
673 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800674 *
675 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800676 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800677 public JsonParser createParser(InputStream in)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800678 throws IOException, JsonParseException
679 {
680 IOContext ctxt = _createContext(in, false);
681 // [JACKSON-512]: allow wrapping with InputDecorator
682 if (_inputDecorator != null) {
683 in = _inputDecorator.decorate(ctxt, in);
684 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700685 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800686 }
687
688 /**
689 * Method for constructing parser for parsing
690 * the contents accessed via specified Reader.
691 <p>
692 * The read stream will <b>not be owned</b> by
693 * the parser, it will still be managed (i.e. closed if
694 * end-of-stream is reacher, or parser close method called)
695 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
696 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800697 *
698 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800699 *
700 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800701 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800702 public JsonParser createParser(Reader r)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800703 throws IOException, JsonParseException
704 {
705 // false -> we do NOT own Reader (did not create it)
706 IOContext ctxt = _createContext(r, false);
707 // [JACKSON-512]: allow wrapping with InputDecorator
708 if (_inputDecorator != null) {
709 r = _inputDecorator.decorate(ctxt, r);
710 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700711 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800712 }
713
714 /**
715 * Method for constructing parser for parsing
716 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800717 *
718 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800719 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800720 public JsonParser createParser(byte[] data)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800721 throws IOException, JsonParseException
722 {
723 IOContext ctxt = _createContext(data, true);
724 // [JACKSON-512]: allow wrapping with InputDecorator
725 if (_inputDecorator != null) {
726 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
727 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700728 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800729 }
730 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700731 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800732 }
733
734 /**
735 * Method for constructing parser for parsing
736 * the contents of given byte array.
737 *
738 * @param data Buffer that contains data to parse
739 * @param offset Offset of the first data byte within buffer
740 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800741 *
742 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800743 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800744 public JsonParser createParser(byte[] data, int offset, int len)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800745 throws IOException, JsonParseException
746 {
747 IOContext ctxt = _createContext(data, true);
748 // [JACKSON-512]: allow wrapping with InputDecorator
749 if (_inputDecorator != null) {
750 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
751 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700752 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800753 }
754 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800755 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800756 }
757
758 /**
759 * Method for constructing parser for parsing
760 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800761 *
762 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800763 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800764 public JsonParser createParser(String content)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800765 throws IOException, JsonParseException
766 {
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700767 Reader r = new StringReader(content);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800768 // true -> we own the Reader (and must close); not a big deal
769 IOContext ctxt = _createContext(r, true);
770 // [JACKSON-512]: allow wrapping with InputDecorator
771 if (_inputDecorator != null) {
772 r = _inputDecorator.decorate(ctxt, r);
773 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700774 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800775 }
776
777 /*
778 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800779 /* Parser factories (old ones, as per [Issue-25])
780 /**********************************************************
781 */
782
783 /**
784 * Method for constructing JSON parser instance to parse
785 * contents of specified file. Encoding is auto-detected
786 * from contents according to JSON specification recommended
787 * mechanism.
788 *<p>
789 * Underlying input stream (needed for reading contents)
790 * will be <b>owned</b> (and managed, i.e. closed as need be) by
791 * the parser, since caller has no access to it.
792 *<p>
793 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
794 * instead, should call <code>createParser</code>.
795 *
796 * @param f File that contains JSON content to parse
797 *
798 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
799 */
800 @Deprecated
801 public JsonParser createJsonParser(File f)
802 throws IOException, JsonParseException
803 {
804 return createParser(f);
805 }
806
807 /**
808 * Method for constructing JSON parser instance to parse
809 * contents of resource reference by given URL.
810 * Encoding is auto-detected
811 * from contents according to JSON specification recommended
812 * mechanism.
813 *<p>
814 * Underlying input stream (needed for reading contents)
815 * will be <b>owned</b> (and managed, i.e. closed as need be) by
816 * the parser, since caller has no access to it.
817 *<p>
818 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
819 * instead, should call <code>createParser</code>.
820 *
821 * @param url URL pointing to resource that contains JSON content to parse
822 *
823 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
824 */
825 @Deprecated
826 public JsonParser createJsonParser(URL url)
827 throws IOException, JsonParseException
828 {
829 return createParser(url);
830 }
831
832 /**
833 * Method for constructing JSON parser instance to parse
834 * the contents accessed via specified input stream.
835 *<p>
836 * The input stream will <b>not be owned</b> by
837 * the parser, it will still be managed (i.e. closed if
838 * end-of-stream is reacher, or parser close method called)
839 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
840 * is enabled.
841 *<p>
842 * Note: no encoding argument is taken since it can always be
843 * auto-detected as suggested by JSON RFC.
844 *<p>
845 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
846 * instead, should call <code>createParser</code>.
847 *
848 * @param in InputStream to use for reading JSON content to parse
849 *
850 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
851 */
852 @Deprecated
853 public JsonParser createJsonParser(InputStream in)
854 throws IOException, JsonParseException
855 {
856 return createParser(in);
857 }
858
859 /**
860 * Method for constructing parser for parsing
861 * the contents accessed via specified Reader.
862 <p>
863 * The read stream will <b>not be owned</b> by
864 * the parser, it will still be managed (i.e. closed if
865 * end-of-stream is reacher, or parser close method called)
866 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
867 * is enabled.
868 *<p>
869 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
870 * instead, should call <code>createParser</code>.
871 *
872 * @param r Reader to use for reading JSON content to parse
873 *
874 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
875 */
876 @Deprecated
877 public JsonParser createJsonParser(Reader r)
878 throws IOException, JsonParseException
879 {
880 return createParser(r);
881 }
882
883 /**
884 * Method for constructing parser for parsing
885 * the contents of given byte array.
886 *<p>
887 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
888 * instead, should call <code>createParser</code>.
889 *
890 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
891 */
892 @Deprecated
893 public JsonParser createJsonParser(byte[] data)
894 throws IOException, JsonParseException
895 {
896 return createParser(data);
897 }
898
899 /**
900 * Method for constructing parser for parsing
901 * the contents of given byte array.
902 *<p>
903 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
904 * instead, should call <code>createParser</code>.
905 *
906 * @param data Buffer that contains data to parse
907 * @param offset Offset of the first data byte within buffer
908 * @param len Length of contents to parse within buffer
909 *
910 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
911 */
912 @Deprecated
913 public JsonParser createJsonParser(byte[] data, int offset, int len)
914 throws IOException, JsonParseException
915 {
916 return createParser(data, offset, len);
917 }
918
919 /**
920 * Method for constructing parser for parsing
921 * contents of given String.
922 *
923 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
924 */
925 @Deprecated
926 public JsonParser createJsonParser(String content)
927 throws IOException, JsonParseException
928 {
929 return createParser(content);
930 }
931
932 /*
933 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700934 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800935 /**********************************************************
936 */
937
938 /**
939 * Method for constructing JSON generator for writing JSON content
940 * using specified output stream.
941 * Encoding to use must be specified, and needs to be one of available
942 * types (as per JSON specification).
943 *<p>
944 * Underlying stream <b>is NOT owned</b> by the generator constructed,
945 * so that generator will NOT close the output stream when
946 * {@link JsonGenerator#close} is called (unless auto-closing
947 * feature,
948 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
949 * is enabled).
950 * Using application needs to close it explicitly if this is the case.
951 *<p>
952 * Note: there are formats that use fixed encoding (like most binary data formats)
953 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700954 *
955 * @param out OutputStream to use for writing JSON content
956 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800957 *
958 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700959 */
960 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
961 throws IOException
962 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800963 // false -> we won't manage the stream unless explicitly directed to
964 IOContext ctxt = _createContext(out, false);
965 ctxt.setEncoding(enc);
966 if (enc == JsonEncoding.UTF8) {
967 // [JACKSON-512]: allow wrapping with _outputDecorator
968 if (_outputDecorator != null) {
969 out = _outputDecorator.decorate(ctxt, out);
970 }
971 return _createUTF8Generator(out, ctxt);
972 }
973 Writer w = _createWriter(out, enc, ctxt);
974 // [JACKSON-512]: allow wrapping with _outputDecorator
975 if (_outputDecorator != null) {
976 w = _outputDecorator.decorate(ctxt, w);
977 }
978 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700979 }
980
981 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800982 * Convenience method for constructing generator that uses default
983 * encoding of the format (UTF-8 for JSON and most other data formats).
984 *<p>
985 * Note: there are formats that use fixed encoding (like most binary data formats).
986 *
987 * @since 2.1
988 */
989 public JsonGenerator createGenerator(OutputStream out) throws IOException {
990 return createGenerator(out, JsonEncoding.UTF8);
991 }
992
993 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700994 * Method for constructing JSON generator for writing JSON content
995 * using specified Writer.
996 *<p>
997 * Underlying stream <b>is NOT owned</b> by the generator constructed,
998 * so that generator will NOT close the Reader when
999 * {@link JsonGenerator#close} is called (unless auto-closing
1000 * feature,
1001 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1002 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001003 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001004 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001005 *
1006 * @param out Writer to use for writing JSON content
1007 */
1008 public JsonGenerator createGenerator(Writer out)
1009 throws IOException
1010 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001011 IOContext ctxt = _createContext(out, false);
1012 // [JACKSON-512]: allow wrapping with _outputDecorator
1013 if (_outputDecorator != null) {
1014 out = _outputDecorator.decorate(ctxt, out);
1015 }
1016 return _createGenerator(out, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001017 }
1018
1019 /**
1020 * Method for constructing JSON generator for writing JSON content
1021 * to specified file, overwriting contents it might have (or creating
1022 * it if such file does not yet exist).
1023 * Encoding to use must be specified, and needs to be one of available
1024 * types (as per JSON specification).
1025 *<p>
1026 * Underlying stream <b>is owned</b> by the generator constructed,
1027 * i.e. generator will handle closing of file when
1028 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001029 *
1030 * @param f File to write contents to
1031 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001032 *
1033 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001034 */
1035 public JsonGenerator createGenerator(File f, JsonEncoding enc)
1036 throws IOException
1037 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001038 OutputStream out = new FileOutputStream(f);
1039 // true -> yes, we have to manage the stream since we created it
1040 IOContext ctxt = _createContext(out, true);
1041 ctxt.setEncoding(enc);
1042 if (enc == JsonEncoding.UTF8) {
1043 // [JACKSON-512]: allow wrapping with _outputDecorator
1044 if (_outputDecorator != null) {
1045 out = _outputDecorator.decorate(ctxt, out);
1046 }
1047 return _createUTF8Generator(out, ctxt);
1048 }
1049 Writer w = _createWriter(out, enc, ctxt);
1050 // [JACKSON-512]: allow wrapping with _outputDecorator
1051 if (_outputDecorator != null) {
1052 w = _outputDecorator.decorate(ctxt, w);
1053 }
1054 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001055 }
1056
1057 /*
1058 /**********************************************************
1059 /* Generator factories, old (as per [Issue-25]
1060 /**********************************************************
1061 */
1062
1063 /**
1064 * Method for constructing JSON generator for writing JSON content
1065 * using specified output stream.
1066 * Encoding to use must be specified, and needs to be one of available
1067 * types (as per JSON specification).
1068 *<p>
1069 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1070 * so that generator will NOT close the output stream when
1071 * {@link JsonGenerator#close} is called (unless auto-closing
1072 * feature,
1073 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1074 * is enabled).
1075 * Using application needs to close it explicitly if this is the case.
1076 *<p>
1077 * Note: there are formats that use fixed encoding (like most binary data formats)
1078 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001079 *
1080 * @param out OutputStream to use for writing JSON content
1081 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001082 *
1083 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001084 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001085 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001086 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc)
1087 throws IOException
1088 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001089 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001090 }
1091
1092 /**
1093 * Method for constructing JSON generator for writing JSON content
1094 * using specified Writer.
1095 *<p>
1096 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1097 * so that generator will NOT close the Reader when
1098 * {@link JsonGenerator#close} is called (unless auto-closing
1099 * feature,
1100 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1101 * Using application needs to close it explicitly.
1102 *
1103 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001104 *
1105 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001106 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001107 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001108 public JsonGenerator createJsonGenerator(Writer out)
1109 throws IOException
1110 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001111 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001112 }
1113
1114 /**
1115 * Convenience method for constructing generator that uses default
1116 * encoding of the format (UTF-8 for JSON and most other data formats).
1117 *<p>
1118 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001119 *
1120 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001121 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001122 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001123 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001124 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001125 }
1126
1127 /**
1128 * Method for constructing JSON generator for writing JSON content
1129 * to specified file, overwriting contents it might have (or creating
1130 * it if such file does not yet exist).
1131 * Encoding to use must be specified, and needs to be one of available
1132 * types (as per JSON specification).
1133 *<p>
1134 * Underlying stream <b>is owned</b> by the generator constructed,
1135 * i.e. generator will handle closing of file when
1136 * {@link JsonGenerator#close} is called.
1137 *
1138 * @param f File to write contents to
1139 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001140 *
1141 *
1142 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001143 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001144 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001145 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc)
1146 throws IOException
1147 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001148 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001149 }
1150
1151 /*
1152 /**********************************************************
1153 /* Factory methods used by factory for creating parser instances,
1154 /* overridable by sub-classes
1155 /**********************************************************
1156 */
1157
1158 /**
1159 * Overridable factory method that actually instantiates desired parser
1160 * given {@link InputStream} and context object.
1161 *<p>
1162 * This method is specifically designed to remain
1163 * compatible between minor versions so that sub-classes can count
1164 * on it being called as expected. That is, it is part of official
1165 * interface from sub-class perspective, although not a public
1166 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001167 *
1168 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001169 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001170 protected JsonParser _createParser(InputStream in, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001171 throws IOException, JsonParseException
1172 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001173 // As per [JACKSON-259], may want to fully disable canonicalization:
1174 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
1175 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1176 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1177 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001178 }
1179
1180 /**
1181 * @deprecated since 2.1 -- use {@link #_createParser(InputStream, IOContext)} instead
1182 */
1183 @Deprecated
1184 protected JsonParser _createJsonParser(InputStream in, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001185 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001186 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001187
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001188 /**
1189 * Overridable factory method that actually instantiates parser
1190 * using given {@link Reader} object for reading content.
1191 *<p>
1192 * This method is specifically designed to remain
1193 * compatible between minor versions so that sub-classes can count
1194 * on it being called as expected. That is, it is part of official
1195 * interface from sub-class perspective, although not a public
1196 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001197 *
1198 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001199 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001200 protected JsonParser _createParser(Reader r, IOContext ctxt)
1201 throws IOException, JsonParseException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001202 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001203 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
1204 _rootCharSymbols.makeChild(isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1205 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES)));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001206 }
1207
1208 /**
1209 * @deprecated since 2.1 -- use {@link #_createParser(Reader, IOContext)} instead
1210 */
1211 @Deprecated
1212 protected JsonParser _createJsonParser(Reader r, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001213 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001214 }
1215
1216 /**
1217 * Overridable factory method that actually instantiates parser
1218 * using given {@link Reader} object for reading content
1219 * passed as raw byte array.
1220 *<p>
1221 * This method is specifically designed to remain
1222 * compatible between minor versions so that sub-classes can count
1223 * on it being called as expected. That is, it is part of official
1224 * interface from sub-class perspective, although not a public
1225 * method available to users of factory implementations.
1226 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001227 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001228 throws IOException, JsonParseException
1229 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001230 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
1231 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1232 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1233 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001234 }
1235
1236 /**
1237 * @deprecated since 2.1 -- use {@link #_createParser(byte[], int, int, IOContext)} instead
1238 */
1239 @Deprecated
1240 protected JsonParser _createJsonParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001241 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001242 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001243
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001244 /*
1245 /**********************************************************
1246 /* Factory methods used by factory for creating generator instances,
1247 /* overridable by sub-classes
1248 /**********************************************************
1249 */
1250
1251 /**
1252 * Overridable factory method that actually instantiates generator for
1253 * given {@link Writer} and context object.
1254 *<p>
1255 * This method is specifically designed to remain
1256 * compatible between minor versions so that sub-classes can count
1257 * on it being called as expected. That is, it is part of official
1258 * interface from sub-class perspective, although not a public
1259 * method available to users of factory implementations.
1260 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001261 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt)
1262 throws IOException
1263 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001264 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1265 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001266 if (_characterEscapes != null) {
1267 gen.setCharacterEscapes(_characterEscapes);
1268 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001269 SerializableString rootSep = _rootValueSeparator;
1270 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1271 gen.setRootValueSeparator(rootSep);
1272 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001273 return gen;
1274 }
1275
1276 /**
Tatu Saloranta68194922012-11-15 20:34:29 -08001277 * @deprecated since 2.1 -- use {@link #_createGenerator(Writer, IOContext)} instead
1278 */
1279 @Deprecated
1280 protected JsonGenerator _createJsonGenerator(Writer out, IOContext ctxt)
1281 throws IOException
1282 {
1283 /* NOTE: MUST call the deprecated method until it is deleted, just so
1284 * that override still works as expected, for now.
1285 */
1286 return _createGenerator(out, ctxt);
1287 }
1288
1289 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001290 * Overridable factory method that actually instantiates generator for
1291 * given {@link OutputStream} and context object, using UTF-8 encoding.
1292 *<p>
1293 * This method is specifically designed to remain
1294 * compatible between minor versions so that sub-classes can count
1295 * on it being called as expected. That is, it is part of official
1296 * interface from sub-class perspective, although not a public
1297 * method available to users of factory implementations.
1298 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001299 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001300 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1301 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001302 if (_characterEscapes != null) {
1303 gen.setCharacterEscapes(_characterEscapes);
1304 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001305 SerializableString rootSep = _rootValueSeparator;
1306 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1307 gen.setRootValueSeparator(rootSep);
1308 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001309 return gen;
1310 }
1311
Tatu Saloranta68194922012-11-15 20:34:29 -08001312 /**
1313 * @deprecated since 2.1
1314 */
1315 @Deprecated
1316 protected JsonGenerator _createUTF8JsonGenerator(OutputStream out, IOContext ctxt)
1317 throws IOException
1318 {
1319 return _createUTF8Generator(out, ctxt);
1320 }
1321
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001322 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1323 {
1324 // note: this should not get called any more (caller checks, dispatches)
1325 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1326 return new UTF8Writer(ctxt, out);
1327 }
1328 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1329 return new OutputStreamWriter(out, enc.getJavaName());
1330 }
1331
1332 /*
1333 /**********************************************************
1334 /* Internal factory methods, other
1335 /**********************************************************
1336 */
1337
1338 /**
1339 * Overridable factory method that actually instantiates desired
1340 * context object.
1341 */
1342 protected IOContext _createContext(Object srcRef, boolean resourceManaged)
1343 {
1344 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1345 }
1346
1347 /**
1348 * Method used by factory to create buffer recycler instances
1349 * for parsers and generators.
1350 *<p>
1351 * Note: only public to give access for <code>ObjectMapper</code>
1352 */
1353 public BufferRecycler _getBufferRecycler()
1354 {
1355 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1356 BufferRecycler br = (ref == null) ? null : ref.get();
1357
1358 if (br == null) {
1359 br = new BufferRecycler();
1360 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1361 }
1362 return br;
1363 }
1364
1365 /**
1366 * Helper methods used for constructing an optimal stream for
1367 * parsers to use, when input is to be read from an URL.
1368 * This helps when reading file content via URL.
1369 */
1370 protected InputStream _optimizedStreamFromURL(URL url)
1371 throws IOException
1372 {
1373 if ("file".equals(url.getProtocol())) {
1374 /* Can not do this if the path refers
1375 * to a network drive on windows. This fixes the problem;
1376 * might not be needed on all platforms (NFS?), but should not
1377 * matter a lot: performance penalty of extra wrapping is more
1378 * relevant when accessing local file system.
1379 */
1380 String host = url.getHost();
1381 if (host == null || host.length() == 0) {
1382 return new FileInputStream(url.getPath());
1383 }
1384 }
1385 return url.openStream();
1386 }
1387}