blob: 68e06834f0bec92930194e20cb54e5e36d772e25 [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 */
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800102 public static int collectDefaults() {
Tatu07351902012-01-19 13:12:13 -0800103 int flags = 0;
104 for (Feature f : values()) {
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800105 if (f.enabledByDefault()) { flags |= f.getMask(); }
Tatu07351902012-01-19 13:12:13 -0800106 }
107 return flags;
108 }
109
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800110 private Feature(boolean defaultState) { _defaultState = defaultState; }
Tatu07351902012-01-19 13:12:13 -0800111
112 public boolean enabledByDefault() { return _defaultState; }
Tatu07351902012-01-19 13:12:13 -0800113 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
Tatu07351902012-01-19 13:12:13 -0800114 public int getMask() { return (1 << ordinal()); }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700115 }
116
117 /*
118 /**********************************************************
119 /* Constants
120 /**********************************************************
121 */
122
123 /**
124 * Name used to identify JSON format
125 * (and returned by {@link #getFormatName()}
126 */
127 public final static String FORMAT_NAME_JSON = "JSON";
128
129 /**
130 * Bitfield (set of flags) of all factory features that are enabled by default.
131 */
132 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
133
134 /**
135 * Bitfield (set of flags) of all parser features that are enabled
136 * by default.
137 */
138 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
139
140 /**
141 * Bitfield (set of flags) of all generator features that are enabled
142 * by default.
143 */
144 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
145
146 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
147
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800148 /*
149 /**********************************************************
150 /* Buffer, symbol table management
151 /**********************************************************
152 */
153
154 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700155 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800156 * to a {@link BufferRecycler} used to provide a low-cost
157 * buffer recycling between reader and writer instances.
158 */
159 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
160 = new ThreadLocal<SoftReference<BufferRecycler>>();
161
162 /**
163 * Each factory comes equipped with a shared root symbol table.
164 * It should not be linked back to the original blueprint, to
165 * avoid contents from leaking between factories.
166 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700167 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800168
169 /**
170 * Alternative to the basic symbol table, some stream-based
171 * parsers use different name canonicalization method.
172 *<p>
173 * TODO: should clean up this; looks messy having 2 alternatives
174 * with not very clear differences.
175 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700176 protected final transient BytesToNameCanonicalizer _rootByteSymbols = BytesToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800177
178 /*
179 /**********************************************************
180 /* Configuration
181 /**********************************************************
182 */
183
184 /**
185 * Object that implements conversion functionality between
186 * Java objects and JSON content. For base JsonFactory implementation
187 * usually not set by default, but can be explicitly set.
188 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
189 * usually provide an implementation.
190 */
191 protected ObjectCodec _objectCodec;
192
193 /**
Tatu07351902012-01-19 13:12:13 -0800194 * Currently enabled factory features.
195 */
196 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
197
198 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800199 * Currently enabled parser features.
200 */
201 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
202
203 /**
204 * Currently enabled generator features.
205 */
206 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
207
208 /**
209 * Definition of custom character escapes to use for generators created
210 * by this factory, if any. If null, standard data format specific
211 * escapes are used.
212 */
213 protected CharacterEscapes _characterEscapes;
214
215 /**
216 * Optional helper object that may decorate input sources, to do
217 * additional processing on input during parsing.
218 */
219 protected InputDecorator _inputDecorator;
220
221 /**
222 * Optional helper object that may decorate output object, to do
223 * additional processing on output during content generation.
224 */
225 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700226
227 /**
228 * Separator used between root-level values, if any; null indicates
229 * "do not add separator".
230 * Default separator is a single space character.
231 *
232 * @since 2.1
233 */
234 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800235
236 /*
237 /**********************************************************
238 /* Construction
239 /**********************************************************
240 */
241
242 /**
243 * Default constructor used to create factory instances.
244 * Creation of a factory instance is a light-weight operation,
245 * but it is still a good idea to reuse limited number of
246 * factory instances (and quite often just a single instance):
247 * factories are used as context for storing some reused
248 * processing objects (such as symbol tables parsers use)
249 * and this reuse only works within context of a single
250 * factory instance.
251 */
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700252 public JsonFactory() { this((ObjectCodec) null); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800253
254 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
255
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700256 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700257 * Constructor used when copy()ing a factory instance.
258 *
259 * @since 2.2.1
260 */
261 protected JsonFactory(JsonFactory src, ObjectCodec codec)
262 {
263 _objectCodec = null;
264 _factoryFeatures = src._factoryFeatures;
265 _parserFeatures = src._parserFeatures;
266 _generatorFeatures = src._generatorFeatures;
267 _characterEscapes = src._characterEscapes;
268 _inputDecorator = src._inputDecorator;
269 _outputDecorator = src._outputDecorator;
270 _rootValueSeparator = src._rootValueSeparator;
271
272 /* 27-Apr-2013, tatu: How about symbol table; should we try to
273 * reuse shared symbol tables? Could be more efficient that way;
274 * although can slightly add to concurrency overhead.
275 */
276 }
277
278 /**
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700279 * Method for constructing a new {@link JsonFactory} that has
280 * the same settings as this instance, but is otherwise
281 * independent (i.e. nothing is actually shared, symbol tables
282 * are separate).
283 * Note that {@link ObjectCodec} reference is not copied but is
284 * set to null; caller typically needs to set it after calling
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700285 * this method. Reason for this is that the codec is used for
286 * callbacks, and assumption is that there is strict 1-to-1
287 * mapping between codec, factory. Caller has to, then, explicitly
288 * set codec after making the copy.
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700289 *
290 * @since 2.1
291 */
292 public JsonFactory copy()
293 {
294 _checkInvalidCopy(JsonFactory.class);
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700295 // as per above, do clear ObjectCodec
296 return new JsonFactory(this, null);
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700297 }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700298
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700299 /**
300 * @since 2.1
301 * @param exp
302 */
303 protected void _checkInvalidCopy(Class<?> exp)
304 {
305 if (getClass() != exp) {
306 throw new IllegalStateException("Failed copy(): "+getClass().getName()
307 +" (version: "+version()+") does not override copy(); it has to");
308 }
309 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700310
311 /*
312 /**********************************************************
313 /* Serializable overrides
314 /**********************************************************
315 */
316
317 /**
318 * Method that we need to override to actually make restoration go
319 * through constructors etc.
320 * Also: must be overridden by sub-classes as well.
321 */
322 protected Object readResolve() {
Tatu Salorantaf7741c52013-04-27 11:06:30 -0700323 return new JsonFactory(this, _objectCodec);
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700324 }
Tatu Salorantaa9e5c9f2013-08-28 19:39:59 -0700325
326 /*
327 /**********************************************************
328 /* Capability introspection
329 /**********************************************************
330 */
331
332 /**
333 * Introspection method that higher-level functionality may call
334 * to see whether underlying data format requires a stable ordering
335 * of object properties or not.
336 * This is usually used for determining
337 * whether to force a stable ordering (like alphabetic ordering by name)
338 * if no ordering if explicitly specified.
339 *<p>
340 * Default implementation returns <code>false</code> as JSON does NOT
341 * require stable ordering. Formats that require ordering include positional
342 * textual formats like <code>CSV</code>, and schema-based binary formats
343 * like <code>Avro</code>.
344 *
345 * @since 2.3
346 */
347 public boolean requiresPropertyOrdering() {
348 return false;
349 }
Tatu Salorantab8835442013-09-14 12:12:57 -0700350
351 /**
352 * Introspection method that higher-level functionality may call
353 * to see whether underlying data format can read and write binary
354 * data natively; that is, embeded it as-is without using encodings
355 * such as Base64.
356 *<p>
357 * Default implementation returns <code>false</code> as JSON does not
358 * support native access: all binary content must use Base64 encoding.
359 * Most binary formats (like Smile and Avro) support native binary content.
360 *
361 * @since 2.3
362 */
Tatu Saloranta67a3b192013-09-14 20:34:04 -0700363 public boolean canHandleBinaryNatively() {
Tatu Salorantab8835442013-09-14 12:12:57 -0700364 return false;
365 }
366
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800367 /*
368 /**********************************************************
369 /* Format detection functionality (since 1.8)
370 /**********************************************************
371 */
372
373 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700374 * Method that can be used to quickly check whether given schema
375 * is something that parsers and/or generators constructed by this
376 * factory could use. Note that this means possible use, at the level
377 * of data format (i.e. schema is for same data format as parsers and
378 * generators this factory constructs); individual schema instances
379 * may have further usage restrictions.
380 *
381 * @since 2.1
382 */
383 public boolean canUseSchema(FormatSchema schema) {
384 String ourFormat = getFormatName();
385 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
386 }
387
388 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800389 * Method that returns short textual id identifying format
390 * this factory supports.
391 *<p>
392 * Note: sub-classes should override this method; default
393 * implementation will return null for all sub-classes
394 */
395 public String getFormatName()
396 {
397 /* Somewhat nasty check: since we can't make this abstract
398 * (due to backwards compatibility concerns), need to prevent
399 * format name "leakage"
400 */
401 if (getClass() == JsonFactory.class) {
402 return FORMAT_NAME_JSON;
403 }
404 return null;
405 }
406
407 public MatchStrength hasFormat(InputAccessor acc) throws IOException
408 {
409 // since we can't keep this abstract, only implement for "vanilla" instance
410 if (getClass() == JsonFactory.class) {
411 return hasJSONFormat(acc);
412 }
413 return null;
414 }
415
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700416 /**
417 * Method that can be called to determine if a custom
418 * {@link ObjectCodec} is needed for binding data parsed
419 * using {@link JsonParser} constructed by this factory
420 * (which typically also implies the same for serialization
421 * with {@link JsonGenerator}).
422 *
423 * @return True if custom codec is needed with parsers and
424 * generators created by this factory; false if a general
425 * {@link ObjectCodec} is enough
426 *
427 * @since 2.1
428 */
429 public boolean requiresCustomCodec() {
430 return false;
431 }
432
433 /**
434 * Helper method that can be called to determine if content accessed
435 * using given accessor seems to be JSON content.
436 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800437 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
438 {
439 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700440 }
441
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800442 /*
443 /**********************************************************
444 /* Versioned
445 /**********************************************************
446 */
447
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800448 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800449 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800450 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800451 }
452
453 /*
454 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800455 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800456 /**********************************************************
457 */
458
459 /**
460 * Method for enabling or disabling specified parser feature
461 * (check {@link JsonParser.Feature} for list of features)
462 */
Tatu07351902012-01-19 13:12:13 -0800463 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
464 return state ? enable(f) : disable(f);
465 }
466
467 /**
468 * Method for enabling specified parser feature
469 * (check {@link JsonFactory.Feature} for list of features)
470 */
471 public JsonFactory enable(JsonFactory.Feature f) {
472 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800473 return this;
474 }
475
476 /**
Tatu07351902012-01-19 13:12:13 -0800477 * Method for disabling specified parser features
478 * (check {@link JsonFactory.Feature} for list of features)
479 */
480 public JsonFactory disable(JsonFactory.Feature f) {
481 _factoryFeatures &= ~f.getMask();
482 return this;
483 }
484
485 /**
486 * Checked whether specified parser feature is enabled.
487 */
488 public final boolean isEnabled(JsonFactory.Feature f) {
489 return (_factoryFeatures & f.getMask()) != 0;
490 }
491
492 /*
493 /**********************************************************
494 /* Configuration, parser configuration
495 /**********************************************************
496 */
497
498 /**
499 * Method for enabling or disabling specified parser feature
500 * (check {@link JsonParser.Feature} for list of features)
501 */
502 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
503 return state ? enable(f) : disable(f);
504 }
505
506 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800507 * Method for enabling specified parser feature
508 * (check {@link JsonParser.Feature} for list of features)
509 */
510 public JsonFactory enable(JsonParser.Feature f) {
511 _parserFeatures |= f.getMask();
512 return this;
513 }
514
515 /**
516 * Method for disabling specified parser features
517 * (check {@link JsonParser.Feature} for list of features)
518 */
519 public JsonFactory disable(JsonParser.Feature f) {
520 _parserFeatures &= ~f.getMask();
521 return this;
522 }
523
524 /**
525 * Checked whether specified parser feature is enabled.
526 */
527 public final boolean isEnabled(JsonParser.Feature f) {
528 return (_parserFeatures & f.getMask()) != 0;
529 }
530
531 /**
532 * Method for getting currently configured input decorator (if any;
533 * there is no default decorator).
534 */
535 public InputDecorator getInputDecorator() {
536 return _inputDecorator;
537 }
538
539 /**
540 * Method for overriding currently configured input decorator
541 */
542 public JsonFactory setInputDecorator(InputDecorator d) {
543 _inputDecorator = d;
544 return this;
545 }
546
547 /*
548 /**********************************************************
549 /* Configuration, generator settings
550 /**********************************************************
551 */
552
553 /**
554 * Method for enabling or disabling specified generator feature
555 * (check {@link JsonGenerator.Feature} for list of features)
556 */
557 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800558 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800559 }
560
561
562 /**
563 * Method for enabling specified generator features
564 * (check {@link JsonGenerator.Feature} for list of features)
565 */
566 public JsonFactory enable(JsonGenerator.Feature f) {
567 _generatorFeatures |= f.getMask();
568 return this;
569 }
570
571 /**
572 * Method for disabling specified generator feature
573 * (check {@link JsonGenerator.Feature} for list of features)
574 */
575 public JsonFactory disable(JsonGenerator.Feature f) {
576 _generatorFeatures &= ~f.getMask();
577 return this;
578 }
579
580 /**
581 * Check whether specified generator feature is enabled.
582 */
583 public final boolean isEnabled(JsonGenerator.Feature f) {
584 return (_generatorFeatures & f.getMask()) != 0;
585 }
586
587 /**
588 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
589 * it creates.
590 */
591 public CharacterEscapes getCharacterEscapes() {
592 return _characterEscapes;
593 }
594
595 /**
596 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
597 * it creates.
598 */
599 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
600 _characterEscapes = esc;
601 return this;
602 }
603
604 /**
605 * Method for getting currently configured output decorator (if any;
606 * there is no default decorator).
607 */
608 public OutputDecorator getOutputDecorator() {
609 return _outputDecorator;
610 }
611
612 /**
613 * Method for overriding currently configured output decorator
614 */
615 public JsonFactory setOutputDecorator(OutputDecorator d) {
616 _outputDecorator = d;
617 return this;
618 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700619
620 /**
621 * Method that allows overriding String used for separating root-level
622 * JSON values (default is single space character)
623 *
624 * @param sep Separator to use, if any; null means that no separator is
625 * automatically added
626 *
627 * @since 2.1
628 */
629 public JsonFactory setRootValueSeparator(String sep) {
630 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
631 return this;
632 }
633
634 /**
635 * @since 2.1
636 */
637 public String getRootValueSeparator() {
638 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
639 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800640
641 /*
642 /**********************************************************
643 /* Configuration, other
644 /**********************************************************
645 */
646
647 /**
648 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800649 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
650 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800651 * it constructs). This is needed to use data-binding methods
652 * of {@link JsonParser} and {@link JsonGenerator} instances.
653 */
654 public JsonFactory setCodec(ObjectCodec oc) {
655 _objectCodec = oc;
656 return this;
657 }
658
659 public ObjectCodec getCodec() { return _objectCodec; }
660
661 /*
662 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700663 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800664 /**********************************************************
665 */
666
667 /**
668 * Method for constructing JSON parser instance to parse
669 * contents of specified file. Encoding is auto-detected
670 * from contents according to JSON specification recommended
671 * mechanism.
672 *<p>
673 * Underlying input stream (needed for reading contents)
674 * will be <b>owned</b> (and managed, i.e. closed as need be) by
675 * the parser, since caller has no access to it.
676 *
677 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700678 *
679 * @since 2.1
680 */
681 public JsonParser createParser(File f)
682 throws IOException, JsonParseException
683 {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800684 // true, since we create InputStream from File
685 IOContext ctxt = _createContext(f, true);
686 InputStream in = new FileInputStream(f);
687 // [JACKSON-512]: allow wrapping with InputDecorator
688 if (_inputDecorator != null) {
689 in = _inputDecorator.decorate(ctxt, in);
690 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700691 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800692 }
693
694 /**
695 * Method for constructing JSON parser instance to parse
696 * contents of resource reference by given URL.
697 * Encoding is auto-detected
698 * from contents according to JSON specification recommended
699 * mechanism.
700 *<p>
701 * Underlying input stream (needed for reading contents)
702 * will be <b>owned</b> (and managed, i.e. closed as need be) by
703 * the parser, since caller has no access to it.
704 *
705 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800706 *
707 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800708 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800709 public JsonParser createParser(URL url)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800710 throws IOException, JsonParseException
711 {
712 // true, since we create InputStream from URL
713 IOContext ctxt = _createContext(url, true);
714 InputStream in = _optimizedStreamFromURL(url);
715 // [JACKSON-512]: allow wrapping with InputDecorator
716 if (_inputDecorator != null) {
717 in = _inputDecorator.decorate(ctxt, in);
718 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700719 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800720 }
721
722 /**
723 * Method for constructing JSON parser instance to parse
724 * the contents accessed via specified input stream.
725 *<p>
726 * The input stream will <b>not be owned</b> by
727 * the parser, it will still be managed (i.e. closed if
728 * end-of-stream is reacher, or parser close method called)
729 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
730 * is enabled.
731 *<p>
732 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700733 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800734 *
735 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800736 *
737 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800738 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800739 public JsonParser createParser(InputStream in)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800740 throws IOException, JsonParseException
741 {
742 IOContext ctxt = _createContext(in, false);
743 // [JACKSON-512]: allow wrapping with InputDecorator
744 if (_inputDecorator != null) {
745 in = _inputDecorator.decorate(ctxt, in);
746 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700747 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800748 }
749
750 /**
751 * Method for constructing parser for parsing
752 * the contents accessed via specified Reader.
753 <p>
754 * The read stream will <b>not be owned</b> by
755 * the parser, it will still be managed (i.e. closed if
756 * end-of-stream is reacher, or parser close method called)
757 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
758 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800759 *
760 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800761 *
762 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800763 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800764 public JsonParser createParser(Reader r)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800765 throws IOException, JsonParseException
766 {
767 // false -> we do NOT own Reader (did not create it)
768 IOContext ctxt = _createContext(r, false);
769 // [JACKSON-512]: allow wrapping with InputDecorator
770 if (_inputDecorator != null) {
771 r = _inputDecorator.decorate(ctxt, r);
772 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700773 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800774 }
775
776 /**
777 * Method for constructing parser for parsing
778 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800779 *
780 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800781 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800782 public JsonParser createParser(byte[] data)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800783 throws IOException, JsonParseException
784 {
785 IOContext ctxt = _createContext(data, true);
786 // [JACKSON-512]: allow wrapping with InputDecorator
787 if (_inputDecorator != null) {
788 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
789 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700790 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800791 }
792 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700793 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800794 }
795
796 /**
797 * Method for constructing parser for parsing
798 * the contents of given byte array.
799 *
800 * @param data Buffer that contains data to parse
801 * @param offset Offset of the first data byte within buffer
802 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800803 *
804 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800805 */
Tatu Saloranta68194922012-11-15 20:34:29 -0800806 public JsonParser createParser(byte[] data, int offset, int len)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800807 throws IOException, JsonParseException
808 {
809 IOContext ctxt = _createContext(data, true);
810 // [JACKSON-512]: allow wrapping with InputDecorator
811 if (_inputDecorator != null) {
812 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
813 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700814 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800815 }
816 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800817 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800818 }
819
820 /**
821 * Method for constructing parser for parsing
822 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800823 *
824 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800825 */
Tatu Salorantaefc23672013-12-30 19:09:39 -0800826 public JsonParser createParser(String content) throws IOException, JsonParseException
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800827 {
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700828 Reader r = new StringReader(content);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800829 // true -> we own the Reader (and must close); not a big deal
830 IOContext ctxt = _createContext(r, true);
831 // [JACKSON-512]: allow wrapping with InputDecorator
832 if (_inputDecorator != null) {
833 r = _inputDecorator.decorate(ctxt, r);
834 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700835 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800836 }
837
838 /*
839 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800840 /* Parser factories (old ones, as per [Issue-25])
841 /**********************************************************
842 */
843
844 /**
845 * Method for constructing JSON parser instance to parse
846 * contents of specified file. Encoding is auto-detected
847 * from contents according to JSON specification recommended
848 * mechanism.
849 *<p>
850 * Underlying input stream (needed for reading contents)
851 * will be <b>owned</b> (and managed, i.e. closed as need be) by
852 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800853 *
854 * @param f File that contains JSON content to parse
855 *
856 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
857 */
858 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800859 public JsonParser createJsonParser(File f) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800860 return createParser(f);
861 }
862
863 /**
864 * Method for constructing JSON parser instance to parse
865 * contents of resource reference by given URL.
866 * Encoding is auto-detected
867 * from contents according to JSON specification recommended
868 * mechanism.
869 *<p>
870 * Underlying input stream (needed for reading contents)
871 * will be <b>owned</b> (and managed, i.e. closed as need be) by
872 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800873 *
874 * @param url URL pointing to resource that contains JSON content to parse
875 *
876 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
877 */
878 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800879 public JsonParser createJsonParser(URL url) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800880 return createParser(url);
881 }
882
883 /**
884 * Method for constructing JSON parser instance to parse
885 * the contents accessed via specified input stream.
886 *<p>
887 * The input stream will <b>not be owned</b> by
888 * the parser, it will still be managed (i.e. closed if
889 * end-of-stream is reacher, or parser close method called)
890 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
891 * is enabled.
892 *<p>
893 * Note: no encoding argument is taken since it can always be
894 * auto-detected as suggested by JSON RFC.
Tatu Saloranta68194922012-11-15 20:34:29 -0800895 *
896 * @param in InputStream to use for reading JSON content to parse
897 *
898 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
899 */
900 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800901 public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800902 return createParser(in);
903 }
904
905 /**
906 * Method for constructing parser for parsing
907 * the contents accessed via specified Reader.
908 <p>
909 * The read stream will <b>not be owned</b> by
910 * the parser, it will still be managed (i.e. closed if
911 * end-of-stream is reacher, or parser close method called)
912 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
913 * is enabled.
Tatu Saloranta68194922012-11-15 20:34:29 -0800914 *
915 * @param r Reader to use for reading JSON content to parse
916 *
917 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
918 */
919 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800920 public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800921 return createParser(r);
922 }
923
924 /**
Tatu Salorantaefc23672013-12-30 19:09:39 -0800925 * Method for constructing parser for parsing the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800926 *
927 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
928 */
929 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800930 public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800931 return createParser(data);
932 }
933
934 /**
935 * Method for constructing parser for parsing
936 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800937 *
938 * @param data Buffer that contains data to parse
939 * @param offset Offset of the first data byte within buffer
940 * @param len Length of contents to parse within buffer
941 *
942 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
943 */
944 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800945 public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800946 return createParser(data, offset, len);
947 }
948
949 /**
950 * Method for constructing parser for parsing
951 * contents of given String.
952 *
953 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
954 */
955 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800956 public JsonParser createJsonParser(String content) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800957 return createParser(content);
958 }
959
960 /*
961 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700962 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800963 /**********************************************************
964 */
965
966 /**
967 * Method for constructing JSON generator for writing JSON content
968 * using specified output stream.
969 * Encoding to use must be specified, and needs to be one of available
970 * types (as per JSON specification).
971 *<p>
972 * Underlying stream <b>is NOT owned</b> by the generator constructed,
973 * so that generator will NOT close the output stream when
974 * {@link JsonGenerator#close} is called (unless auto-closing
975 * feature,
976 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
977 * is enabled).
978 * Using application needs to close it explicitly if this is the case.
979 *<p>
980 * Note: there are formats that use fixed encoding (like most binary data formats)
981 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700982 *
983 * @param out OutputStream to use for writing JSON content
984 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800985 *
986 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700987 */
988 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
989 throws IOException
990 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800991 // false -> we won't manage the stream unless explicitly directed to
992 IOContext ctxt = _createContext(out, false);
993 ctxt.setEncoding(enc);
994 if (enc == JsonEncoding.UTF8) {
995 // [JACKSON-512]: allow wrapping with _outputDecorator
996 if (_outputDecorator != null) {
997 out = _outputDecorator.decorate(ctxt, out);
998 }
999 return _createUTF8Generator(out, ctxt);
1000 }
1001 Writer w = _createWriter(out, enc, ctxt);
1002 // [JACKSON-512]: allow wrapping with _outputDecorator
1003 if (_outputDecorator != null) {
1004 w = _outputDecorator.decorate(ctxt, w);
1005 }
1006 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001007 }
1008
1009 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001010 * Convenience method for constructing generator that uses default
1011 * encoding of the format (UTF-8 for JSON and most other data formats).
1012 *<p>
1013 * Note: there are formats that use fixed encoding (like most binary data formats).
1014 *
1015 * @since 2.1
1016 */
1017 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1018 return createGenerator(out, JsonEncoding.UTF8);
1019 }
1020
1021 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001022 * Method for constructing JSON generator for writing JSON content
1023 * using specified Writer.
1024 *<p>
1025 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1026 * so that generator will NOT close the Reader when
1027 * {@link JsonGenerator#close} is called (unless auto-closing
1028 * feature,
1029 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1030 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001031 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001032 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001033 *
1034 * @param out Writer to use for writing JSON content
1035 */
1036 public JsonGenerator createGenerator(Writer out)
1037 throws IOException
1038 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001039 IOContext ctxt = _createContext(out, false);
1040 // [JACKSON-512]: allow wrapping with _outputDecorator
1041 if (_outputDecorator != null) {
1042 out = _outputDecorator.decorate(ctxt, out);
1043 }
1044 return _createGenerator(out, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001045 }
1046
1047 /**
1048 * Method for constructing JSON generator for writing JSON content
1049 * to specified file, overwriting contents it might have (or creating
1050 * it if such file does not yet exist).
1051 * Encoding to use must be specified, and needs to be one of available
1052 * types (as per JSON specification).
1053 *<p>
1054 * Underlying stream <b>is owned</b> by the generator constructed,
1055 * i.e. generator will handle closing of file when
1056 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001057 *
1058 * @param f File to write contents to
1059 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001060 *
1061 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001062 */
1063 public JsonGenerator createGenerator(File f, JsonEncoding enc)
1064 throws IOException
1065 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001066 OutputStream out = new FileOutputStream(f);
1067 // true -> yes, we have to manage the stream since we created it
1068 IOContext ctxt = _createContext(out, true);
1069 ctxt.setEncoding(enc);
1070 if (enc == JsonEncoding.UTF8) {
1071 // [JACKSON-512]: allow wrapping with _outputDecorator
1072 if (_outputDecorator != null) {
1073 out = _outputDecorator.decorate(ctxt, out);
1074 }
1075 return _createUTF8Generator(out, ctxt);
1076 }
1077 Writer w = _createWriter(out, enc, ctxt);
1078 // [JACKSON-512]: allow wrapping with _outputDecorator
1079 if (_outputDecorator != null) {
1080 w = _outputDecorator.decorate(ctxt, w);
1081 }
1082 return _createGenerator(w, ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001083 }
1084
1085 /*
1086 /**********************************************************
1087 /* Generator factories, old (as per [Issue-25]
1088 /**********************************************************
1089 */
1090
1091 /**
1092 * Method for constructing JSON generator for writing JSON content
1093 * using specified output stream.
1094 * Encoding to use must be specified, and needs to be one of available
1095 * types (as per JSON specification).
1096 *<p>
1097 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1098 * so that generator will NOT close the output stream when
1099 * {@link JsonGenerator#close} is called (unless auto-closing
1100 * feature,
1101 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1102 * is enabled).
1103 * Using application needs to close it explicitly if this is the case.
1104 *<p>
1105 * Note: there are formats that use fixed encoding (like most binary data formats)
1106 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001107 *
1108 * @param out OutputStream to use for writing JSON content
1109 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001110 *
1111 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001112 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001113 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001114 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc)
1115 throws IOException
1116 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001117 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001118 }
1119
1120 /**
1121 * Method for constructing JSON generator for writing JSON content
1122 * using specified Writer.
1123 *<p>
1124 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1125 * so that generator will NOT close the Reader when
1126 * {@link JsonGenerator#close} is called (unless auto-closing
1127 * feature,
1128 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1129 * Using application needs to close it explicitly.
1130 *
1131 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001132 *
1133 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001134 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001135 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001136 public JsonGenerator createJsonGenerator(Writer out)
1137 throws IOException
1138 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001139 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001140 }
1141
1142 /**
1143 * Convenience method for constructing generator that uses default
1144 * encoding of the format (UTF-8 for JSON and most other data formats).
1145 *<p>
1146 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001147 *
1148 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001149 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001150 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001151 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001152 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001153 }
1154
1155 /**
1156 * Method for constructing JSON generator for writing JSON content
1157 * to specified file, overwriting contents it might have (or creating
1158 * it if such file does not yet exist).
1159 * Encoding to use must be specified, and needs to be one of available
1160 * types (as per JSON specification).
1161 *<p>
1162 * Underlying stream <b>is owned</b> by the generator constructed,
1163 * i.e. generator will handle closing of file when
1164 * {@link JsonGenerator#close} is called.
1165 *
1166 * @param f File to write contents to
1167 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001168 *
1169 *
1170 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001171 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001172 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001173 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc)
1174 throws IOException
1175 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001176 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001177 }
1178
1179 /*
1180 /**********************************************************
1181 /* Factory methods used by factory for creating parser instances,
1182 /* overridable by sub-classes
1183 /**********************************************************
1184 */
1185
1186 /**
1187 * Overridable factory method that actually instantiates desired parser
1188 * given {@link InputStream} and context object.
1189 *<p>
1190 * This method is specifically designed to remain
1191 * compatible between minor versions so that sub-classes can count
1192 * on it being called as expected. That is, it is part of official
1193 * interface from sub-class perspective, although not a public
1194 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001195 *
1196 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001197 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001198 protected JsonParser _createParser(InputStream in, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001199 throws IOException, JsonParseException
1200 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001201 // As per [JACKSON-259], may want to fully disable canonicalization:
1202 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
1203 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1204 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1205 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001206 }
1207
1208 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001209 * Overridable factory method that actually instantiates parser
1210 * using given {@link Reader} object for reading content.
1211 *<p>
1212 * This method is specifically designed to remain
1213 * compatible between minor versions so that sub-classes can count
1214 * on it being called as expected. That is, it is part of official
1215 * interface from sub-class perspective, although not a public
1216 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001217 *
1218 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001219 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001220 protected JsonParser _createParser(Reader r, IOContext ctxt)
1221 throws IOException, JsonParseException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001222 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001223 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
1224 _rootCharSymbols.makeChild(isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1225 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES)));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001226 }
1227
1228 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001229 * Overridable factory method that actually instantiates parser
1230 * using given {@link Reader} object for reading content
1231 * passed as raw byte array.
1232 *<p>
1233 * This method is specifically designed to remain
1234 * compatible between minor versions so that sub-classes can count
1235 * on it being called as expected. That is, it is part of official
1236 * interface from sub-class perspective, although not a public
1237 * method available to users of factory implementations.
1238 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001239 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException, JsonParseException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001240 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001241 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
1242 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1243 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1244 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001245 }
1246
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001247 /*
1248 /**********************************************************
1249 /* Factory methods used by factory for creating generator instances,
1250 /* overridable by sub-classes
1251 /**********************************************************
1252 */
1253
1254 /**
1255 * Overridable factory method that actually instantiates generator for
1256 * given {@link Writer} and context object.
1257 *<p>
1258 * This method is specifically designed to remain
1259 * compatible between minor versions so that sub-classes can count
1260 * on it being called as expected. That is, it is part of official
1261 * interface from sub-class perspective, although not a public
1262 * method available to users of factory implementations.
1263 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001264 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001265 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001266 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1267 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001268 if (_characterEscapes != null) {
1269 gen.setCharacterEscapes(_characterEscapes);
1270 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001271 SerializableString rootSep = _rootValueSeparator;
1272 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1273 gen.setRootValueSeparator(rootSep);
1274 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001275 return gen;
1276 }
1277
1278 /**
1279 * Overridable factory method that actually instantiates generator for
1280 * given {@link OutputStream} and context object, using UTF-8 encoding.
1281 *<p>
1282 * This method is specifically designed to remain
1283 * compatible between minor versions so that sub-classes can count
1284 * on it being called as expected. That is, it is part of official
1285 * interface from sub-class perspective, although not a public
1286 * method available to users of factory implementations.
1287 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001288 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001289 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1290 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001291 if (_characterEscapes != null) {
1292 gen.setCharacterEscapes(_characterEscapes);
1293 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001294 SerializableString rootSep = _rootValueSeparator;
1295 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1296 gen.setRootValueSeparator(rootSep);
1297 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001298 return gen;
1299 }
1300
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001301 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1302 {
1303 // note: this should not get called any more (caller checks, dispatches)
1304 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1305 return new UTF8Writer(ctxt, out);
1306 }
1307 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1308 return new OutputStreamWriter(out, enc.getJavaName());
1309 }
1310
1311 /*
1312 /**********************************************************
1313 /* Internal factory methods, other
1314 /**********************************************************
1315 */
1316
1317 /**
1318 * Overridable factory method that actually instantiates desired
1319 * context object.
1320 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001321 protected IOContext _createContext(Object srcRef, boolean resourceManaged) {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001322 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1323 }
1324
1325 /**
1326 * Method used by factory to create buffer recycler instances
1327 * for parsers and generators.
1328 *<p>
1329 * Note: only public to give access for <code>ObjectMapper</code>
1330 */
1331 public BufferRecycler _getBufferRecycler()
1332 {
1333 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1334 BufferRecycler br = (ref == null) ? null : ref.get();
1335
1336 if (br == null) {
1337 br = new BufferRecycler();
1338 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1339 }
1340 return br;
1341 }
1342
1343 /**
1344 * Helper methods used for constructing an optimal stream for
1345 * parsers to use, when input is to be read from an URL.
1346 * This helps when reading file content via URL.
1347 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001348 protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001349 if ("file".equals(url.getProtocol())) {
1350 /* Can not do this if the path refers
1351 * to a network drive on windows. This fixes the problem;
1352 * might not be needed on all platforms (NFS?), but should not
1353 * matter a lot: performance penalty of extra wrapping is more
1354 * relevant when accessing local file system.
1355 */
1356 String host = url.getHost();
1357 if (host == null || host.length() == 0) {
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001358 // [Issue#48]: Let's try to avoid probs with URL encoded stuff
1359 String path = url.getPath();
1360 if (path.indexOf('%') < 0) {
1361 return new FileInputStream(url.getPath());
1362
1363 }
1364 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001365 }
1366 }
1367 return url.openStream();
1368 }
1369}