blob: 08be96e432aaac8d3ab3a9cb8ab9a368efe9a9bf [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 Salorantae92d04f2013-07-24 22:58:08 -070040@SuppressWarnings("resource")
Tatu Saloranta95f76a42012-10-05 13:04:23 -070041public class JsonFactory
42 implements Versioned,
43 java.io.Serializable // since 2.1 (for Android, mostly)
Tatu Salorantaf15531c2011-12-22 23:00:40 -080044{
45 /**
Tatu Salorantae92d04f2013-07-24 22:58:08 -070046 * Computed for Jackson 2.3.0 release
Tatu Saloranta95f76a42012-10-05 13:04:23 -070047 */
Tatu Salorantae92d04f2013-07-24 22:58:08 -070048 private static final long serialVersionUID = 3194418244231611666L;
Tatu Saloranta95f76a42012-10-05 13:04:23 -070049
Tatu Saloranta7b796a82013-04-27 10:18:30 -070050 /*
51 /**********************************************************
52 /* Helper types
53 /**********************************************************
Tatu Salorantaf15531c2011-12-22 23:00:40 -080054 */
Tatu Salorantae92d04f2013-07-24 22:58:08 -070055
Tatu07351902012-01-19 13:12:13 -080056 /**
57 * Enumeration that defines all on/off features that can only be
58 * changed for {@link JsonFactory}.
59 */
60 public enum Feature {
61
62 // // // Symbol handling (interning etc)
63
64 /**
65 * Feature that determines whether JSON object field names are
66 * to be canonicalized using {@link String#intern} or not:
67 * if enabled, all field names will be intern()ed (and caller
68 * can count on this being true for all such names); if disabled,
69 * no intern()ing is done. There may still be basic
70 * canonicalization (that is, same String will be used to represent
71 * all identical object property names for a single document).
72 *<p>
73 * Note: this setting only has effect if
74 * {@link #CANONICALIZE_FIELD_NAMES} is true -- otherwise no
75 * canonicalization of any sort is done.
76 *<p>
77 * This setting is enabled by default.
78 */
79 INTERN_FIELD_NAMES(true),
80
81 /**
82 * Feature that determines whether JSON object field names are
83 * to be canonicalized (details of how canonicalization is done
84 * then further specified by
85 * {@link #INTERN_FIELD_NAMES}).
86 *<p>
87 * This setting is enabled by default.
88 */
89 CANONICALIZE_FIELD_NAMES(true)
90
91 ;
92
93 /**
94 * Whether feature is enabled or disabled by default.
95 */
96 private final boolean _defaultState;
97
98 /**
99 * Method that calculates bit set (flags) of all features that
100 * are enabled by default.
101 */
102 public static int collectDefaults()
103 {
104 int flags = 0;
105 for (Feature f : values()) {
106 if (f.enabledByDefault()) {
107 flags |= f.getMask();
108 }
109 }
110 return flags;
111 }
112
113 private Feature(boolean defaultState)
114 {
115 _defaultState = defaultState;
116 }
117
118 public boolean enabledByDefault() { return _defaultState; }
119
120 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
121
122 public int getMask() { return (1 << ordinal()); }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700123 }
124
125 /*
126 /**********************************************************
127 /* Constants
128 /**********************************************************
129 */
130
131 /**
132 * Name used to identify JSON format
133 * (and returned by {@link #getFormatName()}
134 */
135 public final static String FORMAT_NAME_JSON = "JSON";
136
137 /**
138 * Bitfield (set of flags) of all factory features that are enabled by default.
139 */
140 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
141
142 /**
143 * Bitfield (set of flags) of all parser features that are enabled
144 * by default.
145 */
146 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
147
148 /**
149 * Bitfield (set of flags) of all generator features that are enabled
150 * by default.
151 */
152 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
153
154 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
155
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800156 /*
157 /**********************************************************
158 /* Buffer, symbol table management
159 /**********************************************************
160 */
161
162 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700163 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800164 * to a {@link BufferRecycler} used to provide a low-cost
165 * buffer recycling between reader and writer instances.
166 */
167 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
168 = new ThreadLocal<SoftReference<BufferRecycler>>();
169
170 /**
171 * Each factory comes equipped with a shared root symbol table.
172 * It should not be linked back to the original blueprint, to
173 * avoid contents from leaking between factories.
174 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700175 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800176
177 /**
178 * Alternative to the basic symbol table, some stream-based
179 * parsers use different name canonicalization method.
180 *<p>
181 * TODO: should clean up this; looks messy having 2 alternatives
182 * with not very clear differences.
183 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700184 protected final transient BytesToNameCanonicalizer _rootByteSymbols = BytesToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800185
186 /*
187 /**********************************************************
188 /* Configuration
189 /**********************************************************
190 */
191
192 /**
193 * Object that implements conversion functionality between
194 * Java objects and JSON content. For base JsonFactory implementation
195 * usually not set by default, but can be explicitly set.
196 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
197 * usually provide an implementation.
198 */
199 protected ObjectCodec _objectCodec;
200
201 /**
Tatu07351902012-01-19 13:12:13 -0800202 * Currently enabled factory features.
203 */
204 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
205
206 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800207 * Currently enabled parser features.
208 */
209 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
210
211 /**
212 * Currently enabled generator features.
213 */
214 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
215
216 /**
217 * Definition of custom character escapes to use for generators created
218 * by this factory, if any. If null, standard data format specific
219 * escapes are used.
220 */
221 protected CharacterEscapes _characterEscapes;
222
223 /**
224 * Optional helper object that may decorate input sources, to do
225 * additional processing on input during parsing.
226 */
227 protected InputDecorator _inputDecorator;
228
229 /**
230 * Optional helper object that may decorate output object, to do
231 * additional processing on output during content generation.
232 */
233 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700234
235 /**
236 * Separator used between root-level values, if any; null indicates
237 * "do not add separator".
238 * Default separator is a single space character.
239 *
240 * @since 2.1
241 */
242 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800243
244 /*
245 /**********************************************************
246 /* Construction
247 /**********************************************************
248 */
249
250 /**
251 * Default constructor used to create factory instances.
252 * Creation of a factory instance is a light-weight operation,
253 * but it is still a good idea to reuse limited number of
254 * factory instances (and quite often just a single instance):
255 * factories are used as context for storing some reused
256 * processing objects (such as symbol tables parsers use)
257 * and this reuse only works within context of a single
258 * factory instance.
259 */
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700260 public JsonFactory() { this((ObjectCodec) null); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800261
262 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
263
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700264 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700265 * Constructor used when copy()ing a factory instance.
266 *
267 * @since 2.2.1
268 */
269 protected JsonFactory(JsonFactory src, ObjectCodec codec)
270 {
271 _objectCodec = null;
272 _factoryFeatures = src._factoryFeatures;
273 _parserFeatures = src._parserFeatures;
274 _generatorFeatures = src._generatorFeatures;
275 _characterEscapes = src._characterEscapes;
276 _inputDecorator = src._inputDecorator;
277 _outputDecorator = src._outputDecorator;
278 _rootValueSeparator = src._rootValueSeparator;
279
280 /* 27-Apr-2013, tatu: How about symbol table; should we try to
281 * reuse shared symbol tables? Could be more efficient that way;
282 * although can slightly add to concurrency overhead.
283 */
284 }
285
286 /**
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700287 * Method for constructing a new {@link JsonFactory} that has
288 * the same settings as this instance, but is otherwise
289 * independent (i.e. nothing is actually shared, symbol tables
290 * are separate).
291 * Note that {@link ObjectCodec} reference is not copied but is
292 * set to null; caller typically needs to set it after calling
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700293 * this method. Reason for this is that the codec is used for
294 * callbacks, and assumption is that there is strict 1-to-1
295 * mapping between codec, factory. Caller has to, then, explicitly
296 * set codec after making the copy.
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700297 *
298 * @since 2.1
299 */
300 public JsonFactory copy()
301 {
302 _checkInvalidCopy(JsonFactory.class);
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700303 // as per above, do clear ObjectCodec
304 return new JsonFactory(this, null);
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700305 }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700306
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700307 /**
308 * @since 2.1
309 * @param exp
310 */
311 protected void _checkInvalidCopy(Class<?> exp)
312 {
313 if (getClass() != exp) {
314 throw new IllegalStateException("Failed copy(): "+getClass().getName()
315 +" (version: "+version()+") does not override copy(); it has to");
316 }
317 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700318
319 /*
320 /**********************************************************
321 /* Serializable overrides
322 /**********************************************************
323 */
324
325 /**
326 * Method that we need to override to actually make restoration go
327 * through constructors etc.
328 * Also: must be overridden by sub-classes as well.
329 */
330 protected Object readResolve() {
Tatu Salorantaf7741c52013-04-27 11:06:30 -0700331 return new JsonFactory(this, _objectCodec);
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700332 }
Tatu Salorantaa9e5c9f2013-08-28 19:39:59 -0700333
334 /*
335 /**********************************************************
336 /* Capability introspection
337 /**********************************************************
338 */
339
340 /**
341 * Introspection method that higher-level functionality may call
342 * to see whether underlying data format requires a stable ordering
343 * of object properties or not.
344 * This is usually used for determining
345 * whether to force a stable ordering (like alphabetic ordering by name)
346 * if no ordering if explicitly specified.
347 *<p>
348 * Default implementation returns <code>false</code> as JSON does NOT
349 * require stable ordering. Formats that require ordering include positional
350 * textual formats like <code>CSV</code>, and schema-based binary formats
351 * like <code>Avro</code>.
352 *
353 * @since 2.3
354 */
355 public boolean requiresPropertyOrdering() {
356 return false;
357 }
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700358
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800359 /*
360 /**********************************************************
361 /* Format detection functionality (since 1.8)
362 /**********************************************************
363 */
364
365 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700366 * Method that can be used to quickly check whether given schema
367 * is something that parsers and/or generators constructed by this
368 * factory could use. Note that this means possible use, at the level
369 * of data format (i.e. schema is for same data format as parsers and
370 * generators this factory constructs); individual schema instances
371 * may have further usage restrictions.
372 *
373 * @since 2.1
374 */
375 public boolean canUseSchema(FormatSchema schema) {
376 String ourFormat = getFormatName();
377 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
378 }
379
380 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800381 * Method that returns short textual id identifying format
382 * this factory supports.
383 *<p>
384 * Note: sub-classes should override this method; default
385 * implementation will return null for all sub-classes
386 */
387 public String getFormatName()
388 {
389 /* Somewhat nasty check: since we can't make this abstract
390 * (due to backwards compatibility concerns), need to prevent
391 * format name "leakage"
392 */
393 if (getClass() == JsonFactory.class) {
394 return FORMAT_NAME_JSON;
395 }
396 return null;
397 }
398
399 public MatchStrength hasFormat(InputAccessor acc) throws IOException
400 {
401 // since we can't keep this abstract, only implement for "vanilla" instance
402 if (getClass() == JsonFactory.class) {
403 return hasJSONFormat(acc);
404 }
405 return null;
406 }
407
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700408 /**
409 * Method that can be called to determine if a custom
410 * {@link ObjectCodec} is needed for binding data parsed
411 * using {@link JsonParser} constructed by this factory
412 * (which typically also implies the same for serialization
413 * with {@link JsonGenerator}).
414 *
415 * @return True if custom codec is needed with parsers and
416 * generators created by this factory; false if a general
417 * {@link ObjectCodec} is enough
418 *
419 * @since 2.1
420 */
421 public boolean requiresCustomCodec() {
422 return false;
423 }
424
425 /**
426 * Helper method that can be called to determine if content accessed
427 * using given accessor seems to be JSON content.
428 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800429 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
430 {
431 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700432 }
433
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800434 /*
435 /**********************************************************
436 /* Versioned
437 /**********************************************************
438 */
439
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800440 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800441 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800442 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800443 }
444
445 /*
446 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800447 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800448 /**********************************************************
449 */
450
451 /**
452 * Method for enabling or disabling specified parser feature
453 * (check {@link JsonParser.Feature} for list of features)
454 */
Tatu07351902012-01-19 13:12:13 -0800455 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
456 return state ? enable(f) : disable(f);
457 }
458
459 /**
460 * Method for enabling specified parser feature
461 * (check {@link JsonFactory.Feature} for list of features)
462 */
463 public JsonFactory enable(JsonFactory.Feature f) {
464 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800465 return this;
466 }
467
468 /**
Tatu07351902012-01-19 13:12:13 -0800469 * Method for disabling specified parser features
470 * (check {@link JsonFactory.Feature} for list of features)
471 */
472 public JsonFactory disable(JsonFactory.Feature f) {
473 _factoryFeatures &= ~f.getMask();
474 return this;
475 }
476
477 /**
478 * Checked whether specified parser feature is enabled.
479 */
480 public final boolean isEnabled(JsonFactory.Feature f) {
481 return (_factoryFeatures & f.getMask()) != 0;
482 }
483
484 /*
485 /**********************************************************
486 /* Configuration, parser configuration
487 /**********************************************************
488 */
489
490 /**
491 * Method for enabling or disabling specified parser feature
492 * (check {@link JsonParser.Feature} for list of features)
493 */
494 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
495 return state ? enable(f) : disable(f);
496 }
497
498 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800499 * Method for enabling specified parser feature
500 * (check {@link JsonParser.Feature} for list of features)
501 */
502 public JsonFactory enable(JsonParser.Feature f) {
503 _parserFeatures |= f.getMask();
504 return this;
505 }
506
507 /**
508 * Method for disabling specified parser features
509 * (check {@link JsonParser.Feature} for list of features)
510 */
511 public JsonFactory disable(JsonParser.Feature f) {
512 _parserFeatures &= ~f.getMask();
513 return this;
514 }
515
516 /**
517 * Checked whether specified parser feature is enabled.
518 */
519 public final boolean isEnabled(JsonParser.Feature f) {
520 return (_parserFeatures & f.getMask()) != 0;
521 }
522
523 /**
524 * Method for getting currently configured input decorator (if any;
525 * there is no default decorator).
526 */
527 public InputDecorator getInputDecorator() {
528 return _inputDecorator;
529 }
530
531 /**
532 * Method for overriding currently configured input decorator
533 */
534 public JsonFactory setInputDecorator(InputDecorator d) {
535 _inputDecorator = d;
536 return this;
537 }
538
539 /*
540 /**********************************************************
541 /* Configuration, generator settings
542 /**********************************************************
543 */
544
545 /**
546 * Method for enabling or disabling specified generator feature
547 * (check {@link JsonGenerator.Feature} for list of features)
548 */
549 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800550 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800551 }
552
553
554 /**
555 * Method for enabling specified generator features
556 * (check {@link JsonGenerator.Feature} for list of features)
557 */
558 public JsonFactory enable(JsonGenerator.Feature f) {
559 _generatorFeatures |= f.getMask();
560 return this;
561 }
562
563 /**
564 * Method for disabling specified generator feature
565 * (check {@link JsonGenerator.Feature} for list of features)
566 */
567 public JsonFactory disable(JsonGenerator.Feature f) {
568 _generatorFeatures &= ~f.getMask();
569 return this;
570 }
571
572 /**
573 * Check whether specified generator feature is enabled.
574 */
575 public final boolean isEnabled(JsonGenerator.Feature f) {
576 return (_generatorFeatures & f.getMask()) != 0;
577 }
578
579 /**
580 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
581 * it creates.
582 */
583 public CharacterEscapes getCharacterEscapes() {
584 return _characterEscapes;
585 }
586
587 /**
588 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
589 * it creates.
590 */
591 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
592 _characterEscapes = esc;
593 return this;
594 }
595
596 /**
597 * Method for getting currently configured output decorator (if any;
598 * there is no default decorator).
599 */
600 public OutputDecorator getOutputDecorator() {
601 return _outputDecorator;
602 }
603
604 /**
605 * Method for overriding currently configured output decorator
606 */
607 public JsonFactory setOutputDecorator(OutputDecorator d) {
608 _outputDecorator = d;
609 return this;
610 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700611
612 /**
613 * Method that allows overriding String used for separating root-level
614 * JSON values (default is single space character)
615 *
616 * @param sep Separator to use, if any; null means that no separator is
617 * automatically added
618 *
619 * @since 2.1
620 */
621 public JsonFactory setRootValueSeparator(String sep) {
622 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
623 return this;
624 }
625
626 /**
627 * @since 2.1
628 */
629 public String getRootValueSeparator() {
630 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
631 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800632
633 /*
634 /**********************************************************
635 /* Configuration, other
636 /**********************************************************
637 */
638
639 /**
640 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800641 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
642 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800643 * it constructs). This is needed to use data-binding methods
644 * of {@link JsonParser} and {@link JsonGenerator} instances.
645 */
646 public JsonFactory setCodec(ObjectCodec oc) {
647 _objectCodec = oc;
648 return this;
649 }
650
651 public ObjectCodec getCodec() { return _objectCodec; }
652
653 /*
654 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700655 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800656 /**********************************************************
657 */
658
659 /**
660 * Method for constructing JSON parser instance to parse
661 * contents of specified file. Encoding is auto-detected
662 * from contents according to JSON specification recommended
663 * mechanism.
664 *<p>
665 * Underlying input stream (needed for reading contents)
666 * will be <b>owned</b> (and managed, i.e. closed as need be) by
667 * the parser, since caller has no access to it.
668 *
669 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700670 *
671 * @since 2.1
672 */
673 public JsonParser createParser(File f)
674 throws IOException, JsonParseException
675 {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800676 // true, since we create InputStream from File
677 IOContext ctxt = _createContext(f, true);
678 InputStream in = new FileInputStream(f);
679 // [JACKSON-512]: allow wrapping with InputDecorator
680 if (_inputDecorator != null) {
681 in = _inputDecorator.decorate(ctxt, in);
682 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700683 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800684 }
685
686 /**
687 * Method for constructing JSON parser instance to parse
688 * contents of resource reference by given URL.
689 * Encoding is auto-detected
690 * from contents according to JSON specification recommended
691 * mechanism.
692 *<p>
693 * Underlying input stream (needed for reading contents)
694 * will be <b>owned</b> (and managed, i.e. closed as need be) by
695 * the parser, since caller has no access to it.
696 *
697 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800698 *
699 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800700 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800701 public JsonParser createParser(URL url)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800702 throws IOException, JsonParseException
703 {
704 // true, since we create InputStream from URL
705 IOContext ctxt = _createContext(url, true);
706 InputStream in = _optimizedStreamFromURL(url);
707 // [JACKSON-512]: allow wrapping with InputDecorator
708 if (_inputDecorator != null) {
709 in = _inputDecorator.decorate(ctxt, in);
710 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700711 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800712 }
713
714 /**
715 * Method for constructing JSON parser instance to parse
716 * the contents accessed via specified input stream.
717 *<p>
718 * The input stream will <b>not be owned</b> by
719 * the parser, it will still be managed (i.e. closed if
720 * end-of-stream is reacher, or parser close method called)
721 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
722 * is enabled.
723 *<p>
724 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700725 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800726 *
727 * @param in InputStream 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(InputStream in)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800732 throws IOException, JsonParseException
733 {
734 IOContext ctxt = _createContext(in, false);
735 // [JACKSON-512]: allow wrapping with InputDecorator
736 if (_inputDecorator != null) {
737 in = _inputDecorator.decorate(ctxt, in);
738 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700739 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800740 }
741
742 /**
743 * Method for constructing parser for parsing
744 * the contents accessed via specified Reader.
745 <p>
746 * The read stream will <b>not be owned</b> by
747 * the parser, it will still be managed (i.e. closed if
748 * end-of-stream is reacher, or parser close method called)
749 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
750 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800751 *
752 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800753 *
754 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800755 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800756 public JsonParser createParser(Reader r)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800757 throws IOException, JsonParseException
758 {
759 // false -> we do NOT own Reader (did not create it)
760 IOContext ctxt = _createContext(r, false);
761 // [JACKSON-512]: allow wrapping with InputDecorator
762 if (_inputDecorator != null) {
763 r = _inputDecorator.decorate(ctxt, r);
764 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700765 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800766 }
767
768 /**
769 * Method for constructing parser for parsing
770 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800771 *
772 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800773 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800774 public JsonParser createParser(byte[] data)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800775 throws IOException, JsonParseException
776 {
777 IOContext ctxt = _createContext(data, true);
778 // [JACKSON-512]: allow wrapping with InputDecorator
779 if (_inputDecorator != null) {
780 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
781 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700782 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800783 }
784 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700785 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800786 }
787
788 /**
789 * Method for constructing parser for parsing
790 * the contents of given byte array.
791 *
792 * @param data Buffer that contains data to parse
793 * @param offset Offset of the first data byte within buffer
794 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800795 *
796 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800797 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800798 public JsonParser createParser(byte[] data, int offset, int len)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800799 throws IOException, JsonParseException
800 {
801 IOContext ctxt = _createContext(data, true);
802 // [JACKSON-512]: allow wrapping with InputDecorator
803 if (_inputDecorator != null) {
804 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
805 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700806 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800807 }
808 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800809 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800810 }
811
812 /**
813 * Method for constructing parser for parsing
814 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800815 *
816 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800817 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800818 public JsonParser createParser(String content)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800819 throws IOException, JsonParseException
820 {
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700821 Reader r = new StringReader(content);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800822 // true -> we own the Reader (and must close); not a big deal
823 IOContext ctxt = _createContext(r, true);
824 // [JACKSON-512]: allow wrapping with InputDecorator
825 if (_inputDecorator != null) {
826 r = _inputDecorator.decorate(ctxt, r);
827 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700828 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800829 }
830
831 /*
832 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800833 /* Parser factories (old ones, as per [Issue-25])
834 /**********************************************************
835 */
836
837 /**
838 * Method for constructing JSON parser instance to parse
839 * contents of specified file. 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 f File that contains JSON content to parse
851 *
852 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
853 */
854 @Deprecated
855 public JsonParser createJsonParser(File f)
856 throws IOException, JsonParseException
857 {
858 return createParser(f);
859 }
860
861 /**
862 * Method for constructing JSON parser instance to parse
863 * contents of resource reference by given URL.
864 * Encoding is auto-detected
865 * from contents according to JSON specification recommended
866 * mechanism.
867 *<p>
868 * Underlying input stream (needed for reading contents)
869 * will be <b>owned</b> (and managed, i.e. closed as need be) by
870 * the parser, since caller has no access to it.
871 *<p>
872 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
873 * instead, should call <code>createParser</code>.
874 *
875 * @param url URL pointing to resource that contains JSON content to parse
876 *
877 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
878 */
879 @Deprecated
880 public JsonParser createJsonParser(URL url)
881 throws IOException, JsonParseException
882 {
883 return createParser(url);
884 }
885
886 /**
887 * Method for constructing JSON parser instance to parse
888 * the contents accessed via specified input stream.
889 *<p>
890 * The input stream will <b>not be owned</b> by
891 * the parser, it will still be managed (i.e. closed if
892 * end-of-stream is reacher, or parser close method called)
893 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
894 * is enabled.
895 *<p>
896 * Note: no encoding argument is taken since it can always be
897 * auto-detected as suggested by JSON RFC.
898 *<p>
899 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
900 * instead, should call <code>createParser</code>.
901 *
902 * @param in InputStream to use for reading JSON content to parse
903 *
904 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
905 */
906 @Deprecated
907 public JsonParser createJsonParser(InputStream in)
908 throws IOException, JsonParseException
909 {
910 return createParser(in);
911 }
912
913 /**
914 * Method for constructing parser for parsing
915 * the contents accessed via specified Reader.
916 <p>
917 * The read stream will <b>not be owned</b> by
918 * the parser, it will still be managed (i.e. closed if
919 * end-of-stream is reacher, or parser close method called)
920 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
921 * is enabled.
922 *<p>
923 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
924 * instead, should call <code>createParser</code>.
925 *
926 * @param r Reader to use for reading JSON content to parse
927 *
928 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
929 */
930 @Deprecated
931 public JsonParser createJsonParser(Reader r)
932 throws IOException, JsonParseException
933 {
934 return createParser(r);
935 }
936
937 /**
938 * Method for constructing parser for parsing
939 * the contents of given byte array.
940 *<p>
941 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
942 * instead, should call <code>createParser</code>.
943 *
944 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
945 */
946 @Deprecated
947 public JsonParser createJsonParser(byte[] data)
948 throws IOException, JsonParseException
949 {
950 return createParser(data);
951 }
952
953 /**
954 * Method for constructing parser for parsing
955 * the contents of given byte array.
956 *<p>
957 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
958 * instead, should call <code>createParser</code>.
959 *
960 * @param data Buffer that contains data to parse
961 * @param offset Offset of the first data byte within buffer
962 * @param len Length of contents to parse within buffer
963 *
964 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
965 */
966 @Deprecated
967 public JsonParser createJsonParser(byte[] data, int offset, int len)
968 throws IOException, JsonParseException
969 {
970 return createParser(data, offset, len);
971 }
972
973 /**
974 * Method for constructing parser for parsing
975 * contents of given String.
976 *
977 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
978 */
979 @Deprecated
980 public JsonParser createJsonParser(String content)
981 throws IOException, JsonParseException
982 {
983 return createParser(content);
984 }
985
986 /*
987 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700988 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800989 /**********************************************************
990 */
991
992 /**
993 * Method for constructing JSON generator for writing JSON content
994 * using specified output stream.
995 * Encoding to use must be specified, and needs to be one of available
996 * types (as per JSON specification).
997 *<p>
998 * Underlying stream <b>is NOT owned</b> by the generator constructed,
999 * so that generator will NOT close the output stream when
1000 * {@link JsonGenerator#close} is called (unless auto-closing
1001 * feature,
1002 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1003 * is enabled).
1004 * Using application needs to close it explicitly if this is the case.
1005 *<p>
1006 * Note: there are formats that use fixed encoding (like most binary data formats)
1007 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001008 *
1009 * @param out OutputStream to use for writing JSON content
1010 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001011 *
1012 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001013 */
1014 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
1015 throws IOException
1016 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001017 // false -> we won't manage the stream unless explicitly directed to
1018 IOContext ctxt = _createContext(out, false);
1019 ctxt.setEncoding(enc);
1020 if (enc == JsonEncoding.UTF8) {
1021 // [JACKSON-512]: allow wrapping with _outputDecorator
1022 if (_outputDecorator != null) {
1023 out = _outputDecorator.decorate(ctxt, out);
1024 }
1025 return _createUTF8Generator(out, ctxt);
1026 }
1027 Writer w = _createWriter(out, enc, ctxt);
1028 // [JACKSON-512]: allow wrapping with _outputDecorator
1029 if (_outputDecorator != null) {
1030 w = _outputDecorator.decorate(ctxt, w);
1031 }
1032 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001033 }
1034
1035 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001036 * Convenience method for constructing generator that uses default
1037 * encoding of the format (UTF-8 for JSON and most other data formats).
1038 *<p>
1039 * Note: there are formats that use fixed encoding (like most binary data formats).
1040 *
1041 * @since 2.1
1042 */
1043 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1044 return createGenerator(out, JsonEncoding.UTF8);
1045 }
1046
1047 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001048 * Method for constructing JSON generator for writing JSON content
1049 * using specified Writer.
1050 *<p>
1051 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1052 * so that generator will NOT close the Reader when
1053 * {@link JsonGenerator#close} is called (unless auto-closing
1054 * feature,
1055 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1056 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001057 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001058 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001059 *
1060 * @param out Writer to use for writing JSON content
1061 */
1062 public JsonGenerator createGenerator(Writer out)
1063 throws IOException
1064 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001065 IOContext ctxt = _createContext(out, false);
1066 // [JACKSON-512]: allow wrapping with _outputDecorator
1067 if (_outputDecorator != null) {
1068 out = _outputDecorator.decorate(ctxt, out);
1069 }
1070 return _createGenerator(out, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001071 }
1072
1073 /**
1074 * Method for constructing JSON generator for writing JSON content
1075 * to specified file, overwriting contents it might have (or creating
1076 * it if such file does not yet exist).
1077 * Encoding to use must be specified, and needs to be one of available
1078 * types (as per JSON specification).
1079 *<p>
1080 * Underlying stream <b>is owned</b> by the generator constructed,
1081 * i.e. generator will handle closing of file when
1082 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001083 *
1084 * @param f File to write contents to
1085 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001086 *
1087 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001088 */
1089 public JsonGenerator createGenerator(File f, JsonEncoding enc)
1090 throws IOException
1091 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001092 OutputStream out = new FileOutputStream(f);
1093 // true -> yes, we have to manage the stream since we created it
1094 IOContext ctxt = _createContext(out, true);
1095 ctxt.setEncoding(enc);
1096 if (enc == JsonEncoding.UTF8) {
1097 // [JACKSON-512]: allow wrapping with _outputDecorator
1098 if (_outputDecorator != null) {
1099 out = _outputDecorator.decorate(ctxt, out);
1100 }
1101 return _createUTF8Generator(out, ctxt);
1102 }
1103 Writer w = _createWriter(out, enc, ctxt);
1104 // [JACKSON-512]: allow wrapping with _outputDecorator
1105 if (_outputDecorator != null) {
1106 w = _outputDecorator.decorate(ctxt, w);
1107 }
1108 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001109 }
1110
1111 /*
1112 /**********************************************************
1113 /* Generator factories, old (as per [Issue-25]
1114 /**********************************************************
1115 */
1116
1117 /**
1118 * Method for constructing JSON generator for writing JSON content
1119 * using specified output stream.
1120 * Encoding to use must be specified, and needs to be one of available
1121 * types (as per JSON specification).
1122 *<p>
1123 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1124 * so that generator will NOT close the output stream when
1125 * {@link JsonGenerator#close} is called (unless auto-closing
1126 * feature,
1127 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1128 * is enabled).
1129 * Using application needs to close it explicitly if this is the case.
1130 *<p>
1131 * Note: there are formats that use fixed encoding (like most binary data formats)
1132 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001133 *
1134 * @param out OutputStream to use for writing JSON content
1135 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001136 *
1137 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001138 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001139 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001140 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc)
1141 throws IOException
1142 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001143 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001144 }
1145
1146 /**
1147 * Method for constructing JSON generator for writing JSON content
1148 * using specified Writer.
1149 *<p>
1150 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1151 * so that generator will NOT close the Reader when
1152 * {@link JsonGenerator#close} is called (unless auto-closing
1153 * feature,
1154 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1155 * Using application needs to close it explicitly.
1156 *
1157 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001158 *
1159 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001160 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001161 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001162 public JsonGenerator createJsonGenerator(Writer out)
1163 throws IOException
1164 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001165 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001166 }
1167
1168 /**
1169 * Convenience method for constructing generator that uses default
1170 * encoding of the format (UTF-8 for JSON and most other data formats).
1171 *<p>
1172 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001173 *
1174 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001175 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001176 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001177 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001178 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001179 }
1180
1181 /**
1182 * Method for constructing JSON generator for writing JSON content
1183 * to specified file, overwriting contents it might have (or creating
1184 * it if such file does not yet exist).
1185 * Encoding to use must be specified, and needs to be one of available
1186 * types (as per JSON specification).
1187 *<p>
1188 * Underlying stream <b>is owned</b> by the generator constructed,
1189 * i.e. generator will handle closing of file when
1190 * {@link JsonGenerator#close} is called.
1191 *
1192 * @param f File to write contents to
1193 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001194 *
1195 *
1196 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001197 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001198 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001199 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc)
1200 throws IOException
1201 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001202 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001203 }
1204
1205 /*
1206 /**********************************************************
1207 /* Factory methods used by factory for creating parser instances,
1208 /* overridable by sub-classes
1209 /**********************************************************
1210 */
1211
1212 /**
1213 * Overridable factory method that actually instantiates desired parser
1214 * given {@link InputStream} and context object.
1215 *<p>
1216 * This method is specifically designed to remain
1217 * compatible between minor versions so that sub-classes can count
1218 * on it being called as expected. That is, it is part of official
1219 * interface from sub-class perspective, although not a public
1220 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001221 *
1222 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001223 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001224 protected JsonParser _createParser(InputStream in, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001225 throws IOException, JsonParseException
1226 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001227 // As per [JACKSON-259], may want to fully disable canonicalization:
1228 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
1229 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1230 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1231 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001232 }
1233
1234 /**
1235 * @deprecated since 2.1 -- use {@link #_createParser(InputStream, IOContext)} instead
1236 */
1237 @Deprecated
1238 protected JsonParser _createJsonParser(InputStream in, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001239 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001240 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001241
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001242 /**
1243 * Overridable factory method that actually instantiates parser
1244 * using given {@link Reader} object for reading content.
1245 *<p>
1246 * This method is specifically designed to remain
1247 * compatible between minor versions so that sub-classes can count
1248 * on it being called as expected. That is, it is part of official
1249 * interface from sub-class perspective, although not a public
1250 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001251 *
1252 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001253 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001254 protected JsonParser _createParser(Reader r, IOContext ctxt)
1255 throws IOException, JsonParseException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001256 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001257 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
1258 _rootCharSymbols.makeChild(isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1259 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES)));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001260 }
1261
1262 /**
1263 * @deprecated since 2.1 -- use {@link #_createParser(Reader, IOContext)} instead
1264 */
1265 @Deprecated
1266 protected JsonParser _createJsonParser(Reader r, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001267 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001268 }
1269
1270 /**
1271 * Overridable factory method that actually instantiates parser
1272 * using given {@link Reader} object for reading content
1273 * passed as raw byte array.
1274 *<p>
1275 * This method is specifically designed to remain
1276 * compatible between minor versions so that sub-classes can count
1277 * on it being called as expected. That is, it is part of official
1278 * interface from sub-class perspective, although not a public
1279 * method available to users of factory implementations.
1280 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001281 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001282 throws IOException, JsonParseException
1283 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001284 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
1285 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1286 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1287 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001288 }
1289
1290 /**
1291 * @deprecated since 2.1 -- use {@link #_createParser(byte[], int, int, IOContext)} instead
1292 */
1293 @Deprecated
1294 protected JsonParser _createJsonParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001295 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001296 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001297
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001298 /*
1299 /**********************************************************
1300 /* Factory methods used by factory for creating generator instances,
1301 /* overridable by sub-classes
1302 /**********************************************************
1303 */
1304
1305 /**
1306 * Overridable factory method that actually instantiates generator for
1307 * given {@link Writer} and context object.
1308 *<p>
1309 * This method is specifically designed to remain
1310 * compatible between minor versions so that sub-classes can count
1311 * on it being called as expected. That is, it is part of official
1312 * interface from sub-class perspective, although not a public
1313 * method available to users of factory implementations.
1314 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001315 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt)
1316 throws IOException
1317 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001318 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1319 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001320 if (_characterEscapes != null) {
1321 gen.setCharacterEscapes(_characterEscapes);
1322 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001323 SerializableString rootSep = _rootValueSeparator;
1324 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1325 gen.setRootValueSeparator(rootSep);
1326 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001327 return gen;
1328 }
1329
1330 /**
Tatu Saloranta68194922012-11-15 20:34:29 -08001331 * @deprecated since 2.1 -- use {@link #_createGenerator(Writer, IOContext)} instead
1332 */
1333 @Deprecated
1334 protected JsonGenerator _createJsonGenerator(Writer out, IOContext ctxt)
1335 throws IOException
1336 {
1337 /* NOTE: MUST call the deprecated method until it is deleted, just so
1338 * that override still works as expected, for now.
1339 */
1340 return _createGenerator(out, ctxt);
1341 }
1342
1343 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001344 * Overridable factory method that actually instantiates generator for
1345 * given {@link OutputStream} and context object, using UTF-8 encoding.
1346 *<p>
1347 * This method is specifically designed to remain
1348 * compatible between minor versions so that sub-classes can count
1349 * on it being called as expected. That is, it is part of official
1350 * interface from sub-class perspective, although not a public
1351 * method available to users of factory implementations.
1352 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001353 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001354 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1355 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001356 if (_characterEscapes != null) {
1357 gen.setCharacterEscapes(_characterEscapes);
1358 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001359 SerializableString rootSep = _rootValueSeparator;
1360 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1361 gen.setRootValueSeparator(rootSep);
1362 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001363 return gen;
1364 }
1365
Tatu Saloranta68194922012-11-15 20:34:29 -08001366 /**
1367 * @deprecated since 2.1
1368 */
1369 @Deprecated
1370 protected JsonGenerator _createUTF8JsonGenerator(OutputStream out, IOContext ctxt)
1371 throws IOException
1372 {
1373 return _createUTF8Generator(out, ctxt);
1374 }
1375
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001376 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1377 {
1378 // note: this should not get called any more (caller checks, dispatches)
1379 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1380 return new UTF8Writer(ctxt, out);
1381 }
1382 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1383 return new OutputStreamWriter(out, enc.getJavaName());
1384 }
1385
1386 /*
1387 /**********************************************************
1388 /* Internal factory methods, other
1389 /**********************************************************
1390 */
1391
1392 /**
1393 * Overridable factory method that actually instantiates desired
1394 * context object.
1395 */
1396 protected IOContext _createContext(Object srcRef, boolean resourceManaged)
1397 {
1398 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1399 }
1400
1401 /**
1402 * Method used by factory to create buffer recycler instances
1403 * for parsers and generators.
1404 *<p>
1405 * Note: only public to give access for <code>ObjectMapper</code>
1406 */
1407 public BufferRecycler _getBufferRecycler()
1408 {
1409 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1410 BufferRecycler br = (ref == null) ? null : ref.get();
1411
1412 if (br == null) {
1413 br = new BufferRecycler();
1414 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1415 }
1416 return br;
1417 }
1418
1419 /**
1420 * Helper methods used for constructing an optimal stream for
1421 * parsers to use, when input is to be read from an URL.
1422 * This helps when reading file content via URL.
1423 */
1424 protected InputStream _optimizedStreamFromURL(URL url)
1425 throws IOException
1426 {
1427 if ("file".equals(url.getProtocol())) {
1428 /* Can not do this if the path refers
1429 * to a network drive on windows. This fixes the problem;
1430 * might not be needed on all platforms (NFS?), but should not
1431 * matter a lot: performance penalty of extra wrapping is more
1432 * relevant when accessing local file system.
1433 */
1434 String host = url.getHost();
1435 if (host == null || host.length() == 0) {
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001436 // [Issue#48]: Let's try to avoid probs with URL encoded stuff
1437 String path = url.getPath();
1438 if (path.indexOf('%') < 0) {
1439 return new FileInputStream(url.getPath());
1440
1441 }
1442 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001443 }
1444 }
1445 return url.openStream();
1446 }
1447}