blob: c8428e6834084a3e137556c2cab185b52cc6c410 [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 */
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 */
Gonçalo Silva2bba5dd2014-01-25 17:06:21 +0000252 public JsonFactory() { this(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 */
Tatu475a99b2014-04-22 14:32:50 -0700347 public boolean requiresPropertyOrdering() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700348
349 /**
350 * Introspection method that higher-level functionality may call
351 * to see whether underlying data format can read and write binary
352 * data natively; that is, embeded it as-is without using encodings
353 * such as Base64.
354 *<p>
355 * Default implementation returns <code>false</code> as JSON does not
356 * support native access: all binary content must use Base64 encoding.
357 * Most binary formats (like Smile and Avro) support native binary content.
358 *
359 * @since 2.3
360 */
Tatu475a99b2014-04-22 14:32:50 -0700361 public boolean canHandleBinaryNatively() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700362
Tatu475a99b2014-04-22 14:32:50 -0700363 /**
364 * Introspection method that can be used by base factory to check
365 * whether access using <code>char[]</code> is something that actual
366 * parser implementations can take advantage of, over having to
367 * use {@link java.io.Reader}. Sub-types are expected to override
368 * definition; default implementation (suitable for JSON) alleges
369 * that optimization are possible; and thereby is likely to try
370 * to access {@link java.lang.String} content by first copying it into
371 * recyclable intermediate buffer.
372 *
373 * @since 2.4
374 */
375 public boolean canUseCharArrays() { return true; }
376
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800377 /*
378 /**********************************************************
379 /* Format detection functionality (since 1.8)
380 /**********************************************************
381 */
382
383 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700384 * Method that can be used to quickly check whether given schema
385 * is something that parsers and/or generators constructed by this
386 * factory could use. Note that this means possible use, at the level
387 * of data format (i.e. schema is for same data format as parsers and
388 * generators this factory constructs); individual schema instances
389 * may have further usage restrictions.
390 *
391 * @since 2.1
392 */
393 public boolean canUseSchema(FormatSchema schema) {
394 String ourFormat = getFormatName();
395 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
396 }
397
398 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800399 * Method that returns short textual id identifying format
400 * this factory supports.
401 *<p>
402 * Note: sub-classes should override this method; default
403 * implementation will return null for all sub-classes
404 */
405 public String getFormatName()
406 {
407 /* Somewhat nasty check: since we can't make this abstract
408 * (due to backwards compatibility concerns), need to prevent
409 * format name "leakage"
410 */
411 if (getClass() == JsonFactory.class) {
412 return FORMAT_NAME_JSON;
413 }
414 return null;
415 }
416
417 public MatchStrength hasFormat(InputAccessor acc) throws IOException
418 {
419 // since we can't keep this abstract, only implement for "vanilla" instance
420 if (getClass() == JsonFactory.class) {
421 return hasJSONFormat(acc);
422 }
423 return null;
424 }
425
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700426 /**
427 * Method that can be called to determine if a custom
428 * {@link ObjectCodec} is needed for binding data parsed
429 * using {@link JsonParser} constructed by this factory
430 * (which typically also implies the same for serialization
431 * with {@link JsonGenerator}).
432 *
433 * @return True if custom codec is needed with parsers and
434 * generators created by this factory; false if a general
435 * {@link ObjectCodec} is enough
436 *
437 * @since 2.1
438 */
439 public boolean requiresCustomCodec() {
440 return false;
441 }
442
443 /**
444 * Helper method that can be called to determine if content accessed
445 * using given accessor seems to be JSON content.
446 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800447 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
448 {
449 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700450 }
451
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800452 /*
453 /**********************************************************
454 /* Versioned
455 /**********************************************************
456 */
457
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800458 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800459 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800460 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800461 }
462
463 /*
464 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800465 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800466 /**********************************************************
467 */
468
469 /**
470 * Method for enabling or disabling specified parser feature
471 * (check {@link JsonParser.Feature} for list of features)
472 */
Tatu07351902012-01-19 13:12:13 -0800473 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
474 return state ? enable(f) : disable(f);
475 }
476
477 /**
478 * Method for enabling specified parser feature
479 * (check {@link JsonFactory.Feature} for list of features)
480 */
481 public JsonFactory enable(JsonFactory.Feature f) {
482 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800483 return this;
484 }
485
486 /**
Tatu07351902012-01-19 13:12:13 -0800487 * Method for disabling specified parser features
488 * (check {@link JsonFactory.Feature} for list of features)
489 */
490 public JsonFactory disable(JsonFactory.Feature f) {
491 _factoryFeatures &= ~f.getMask();
492 return this;
493 }
494
495 /**
496 * Checked whether specified parser feature is enabled.
497 */
498 public final boolean isEnabled(JsonFactory.Feature f) {
499 return (_factoryFeatures & f.getMask()) != 0;
500 }
501
502 /*
503 /**********************************************************
504 /* Configuration, parser configuration
505 /**********************************************************
506 */
507
508 /**
509 * Method for enabling or disabling specified parser feature
510 * (check {@link JsonParser.Feature} for list of features)
511 */
512 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
513 return state ? enable(f) : disable(f);
514 }
515
516 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800517 * Method for enabling specified parser feature
518 * (check {@link JsonParser.Feature} for list of features)
519 */
520 public JsonFactory enable(JsonParser.Feature f) {
521 _parserFeatures |= f.getMask();
522 return this;
523 }
524
525 /**
526 * Method for disabling specified parser features
527 * (check {@link JsonParser.Feature} for list of features)
528 */
529 public JsonFactory disable(JsonParser.Feature f) {
530 _parserFeatures &= ~f.getMask();
531 return this;
532 }
533
534 /**
535 * Checked whether specified parser feature is enabled.
536 */
537 public final boolean isEnabled(JsonParser.Feature f) {
538 return (_parserFeatures & f.getMask()) != 0;
539 }
540
541 /**
542 * Method for getting currently configured input decorator (if any;
543 * there is no default decorator).
544 */
545 public InputDecorator getInputDecorator() {
546 return _inputDecorator;
547 }
548
549 /**
550 * Method for overriding currently configured input decorator
551 */
552 public JsonFactory setInputDecorator(InputDecorator d) {
553 _inputDecorator = d;
554 return this;
555 }
556
557 /*
558 /**********************************************************
559 /* Configuration, generator settings
560 /**********************************************************
561 */
562
563 /**
564 * Method for enabling or disabling specified generator feature
565 * (check {@link JsonGenerator.Feature} for list of features)
566 */
567 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800568 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800569 }
570
571
572 /**
573 * Method for enabling specified generator features
574 * (check {@link JsonGenerator.Feature} for list of features)
575 */
576 public JsonFactory enable(JsonGenerator.Feature f) {
577 _generatorFeatures |= f.getMask();
578 return this;
579 }
580
581 /**
582 * Method for disabling specified generator feature
583 * (check {@link JsonGenerator.Feature} for list of features)
584 */
585 public JsonFactory disable(JsonGenerator.Feature f) {
586 _generatorFeatures &= ~f.getMask();
587 return this;
588 }
589
590 /**
591 * Check whether specified generator feature is enabled.
592 */
593 public final boolean isEnabled(JsonGenerator.Feature f) {
594 return (_generatorFeatures & f.getMask()) != 0;
595 }
596
597 /**
598 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
599 * it creates.
600 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800601 public CharacterEscapes getCharacterEscapes() { return _characterEscapes; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800602
603 /**
604 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
605 * it creates.
606 */
607 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
608 _characterEscapes = esc;
609 return this;
610 }
611
612 /**
613 * Method for getting currently configured output decorator (if any;
614 * there is no default decorator).
615 */
616 public OutputDecorator getOutputDecorator() {
617 return _outputDecorator;
618 }
619
620 /**
621 * Method for overriding currently configured output decorator
622 */
623 public JsonFactory setOutputDecorator(OutputDecorator d) {
624 _outputDecorator = d;
625 return this;
626 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700627
628 /**
629 * Method that allows overriding String used for separating root-level
630 * JSON values (default is single space character)
631 *
632 * @param sep Separator to use, if any; null means that no separator is
633 * automatically added
634 *
635 * @since 2.1
636 */
637 public JsonFactory setRootValueSeparator(String sep) {
638 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
639 return this;
640 }
641
642 /**
643 * @since 2.1
644 */
645 public String getRootValueSeparator() {
646 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
647 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800648
649 /*
650 /**********************************************************
651 /* Configuration, other
652 /**********************************************************
653 */
654
655 /**
656 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800657 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
658 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800659 * it constructs). This is needed to use data-binding methods
660 * of {@link JsonParser} and {@link JsonGenerator} instances.
661 */
662 public JsonFactory setCodec(ObjectCodec oc) {
663 _objectCodec = oc;
664 return this;
665 }
666
667 public ObjectCodec getCodec() { return _objectCodec; }
668
669 /*
670 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700671 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800672 /**********************************************************
673 */
674
675 /**
676 * Method for constructing JSON parser instance to parse
677 * contents of specified file. Encoding is auto-detected
678 * from contents according to JSON specification recommended
679 * mechanism.
680 *<p>
681 * Underlying input stream (needed for reading contents)
682 * will be <b>owned</b> (and managed, i.e. closed as need be) by
683 * the parser, since caller has no access to it.
684 *
685 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700686 *
687 * @since 2.1
688 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800689 public JsonParser createParser(File f) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800690 // true, since we create InputStream from File
691 IOContext ctxt = _createContext(f, true);
692 InputStream in = new FileInputStream(f);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700693 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800694 }
695
696 /**
697 * Method for constructing JSON parser instance to parse
698 * contents of resource reference by given URL.
699 * Encoding is auto-detected
700 * from contents according to JSON specification recommended
701 * mechanism.
702 *<p>
703 * Underlying input stream (needed for reading contents)
704 * will be <b>owned</b> (and managed, i.e. closed as need be) by
705 * the parser, since caller has no access to it.
706 *
707 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800708 *
709 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800710 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800711 public JsonParser createParser(URL url) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800712 // true, since we create InputStream from URL
713 IOContext ctxt = _createContext(url, true);
714 InputStream in = _optimizedStreamFromURL(url);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700715 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800716 }
717
718 /**
719 * Method for constructing JSON parser instance to parse
720 * the contents accessed via specified input stream.
721 *<p>
722 * The input stream will <b>not be owned</b> by
723 * the parser, it will still be managed (i.e. closed if
724 * end-of-stream is reacher, or parser close method called)
725 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
726 * is enabled.
727 *<p>
728 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700729 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800730 *
731 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800732 *
733 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800734 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800735 public JsonParser createParser(InputStream in) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800736 IOContext ctxt = _createContext(in, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700737 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800738 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700739
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800740 /**
741 * Method for constructing parser for parsing
742 * the contents accessed via specified Reader.
743 <p>
744 * The read stream will <b>not be owned</b> by
745 * the parser, it will still be managed (i.e. closed if
746 * end-of-stream is reacher, or parser close method called)
747 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
748 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800749 *
750 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800751 *
752 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800753 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800754 public JsonParser createParser(Reader r) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800755 // false -> we do NOT own Reader (did not create it)
756 IOContext ctxt = _createContext(r, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700757 return _createParser(_decorate(r, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800758 }
759
760 /**
761 * Method for constructing parser for parsing
762 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800763 *
764 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800765 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800766 public JsonParser createParser(byte[] data) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800767 IOContext ctxt = _createContext(data, true);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800768 if (_inputDecorator != null) {
769 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
770 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700771 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800772 }
773 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700774 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800775 }
776
777 /**
778 * Method for constructing parser for parsing
779 * the contents of given byte array.
780 *
781 * @param data Buffer that contains data to parse
782 * @param offset Offset of the first data byte within buffer
783 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800784 *
785 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800786 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800787 public JsonParser createParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800788 IOContext ctxt = _createContext(data, true);
789 // [JACKSON-512]: allow wrapping with InputDecorator
790 if (_inputDecorator != null) {
791 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
792 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700793 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800794 }
795 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800796 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800797 }
798
799 /**
800 * Method for constructing parser for parsing
801 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800802 *
803 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800804 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800805 public JsonParser createParser(String content) throws IOException, JsonParseException {
Tatu64aa9d22014-04-18 14:07:38 -0700806 final int strLen = content.length();
807 // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char)
Tatu475a99b2014-04-22 14:32:50 -0700808 if (_inputDecorator != null || strLen > 0x8000 || !canUseCharArrays()) {
Tatu64aa9d22014-04-18 14:07:38 -0700809 // easier to just wrap in a Reader than extend InputDecorator; or, if content
810 // is too long for us to copy it over
811 return createParser(new StringReader(content));
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800812 }
Tatu64aa9d22014-04-18 14:07:38 -0700813 IOContext ctxt = _createContext(content, true);
Tatu Saloranta84dc1842014-04-18 21:07:57 -0700814 char[] buf = ctxt.allocTokenBuffer(strLen);
Tatu64aa9d22014-04-18 14:07:38 -0700815 content.getChars(0, strLen, buf, 0);
816 return _createParser(buf, 0, strLen, ctxt, true);
817 }
818
819 /**
820 * Method for constructing parser for parsing
821 * contents of given char array.
822 *
823 * @since 2.4
824 */
825 public JsonParser createParser(char[] content) throws IOException {
826 return createParser(content, 0, content.length);
827 }
828
829 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -0700830 * Method for constructing parser for parsing contents of given char array.
Tatu64aa9d22014-04-18 14:07:38 -0700831 *
832 * @since 2.4
833 */
834 public JsonParser createParser(char[] content, int offset, int len) throws IOException {
835 if (_inputDecorator != null) { // easier to just wrap in a Reader than extend InputDecorator
836 return createParser(new CharArrayReader(content, offset, len));
837 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700838 return _createParser(content, offset, len, _createContext(content, true),
839 // important: buffer is NOT recyclable, as it's from caller
840 false);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800841 }
842
843 /*
844 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800845 /* Parser factories (old ones, as per [Issue-25])
846 /**********************************************************
847 */
848
849 /**
850 * Method for constructing JSON parser instance to parse
851 * contents of specified file. Encoding is auto-detected
852 * from contents according to JSON specification recommended
853 * mechanism.
854 *<p>
855 * Underlying input stream (needed for reading contents)
856 * will be <b>owned</b> (and managed, i.e. closed as need be) by
857 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800858 *
859 * @param f File that contains JSON content to parse
860 *
861 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
862 */
863 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800864 public JsonParser createJsonParser(File f) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800865 return createParser(f);
866 }
867
868 /**
869 * Method for constructing JSON parser instance to parse
870 * contents of resource reference by given URL.
871 * Encoding is auto-detected
872 * from contents according to JSON specification recommended
873 * mechanism.
874 *<p>
875 * Underlying input stream (needed for reading contents)
876 * will be <b>owned</b> (and managed, i.e. closed as need be) by
877 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800878 *
879 * @param url URL pointing to resource that contains JSON content to parse
880 *
881 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
882 */
883 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800884 public JsonParser createJsonParser(URL url) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800885 return createParser(url);
886 }
887
888 /**
889 * Method for constructing JSON parser instance to parse
890 * the contents accessed via specified input stream.
891 *<p>
892 * The input stream will <b>not be owned</b> by
893 * the parser, it will still be managed (i.e. closed if
894 * end-of-stream is reacher, or parser close method called)
895 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
896 * is enabled.
897 *<p>
898 * Note: no encoding argument is taken since it can always be
899 * auto-detected as suggested by JSON RFC.
Tatu Saloranta68194922012-11-15 20:34:29 -0800900 *
901 * @param in InputStream to use for reading JSON content to parse
902 *
903 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
904 */
905 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800906 public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800907 return createParser(in);
908 }
909
910 /**
911 * Method for constructing parser for parsing
912 * the contents accessed via specified Reader.
913 <p>
914 * The read stream will <b>not be owned</b> by
915 * the parser, it will still be managed (i.e. closed if
916 * end-of-stream is reacher, or parser close method called)
917 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
918 * is enabled.
Tatu Saloranta68194922012-11-15 20:34:29 -0800919 *
920 * @param r Reader to use for reading JSON content to parse
921 *
922 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
923 */
924 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800925 public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800926 return createParser(r);
927 }
928
929 /**
Tatu Salorantaefc23672013-12-30 19:09:39 -0800930 * Method for constructing parser for parsing the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800931 *
932 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
933 */
934 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800935 public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800936 return createParser(data);
937 }
938
939 /**
940 * Method for constructing parser for parsing
941 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800942 *
943 * @param data Buffer that contains data to parse
944 * @param offset Offset of the first data byte within buffer
945 * @param len Length of contents to parse within buffer
946 *
947 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
948 */
949 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800950 public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800951 return createParser(data, offset, len);
952 }
953
954 /**
955 * Method for constructing parser for parsing
956 * contents of given String.
957 *
958 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
959 */
960 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800961 public JsonParser createJsonParser(String content) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800962 return createParser(content);
963 }
964
965 /*
966 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700967 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800968 /**********************************************************
969 */
970
971 /**
972 * Method for constructing JSON generator for writing JSON content
973 * using specified output stream.
974 * Encoding to use must be specified, and needs to be one of available
975 * types (as per JSON specification).
976 *<p>
977 * Underlying stream <b>is NOT owned</b> by the generator constructed,
978 * so that generator will NOT close the output stream when
979 * {@link JsonGenerator#close} is called (unless auto-closing
980 * feature,
981 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
982 * is enabled).
983 * Using application needs to close it explicitly if this is the case.
984 *<p>
985 * Note: there are formats that use fixed encoding (like most binary data formats)
986 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700987 *
988 * @param out OutputStream to use for writing JSON content
989 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800990 *
991 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700992 */
993 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
994 throws IOException
995 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800996 // false -> we won't manage the stream unless explicitly directed to
997 IOContext ctxt = _createContext(out, false);
998 ctxt.setEncoding(enc);
999 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001000 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001001 }
1002 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001003 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001004 }
1005
1006 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001007 * Convenience method for constructing generator that uses default
1008 * encoding of the format (UTF-8 for JSON and most other data formats).
1009 *<p>
1010 * Note: there are formats that use fixed encoding (like most binary data formats).
1011 *
1012 * @since 2.1
1013 */
1014 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1015 return createGenerator(out, JsonEncoding.UTF8);
1016 }
1017
1018 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001019 * Method for constructing JSON generator for writing JSON content
1020 * using specified Writer.
1021 *<p>
1022 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1023 * so that generator will NOT close the Reader when
1024 * {@link JsonGenerator#close} is called (unless auto-closing
1025 * feature,
1026 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1027 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001028 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001029 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001030 *
Tatu Saloranta896000f2014-04-19 12:53:40 -07001031 * @param w Writer to use for writing JSON content
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001032 */
Tatu Saloranta896000f2014-04-19 12:53:40 -07001033 public JsonGenerator createGenerator(Writer w) throws IOException {
1034 IOContext ctxt = _createContext(w, false);
1035 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001036 }
1037
1038 /**
1039 * Method for constructing JSON generator for writing JSON content
1040 * to specified file, overwriting contents it might have (or creating
1041 * it if such file does not yet exist).
1042 * Encoding to use must be specified, and needs to be one of available
1043 * types (as per JSON specification).
1044 *<p>
1045 * Underlying stream <b>is owned</b> by the generator constructed,
1046 * i.e. generator will handle closing of file when
1047 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001048 *
1049 * @param f File to write contents to
1050 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001051 *
1052 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001053 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001054 public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001055 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001056 OutputStream out = new FileOutputStream(f);
1057 // true -> yes, we have to manage the stream since we created it
1058 IOContext ctxt = _createContext(out, true);
1059 ctxt.setEncoding(enc);
1060 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001061 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001062 }
1063 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001064 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001065 }
1066
1067 /*
1068 /**********************************************************
1069 /* Generator factories, old (as per [Issue-25]
1070 /**********************************************************
1071 */
1072
1073 /**
1074 * Method for constructing JSON generator for writing JSON content
1075 * using specified output stream.
1076 * Encoding to use must be specified, and needs to be one of available
1077 * types (as per JSON specification).
1078 *<p>
1079 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1080 * so that generator will NOT close the output stream when
1081 * {@link JsonGenerator#close} is called (unless auto-closing
1082 * feature,
1083 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1084 * is enabled).
1085 * Using application needs to close it explicitly if this is the case.
1086 *<p>
1087 * Note: there are formats that use fixed encoding (like most binary data formats)
1088 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001089 *
1090 * @param out OutputStream to use for writing JSON content
1091 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001092 *
1093 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001094 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001095 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001096 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001097 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001098 }
1099
1100 /**
1101 * Method for constructing JSON generator for writing JSON content
1102 * using specified Writer.
1103 *<p>
1104 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1105 * so that generator will NOT close the Reader when
1106 * {@link JsonGenerator#close} is called (unless auto-closing
1107 * feature,
1108 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1109 * Using application needs to close it explicitly.
1110 *
1111 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001112 *
1113 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001114 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001115 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001116 public JsonGenerator createJsonGenerator(Writer out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001117 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001118 }
1119
1120 /**
1121 * Convenience method for constructing generator that uses default
1122 * encoding of the format (UTF-8 for JSON and most other data formats).
1123 *<p>
1124 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001125 *
1126 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001127 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001128 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001129 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001130 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001131 }
1132
1133 /**
1134 * Method for constructing JSON generator for writing JSON content
1135 * to specified file, overwriting contents it might have (or creating
1136 * it if such file does not yet exist).
1137 * Encoding to use must be specified, and needs to be one of available
1138 * types (as per JSON specification).
1139 *<p>
1140 * Underlying stream <b>is owned</b> by the generator constructed,
1141 * i.e. generator will handle closing of file when
1142 * {@link JsonGenerator#close} is called.
1143 *
1144 * @param f File to write contents to
1145 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001146 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001147 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001148 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001149 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001150 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001151 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001152 }
1153
1154 /*
1155 /**********************************************************
1156 /* Factory methods used by factory for creating parser instances,
1157 /* overridable by sub-classes
1158 /**********************************************************
1159 */
1160
1161 /**
1162 * Overridable factory method that actually instantiates desired parser
1163 * given {@link InputStream} and context object.
1164 *<p>
1165 * This method is specifically designed to remain
1166 * compatible between minor versions so that sub-classes can count
1167 * on it being called as expected. That is, it is part of official
1168 * interface from sub-class perspective, although not a public
1169 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001170 *
1171 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001172 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001173 protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001174 // As per [JACKSON-259], may want to fully disable canonicalization:
1175 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001176 _objectCodec, _rootByteSymbols, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001177 }
1178
1179 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001180 * Overridable factory method that actually instantiates parser
1181 * using given {@link Reader} object for reading content.
1182 *<p>
1183 * This method is specifically designed to remain
1184 * compatible between minor versions so that sub-classes can count
1185 * on it being called as expected. That is, it is part of official
1186 * interface from sub-class perspective, although not a public
1187 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001188 *
1189 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001190 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001191 protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001192 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001193 _rootCharSymbols.makeChild(_factoryFeatures));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001194 }
1195
1196 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001197 * Overridable factory method that actually instantiates parser
Tatu64aa9d22014-04-18 14:07:38 -07001198 * using given <code>char[]</code> object for accessing content.
1199 *
1200 * @since 2.4
1201 */
1202 protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
1203 boolean recyclable) throws IOException {
1204 return new ReaderBasedJsonParser(ctxt, _parserFeatures, null, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001205 _rootCharSymbols.makeChild(_factoryFeatures),
1206 data, offset, offset+len, recyclable);
Tatu64aa9d22014-04-18 14:07:38 -07001207 }
1208
1209 /**
1210 * Overridable factory method that actually instantiates parser
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001211 * using given {@link Reader} object for reading content
1212 * passed as raw byte array.
1213 *<p>
1214 * This method is specifically designed to remain
1215 * compatible between minor versions so that sub-classes can count
1216 * on it being called as expected. That is, it is part of official
1217 * interface from sub-class perspective, although not a public
1218 * method available to users of factory implementations.
1219 */
Tatu64aa9d22014-04-18 14:07:38 -07001220 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001221 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001222 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001223 _objectCodec, _rootByteSymbols, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001224 }
1225
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001226 /*
1227 /**********************************************************
1228 /* Factory methods used by factory for creating generator instances,
1229 /* overridable by sub-classes
1230 /**********************************************************
1231 */
1232
1233 /**
1234 * Overridable factory method that actually instantiates generator for
1235 * given {@link Writer} and context object.
1236 *<p>
1237 * This method is specifically designed to remain
1238 * compatible between minor versions so that sub-classes can count
1239 * on it being called as expected. That is, it is part of official
1240 * interface from sub-class perspective, although not a public
1241 * method available to users of factory implementations.
1242 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001243 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001244 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001245 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1246 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001247 if (_characterEscapes != null) {
1248 gen.setCharacterEscapes(_characterEscapes);
1249 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001250 SerializableString rootSep = _rootValueSeparator;
1251 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1252 gen.setRootValueSeparator(rootSep);
1253 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001254 return gen;
1255 }
1256
1257 /**
1258 * Overridable factory method that actually instantiates generator for
1259 * given {@link OutputStream} and context object, using UTF-8 encoding.
1260 *<p>
1261 * This method is specifically designed to remain
1262 * compatible between minor versions so that sub-classes can count
1263 * on it being called as expected. That is, it is part of official
1264 * interface from sub-class perspective, although not a public
1265 * method available to users of factory implementations.
1266 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001267 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001268 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1269 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001270 if (_characterEscapes != null) {
1271 gen.setCharacterEscapes(_characterEscapes);
1272 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001273 SerializableString rootSep = _rootValueSeparator;
1274 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1275 gen.setRootValueSeparator(rootSep);
1276 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001277 return gen;
1278 }
1279
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001280 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1281 {
1282 // note: this should not get called any more (caller checks, dispatches)
1283 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1284 return new UTF8Writer(ctxt, out);
1285 }
1286 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1287 return new OutputStreamWriter(out, enc.getJavaName());
1288 }
Tatu Saloranta896000f2014-04-19 12:53:40 -07001289
1290 /*
1291 /**********************************************************
1292 /* Internal factory methods, decorator handling
1293 /**********************************************************
1294 */
1295
1296 /**
1297 * @since 2.4
1298 */
1299 protected final InputStream _decorate(InputStream in, IOContext ctxt) throws IOException {
1300 if (_inputDecorator != null) {
1301 InputStream in2 = _inputDecorator.decorate(ctxt, in);
1302 if (in2 != null) {
1303 return in2;
1304 }
1305 }
1306 return in;
1307 }
1308
1309 /**
1310 * @since 2.4
1311 */
1312 protected final Reader _decorate(Reader in, IOContext ctxt) throws IOException {
1313 if (_inputDecorator != null) {
1314 Reader in2 = _inputDecorator.decorate(ctxt, in);
1315 if (in2 != null) {
1316 return in2;
1317 }
1318 }
1319 return in;
1320 }
1321
1322 /**
1323 * @since 2.4
1324 */
1325 protected final OutputStream _decorate(OutputStream out, IOContext ctxt) throws IOException {
1326 if (_outputDecorator != null) {
1327 OutputStream out2 = _outputDecorator.decorate(ctxt, out);
1328 if (out2 != null) {
1329 return out2;
1330 }
1331 }
1332 return out;
1333 }
1334
1335 /**
1336 * @since 2.4
1337 */
1338 protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException {
1339 if (_outputDecorator != null) {
1340 Writer out2 = _outputDecorator.decorate(ctxt, out);
1341 if (out2 != null) {
1342 return out2;
1343 }
1344 }
1345 return out;
1346 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001347
1348 /*
1349 /**********************************************************
1350 /* Internal factory methods, other
1351 /**********************************************************
1352 */
1353
1354 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001355 * Method used by factory to create buffer recycler instances
1356 * for parsers and generators.
1357 *<p>
1358 * Note: only public to give access for <code>ObjectMapper</code>
1359 */
1360 public BufferRecycler _getBufferRecycler()
1361 {
1362 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1363 BufferRecycler br = (ref == null) ? null : ref.get();
1364
1365 if (br == null) {
1366 br = new BufferRecycler();
1367 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1368 }
1369 return br;
1370 }
1371
1372 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001373 * Overridable factory method that actually instantiates desired
1374 * context object.
1375 */
1376 protected IOContext _createContext(Object srcRef, boolean resourceManaged) {
1377 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1378 }
1379
1380 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001381 * Helper methods used for constructing an optimal stream for
1382 * parsers to use, when input is to be read from an URL.
1383 * This helps when reading file content via URL.
1384 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001385 protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001386 if ("file".equals(url.getProtocol())) {
1387 /* Can not do this if the path refers
1388 * to a network drive on windows. This fixes the problem;
1389 * might not be needed on all platforms (NFS?), but should not
1390 * matter a lot: performance penalty of extra wrapping is more
1391 * relevant when accessing local file system.
1392 */
1393 String host = url.getHost();
1394 if (host == null || host.length() == 0) {
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001395 // [Issue#48]: Let's try to avoid probs with URL encoded stuff
1396 String path = url.getPath();
1397 if (path.indexOf('%') < 0) {
1398 return new FileInputStream(url.getPath());
1399
1400 }
1401 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001402 }
1403 }
1404 return url.openStream();
1405 }
1406}