blob: cc5ee743ad9bc6f542ed030c52f84e36c3b096d1 [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
Tatu Salorantaf15531c2011-12-22 23:00:40 -08004 */
5package com.fasterxml.jackson.core;
6
7import java.io.*;
8import java.lang.ref.SoftReference;
9import java.net.URL;
10
11import com.fasterxml.jackson.core.format.InputAccessor;
12import com.fasterxml.jackson.core.format.MatchStrength;
13import com.fasterxml.jackson.core.io.*;
Tatu07351902012-01-19 13:12:13 -080014import com.fasterxml.jackson.core.json.*;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080015import com.fasterxml.jackson.core.sym.BytesToNameCanonicalizer;
16import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
17import com.fasterxml.jackson.core.util.BufferRecycler;
Tatu Salorantae6dfc692012-09-28 15:34:05 -070018import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080019
20/**
21 * The main factory class of Jackson package, used to configure and
22 * construct reader (aka parser, {@link JsonParser})
23 * and writer (aka generator, {@link JsonGenerator})
24 * instances.
25 *<p>
26 * Factory instances are thread-safe and reusable after configuration
27 * (if any). Typically applications and services use only a single
28 * globally shared factory instance, unless they need differently
29 * configured factories. Factory reuse is important if efficiency matters;
30 * most recycling of expensive construct is done on per-factory basis.
31 *<p>
32 * Creation of a factory instance is a light-weight operation,
33 * and since there is no need for pluggable alternative implementations
34 * (as there is no "standard" JSON processor API to implement),
35 * the default constructor is used for constructing factory
36 * instances.
37 *
38 * @author Tatu Saloranta
39 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -070040public class JsonFactory
41 implements Versioned,
42 java.io.Serializable // since 2.1 (for Android, mostly)
Tatu Salorantaf15531c2011-12-22 23:00:40 -080043{
44 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -070045 * Computed for Jackson 2.2.0 release
Tatu Saloranta95f76a42012-10-05 13:04:23 -070046 */
Tatu Saloranta101820d2012-10-05 13:42:49 -070047 private static final long serialVersionUID = 8726401676402117450L;
Tatu Saloranta95f76a42012-10-05 13:04:23 -070048
Tatu Saloranta7b796a82013-04-27 10:18:30 -070049 /*
50 /**********************************************************
51 /* Helper types
52 /**********************************************************
Tatu Salorantaf15531c2011-12-22 23:00:40 -080053 */
Tatu Salorantae6dfc692012-09-28 15:34:05 -070054
Tatu07351902012-01-19 13:12:13 -080055 /**
56 * Enumeration that defines all on/off features that can only be
57 * changed for {@link JsonFactory}.
58 */
59 public enum Feature {
60
61 // // // Symbol handling (interning etc)
62
63 /**
64 * Feature that determines whether JSON object field names are
65 * to be canonicalized using {@link String#intern} or not:
66 * if enabled, all field names will be intern()ed (and caller
67 * can count on this being true for all such names); if disabled,
68 * no intern()ing is done. There may still be basic
69 * canonicalization (that is, same String will be used to represent
70 * all identical object property names for a single document).
71 *<p>
72 * Note: this setting only has effect if
73 * {@link #CANONICALIZE_FIELD_NAMES} is true -- otherwise no
74 * canonicalization of any sort is done.
75 *<p>
76 * This setting is enabled by default.
77 */
78 INTERN_FIELD_NAMES(true),
79
80 /**
81 * Feature that determines whether JSON object field names are
82 * to be canonicalized (details of how canonicalization is done
83 * then further specified by
84 * {@link #INTERN_FIELD_NAMES}).
85 *<p>
86 * This setting is enabled by default.
87 */
88 CANONICALIZE_FIELD_NAMES(true)
89
90 ;
91
92 /**
93 * Whether feature is enabled or disabled by default.
94 */
95 private final boolean _defaultState;
96
97 /**
98 * Method that calculates bit set (flags) of all features that
99 * are enabled by default.
100 */
101 public static int collectDefaults()
102 {
103 int flags = 0;
104 for (Feature f : values()) {
105 if (f.enabledByDefault()) {
106 flags |= f.getMask();
107 }
108 }
109 return flags;
110 }
111
112 private Feature(boolean defaultState)
113 {
114 _defaultState = defaultState;
115 }
116
117 public boolean enabledByDefault() { return _defaultState; }
118
119 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
120
121 public int getMask() { return (1 << ordinal()); }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700122 }
123
124 /*
125 /**********************************************************
126 /* Constants
127 /**********************************************************
128 */
129
130 /**
131 * Name used to identify JSON format
132 * (and returned by {@link #getFormatName()}
133 */
134 public final static String FORMAT_NAME_JSON = "JSON";
135
136 /**
137 * Bitfield (set of flags) of all factory features that are enabled by default.
138 */
139 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
140
141 /**
142 * Bitfield (set of flags) of all parser features that are enabled
143 * by default.
144 */
145 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
146
147 /**
148 * Bitfield (set of flags) of all generator features that are enabled
149 * by default.
150 */
151 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
152
153 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
154
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800155 /*
156 /**********************************************************
157 /* Buffer, symbol table management
158 /**********************************************************
159 */
160
161 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700162 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800163 * to a {@link BufferRecycler} used to provide a low-cost
164 * buffer recycling between reader and writer instances.
165 */
166 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
167 = new ThreadLocal<SoftReference<BufferRecycler>>();
168
169 /**
170 * Each factory comes equipped with a shared root symbol table.
171 * It should not be linked back to the original blueprint, to
172 * avoid contents from leaking between factories.
173 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700174 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800175
176 /**
177 * Alternative to the basic symbol table, some stream-based
178 * parsers use different name canonicalization method.
179 *<p>
180 * TODO: should clean up this; looks messy having 2 alternatives
181 * with not very clear differences.
182 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700183 protected final transient BytesToNameCanonicalizer _rootByteSymbols = BytesToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800184
185 /*
186 /**********************************************************
187 /* Configuration
188 /**********************************************************
189 */
190
191 /**
192 * Object that implements conversion functionality between
193 * Java objects and JSON content. For base JsonFactory implementation
194 * usually not set by default, but can be explicitly set.
195 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
196 * usually provide an implementation.
197 */
198 protected ObjectCodec _objectCodec;
199
200 /**
Tatu07351902012-01-19 13:12:13 -0800201 * Currently enabled factory features.
202 */
203 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
204
205 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800206 * Currently enabled parser features.
207 */
208 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
209
210 /**
211 * Currently enabled generator features.
212 */
213 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
214
215 /**
216 * Definition of custom character escapes to use for generators created
217 * by this factory, if any. If null, standard data format specific
218 * escapes are used.
219 */
220 protected CharacterEscapes _characterEscapes;
221
222 /**
223 * Optional helper object that may decorate input sources, to do
224 * additional processing on input during parsing.
225 */
226 protected InputDecorator _inputDecorator;
227
228 /**
229 * Optional helper object that may decorate output object, to do
230 * additional processing on output during content generation.
231 */
232 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700233
234 /**
235 * Separator used between root-level values, if any; null indicates
236 * "do not add separator".
237 * Default separator is a single space character.
238 *
239 * @since 2.1
240 */
241 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800242
243 /*
244 /**********************************************************
245 /* Construction
246 /**********************************************************
247 */
248
249 /**
250 * Default constructor used to create factory instances.
251 * Creation of a factory instance is a light-weight operation,
252 * but it is still a good idea to reuse limited number of
253 * factory instances (and quite often just a single instance):
254 * factories are used as context for storing some reused
255 * processing objects (such as symbol tables parsers use)
256 * and this reuse only works within context of a single
257 * factory instance.
258 */
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700259 public JsonFactory() { this((ObjectCodec) null); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800260
261 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
262
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700263 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700264 * Constructor used when copy()ing a factory instance.
265 *
266 * @since 2.2.1
267 */
268 protected JsonFactory(JsonFactory src, ObjectCodec codec)
269 {
270 _objectCodec = null;
271 _factoryFeatures = src._factoryFeatures;
272 _parserFeatures = src._parserFeatures;
273 _generatorFeatures = src._generatorFeatures;
274 _characterEscapes = src._characterEscapes;
275 _inputDecorator = src._inputDecorator;
276 _outputDecorator = src._outputDecorator;
277 _rootValueSeparator = src._rootValueSeparator;
278
279 /* 27-Apr-2013, tatu: How about symbol table; should we try to
280 * reuse shared symbol tables? Could be more efficient that way;
281 * although can slightly add to concurrency overhead.
282 */
283 }
284
285 /**
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700286 * Method for constructing a new {@link JsonFactory} that has
287 * the same settings as this instance, but is otherwise
288 * independent (i.e. nothing is actually shared, symbol tables
289 * are separate).
290 * Note that {@link ObjectCodec} reference is not copied but is
291 * set to null; caller typically needs to set it after calling
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700292 * this method. Reason for this is that the codec is used for
293 * callbacks, and assumption is that there is strict 1-to-1
294 * mapping between codec, factory. Caller has to, then, explicitly
295 * set codec after making the copy.
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700296 *
297 * @since 2.1
298 */
299 public JsonFactory copy()
300 {
301 _checkInvalidCopy(JsonFactory.class);
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700302 // as per above, do clear ObjectCodec
303 return new JsonFactory(this, null);
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700304 }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700305
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700306 /**
307 * @since 2.1
308 * @param exp
309 */
310 protected void _checkInvalidCopy(Class<?> exp)
311 {
312 if (getClass() != exp) {
313 throw new IllegalStateException("Failed copy(): "+getClass().getName()
314 +" (version: "+version()+") does not override copy(); it has to");
315 }
316 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700317
318 /*
319 /**********************************************************
320 /* Serializable overrides
321 /**********************************************************
322 */
323
324 /**
325 * Method that we need to override to actually make restoration go
326 * through constructors etc.
327 * Also: must be overridden by sub-classes as well.
328 */
329 protected Object readResolve() {
Tatu Salorantaf7741c52013-04-27 11:06:30 -0700330 return new JsonFactory(this, _objectCodec);
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700331 }
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700332
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800333 /*
334 /**********************************************************
335 /* Format detection functionality (since 1.8)
336 /**********************************************************
337 */
338
339 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700340 * Method that can be used to quickly check whether given schema
341 * is something that parsers and/or generators constructed by this
342 * factory could use. Note that this means possible use, at the level
343 * of data format (i.e. schema is for same data format as parsers and
344 * generators this factory constructs); individual schema instances
345 * may have further usage restrictions.
346 *
347 * @since 2.1
348 */
349 public boolean canUseSchema(FormatSchema schema) {
350 String ourFormat = getFormatName();
351 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
352 }
353
354 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800355 * Method that returns short textual id identifying format
356 * this factory supports.
357 *<p>
358 * Note: sub-classes should override this method; default
359 * implementation will return null for all sub-classes
360 */
361 public String getFormatName()
362 {
363 /* Somewhat nasty check: since we can't make this abstract
364 * (due to backwards compatibility concerns), need to prevent
365 * format name "leakage"
366 */
367 if (getClass() == JsonFactory.class) {
368 return FORMAT_NAME_JSON;
369 }
370 return null;
371 }
372
373 public MatchStrength hasFormat(InputAccessor acc) throws IOException
374 {
375 // since we can't keep this abstract, only implement for "vanilla" instance
376 if (getClass() == JsonFactory.class) {
377 return hasJSONFormat(acc);
378 }
379 return null;
380 }
381
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700382 /**
383 * Method that can be called to determine if a custom
384 * {@link ObjectCodec} is needed for binding data parsed
385 * using {@link JsonParser} constructed by this factory
386 * (which typically also implies the same for serialization
387 * with {@link JsonGenerator}).
388 *
389 * @return True if custom codec is needed with parsers and
390 * generators created by this factory; false if a general
391 * {@link ObjectCodec} is enough
392 *
393 * @since 2.1
394 */
395 public boolean requiresCustomCodec() {
396 return false;
397 }
398
399 /**
400 * Helper method that can be called to determine if content accessed
401 * using given accessor seems to be JSON content.
402 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800403 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
404 {
405 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700406 }
407
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800408 /*
409 /**********************************************************
410 /* Versioned
411 /**********************************************************
412 */
413
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800414 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800415 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800416 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800417 }
418
419 /*
420 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800421 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800422 /**********************************************************
423 */
424
425 /**
426 * Method for enabling or disabling specified parser feature
427 * (check {@link JsonParser.Feature} for list of features)
428 */
Tatu07351902012-01-19 13:12:13 -0800429 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
430 return state ? enable(f) : disable(f);
431 }
432
433 /**
434 * Method for enabling specified parser feature
435 * (check {@link JsonFactory.Feature} for list of features)
436 */
437 public JsonFactory enable(JsonFactory.Feature f) {
438 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800439 return this;
440 }
441
442 /**
Tatu07351902012-01-19 13:12:13 -0800443 * Method for disabling specified parser features
444 * (check {@link JsonFactory.Feature} for list of features)
445 */
446 public JsonFactory disable(JsonFactory.Feature f) {
447 _factoryFeatures &= ~f.getMask();
448 return this;
449 }
450
451 /**
452 * Checked whether specified parser feature is enabled.
453 */
454 public final boolean isEnabled(JsonFactory.Feature f) {
455 return (_factoryFeatures & f.getMask()) != 0;
456 }
457
458 /*
459 /**********************************************************
460 /* Configuration, parser configuration
461 /**********************************************************
462 */
463
464 /**
465 * Method for enabling or disabling specified parser feature
466 * (check {@link JsonParser.Feature} for list of features)
467 */
468 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
469 return state ? enable(f) : disable(f);
470 }
471
472 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800473 * Method for enabling specified parser feature
474 * (check {@link JsonParser.Feature} for list of features)
475 */
476 public JsonFactory enable(JsonParser.Feature f) {
477 _parserFeatures |= f.getMask();
478 return this;
479 }
480
481 /**
482 * Method for disabling specified parser features
483 * (check {@link JsonParser.Feature} for list of features)
484 */
485 public JsonFactory disable(JsonParser.Feature f) {
486 _parserFeatures &= ~f.getMask();
487 return this;
488 }
489
490 /**
491 * Checked whether specified parser feature is enabled.
492 */
493 public final boolean isEnabled(JsonParser.Feature f) {
494 return (_parserFeatures & f.getMask()) != 0;
495 }
496
497 /**
498 * Method for getting currently configured input decorator (if any;
499 * there is no default decorator).
500 */
501 public InputDecorator getInputDecorator() {
502 return _inputDecorator;
503 }
504
505 /**
506 * Method for overriding currently configured input decorator
507 */
508 public JsonFactory setInputDecorator(InputDecorator d) {
509 _inputDecorator = d;
510 return this;
511 }
512
513 /*
514 /**********************************************************
515 /* Configuration, generator settings
516 /**********************************************************
517 */
518
519 /**
520 * Method for enabling or disabling specified generator feature
521 * (check {@link JsonGenerator.Feature} for list of features)
522 */
523 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800524 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800525 }
526
527
528 /**
529 * Method for enabling specified generator features
530 * (check {@link JsonGenerator.Feature} for list of features)
531 */
532 public JsonFactory enable(JsonGenerator.Feature f) {
533 _generatorFeatures |= f.getMask();
534 return this;
535 }
536
537 /**
538 * Method for disabling specified generator feature
539 * (check {@link JsonGenerator.Feature} for list of features)
540 */
541 public JsonFactory disable(JsonGenerator.Feature f) {
542 _generatorFeatures &= ~f.getMask();
543 return this;
544 }
545
546 /**
547 * Check whether specified generator feature is enabled.
548 */
549 public final boolean isEnabled(JsonGenerator.Feature f) {
550 return (_generatorFeatures & f.getMask()) != 0;
551 }
552
553 /**
554 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
555 * it creates.
556 */
557 public CharacterEscapes getCharacterEscapes() {
558 return _characterEscapes;
559 }
560
561 /**
562 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
563 * it creates.
564 */
565 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
566 _characterEscapes = esc;
567 return this;
568 }
569
570 /**
571 * Method for getting currently configured output decorator (if any;
572 * there is no default decorator).
573 */
574 public OutputDecorator getOutputDecorator() {
575 return _outputDecorator;
576 }
577
578 /**
579 * Method for overriding currently configured output decorator
580 */
581 public JsonFactory setOutputDecorator(OutputDecorator d) {
582 _outputDecorator = d;
583 return this;
584 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700585
586 /**
587 * Method that allows overriding String used for separating root-level
588 * JSON values (default is single space character)
589 *
590 * @param sep Separator to use, if any; null means that no separator is
591 * automatically added
592 *
593 * @since 2.1
594 */
595 public JsonFactory setRootValueSeparator(String sep) {
596 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
597 return this;
598 }
599
600 /**
601 * @since 2.1
602 */
603 public String getRootValueSeparator() {
604 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
605 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800606
607 /*
608 /**********************************************************
609 /* Configuration, other
610 /**********************************************************
611 */
612
613 /**
614 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800615 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
616 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800617 * it constructs). This is needed to use data-binding methods
618 * of {@link JsonParser} and {@link JsonGenerator} instances.
619 */
620 public JsonFactory setCodec(ObjectCodec oc) {
621 _objectCodec = oc;
622 return this;
623 }
624
625 public ObjectCodec getCodec() { return _objectCodec; }
626
627 /*
628 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700629 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800630 /**********************************************************
631 */
632
633 /**
634 * Method for constructing JSON parser instance to parse
635 * contents of specified file. 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 f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700644 *
645 * @since 2.1
646 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800647 @SuppressWarnings("resource")
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700648 public JsonParser createParser(File f)
649 throws IOException, JsonParseException
650 {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800651 // true, since we create InputStream from File
652 IOContext ctxt = _createContext(f, true);
653 InputStream in = new FileInputStream(f);
654 // [JACKSON-512]: allow wrapping with InputDecorator
655 if (_inputDecorator != null) {
656 in = _inputDecorator.decorate(ctxt, in);
657 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700658 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800659 }
660
661 /**
662 * Method for constructing JSON parser instance to parse
663 * contents of resource reference by given URL.
664 * Encoding is auto-detected
665 * from contents according to JSON specification recommended
666 * mechanism.
667 *<p>
668 * Underlying input stream (needed for reading contents)
669 * will be <b>owned</b> (and managed, i.e. closed as need be) by
670 * the parser, since caller has no access to it.
671 *
672 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800673 *
674 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800675 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800676 public JsonParser createParser(URL url)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800677 throws IOException, JsonParseException
678 {
679 // true, since we create InputStream from URL
680 IOContext ctxt = _createContext(url, true);
681 InputStream in = _optimizedStreamFromURL(url);
682 // [JACKSON-512]: allow wrapping with InputDecorator
683 if (_inputDecorator != null) {
684 in = _inputDecorator.decorate(ctxt, in);
685 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700686 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800687 }
688
689 /**
690 * Method for constructing JSON parser instance to parse
691 * the contents accessed via specified input stream.
692 *<p>
693 * The input stream will <b>not be owned</b> by
694 * the parser, it will still be managed (i.e. closed if
695 * end-of-stream is reacher, or parser close method called)
696 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
697 * is enabled.
698 *<p>
699 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700700 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800701 *
702 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800703 *
704 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800705 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800706 public JsonParser createParser(InputStream in)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800707 throws IOException, JsonParseException
708 {
709 IOContext ctxt = _createContext(in, false);
710 // [JACKSON-512]: allow wrapping with InputDecorator
711 if (_inputDecorator != null) {
712 in = _inputDecorator.decorate(ctxt, in);
713 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700714 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800715 }
716
717 /**
718 * Method for constructing parser for parsing
719 * the contents accessed via specified Reader.
720 <p>
721 * The read stream will <b>not be owned</b> by
722 * the parser, it will still be managed (i.e. closed if
723 * end-of-stream is reacher, or parser close method called)
724 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
725 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800726 *
727 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800728 *
729 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800730 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800731 public JsonParser createParser(Reader r)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800732 throws IOException, JsonParseException
733 {
734 // false -> we do NOT own Reader (did not create it)
735 IOContext ctxt = _createContext(r, false);
736 // [JACKSON-512]: allow wrapping with InputDecorator
737 if (_inputDecorator != null) {
738 r = _inputDecorator.decorate(ctxt, r);
739 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700740 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800741 }
742
743 /**
744 * Method for constructing parser for parsing
745 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800746 *
747 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800748 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800749 public JsonParser createParser(byte[] data)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800750 throws IOException, JsonParseException
751 {
752 IOContext ctxt = _createContext(data, true);
753 // [JACKSON-512]: allow wrapping with InputDecorator
754 if (_inputDecorator != null) {
755 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
756 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700757 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800758 }
759 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700760 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800761 }
762
763 /**
764 * Method for constructing parser for parsing
765 * the contents of given byte array.
766 *
767 * @param data Buffer that contains data to parse
768 * @param offset Offset of the first data byte within buffer
769 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800770 *
771 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800772 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800773 public JsonParser createParser(byte[] data, int offset, int len)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800774 throws IOException, JsonParseException
775 {
776 IOContext ctxt = _createContext(data, true);
777 // [JACKSON-512]: allow wrapping with InputDecorator
778 if (_inputDecorator != null) {
779 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
780 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700781 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800782 }
783 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800784 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800785 }
786
787 /**
788 * Method for constructing parser for parsing
789 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800790 *
791 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800792 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800793 public JsonParser createParser(String content)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800794 throws IOException, JsonParseException
795 {
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700796 Reader r = new StringReader(content);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800797 // true -> we own the Reader (and must close); not a big deal
798 IOContext ctxt = _createContext(r, true);
799 // [JACKSON-512]: allow wrapping with InputDecorator
800 if (_inputDecorator != null) {
801 r = _inputDecorator.decorate(ctxt, r);
802 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700803 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800804 }
805
806 /*
807 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800808 /* Parser factories (old ones, as per [Issue-25])
809 /**********************************************************
810 */
811
812 /**
813 * Method for constructing JSON parser instance to parse
814 * contents of specified file. Encoding is auto-detected
815 * from contents according to JSON specification recommended
816 * mechanism.
817 *<p>
818 * Underlying input stream (needed for reading contents)
819 * will be <b>owned</b> (and managed, i.e. closed as need be) by
820 * the parser, since caller has no access to it.
821 *<p>
822 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
823 * instead, should call <code>createParser</code>.
824 *
825 * @param f File that contains JSON content to parse
826 *
827 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
828 */
829 @Deprecated
830 public JsonParser createJsonParser(File f)
831 throws IOException, JsonParseException
832 {
833 return createParser(f);
834 }
835
836 /**
837 * Method for constructing JSON parser instance to parse
838 * contents of resource reference by given URL.
839 * Encoding is auto-detected
840 * from contents according to JSON specification recommended
841 * mechanism.
842 *<p>
843 * Underlying input stream (needed for reading contents)
844 * will be <b>owned</b> (and managed, i.e. closed as need be) by
845 * the parser, since caller has no access to it.
846 *<p>
847 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
848 * instead, should call <code>createParser</code>.
849 *
850 * @param url URL pointing to resource that contains JSON content to parse
851 *
852 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
853 */
854 @Deprecated
855 public JsonParser createJsonParser(URL url)
856 throws IOException, JsonParseException
857 {
858 return createParser(url);
859 }
860
861 /**
862 * Method for constructing JSON parser instance to parse
863 * the contents accessed via specified input stream.
864 *<p>
865 * The input stream will <b>not be owned</b> by
866 * the parser, it will still be managed (i.e. closed if
867 * end-of-stream is reacher, or parser close method called)
868 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
869 * is enabled.
870 *<p>
871 * Note: no encoding argument is taken since it can always be
872 * auto-detected as suggested by JSON RFC.
873 *<p>
874 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
875 * instead, should call <code>createParser</code>.
876 *
877 * @param in InputStream to use for reading JSON content to parse
878 *
879 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
880 */
881 @Deprecated
882 public JsonParser createJsonParser(InputStream in)
883 throws IOException, JsonParseException
884 {
885 return createParser(in);
886 }
887
888 /**
889 * Method for constructing parser for parsing
890 * the contents accessed via specified Reader.
891 <p>
892 * The read stream will <b>not be owned</b> by
893 * the parser, it will still be managed (i.e. closed if
894 * end-of-stream is reacher, or parser close method called)
895 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
896 * is enabled.
897 *<p>
898 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
899 * instead, should call <code>createParser</code>.
900 *
901 * @param r Reader to use for reading JSON content to parse
902 *
903 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
904 */
905 @Deprecated
906 public JsonParser createJsonParser(Reader r)
907 throws IOException, JsonParseException
908 {
909 return createParser(r);
910 }
911
912 /**
913 * Method for constructing parser for parsing
914 * the contents of given byte array.
915 *<p>
916 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
917 * instead, should call <code>createParser</code>.
918 *
919 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
920 */
921 @Deprecated
922 public JsonParser createJsonParser(byte[] data)
923 throws IOException, JsonParseException
924 {
925 return createParser(data);
926 }
927
928 /**
929 * Method for constructing parser for parsing
930 * the contents of given byte array.
931 *<p>
932 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
933 * instead, should call <code>createParser</code>.
934 *
935 * @param data Buffer that contains data to parse
936 * @param offset Offset of the first data byte within buffer
937 * @param len Length of contents to parse within buffer
938 *
939 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
940 */
941 @Deprecated
942 public JsonParser createJsonParser(byte[] data, int offset, int len)
943 throws IOException, JsonParseException
944 {
945 return createParser(data, offset, len);
946 }
947
948 /**
949 * Method for constructing parser for parsing
950 * contents of given String.
951 *
952 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
953 */
954 @Deprecated
955 public JsonParser createJsonParser(String content)
956 throws IOException, JsonParseException
957 {
958 return createParser(content);
959 }
960
961 /*
962 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700963 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800964 /**********************************************************
965 */
966
967 /**
968 * Method for constructing JSON generator for writing JSON content
969 * using specified output stream.
970 * Encoding to use must be specified, and needs to be one of available
971 * types (as per JSON specification).
972 *<p>
973 * Underlying stream <b>is NOT owned</b> by the generator constructed,
974 * so that generator will NOT close the output stream when
975 * {@link JsonGenerator#close} is called (unless auto-closing
976 * feature,
977 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
978 * is enabled).
979 * Using application needs to close it explicitly if this is the case.
980 *<p>
981 * Note: there are formats that use fixed encoding (like most binary data formats)
982 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700983 *
984 * @param out OutputStream to use for writing JSON content
985 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800986 *
987 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700988 */
989 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
990 throws IOException
991 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800992 // false -> we won't manage the stream unless explicitly directed to
993 IOContext ctxt = _createContext(out, false);
994 ctxt.setEncoding(enc);
995 if (enc == JsonEncoding.UTF8) {
996 // [JACKSON-512]: allow wrapping with _outputDecorator
997 if (_outputDecorator != null) {
998 out = _outputDecorator.decorate(ctxt, out);
999 }
1000 return _createUTF8Generator(out, ctxt);
1001 }
1002 Writer w = _createWriter(out, enc, ctxt);
1003 // [JACKSON-512]: allow wrapping with _outputDecorator
1004 if (_outputDecorator != null) {
1005 w = _outputDecorator.decorate(ctxt, w);
1006 }
1007 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001008 }
1009
1010 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001011 * Convenience method for constructing generator that uses default
1012 * encoding of the format (UTF-8 for JSON and most other data formats).
1013 *<p>
1014 * Note: there are formats that use fixed encoding (like most binary data formats).
1015 *
1016 * @since 2.1
1017 */
1018 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1019 return createGenerator(out, JsonEncoding.UTF8);
1020 }
1021
1022 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001023 * Method for constructing JSON generator for writing JSON content
1024 * using specified Writer.
1025 *<p>
1026 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1027 * so that generator will NOT close the Reader when
1028 * {@link JsonGenerator#close} is called (unless auto-closing
1029 * feature,
1030 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1031 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001032 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001033 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001034 *
1035 * @param out Writer to use for writing JSON content
1036 */
1037 public JsonGenerator createGenerator(Writer out)
1038 throws IOException
1039 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001040 IOContext ctxt = _createContext(out, false);
1041 // [JACKSON-512]: allow wrapping with _outputDecorator
1042 if (_outputDecorator != null) {
1043 out = _outputDecorator.decorate(ctxt, out);
1044 }
1045 return _createGenerator(out, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001046 }
1047
1048 /**
1049 * Method for constructing JSON generator for writing JSON content
1050 * to specified file, overwriting contents it might have (or creating
1051 * it if such file does not yet exist).
1052 * Encoding to use must be specified, and needs to be one of available
1053 * types (as per JSON specification).
1054 *<p>
1055 * Underlying stream <b>is owned</b> by the generator constructed,
1056 * i.e. generator will handle closing of file when
1057 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001058 *
1059 * @param f File to write contents to
1060 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001061 *
1062 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001063 */
1064 public JsonGenerator createGenerator(File f, JsonEncoding enc)
1065 throws IOException
1066 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001067 OutputStream out = new FileOutputStream(f);
1068 // true -> yes, we have to manage the stream since we created it
1069 IOContext ctxt = _createContext(out, true);
1070 ctxt.setEncoding(enc);
1071 if (enc == JsonEncoding.UTF8) {
1072 // [JACKSON-512]: allow wrapping with _outputDecorator
1073 if (_outputDecorator != null) {
1074 out = _outputDecorator.decorate(ctxt, out);
1075 }
1076 return _createUTF8Generator(out, ctxt);
1077 }
1078 Writer w = _createWriter(out, enc, ctxt);
1079 // [JACKSON-512]: allow wrapping with _outputDecorator
1080 if (_outputDecorator != null) {
1081 w = _outputDecorator.decorate(ctxt, w);
1082 }
1083 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001084 }
1085
1086 /*
1087 /**********************************************************
1088 /* Generator factories, old (as per [Issue-25]
1089 /**********************************************************
1090 */
1091
1092 /**
1093 * Method for constructing JSON generator for writing JSON content
1094 * using specified output stream.
1095 * Encoding to use must be specified, and needs to be one of available
1096 * types (as per JSON specification).
1097 *<p>
1098 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1099 * so that generator will NOT close the output stream when
1100 * {@link JsonGenerator#close} is called (unless auto-closing
1101 * feature,
1102 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1103 * is enabled).
1104 * Using application needs to close it explicitly if this is the case.
1105 *<p>
1106 * Note: there are formats that use fixed encoding (like most binary data formats)
1107 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001108 *
1109 * @param out OutputStream to use for writing JSON content
1110 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001111 *
1112 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001113 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001114 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001115 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc)
1116 throws IOException
1117 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001118 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001119 }
1120
1121 /**
1122 * Method for constructing JSON generator for writing JSON content
1123 * using specified Writer.
1124 *<p>
1125 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1126 * so that generator will NOT close the Reader when
1127 * {@link JsonGenerator#close} is called (unless auto-closing
1128 * feature,
1129 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1130 * Using application needs to close it explicitly.
1131 *
1132 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001133 *
1134 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001135 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001136 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001137 public JsonGenerator createJsonGenerator(Writer out)
1138 throws IOException
1139 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001140 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001141 }
1142
1143 /**
1144 * Convenience method for constructing generator that uses default
1145 * encoding of the format (UTF-8 for JSON and most other data formats).
1146 *<p>
1147 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001148 *
1149 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001150 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001151 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001152 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001153 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001154 }
1155
1156 /**
1157 * Method for constructing JSON generator for writing JSON content
1158 * to specified file, overwriting contents it might have (or creating
1159 * it if such file does not yet exist).
1160 * Encoding to use must be specified, and needs to be one of available
1161 * types (as per JSON specification).
1162 *<p>
1163 * Underlying stream <b>is owned</b> by the generator constructed,
1164 * i.e. generator will handle closing of file when
1165 * {@link JsonGenerator#close} is called.
1166 *
1167 * @param f File to write contents to
1168 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001169 *
1170 *
1171 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001172 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001173 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001174 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc)
1175 throws IOException
1176 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001177 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001178 }
1179
1180 /*
1181 /**********************************************************
1182 /* Factory methods used by factory for creating parser instances,
1183 /* overridable by sub-classes
1184 /**********************************************************
1185 */
1186
1187 /**
1188 * Overridable factory method that actually instantiates desired parser
1189 * given {@link InputStream} and context object.
1190 *<p>
1191 * This method is specifically designed to remain
1192 * compatible between minor versions so that sub-classes can count
1193 * on it being called as expected. That is, it is part of official
1194 * interface from sub-class perspective, although not a public
1195 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001196 *
1197 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001198 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001199 protected JsonParser _createParser(InputStream in, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001200 throws IOException, JsonParseException
1201 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001202 // As per [JACKSON-259], may want to fully disable canonicalization:
1203 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
1204 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1205 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1206 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001207 }
1208
1209 /**
1210 * @deprecated since 2.1 -- use {@link #_createParser(InputStream, IOContext)} instead
1211 */
1212 @Deprecated
1213 protected JsonParser _createJsonParser(InputStream in, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001214 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001215 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001216
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001217 /**
1218 * Overridable factory method that actually instantiates parser
1219 * using given {@link Reader} object for reading content.
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.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001226 *
1227 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001228 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001229 protected JsonParser _createParser(Reader r, IOContext ctxt)
1230 throws IOException, JsonParseException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001231 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001232 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
1233 _rootCharSymbols.makeChild(isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1234 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES)));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001235 }
1236
1237 /**
1238 * @deprecated since 2.1 -- use {@link #_createParser(Reader, IOContext)} instead
1239 */
1240 @Deprecated
1241 protected JsonParser _createJsonParser(Reader r, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001242 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001243 }
1244
1245 /**
1246 * Overridable factory method that actually instantiates parser
1247 * using given {@link Reader} object for reading content
1248 * passed as raw byte array.
1249 *<p>
1250 * This method is specifically designed to remain
1251 * compatible between minor versions so that sub-classes can count
1252 * on it being called as expected. That is, it is part of official
1253 * interface from sub-class perspective, although not a public
1254 * method available to users of factory implementations.
1255 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001256 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001257 throws IOException, JsonParseException
1258 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001259 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
1260 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1261 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1262 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001263 }
1264
1265 /**
1266 * @deprecated since 2.1 -- use {@link #_createParser(byte[], int, int, IOContext)} instead
1267 */
1268 @Deprecated
1269 protected JsonParser _createJsonParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001270 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001271 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001272
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001273 /*
1274 /**********************************************************
1275 /* Factory methods used by factory for creating generator instances,
1276 /* overridable by sub-classes
1277 /**********************************************************
1278 */
1279
1280 /**
1281 * Overridable factory method that actually instantiates generator for
1282 * given {@link Writer} and context object.
1283 *<p>
1284 * This method is specifically designed to remain
1285 * compatible between minor versions so that sub-classes can count
1286 * on it being called as expected. That is, it is part of official
1287 * interface from sub-class perspective, although not a public
1288 * method available to users of factory implementations.
1289 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001290 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt)
1291 throws IOException
1292 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001293 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1294 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001295 if (_characterEscapes != null) {
1296 gen.setCharacterEscapes(_characterEscapes);
1297 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001298 SerializableString rootSep = _rootValueSeparator;
1299 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1300 gen.setRootValueSeparator(rootSep);
1301 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001302 return gen;
1303 }
1304
1305 /**
Tatu Saloranta68194922012-11-15 20:34:29 -08001306 * @deprecated since 2.1 -- use {@link #_createGenerator(Writer, IOContext)} instead
1307 */
1308 @Deprecated
1309 protected JsonGenerator _createJsonGenerator(Writer out, IOContext ctxt)
1310 throws IOException
1311 {
1312 /* NOTE: MUST call the deprecated method until it is deleted, just so
1313 * that override still works as expected, for now.
1314 */
1315 return _createGenerator(out, ctxt);
1316 }
1317
1318 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001319 * Overridable factory method that actually instantiates generator for
1320 * given {@link OutputStream} and context object, using UTF-8 encoding.
1321 *<p>
1322 * This method is specifically designed to remain
1323 * compatible between minor versions so that sub-classes can count
1324 * on it being called as expected. That is, it is part of official
1325 * interface from sub-class perspective, although not a public
1326 * method available to users of factory implementations.
1327 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001328 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001329 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1330 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001331 if (_characterEscapes != null) {
1332 gen.setCharacterEscapes(_characterEscapes);
1333 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001334 SerializableString rootSep = _rootValueSeparator;
1335 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1336 gen.setRootValueSeparator(rootSep);
1337 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001338 return gen;
1339 }
1340
Tatu Saloranta68194922012-11-15 20:34:29 -08001341 /**
1342 * @deprecated since 2.1
1343 */
1344 @Deprecated
1345 protected JsonGenerator _createUTF8JsonGenerator(OutputStream out, IOContext ctxt)
1346 throws IOException
1347 {
1348 return _createUTF8Generator(out, ctxt);
1349 }
1350
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001351 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1352 {
1353 // note: this should not get called any more (caller checks, dispatches)
1354 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1355 return new UTF8Writer(ctxt, out);
1356 }
1357 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1358 return new OutputStreamWriter(out, enc.getJavaName());
1359 }
1360
1361 /*
1362 /**********************************************************
1363 /* Internal factory methods, other
1364 /**********************************************************
1365 */
1366
1367 /**
1368 * Overridable factory method that actually instantiates desired
1369 * context object.
1370 */
1371 protected IOContext _createContext(Object srcRef, boolean resourceManaged)
1372 {
1373 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1374 }
1375
1376 /**
1377 * Method used by factory to create buffer recycler instances
1378 * for parsers and generators.
1379 *<p>
1380 * Note: only public to give access for <code>ObjectMapper</code>
1381 */
1382 public BufferRecycler _getBufferRecycler()
1383 {
1384 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1385 BufferRecycler br = (ref == null) ? null : ref.get();
1386
1387 if (br == null) {
1388 br = new BufferRecycler();
1389 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1390 }
1391 return br;
1392 }
1393
1394 /**
1395 * Helper methods used for constructing an optimal stream for
1396 * parsers to use, when input is to be read from an URL.
1397 * This helps when reading file content via URL.
1398 */
1399 protected InputStream _optimizedStreamFromURL(URL url)
1400 throws IOException
1401 {
1402 if ("file".equals(url.getProtocol())) {
1403 /* Can not do this if the path refers
1404 * to a network drive on windows. This fixes the problem;
1405 * might not be needed on all platforms (NFS?), but should not
1406 * matter a lot: performance penalty of extra wrapping is more
1407 * relevant when accessing local file system.
1408 */
1409 String host = url.getHost();
1410 if (host == null || host.length() == 0) {
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001411 // [Issue#48]: Let's try to avoid probs with URL encoded stuff
1412 String path = url.getPath();
1413 if (path.indexOf('%') < 0) {
1414 return new FileInputStream(url.getPath());
1415
1416 }
1417 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001418 }
1419 }
1420 return url.openStream();
1421 }
1422}