blob: 730413fb49d501a030513177adf3f2b861aab4a4 [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 /**
Tatu64aa9d22014-04-18 14:07:38 -070046 * Computed for Jackson 2.4.0 release
Tatu Saloranta95f76a42012-10-05 13:04:23 -070047 */
Tatu64aa9d22014-04-18 14:07:38 -070048 private static final long serialVersionUID = 3306684576057132431L;
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 */
Tatu Saloranta383bc8f2014-05-24 00:47:07 -070089 CANONICALIZE_FIELD_NAMES(true),
90
91 /**
92 * Feature that determines what happens if we encounter a case in symbol
93 * handling where number of hash collisions exceeds a safety threshold
94 * -- which almost certainly means a denial-of-service attack via generated
95 * duplicate hash codes.
96 * If feature is enabled, an {@link IllegalStateException} is
97 * thrown to indicate the suspected denial-of-service attack; if disabled, processing continues but
98 * canonicalization (and thereby <code>intern()</code>ing) is disabled) as protective
99 * measure.
100 *<p>
101 * This setting is enabled by default.
102 *
103 * @since 2.4
104 */
105 FAIL_ON_SYMBOL_HASH_OVERFLOW(true),
Tatu07351902012-01-19 13:12:13 -0800106
107 ;
108
109 /**
110 * Whether feature is enabled or disabled by default.
111 */
112 private final boolean _defaultState;
113
114 /**
115 * Method that calculates bit set (flags) of all features that
116 * are enabled by default.
117 */
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800118 public static int collectDefaults() {
Tatu07351902012-01-19 13:12:13 -0800119 int flags = 0;
120 for (Feature f : values()) {
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800121 if (f.enabledByDefault()) { flags |= f.getMask(); }
Tatu07351902012-01-19 13:12:13 -0800122 }
123 return flags;
124 }
125
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800126 private Feature(boolean defaultState) { _defaultState = defaultState; }
Tatu07351902012-01-19 13:12:13 -0800127
128 public boolean enabledByDefault() { return _defaultState; }
Tatu07351902012-01-19 13:12:13 -0800129 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
Tatu07351902012-01-19 13:12:13 -0800130 public int getMask() { return (1 << ordinal()); }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700131 }
132
133 /*
134 /**********************************************************
135 /* Constants
136 /**********************************************************
137 */
138
139 /**
140 * Name used to identify JSON format
141 * (and returned by {@link #getFormatName()}
142 */
143 public final static String FORMAT_NAME_JSON = "JSON";
144
145 /**
146 * Bitfield (set of flags) of all factory features that are enabled by default.
147 */
148 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
149
150 /**
151 * Bitfield (set of flags) of all parser features that are enabled
152 * by default.
153 */
154 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
155
156 /**
157 * Bitfield (set of flags) of all generator features that are enabled
158 * by default.
159 */
160 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
161
162 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
163
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800164 /*
165 /**********************************************************
166 /* Buffer, symbol table management
167 /**********************************************************
168 */
169
170 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700171 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800172 * to a {@link BufferRecycler} used to provide a low-cost
173 * buffer recycling between reader and writer instances.
174 */
175 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
176 = new ThreadLocal<SoftReference<BufferRecycler>>();
177
178 /**
179 * Each factory comes equipped with a shared root symbol table.
180 * It should not be linked back to the original blueprint, to
181 * avoid contents from leaking between factories.
182 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700183 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800184
185 /**
186 * Alternative to the basic symbol table, some stream-based
187 * parsers use different name canonicalization method.
188 *<p>
189 * TODO: should clean up this; looks messy having 2 alternatives
190 * with not very clear differences.
191 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700192 protected final transient BytesToNameCanonicalizer _rootByteSymbols = BytesToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800193
194 /*
195 /**********************************************************
196 /* Configuration
197 /**********************************************************
198 */
199
200 /**
201 * Object that implements conversion functionality between
202 * Java objects and JSON content. For base JsonFactory implementation
203 * usually not set by default, but can be explicitly set.
204 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
205 * usually provide an implementation.
206 */
207 protected ObjectCodec _objectCodec;
208
209 /**
Tatu07351902012-01-19 13:12:13 -0800210 * Currently enabled factory features.
211 */
212 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
213
214 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800215 * Currently enabled parser features.
216 */
217 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
218
219 /**
220 * Currently enabled generator features.
221 */
222 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
223
224 /**
225 * Definition of custom character escapes to use for generators created
226 * by this factory, if any. If null, standard data format specific
227 * escapes are used.
228 */
229 protected CharacterEscapes _characterEscapes;
230
231 /**
232 * Optional helper object that may decorate input sources, to do
233 * additional processing on input during parsing.
234 */
235 protected InputDecorator _inputDecorator;
236
237 /**
238 * Optional helper object that may decorate output object, to do
239 * additional processing on output during content generation.
240 */
241 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700242
243 /**
244 * Separator used between root-level values, if any; null indicates
245 * "do not add separator".
246 * Default separator is a single space character.
247 *
248 * @since 2.1
249 */
250 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800251
252 /*
253 /**********************************************************
254 /* Construction
255 /**********************************************************
256 */
257
258 /**
259 * Default constructor used to create factory instances.
260 * Creation of a factory instance is a light-weight operation,
261 * but it is still a good idea to reuse limited number of
262 * factory instances (and quite often just a single instance):
263 * factories are used as context for storing some reused
264 * processing objects (such as symbol tables parsers use)
265 * and this reuse only works within context of a single
266 * factory instance.
267 */
Gonçalo Silva2bba5dd2014-01-25 17:06:21 +0000268 public JsonFactory() { this(null); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800269
270 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
271
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700272 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700273 * Constructor used when copy()ing a factory instance.
274 *
275 * @since 2.2.1
276 */
277 protected JsonFactory(JsonFactory src, ObjectCodec codec)
278 {
279 _objectCodec = null;
280 _factoryFeatures = src._factoryFeatures;
281 _parserFeatures = src._parserFeatures;
282 _generatorFeatures = src._generatorFeatures;
283 _characterEscapes = src._characterEscapes;
284 _inputDecorator = src._inputDecorator;
285 _outputDecorator = src._outputDecorator;
286 _rootValueSeparator = src._rootValueSeparator;
287
288 /* 27-Apr-2013, tatu: How about symbol table; should we try to
289 * reuse shared symbol tables? Could be more efficient that way;
290 * although can slightly add to concurrency overhead.
291 */
292 }
293
294 /**
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700295 * Method for constructing a new {@link JsonFactory} that has
296 * the same settings as this instance, but is otherwise
297 * independent (i.e. nothing is actually shared, symbol tables
298 * are separate).
299 * Note that {@link ObjectCodec} reference is not copied but is
300 * set to null; caller typically needs to set it after calling
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700301 * this method. Reason for this is that the codec is used for
302 * callbacks, and assumption is that there is strict 1-to-1
303 * mapping between codec, factory. Caller has to, then, explicitly
304 * set codec after making the copy.
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700305 *
306 * @since 2.1
307 */
308 public JsonFactory copy()
309 {
310 _checkInvalidCopy(JsonFactory.class);
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700311 // as per above, do clear ObjectCodec
312 return new JsonFactory(this, null);
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700313 }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700314
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700315 /**
316 * @since 2.1
317 * @param exp
318 */
319 protected void _checkInvalidCopy(Class<?> exp)
320 {
321 if (getClass() != exp) {
322 throw new IllegalStateException("Failed copy(): "+getClass().getName()
323 +" (version: "+version()+") does not override copy(); it has to");
324 }
325 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700326
327 /*
328 /**********************************************************
329 /* Serializable overrides
330 /**********************************************************
331 */
332
333 /**
334 * Method that we need to override to actually make restoration go
335 * through constructors etc.
336 * Also: must be overridden by sub-classes as well.
337 */
338 protected Object readResolve() {
Tatu Salorantaf7741c52013-04-27 11:06:30 -0700339 return new JsonFactory(this, _objectCodec);
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700340 }
Tatu Salorantaa9e5c9f2013-08-28 19:39:59 -0700341
342 /*
343 /**********************************************************
344 /* Capability introspection
345 /**********************************************************
346 */
347
348 /**
349 * Introspection method that higher-level functionality may call
350 * to see whether underlying data format requires a stable ordering
351 * of object properties or not.
352 * This is usually used for determining
353 * whether to force a stable ordering (like alphabetic ordering by name)
354 * if no ordering if explicitly specified.
355 *<p>
356 * Default implementation returns <code>false</code> as JSON does NOT
357 * require stable ordering. Formats that require ordering include positional
358 * textual formats like <code>CSV</code>, and schema-based binary formats
359 * like <code>Avro</code>.
360 *
361 * @since 2.3
362 */
Tatu475a99b2014-04-22 14:32:50 -0700363 public boolean requiresPropertyOrdering() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700364
365 /**
366 * Introspection method that higher-level functionality may call
367 * to see whether underlying data format can read and write binary
368 * data natively; that is, embeded it as-is without using encodings
369 * such as Base64.
370 *<p>
371 * Default implementation returns <code>false</code> as JSON does not
372 * support native access: all binary content must use Base64 encoding.
373 * Most binary formats (like Smile and Avro) support native binary content.
374 *
375 * @since 2.3
376 */
Tatu475a99b2014-04-22 14:32:50 -0700377 public boolean canHandleBinaryNatively() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700378
Tatu475a99b2014-04-22 14:32:50 -0700379 /**
380 * Introspection method that can be used by base factory to check
381 * whether access using <code>char[]</code> is something that actual
382 * parser implementations can take advantage of, over having to
383 * use {@link java.io.Reader}. Sub-types are expected to override
384 * definition; default implementation (suitable for JSON) alleges
385 * that optimization are possible; and thereby is likely to try
386 * to access {@link java.lang.String} content by first copying it into
387 * recyclable intermediate buffer.
388 *
389 * @since 2.4
390 */
391 public boolean canUseCharArrays() { return true; }
392
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800393 /*
394 /**********************************************************
395 /* Format detection functionality (since 1.8)
396 /**********************************************************
397 */
398
399 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700400 * Method that can be used to quickly check whether given schema
401 * is something that parsers and/or generators constructed by this
402 * factory could use. Note that this means possible use, at the level
403 * of data format (i.e. schema is for same data format as parsers and
404 * generators this factory constructs); individual schema instances
405 * may have further usage restrictions.
406 *
407 * @since 2.1
408 */
409 public boolean canUseSchema(FormatSchema schema) {
410 String ourFormat = getFormatName();
411 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
412 }
413
414 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800415 * Method that returns short textual id identifying format
416 * this factory supports.
417 *<p>
418 * Note: sub-classes should override this method; default
419 * implementation will return null for all sub-classes
420 */
421 public String getFormatName()
422 {
423 /* Somewhat nasty check: since we can't make this abstract
424 * (due to backwards compatibility concerns), need to prevent
425 * format name "leakage"
426 */
427 if (getClass() == JsonFactory.class) {
428 return FORMAT_NAME_JSON;
429 }
430 return null;
431 }
432
433 public MatchStrength hasFormat(InputAccessor acc) throws IOException
434 {
435 // since we can't keep this abstract, only implement for "vanilla" instance
436 if (getClass() == JsonFactory.class) {
437 return hasJSONFormat(acc);
438 }
439 return null;
440 }
441
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700442 /**
443 * Method that can be called to determine if a custom
444 * {@link ObjectCodec} is needed for binding data parsed
445 * using {@link JsonParser} constructed by this factory
446 * (which typically also implies the same for serialization
447 * with {@link JsonGenerator}).
448 *
449 * @return True if custom codec is needed with parsers and
450 * generators created by this factory; false if a general
451 * {@link ObjectCodec} is enough
452 *
453 * @since 2.1
454 */
455 public boolean requiresCustomCodec() {
456 return false;
457 }
458
459 /**
460 * Helper method that can be called to determine if content accessed
461 * using given accessor seems to be JSON content.
462 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800463 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
464 {
465 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700466 }
467
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800468 /*
469 /**********************************************************
470 /* Versioned
471 /**********************************************************
472 */
473
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800474 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800475 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800476 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800477 }
478
479 /*
480 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800481 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800482 /**********************************************************
483 */
484
485 /**
486 * Method for enabling or disabling specified parser feature
487 * (check {@link JsonParser.Feature} for list of features)
488 */
Tatu07351902012-01-19 13:12:13 -0800489 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
490 return state ? enable(f) : disable(f);
491 }
492
493 /**
494 * Method for enabling specified parser feature
495 * (check {@link JsonFactory.Feature} for list of features)
496 */
497 public JsonFactory enable(JsonFactory.Feature f) {
498 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800499 return this;
500 }
501
502 /**
Tatu07351902012-01-19 13:12:13 -0800503 * Method for disabling specified parser features
504 * (check {@link JsonFactory.Feature} for list of features)
505 */
506 public JsonFactory disable(JsonFactory.Feature f) {
507 _factoryFeatures &= ~f.getMask();
508 return this;
509 }
510
511 /**
512 * Checked whether specified parser feature is enabled.
513 */
514 public final boolean isEnabled(JsonFactory.Feature f) {
515 return (_factoryFeatures & f.getMask()) != 0;
516 }
517
518 /*
519 /**********************************************************
520 /* Configuration, parser configuration
521 /**********************************************************
522 */
523
524 /**
525 * Method for enabling or disabling specified parser feature
526 * (check {@link JsonParser.Feature} for list of features)
527 */
528 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
529 return state ? enable(f) : disable(f);
530 }
531
532 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800533 * Method for enabling specified parser feature
534 * (check {@link JsonParser.Feature} for list of features)
535 */
536 public JsonFactory enable(JsonParser.Feature f) {
537 _parserFeatures |= f.getMask();
538 return this;
539 }
540
541 /**
542 * Method for disabling specified parser features
543 * (check {@link JsonParser.Feature} for list of features)
544 */
545 public JsonFactory disable(JsonParser.Feature f) {
546 _parserFeatures &= ~f.getMask();
547 return this;
548 }
549
550 /**
551 * Checked whether specified parser feature is enabled.
552 */
553 public final boolean isEnabled(JsonParser.Feature f) {
554 return (_parserFeatures & f.getMask()) != 0;
555 }
556
557 /**
558 * Method for getting currently configured input decorator (if any;
559 * there is no default decorator).
560 */
561 public InputDecorator getInputDecorator() {
562 return _inputDecorator;
563 }
564
565 /**
566 * Method for overriding currently configured input decorator
567 */
568 public JsonFactory setInputDecorator(InputDecorator d) {
569 _inputDecorator = d;
570 return this;
571 }
572
573 /*
574 /**********************************************************
575 /* Configuration, generator settings
576 /**********************************************************
577 */
578
579 /**
580 * Method for enabling or disabling specified generator feature
581 * (check {@link JsonGenerator.Feature} for list of features)
582 */
583 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800584 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800585 }
586
587
588 /**
589 * Method for enabling specified generator features
590 * (check {@link JsonGenerator.Feature} for list of features)
591 */
592 public JsonFactory enable(JsonGenerator.Feature f) {
593 _generatorFeatures |= f.getMask();
594 return this;
595 }
596
597 /**
598 * Method for disabling specified generator feature
599 * (check {@link JsonGenerator.Feature} for list of features)
600 */
601 public JsonFactory disable(JsonGenerator.Feature f) {
602 _generatorFeatures &= ~f.getMask();
603 return this;
604 }
605
606 /**
607 * Check whether specified generator feature is enabled.
608 */
609 public final boolean isEnabled(JsonGenerator.Feature f) {
610 return (_generatorFeatures & f.getMask()) != 0;
611 }
612
613 /**
614 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
615 * it creates.
616 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800617 public CharacterEscapes getCharacterEscapes() { return _characterEscapes; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800618
619 /**
620 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
621 * it creates.
622 */
623 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
624 _characterEscapes = esc;
625 return this;
626 }
627
628 /**
629 * Method for getting currently configured output decorator (if any;
630 * there is no default decorator).
631 */
632 public OutputDecorator getOutputDecorator() {
633 return _outputDecorator;
634 }
635
636 /**
637 * Method for overriding currently configured output decorator
638 */
639 public JsonFactory setOutputDecorator(OutputDecorator d) {
640 _outputDecorator = d;
641 return this;
642 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700643
644 /**
645 * Method that allows overriding String used for separating root-level
646 * JSON values (default is single space character)
647 *
648 * @param sep Separator to use, if any; null means that no separator is
649 * automatically added
650 *
651 * @since 2.1
652 */
653 public JsonFactory setRootValueSeparator(String sep) {
654 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
655 return this;
656 }
657
658 /**
659 * @since 2.1
660 */
661 public String getRootValueSeparator() {
662 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
663 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800664
665 /*
666 /**********************************************************
667 /* Configuration, other
668 /**********************************************************
669 */
670
671 /**
672 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800673 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
674 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800675 * it constructs). This is needed to use data-binding methods
676 * of {@link JsonParser} and {@link JsonGenerator} instances.
677 */
678 public JsonFactory setCodec(ObjectCodec oc) {
679 _objectCodec = oc;
680 return this;
681 }
682
683 public ObjectCodec getCodec() { return _objectCodec; }
684
685 /*
686 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700687 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800688 /**********************************************************
689 */
690
691 /**
692 * Method for constructing JSON parser instance to parse
693 * contents of specified file. Encoding is auto-detected
694 * from contents according to JSON specification recommended
695 * mechanism.
696 *<p>
697 * Underlying input stream (needed for reading contents)
698 * will be <b>owned</b> (and managed, i.e. closed as need be) by
699 * the parser, since caller has no access to it.
700 *
701 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700702 *
703 * @since 2.1
704 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800705 public JsonParser createParser(File f) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800706 // true, since we create InputStream from File
707 IOContext ctxt = _createContext(f, true);
708 InputStream in = new FileInputStream(f);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700709 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800710 }
711
712 /**
713 * Method for constructing JSON parser instance to parse
714 * contents of resource reference by given URL.
715 * Encoding is auto-detected
716 * from contents according to JSON specification recommended
717 * mechanism.
718 *<p>
719 * Underlying input stream (needed for reading contents)
720 * will be <b>owned</b> (and managed, i.e. closed as need be) by
721 * the parser, since caller has no access to it.
722 *
723 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800724 *
725 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800726 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800727 public JsonParser createParser(URL url) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800728 // true, since we create InputStream from URL
729 IOContext ctxt = _createContext(url, true);
730 InputStream in = _optimizedStreamFromURL(url);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700731 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800732 }
733
734 /**
735 * Method for constructing JSON parser instance to parse
736 * the contents accessed via specified input stream.
737 *<p>
738 * The input stream will <b>not be owned</b> by
739 * the parser, it will still be managed (i.e. closed if
740 * end-of-stream is reacher, or parser close method called)
741 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
742 * is enabled.
743 *<p>
744 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700745 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800746 *
747 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800748 *
749 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800750 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800751 public JsonParser createParser(InputStream in) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800752 IOContext ctxt = _createContext(in, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700753 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800754 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700755
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800756 /**
757 * Method for constructing parser for parsing
758 * the contents accessed via specified Reader.
759 <p>
760 * The read stream will <b>not be owned</b> by
761 * the parser, it will still be managed (i.e. closed if
762 * end-of-stream is reacher, or parser close method called)
763 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
764 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800765 *
766 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800767 *
768 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800769 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800770 public JsonParser createParser(Reader r) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800771 // false -> we do NOT own Reader (did not create it)
772 IOContext ctxt = _createContext(r, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700773 return _createParser(_decorate(r, ctxt), 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 Saloranta32e4e912014-01-26 19:59:28 -0800782 public JsonParser createParser(byte[] data) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800783 IOContext ctxt = _createContext(data, true);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800784 if (_inputDecorator != null) {
785 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
786 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700787 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800788 }
789 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700790 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800791 }
792
793 /**
794 * Method for constructing parser for parsing
795 * the contents of given byte array.
796 *
797 * @param data Buffer that contains data to parse
798 * @param offset Offset of the first data byte within buffer
799 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800800 *
801 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800802 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800803 public JsonParser createParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800804 IOContext ctxt = _createContext(data, true);
805 // [JACKSON-512]: allow wrapping with InputDecorator
806 if (_inputDecorator != null) {
807 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
808 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700809 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800810 }
811 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800812 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800813 }
814
815 /**
816 * Method for constructing parser for parsing
817 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800818 *
819 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800820 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800821 public JsonParser createParser(String content) throws IOException, JsonParseException {
Tatu64aa9d22014-04-18 14:07:38 -0700822 final int strLen = content.length();
823 // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char)
Tatu475a99b2014-04-22 14:32:50 -0700824 if (_inputDecorator != null || strLen > 0x8000 || !canUseCharArrays()) {
Tatu64aa9d22014-04-18 14:07:38 -0700825 // easier to just wrap in a Reader than extend InputDecorator; or, if content
826 // is too long for us to copy it over
827 return createParser(new StringReader(content));
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800828 }
Tatu64aa9d22014-04-18 14:07:38 -0700829 IOContext ctxt = _createContext(content, true);
Tatu Saloranta84dc1842014-04-18 21:07:57 -0700830 char[] buf = ctxt.allocTokenBuffer(strLen);
Tatu64aa9d22014-04-18 14:07:38 -0700831 content.getChars(0, strLen, buf, 0);
832 return _createParser(buf, 0, strLen, ctxt, true);
833 }
834
835 /**
836 * Method for constructing parser for parsing
837 * contents of given char array.
838 *
839 * @since 2.4
840 */
841 public JsonParser createParser(char[] content) throws IOException {
842 return createParser(content, 0, content.length);
843 }
844
845 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -0700846 * Method for constructing parser for parsing contents of given char array.
Tatu64aa9d22014-04-18 14:07:38 -0700847 *
848 * @since 2.4
849 */
850 public JsonParser createParser(char[] content, int offset, int len) throws IOException {
851 if (_inputDecorator != null) { // easier to just wrap in a Reader than extend InputDecorator
852 return createParser(new CharArrayReader(content, offset, len));
853 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700854 return _createParser(content, offset, len, _createContext(content, true),
855 // important: buffer is NOT recyclable, as it's from caller
856 false);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800857 }
858
859 /*
860 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800861 /* Parser factories (old ones, as per [Issue-25])
862 /**********************************************************
863 */
864
865 /**
866 * Method for constructing JSON parser instance to parse
867 * contents of specified file. Encoding is auto-detected
868 * from contents according to JSON specification recommended
869 * mechanism.
870 *<p>
871 * Underlying input stream (needed for reading contents)
872 * will be <b>owned</b> (and managed, i.e. closed as need be) by
873 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800874 *
875 * @param f File that contains JSON content to parse
876 *
877 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
878 */
879 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800880 public JsonParser createJsonParser(File f) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800881 return createParser(f);
882 }
883
884 /**
885 * Method for constructing JSON parser instance to parse
886 * contents of resource reference by given URL.
887 * Encoding is auto-detected
888 * from contents according to JSON specification recommended
889 * mechanism.
890 *<p>
891 * Underlying input stream (needed for reading contents)
892 * will be <b>owned</b> (and managed, i.e. closed as need be) by
893 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800894 *
895 * @param url URL pointing to resource that contains JSON content to parse
896 *
897 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
898 */
899 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800900 public JsonParser createJsonParser(URL url) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800901 return createParser(url);
902 }
903
904 /**
905 * Method for constructing JSON parser instance to parse
906 * the contents accessed via specified input stream.
907 *<p>
908 * The input stream will <b>not be owned</b> by
909 * the parser, it will still be managed (i.e. closed if
910 * end-of-stream is reacher, or parser close method called)
911 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
912 * is enabled.
913 *<p>
914 * Note: no encoding argument is taken since it can always be
915 * auto-detected as suggested by JSON RFC.
Tatu Saloranta68194922012-11-15 20:34:29 -0800916 *
917 * @param in InputStream to use for reading JSON content to parse
918 *
919 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
920 */
921 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800922 public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800923 return createParser(in);
924 }
925
926 /**
927 * Method for constructing parser for parsing
928 * the contents accessed via specified Reader.
929 <p>
930 * The read stream will <b>not be owned</b> by
931 * the parser, it will still be managed (i.e. closed if
932 * end-of-stream is reacher, or parser close method called)
933 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
934 * is enabled.
Tatu Saloranta68194922012-11-15 20:34:29 -0800935 *
936 * @param r Reader to use for reading JSON content to parse
937 *
938 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
939 */
940 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800941 public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800942 return createParser(r);
943 }
944
945 /**
Tatu Salorantaefc23672013-12-30 19:09:39 -0800946 * Method for constructing parser for parsing the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800947 *
948 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
949 */
950 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800951 public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800952 return createParser(data);
953 }
954
955 /**
956 * Method for constructing parser for parsing
957 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800958 *
959 * @param data Buffer that contains data to parse
960 * @param offset Offset of the first data byte within buffer
961 * @param len Length of contents to parse within buffer
962 *
963 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
964 */
965 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800966 public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800967 return createParser(data, offset, len);
968 }
969
970 /**
971 * Method for constructing parser for parsing
972 * contents of given String.
973 *
974 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
975 */
976 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800977 public JsonParser createJsonParser(String content) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800978 return createParser(content);
979 }
980
981 /*
982 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700983 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800984 /**********************************************************
985 */
986
987 /**
988 * Method for constructing JSON generator for writing JSON content
989 * using specified output stream.
990 * Encoding to use must be specified, and needs to be one of available
991 * types (as per JSON specification).
992 *<p>
993 * Underlying stream <b>is NOT owned</b> by the generator constructed,
994 * so that generator will NOT close the output stream when
995 * {@link JsonGenerator#close} is called (unless auto-closing
996 * feature,
997 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
998 * is enabled).
999 * Using application needs to close it explicitly if this is the case.
1000 *<p>
1001 * Note: there are formats that use fixed encoding (like most binary data formats)
1002 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001003 *
1004 * @param out OutputStream to use for writing JSON content
1005 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001006 *
1007 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001008 */
1009 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
1010 throws IOException
1011 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001012 // false -> we won't manage the stream unless explicitly directed to
1013 IOContext ctxt = _createContext(out, false);
1014 ctxt.setEncoding(enc);
1015 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001016 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001017 }
1018 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001019 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001020 }
1021
1022 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001023 * Convenience method for constructing generator that uses default
1024 * encoding of the format (UTF-8 for JSON and most other data formats).
1025 *<p>
1026 * Note: there are formats that use fixed encoding (like most binary data formats).
1027 *
1028 * @since 2.1
1029 */
1030 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1031 return createGenerator(out, JsonEncoding.UTF8);
1032 }
1033
1034 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001035 * Method for constructing JSON generator for writing JSON content
1036 * using specified Writer.
1037 *<p>
1038 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1039 * so that generator will NOT close the Reader when
1040 * {@link JsonGenerator#close} is called (unless auto-closing
1041 * feature,
1042 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1043 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001044 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001045 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001046 *
Tatu Saloranta896000f2014-04-19 12:53:40 -07001047 * @param w Writer to use for writing JSON content
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001048 */
Tatu Saloranta896000f2014-04-19 12:53:40 -07001049 public JsonGenerator createGenerator(Writer w) throws IOException {
1050 IOContext ctxt = _createContext(w, false);
1051 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001052 }
1053
1054 /**
1055 * Method for constructing JSON generator for writing JSON content
1056 * to specified file, overwriting contents it might have (or creating
1057 * it if such file does not yet exist).
1058 * Encoding to use must be specified, and needs to be one of available
1059 * types (as per JSON specification).
1060 *<p>
1061 * Underlying stream <b>is owned</b> by the generator constructed,
1062 * i.e. generator will handle closing of file when
1063 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001064 *
1065 * @param f File to write contents to
1066 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001067 *
1068 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001069 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001070 public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001071 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001072 OutputStream out = new FileOutputStream(f);
1073 // true -> yes, we have to manage the stream since we created it
1074 IOContext ctxt = _createContext(out, true);
1075 ctxt.setEncoding(enc);
1076 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001077 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001078 }
1079 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001080 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001081 }
1082
1083 /*
1084 /**********************************************************
1085 /* Generator factories, old (as per [Issue-25]
1086 /**********************************************************
1087 */
1088
1089 /**
1090 * Method for constructing JSON generator for writing JSON content
1091 * using specified output stream.
1092 * Encoding to use must be specified, and needs to be one of available
1093 * types (as per JSON specification).
1094 *<p>
1095 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1096 * so that generator will NOT close the output stream when
1097 * {@link JsonGenerator#close} is called (unless auto-closing
1098 * feature,
1099 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1100 * is enabled).
1101 * Using application needs to close it explicitly if this is the case.
1102 *<p>
1103 * Note: there are formats that use fixed encoding (like most binary data formats)
1104 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001105 *
1106 * @param out OutputStream to use for writing JSON content
1107 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001108 *
1109 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001110 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001111 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001112 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001113 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001114 }
1115
1116 /**
1117 * Method for constructing JSON generator for writing JSON content
1118 * using specified Writer.
1119 *<p>
1120 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1121 * so that generator will NOT close the Reader when
1122 * {@link JsonGenerator#close} is called (unless auto-closing
1123 * feature,
1124 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1125 * Using application needs to close it explicitly.
1126 *
1127 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001128 *
1129 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001130 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001131 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001132 public JsonGenerator createJsonGenerator(Writer out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001133 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001134 }
1135
1136 /**
1137 * Convenience method for constructing generator that uses default
1138 * encoding of the format (UTF-8 for JSON and most other data formats).
1139 *<p>
1140 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001141 *
1142 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001143 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001144 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001145 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001146 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001147 }
1148
1149 /**
1150 * Method for constructing JSON generator for writing JSON content
1151 * to specified file, overwriting contents it might have (or creating
1152 * it if such file does not yet exist).
1153 * Encoding to use must be specified, and needs to be one of available
1154 * types (as per JSON specification).
1155 *<p>
1156 * Underlying stream <b>is owned</b> by the generator constructed,
1157 * i.e. generator will handle closing of file when
1158 * {@link JsonGenerator#close} is called.
1159 *
1160 * @param f File to write contents to
1161 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001162 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001163 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001164 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001165 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001166 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001167 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001168 }
1169
1170 /*
1171 /**********************************************************
1172 /* Factory methods used by factory for creating parser instances,
1173 /* overridable by sub-classes
1174 /**********************************************************
1175 */
1176
1177 /**
1178 * Overridable factory method that actually instantiates desired parser
1179 * given {@link InputStream} and context object.
1180 *<p>
1181 * This method is specifically designed to remain
1182 * compatible between minor versions so that sub-classes can count
1183 * on it being called as expected. That is, it is part of official
1184 * interface from sub-class perspective, although not a public
1185 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001186 *
1187 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001188 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001189 protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001190 // As per [JACKSON-259], may want to fully disable canonicalization:
1191 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001192 _objectCodec, _rootByteSymbols, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001193 }
1194
1195 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001196 * Overridable factory method that actually instantiates parser
1197 * using given {@link Reader} object for reading content.
1198 *<p>
1199 * This method is specifically designed to remain
1200 * compatible between minor versions so that sub-classes can count
1201 * on it being called as expected. That is, it is part of official
1202 * interface from sub-class perspective, although not a public
1203 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001204 *
1205 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001206 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001207 protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001208 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001209 _rootCharSymbols.makeChild(_factoryFeatures));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001210 }
1211
1212 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001213 * Overridable factory method that actually instantiates parser
Tatu64aa9d22014-04-18 14:07:38 -07001214 * using given <code>char[]</code> object for accessing content.
1215 *
1216 * @since 2.4
1217 */
1218 protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
1219 boolean recyclable) throws IOException {
1220 return new ReaderBasedJsonParser(ctxt, _parserFeatures, null, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001221 _rootCharSymbols.makeChild(_factoryFeatures),
1222 data, offset, offset+len, recyclable);
Tatu64aa9d22014-04-18 14:07:38 -07001223 }
1224
1225 /**
1226 * Overridable factory method that actually instantiates parser
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001227 * using given {@link Reader} object for reading content
1228 * passed as raw byte array.
1229 *<p>
1230 * This method is specifically designed to remain
1231 * compatible between minor versions so that sub-classes can count
1232 * on it being called as expected. That is, it is part of official
1233 * interface from sub-class perspective, although not a public
1234 * method available to users of factory implementations.
1235 */
Tatu64aa9d22014-04-18 14:07:38 -07001236 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001237 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001238 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001239 _objectCodec, _rootByteSymbols, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001240 }
1241
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001242 /*
1243 /**********************************************************
1244 /* Factory methods used by factory for creating generator instances,
1245 /* overridable by sub-classes
1246 /**********************************************************
1247 */
1248
1249 /**
1250 * Overridable factory method that actually instantiates generator for
1251 * given {@link Writer} and context object.
1252 *<p>
1253 * This method is specifically designed to remain
1254 * compatible between minor versions so that sub-classes can count
1255 * on it being called as expected. That is, it is part of official
1256 * interface from sub-class perspective, although not a public
1257 * method available to users of factory implementations.
1258 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001259 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001260 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001261 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1262 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001263 if (_characterEscapes != null) {
1264 gen.setCharacterEscapes(_characterEscapes);
1265 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001266 SerializableString rootSep = _rootValueSeparator;
1267 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1268 gen.setRootValueSeparator(rootSep);
1269 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001270 return gen;
1271 }
1272
1273 /**
1274 * Overridable factory method that actually instantiates generator for
1275 * given {@link OutputStream} and context object, using UTF-8 encoding.
1276 *<p>
1277 * This method is specifically designed to remain
1278 * compatible between minor versions so that sub-classes can count
1279 * on it being called as expected. That is, it is part of official
1280 * interface from sub-class perspective, although not a public
1281 * method available to users of factory implementations.
1282 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001283 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001284 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1285 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001286 if (_characterEscapes != null) {
1287 gen.setCharacterEscapes(_characterEscapes);
1288 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001289 SerializableString rootSep = _rootValueSeparator;
1290 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1291 gen.setRootValueSeparator(rootSep);
1292 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001293 return gen;
1294 }
1295
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001296 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1297 {
1298 // note: this should not get called any more (caller checks, dispatches)
1299 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1300 return new UTF8Writer(ctxt, out);
1301 }
1302 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1303 return new OutputStreamWriter(out, enc.getJavaName());
1304 }
Tatu Saloranta896000f2014-04-19 12:53:40 -07001305
1306 /*
1307 /**********************************************************
1308 /* Internal factory methods, decorator handling
1309 /**********************************************************
1310 */
1311
1312 /**
1313 * @since 2.4
1314 */
1315 protected final InputStream _decorate(InputStream in, IOContext ctxt) throws IOException {
1316 if (_inputDecorator != null) {
1317 InputStream in2 = _inputDecorator.decorate(ctxt, in);
1318 if (in2 != null) {
1319 return in2;
1320 }
1321 }
1322 return in;
1323 }
1324
1325 /**
1326 * @since 2.4
1327 */
1328 protected final Reader _decorate(Reader in, IOContext ctxt) throws IOException {
1329 if (_inputDecorator != null) {
1330 Reader in2 = _inputDecorator.decorate(ctxt, in);
1331 if (in2 != null) {
1332 return in2;
1333 }
1334 }
1335 return in;
1336 }
1337
1338 /**
1339 * @since 2.4
1340 */
1341 protected final OutputStream _decorate(OutputStream out, IOContext ctxt) throws IOException {
1342 if (_outputDecorator != null) {
1343 OutputStream out2 = _outputDecorator.decorate(ctxt, out);
1344 if (out2 != null) {
1345 return out2;
1346 }
1347 }
1348 return out;
1349 }
1350
1351 /**
1352 * @since 2.4
1353 */
1354 protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException {
1355 if (_outputDecorator != null) {
1356 Writer out2 = _outputDecorator.decorate(ctxt, out);
1357 if (out2 != null) {
1358 return out2;
1359 }
1360 }
1361 return out;
1362 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001363
1364 /*
1365 /**********************************************************
1366 /* Internal factory methods, other
1367 /**********************************************************
1368 */
1369
1370 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001371 * Method used by factory to create buffer recycler instances
1372 * for parsers and generators.
1373 *<p>
1374 * Note: only public to give access for <code>ObjectMapper</code>
1375 */
1376 public BufferRecycler _getBufferRecycler()
1377 {
1378 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1379 BufferRecycler br = (ref == null) ? null : ref.get();
1380
1381 if (br == null) {
1382 br = new BufferRecycler();
1383 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1384 }
1385 return br;
1386 }
1387
1388 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001389 * Overridable factory method that actually instantiates desired
1390 * context object.
1391 */
1392 protected IOContext _createContext(Object srcRef, boolean resourceManaged) {
1393 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1394 }
1395
1396 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001397 * Helper methods used for constructing an optimal stream for
1398 * parsers to use, when input is to be read from an URL.
1399 * This helps when reading file content via URL.
1400 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001401 protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001402 if ("file".equals(url.getProtocol())) {
1403 /* Can not do this if the path refers
1404 * to a network drive on windows. This fixes the problem;
1405 * might not be needed on all platforms (NFS?), but should not
1406 * matter a lot: performance penalty of extra wrapping is more
1407 * relevant when accessing local file system.
1408 */
1409 String host = url.getHost();
1410 if (host == null || host.length() == 0) {
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001411 // [Issue#48]: Let's try to avoid probs with URL encoded stuff
1412 String path = url.getPath();
1413 if (path.indexOf('%') < 0) {
1414 return new FileInputStream(url.getPath());
1415
1416 }
1417 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001418 }
1419 }
1420 return url.openStream();
1421 }
1422}