blob: ace19e691052040c83c13a5c6f27b3c06e688a2c [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.*;
Cowtowncoder3a00c862015-02-04 22:30:30 -080015import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080016import 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{
Tatu Salorantadccffbe2015-02-05 21:17:57 -080045 private static final long serialVersionUID = 1; // since 2.6.0
Tatu Saloranta95f76a42012-10-05 13:04:23 -070046
Tatu Saloranta7b796a82013-04-27 10:18:30 -070047 /*
48 /**********************************************************
49 /* Helper types
50 /**********************************************************
Tatu Salorantaf15531c2011-12-22 23:00:40 -080051 */
Tatu Salorantae92d04f2013-07-24 22:58:08 -070052
Tatu07351902012-01-19 13:12:13 -080053 /**
54 * Enumeration that defines all on/off features that can only be
55 * changed for {@link JsonFactory}.
56 */
57 public enum Feature {
58
59 // // // Symbol handling (interning etc)
60
61 /**
62 * Feature that determines whether JSON object field names are
63 * to be canonicalized using {@link String#intern} or not:
64 * if enabled, all field names will be intern()ed (and caller
65 * can count on this being true for all such names); if disabled,
66 * no intern()ing is done. There may still be basic
67 * canonicalization (that is, same String will be used to represent
68 * all identical object property names for a single document).
69 *<p>
70 * Note: this setting only has effect if
71 * {@link #CANONICALIZE_FIELD_NAMES} is true -- otherwise no
72 * canonicalization of any sort is done.
73 *<p>
74 * This setting is enabled by default.
75 */
76 INTERN_FIELD_NAMES(true),
77
78 /**
79 * Feature that determines whether JSON object field names are
80 * to be canonicalized (details of how canonicalization is done
81 * then further specified by
82 * {@link #INTERN_FIELD_NAMES}).
83 *<p>
84 * This setting is enabled by default.
85 */
Tatu Saloranta383bc8f2014-05-24 00:47:07 -070086 CANONICALIZE_FIELD_NAMES(true),
87
88 /**
89 * Feature that determines what happens if we encounter a case in symbol
90 * handling where number of hash collisions exceeds a safety threshold
91 * -- which almost certainly means a denial-of-service attack via generated
92 * duplicate hash codes.
93 * If feature is enabled, an {@link IllegalStateException} is
94 * thrown to indicate the suspected denial-of-service attack; if disabled, processing continues but
95 * canonicalization (and thereby <code>intern()</code>ing) is disabled) as protective
96 * measure.
97 *<p>
98 * This setting is enabled by default.
99 *
100 * @since 2.4
101 */
102 FAIL_ON_SYMBOL_HASH_OVERFLOW(true),
Tatu07351902012-01-19 13:12:13 -0800103
104 ;
105
106 /**
107 * Whether feature is enabled or disabled by default.
108 */
109 private final boolean _defaultState;
110
111 /**
112 * Method that calculates bit set (flags) of all features that
113 * are enabled by default.
114 */
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800115 public static int collectDefaults() {
Tatu07351902012-01-19 13:12:13 -0800116 int flags = 0;
117 for (Feature f : values()) {
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800118 if (f.enabledByDefault()) { flags |= f.getMask(); }
Tatu07351902012-01-19 13:12:13 -0800119 }
120 return flags;
121 }
122
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800123 private Feature(boolean defaultState) { _defaultState = defaultState; }
Tatu07351902012-01-19 13:12:13 -0800124
125 public boolean enabledByDefault() { return _defaultState; }
Tatu07351902012-01-19 13:12:13 -0800126 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
Tatu07351902012-01-19 13:12:13 -0800127 public int getMask() { return (1 << ordinal()); }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700128 }
129
130 /*
131 /**********************************************************
132 /* Constants
133 /**********************************************************
134 */
135
136 /**
137 * Name used to identify JSON format
138 * (and returned by {@link #getFormatName()}
139 */
140 public final static String FORMAT_NAME_JSON = "JSON";
141
142 /**
143 * Bitfield (set of flags) of all factory features that are enabled by default.
144 */
145 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
146
147 /**
148 * Bitfield (set of flags) of all parser features that are enabled
149 * by default.
150 */
151 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
152
153 /**
154 * Bitfield (set of flags) of all generator features that are enabled
155 * by default.
156 */
157 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
158
159 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
160
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800161 /*
162 /**********************************************************
163 /* Buffer, symbol table management
164 /**********************************************************
165 */
166
167 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700168 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800169 * to a {@link BufferRecycler} used to provide a low-cost
170 * buffer recycling between reader and writer instances.
171 */
172 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
173 = new ThreadLocal<SoftReference<BufferRecycler>>();
174
175 /**
176 * Each factory comes equipped with a shared root symbol table.
177 * It should not be linked back to the original blueprint, to
178 * avoid contents from leaking between factories.
179 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700180 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800181
182 /**
183 * Alternative to the basic symbol table, some stream-based
184 * parsers use different name canonicalization method.
185 *<p>
186 * TODO: should clean up this; looks messy having 2 alternatives
187 * with not very clear differences.
Tatu Salorantadccffbe2015-02-05 21:17:57 -0800188 *
189 * @since 2.6.0
190 */
191 protected final transient ByteQuadsCanonicalizer _byteSymbolCanonicalizer = ByteQuadsCanonicalizer.createRoot();
192
193 /**
194 * Earlier byte-based symbol table; replaced with 2.6 with a new implementation.
195 * Left in for version 2.6.0: will be removed in 2.7 or later.
196 *
197 * @deprecated Since 2.6.0, only use {@link #_byteSymbolCanonicalizer}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800198 */
Cowtowncoder3a00c862015-02-04 22:30:30 -0800199 protected final transient ByteQuadsCanonicalizer _rootByteSymbols = ByteQuadsCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800200
201 /*
202 /**********************************************************
203 /* Configuration
204 /**********************************************************
205 */
206
207 /**
208 * Object that implements conversion functionality between
209 * Java objects and JSON content. For base JsonFactory implementation
210 * usually not set by default, but can be explicitly set.
211 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
212 * usually provide an implementation.
213 */
214 protected ObjectCodec _objectCodec;
215
216 /**
Tatu07351902012-01-19 13:12:13 -0800217 * Currently enabled factory features.
218 */
219 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
220
221 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800222 * Currently enabled parser features.
223 */
224 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
225
226 /**
227 * Currently enabled generator features.
228 */
229 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
230
231 /**
232 * Definition of custom character escapes to use for generators created
233 * by this factory, if any. If null, standard data format specific
234 * escapes are used.
235 */
236 protected CharacterEscapes _characterEscapes;
237
238 /**
239 * Optional helper object that may decorate input sources, to do
240 * additional processing on input during parsing.
241 */
242 protected InputDecorator _inputDecorator;
243
244 /**
245 * Optional helper object that may decorate output object, to do
246 * additional processing on output during content generation.
247 */
248 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700249
250 /**
251 * Separator used between root-level values, if any; null indicates
252 * "do not add separator".
253 * Default separator is a single space character.
254 *
255 * @since 2.1
256 */
257 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800258
259 /*
260 /**********************************************************
261 /* Construction
262 /**********************************************************
263 */
264
265 /**
266 * Default constructor used to create factory instances.
267 * Creation of a factory instance is a light-weight operation,
268 * but it is still a good idea to reuse limited number of
269 * factory instances (and quite often just a single instance):
270 * factories are used as context for storing some reused
271 * processing objects (such as symbol tables parsers use)
272 * and this reuse only works within context of a single
273 * factory instance.
274 */
Gonçalo Silva2bba5dd2014-01-25 17:06:21 +0000275 public JsonFactory() { this(null); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800276
277 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
278
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700279 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700280 * Constructor used when copy()ing a factory instance.
281 *
282 * @since 2.2.1
283 */
284 protected JsonFactory(JsonFactory src, ObjectCodec codec)
285 {
286 _objectCodec = null;
287 _factoryFeatures = src._factoryFeatures;
288 _parserFeatures = src._parserFeatures;
289 _generatorFeatures = src._generatorFeatures;
290 _characterEscapes = src._characterEscapes;
291 _inputDecorator = src._inputDecorator;
292 _outputDecorator = src._outputDecorator;
293 _rootValueSeparator = src._rootValueSeparator;
294
295 /* 27-Apr-2013, tatu: How about symbol table; should we try to
296 * reuse shared symbol tables? Could be more efficient that way;
297 * although can slightly add to concurrency overhead.
298 */
299 }
300
301 /**
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700302 * Method for constructing a new {@link JsonFactory} that has
303 * the same settings as this instance, but is otherwise
304 * independent (i.e. nothing is actually shared, symbol tables
305 * are separate).
306 * Note that {@link ObjectCodec} reference is not copied but is
307 * set to null; caller typically needs to set it after calling
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700308 * this method. Reason for this is that the codec is used for
309 * callbacks, and assumption is that there is strict 1-to-1
310 * mapping between codec, factory. Caller has to, then, explicitly
311 * set codec after making the copy.
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700312 *
313 * @since 2.1
314 */
315 public JsonFactory copy()
316 {
317 _checkInvalidCopy(JsonFactory.class);
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700318 // as per above, do clear ObjectCodec
319 return new JsonFactory(this, null);
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700320 }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700321
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700322 /**
323 * @since 2.1
324 * @param exp
325 */
326 protected void _checkInvalidCopy(Class<?> exp)
327 {
328 if (getClass() != exp) {
329 throw new IllegalStateException("Failed copy(): "+getClass().getName()
330 +" (version: "+version()+") does not override copy(); it has to");
331 }
332 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700333
334 /*
335 /**********************************************************
336 /* Serializable overrides
337 /**********************************************************
338 */
339
340 /**
341 * Method that we need to override to actually make restoration go
342 * through constructors etc.
343 * Also: must be overridden by sub-classes as well.
344 */
345 protected Object readResolve() {
Tatu Salorantaf7741c52013-04-27 11:06:30 -0700346 return new JsonFactory(this, _objectCodec);
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700347 }
Tatu Salorantaa9e5c9f2013-08-28 19:39:59 -0700348
349 /*
350 /**********************************************************
351 /* Capability introspection
352 /**********************************************************
353 */
354
355 /**
356 * Introspection method that higher-level functionality may call
357 * to see whether underlying data format requires a stable ordering
358 * of object properties or not.
359 * This is usually used for determining
360 * whether to force a stable ordering (like alphabetic ordering by name)
361 * if no ordering if explicitly specified.
362 *<p>
363 * Default implementation returns <code>false</code> as JSON does NOT
364 * require stable ordering. Formats that require ordering include positional
365 * textual formats like <code>CSV</code>, and schema-based binary formats
366 * like <code>Avro</code>.
367 *
368 * @since 2.3
369 */
Tatu475a99b2014-04-22 14:32:50 -0700370 public boolean requiresPropertyOrdering() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700371
372 /**
373 * Introspection method that higher-level functionality may call
374 * to see whether underlying data format can read and write binary
375 * data natively; that is, embeded it as-is without using encodings
376 * such as Base64.
377 *<p>
378 * Default implementation returns <code>false</code> as JSON does not
379 * support native access: all binary content must use Base64 encoding.
380 * Most binary formats (like Smile and Avro) support native binary content.
381 *
382 * @since 2.3
383 */
Tatu475a99b2014-04-22 14:32:50 -0700384 public boolean canHandleBinaryNatively() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700385
Tatu475a99b2014-04-22 14:32:50 -0700386 /**
387 * Introspection method that can be used by base factory to check
388 * whether access using <code>char[]</code> is something that actual
389 * parser implementations can take advantage of, over having to
390 * use {@link java.io.Reader}. Sub-types are expected to override
391 * definition; default implementation (suitable for JSON) alleges
392 * that optimization are possible; and thereby is likely to try
393 * to access {@link java.lang.String} content by first copying it into
394 * recyclable intermediate buffer.
395 *
396 * @since 2.4
397 */
398 public boolean canUseCharArrays() { return true; }
399
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800400 /*
401 /**********************************************************
402 /* Format detection functionality (since 1.8)
403 /**********************************************************
404 */
405
406 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700407 * Method that can be used to quickly check whether given schema
408 * is something that parsers and/or generators constructed by this
409 * factory could use. Note that this means possible use, at the level
410 * of data format (i.e. schema is for same data format as parsers and
411 * generators this factory constructs); individual schema instances
412 * may have further usage restrictions.
413 *
414 * @since 2.1
415 */
416 public boolean canUseSchema(FormatSchema schema) {
417 String ourFormat = getFormatName();
418 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
419 }
420
421 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800422 * Method that returns short textual id identifying format
423 * this factory supports.
424 *<p>
425 * Note: sub-classes should override this method; default
426 * implementation will return null for all sub-classes
427 */
428 public String getFormatName()
429 {
430 /* Somewhat nasty check: since we can't make this abstract
431 * (due to backwards compatibility concerns), need to prevent
432 * format name "leakage"
433 */
434 if (getClass() == JsonFactory.class) {
435 return FORMAT_NAME_JSON;
436 }
437 return null;
438 }
439
440 public MatchStrength hasFormat(InputAccessor acc) throws IOException
441 {
442 // since we can't keep this abstract, only implement for "vanilla" instance
443 if (getClass() == JsonFactory.class) {
444 return hasJSONFormat(acc);
445 }
446 return null;
447 }
448
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700449 /**
450 * Method that can be called to determine if a custom
451 * {@link ObjectCodec} is needed for binding data parsed
452 * using {@link JsonParser} constructed by this factory
453 * (which typically also implies the same for serialization
454 * with {@link JsonGenerator}).
455 *
456 * @return True if custom codec is needed with parsers and
457 * generators created by this factory; false if a general
458 * {@link ObjectCodec} is enough
459 *
460 * @since 2.1
461 */
462 public boolean requiresCustomCodec() {
463 return false;
464 }
465
466 /**
467 * Helper method that can be called to determine if content accessed
468 * using given accessor seems to be JSON content.
469 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800470 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
471 {
472 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700473 }
474
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800475 /*
476 /**********************************************************
477 /* Versioned
478 /**********************************************************
479 */
480
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800481 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800482 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800483 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800484 }
485
486 /*
487 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800488 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800489 /**********************************************************
490 */
491
492 /**
493 * Method for enabling or disabling specified parser feature
494 * (check {@link JsonParser.Feature} for list of features)
495 */
Tatu07351902012-01-19 13:12:13 -0800496 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
497 return state ? enable(f) : disable(f);
498 }
499
500 /**
501 * Method for enabling specified parser feature
502 * (check {@link JsonFactory.Feature} for list of features)
503 */
504 public JsonFactory enable(JsonFactory.Feature f) {
505 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800506 return this;
507 }
508
509 /**
Tatu07351902012-01-19 13:12:13 -0800510 * Method for disabling specified parser features
511 * (check {@link JsonFactory.Feature} for list of features)
512 */
513 public JsonFactory disable(JsonFactory.Feature f) {
514 _factoryFeatures &= ~f.getMask();
515 return this;
516 }
517
518 /**
519 * Checked whether specified parser feature is enabled.
520 */
521 public final boolean isEnabled(JsonFactory.Feature f) {
522 return (_factoryFeatures & f.getMask()) != 0;
523 }
524
525 /*
526 /**********************************************************
527 /* Configuration, parser configuration
528 /**********************************************************
529 */
530
531 /**
532 * Method for enabling or disabling specified parser feature
533 * (check {@link JsonParser.Feature} for list of features)
534 */
535 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
536 return state ? enable(f) : disable(f);
537 }
538
539 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800540 * Method for enabling specified parser feature
541 * (check {@link JsonParser.Feature} for list of features)
542 */
543 public JsonFactory enable(JsonParser.Feature f) {
544 _parserFeatures |= f.getMask();
545 return this;
546 }
547
548 /**
549 * Method for disabling specified parser features
550 * (check {@link JsonParser.Feature} for list of features)
551 */
552 public JsonFactory disable(JsonParser.Feature f) {
553 _parserFeatures &= ~f.getMask();
554 return this;
555 }
556
557 /**
558 * Checked whether specified parser feature is enabled.
559 */
560 public final boolean isEnabled(JsonParser.Feature f) {
561 return (_parserFeatures & f.getMask()) != 0;
562 }
563
564 /**
565 * Method for getting currently configured input decorator (if any;
566 * there is no default decorator).
567 */
568 public InputDecorator getInputDecorator() {
569 return _inputDecorator;
570 }
571
572 /**
573 * Method for overriding currently configured input decorator
574 */
575 public JsonFactory setInputDecorator(InputDecorator d) {
576 _inputDecorator = d;
577 return this;
578 }
579
580 /*
581 /**********************************************************
582 /* Configuration, generator settings
583 /**********************************************************
584 */
585
586 /**
587 * Method for enabling or disabling specified generator feature
588 * (check {@link JsonGenerator.Feature} for list of features)
589 */
590 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800591 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800592 }
593
594
595 /**
596 * Method for enabling specified generator features
597 * (check {@link JsonGenerator.Feature} for list of features)
598 */
599 public JsonFactory enable(JsonGenerator.Feature f) {
600 _generatorFeatures |= f.getMask();
601 return this;
602 }
603
604 /**
605 * Method for disabling specified generator feature
606 * (check {@link JsonGenerator.Feature} for list of features)
607 */
608 public JsonFactory disable(JsonGenerator.Feature f) {
609 _generatorFeatures &= ~f.getMask();
610 return this;
611 }
612
613 /**
614 * Check whether specified generator feature is enabled.
615 */
616 public final boolean isEnabled(JsonGenerator.Feature f) {
617 return (_generatorFeatures & f.getMask()) != 0;
618 }
619
620 /**
621 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
622 * it creates.
623 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800624 public CharacterEscapes getCharacterEscapes() { return _characterEscapes; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800625
626 /**
627 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
628 * it creates.
629 */
630 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
631 _characterEscapes = esc;
632 return this;
633 }
634
635 /**
636 * Method for getting currently configured output decorator (if any;
637 * there is no default decorator).
638 */
639 public OutputDecorator getOutputDecorator() {
640 return _outputDecorator;
641 }
642
643 /**
644 * Method for overriding currently configured output decorator
645 */
646 public JsonFactory setOutputDecorator(OutputDecorator d) {
647 _outputDecorator = d;
648 return this;
649 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700650
651 /**
652 * Method that allows overriding String used for separating root-level
653 * JSON values (default is single space character)
654 *
655 * @param sep Separator to use, if any; null means that no separator is
656 * automatically added
657 *
658 * @since 2.1
659 */
660 public JsonFactory setRootValueSeparator(String sep) {
661 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
662 return this;
663 }
664
665 /**
666 * @since 2.1
667 */
668 public String getRootValueSeparator() {
669 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
670 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800671
672 /*
673 /**********************************************************
674 /* Configuration, other
675 /**********************************************************
676 */
677
678 /**
679 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800680 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
681 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800682 * it constructs). This is needed to use data-binding methods
683 * of {@link JsonParser} and {@link JsonGenerator} instances.
684 */
685 public JsonFactory setCodec(ObjectCodec oc) {
686 _objectCodec = oc;
687 return this;
688 }
689
690 public ObjectCodec getCodec() { return _objectCodec; }
691
692 /*
693 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700694 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800695 /**********************************************************
696 */
697
698 /**
699 * Method for constructing JSON parser instance to parse
700 * contents of specified file. Encoding is auto-detected
701 * from contents according to JSON specification recommended
702 * mechanism.
703 *<p>
704 * Underlying input stream (needed for reading contents)
705 * will be <b>owned</b> (and managed, i.e. closed as need be) by
706 * the parser, since caller has no access to it.
707 *
708 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700709 *
710 * @since 2.1
711 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800712 public JsonParser createParser(File f) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800713 // true, since we create InputStream from File
714 IOContext ctxt = _createContext(f, true);
715 InputStream in = new FileInputStream(f);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700716 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800717 }
718
719 /**
720 * Method for constructing JSON parser instance to parse
721 * contents of resource reference by given URL.
722 * Encoding is auto-detected
723 * from contents according to JSON specification recommended
724 * mechanism.
725 *<p>
726 * Underlying input stream (needed for reading contents)
727 * will be <b>owned</b> (and managed, i.e. closed as need be) by
728 * the parser, since caller has no access to it.
729 *
730 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800731 *
732 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800733 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800734 public JsonParser createParser(URL url) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800735 // true, since we create InputStream from URL
736 IOContext ctxt = _createContext(url, true);
737 InputStream in = _optimizedStreamFromURL(url);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700738 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800739 }
740
741 /**
742 * Method for constructing JSON parser instance to parse
743 * the contents accessed via specified input stream.
744 *<p>
745 * The input stream will <b>not be owned</b> by
746 * the parser, it will still be managed (i.e. closed if
747 * end-of-stream is reacher, or parser close method called)
748 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
749 * is enabled.
750 *<p>
751 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700752 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800753 *
754 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800755 *
756 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800757 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800758 public JsonParser createParser(InputStream in) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800759 IOContext ctxt = _createContext(in, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700760 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800761 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700762
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800763 /**
764 * Method for constructing parser for parsing
765 * the contents accessed via specified Reader.
766 <p>
767 * The read stream will <b>not be owned</b> by
768 * the parser, it will still be managed (i.e. closed if
769 * end-of-stream is reacher, or parser close method called)
770 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
771 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800772 *
773 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800774 *
775 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800776 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800777 public JsonParser createParser(Reader r) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800778 // false -> we do NOT own Reader (did not create it)
779 IOContext ctxt = _createContext(r, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700780 return _createParser(_decorate(r, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800781 }
782
783 /**
784 * Method for constructing parser for parsing
785 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800786 *
787 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800788 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800789 public JsonParser createParser(byte[] data) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800790 IOContext ctxt = _createContext(data, true);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800791 if (_inputDecorator != null) {
792 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
793 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700794 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800795 }
796 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700797 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800798 }
799
800 /**
801 * Method for constructing parser for parsing
802 * the contents of given byte array.
803 *
804 * @param data Buffer that contains data to parse
805 * @param offset Offset of the first data byte within buffer
806 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800807 *
808 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800809 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800810 public JsonParser createParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800811 IOContext ctxt = _createContext(data, true);
812 // [JACKSON-512]: allow wrapping with InputDecorator
813 if (_inputDecorator != null) {
814 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
815 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700816 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800817 }
818 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800819 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800820 }
821
822 /**
823 * Method for constructing parser for parsing
824 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800825 *
826 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800827 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800828 public JsonParser createParser(String content) throws IOException, JsonParseException {
Tatu64aa9d22014-04-18 14:07:38 -0700829 final int strLen = content.length();
830 // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char)
Tatu475a99b2014-04-22 14:32:50 -0700831 if (_inputDecorator != null || strLen > 0x8000 || !canUseCharArrays()) {
Tatu64aa9d22014-04-18 14:07:38 -0700832 // easier to just wrap in a Reader than extend InputDecorator; or, if content
833 // is too long for us to copy it over
834 return createParser(new StringReader(content));
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800835 }
Tatu64aa9d22014-04-18 14:07:38 -0700836 IOContext ctxt = _createContext(content, true);
Tatu Saloranta84dc1842014-04-18 21:07:57 -0700837 char[] buf = ctxt.allocTokenBuffer(strLen);
Tatu64aa9d22014-04-18 14:07:38 -0700838 content.getChars(0, strLen, buf, 0);
839 return _createParser(buf, 0, strLen, ctxt, true);
840 }
841
842 /**
843 * Method for constructing parser for parsing
844 * contents of given char array.
845 *
846 * @since 2.4
847 */
848 public JsonParser createParser(char[] content) throws IOException {
849 return createParser(content, 0, content.length);
850 }
851
852 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -0700853 * Method for constructing parser for parsing contents of given char array.
Tatu64aa9d22014-04-18 14:07:38 -0700854 *
855 * @since 2.4
856 */
857 public JsonParser createParser(char[] content, int offset, int len) throws IOException {
858 if (_inputDecorator != null) { // easier to just wrap in a Reader than extend InputDecorator
859 return createParser(new CharArrayReader(content, offset, len));
860 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700861 return _createParser(content, offset, len, _createContext(content, true),
862 // important: buffer is NOT recyclable, as it's from caller
863 false);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800864 }
865
866 /*
867 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800868 /* Parser factories (old ones, as per [Issue-25])
869 /**********************************************************
870 */
871
872 /**
873 * Method for constructing JSON parser instance to parse
874 * contents of specified file. Encoding is auto-detected
875 * from contents according to JSON specification recommended
876 * mechanism.
877 *<p>
878 * Underlying input stream (needed for reading contents)
879 * will be <b>owned</b> (and managed, i.e. closed as need be) by
880 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800881 *
882 * @param f File that contains JSON content to parse
883 *
884 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
885 */
886 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800887 public JsonParser createJsonParser(File f) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800888 return createParser(f);
889 }
890
891 /**
892 * Method for constructing JSON parser instance to parse
893 * contents of resource reference by given URL.
894 * Encoding is auto-detected
895 * from contents according to JSON specification recommended
896 * mechanism.
897 *<p>
898 * Underlying input stream (needed for reading contents)
899 * will be <b>owned</b> (and managed, i.e. closed as need be) by
900 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800901 *
902 * @param url URL pointing to resource that contains JSON content to parse
903 *
904 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
905 */
906 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800907 public JsonParser createJsonParser(URL url) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800908 return createParser(url);
909 }
910
911 /**
912 * Method for constructing JSON parser instance to parse
913 * the contents accessed via specified input stream.
914 *<p>
915 * The input stream will <b>not be owned</b> by
916 * the parser, it will still be managed (i.e. closed if
917 * end-of-stream is reacher, or parser close method called)
918 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
919 * is enabled.
920 *<p>
921 * Note: no encoding argument is taken since it can always be
922 * auto-detected as suggested by JSON RFC.
Tatu Saloranta68194922012-11-15 20:34:29 -0800923 *
924 * @param in InputStream to use for reading JSON content to parse
925 *
926 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
927 */
928 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800929 public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800930 return createParser(in);
931 }
932
933 /**
934 * Method for constructing parser for parsing
935 * the contents accessed via specified Reader.
936 <p>
937 * The read stream will <b>not be owned</b> by
938 * the parser, it will still be managed (i.e. closed if
939 * end-of-stream is reacher, or parser close method called)
940 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
941 * is enabled.
Tatu Saloranta68194922012-11-15 20:34:29 -0800942 *
943 * @param r Reader to use for reading JSON content to parse
944 *
945 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
946 */
947 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800948 public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800949 return createParser(r);
950 }
951
952 /**
Tatu Salorantaefc23672013-12-30 19:09:39 -0800953 * Method for constructing parser for parsing the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800954 *
955 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
956 */
957 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800958 public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800959 return createParser(data);
960 }
961
962 /**
963 * Method for constructing parser for parsing
964 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800965 *
966 * @param data Buffer that contains data to parse
967 * @param offset Offset of the first data byte within buffer
968 * @param len Length of contents to parse within buffer
969 *
970 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
971 */
972 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800973 public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800974 return createParser(data, offset, len);
975 }
976
977 /**
978 * Method for constructing parser for parsing
979 * contents of given String.
980 *
981 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
982 */
983 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800984 public JsonParser createJsonParser(String content) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800985 return createParser(content);
986 }
987
988 /*
989 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700990 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800991 /**********************************************************
992 */
993
994 /**
995 * Method for constructing JSON generator for writing JSON content
996 * using specified output stream.
997 * Encoding to use must be specified, and needs to be one of available
998 * types (as per JSON specification).
999 *<p>
1000 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1001 * so that generator will NOT close the output stream when
1002 * {@link JsonGenerator#close} is called (unless auto-closing
1003 * feature,
1004 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1005 * is enabled).
1006 * Using application needs to close it explicitly if this is the case.
1007 *<p>
1008 * Note: there are formats that use fixed encoding (like most binary data formats)
1009 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001010 *
1011 * @param out OutputStream to use for writing JSON content
1012 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001013 *
1014 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001015 */
1016 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
1017 throws IOException
1018 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001019 // false -> we won't manage the stream unless explicitly directed to
1020 IOContext ctxt = _createContext(out, false);
1021 ctxt.setEncoding(enc);
1022 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001023 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001024 }
1025 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001026 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001027 }
1028
1029 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001030 * Convenience method for constructing generator that uses default
1031 * encoding of the format (UTF-8 for JSON and most other data formats).
1032 *<p>
1033 * Note: there are formats that use fixed encoding (like most binary data formats).
1034 *
1035 * @since 2.1
1036 */
1037 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1038 return createGenerator(out, JsonEncoding.UTF8);
1039 }
1040
1041 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001042 * Method for constructing JSON generator for writing JSON content
1043 * using specified Writer.
1044 *<p>
1045 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1046 * so that generator will NOT close the Reader when
1047 * {@link JsonGenerator#close} is called (unless auto-closing
1048 * feature,
1049 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1050 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001051 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001052 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001053 *
Tatu Saloranta896000f2014-04-19 12:53:40 -07001054 * @param w Writer to use for writing JSON content
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001055 */
Tatu Saloranta896000f2014-04-19 12:53:40 -07001056 public JsonGenerator createGenerator(Writer w) throws IOException {
1057 IOContext ctxt = _createContext(w, false);
1058 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001059 }
1060
1061 /**
1062 * Method for constructing JSON generator for writing JSON content
1063 * to specified file, overwriting contents it might have (or creating
1064 * it if such file does not yet exist).
1065 * Encoding to use must be specified, and needs to be one of available
1066 * types (as per JSON specification).
1067 *<p>
1068 * Underlying stream <b>is owned</b> by the generator constructed,
1069 * i.e. generator will handle closing of file when
1070 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001071 *
1072 * @param f File to write contents to
1073 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001074 *
1075 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001076 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001077 public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001078 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001079 OutputStream out = new FileOutputStream(f);
1080 // true -> yes, we have to manage the stream since we created it
1081 IOContext ctxt = _createContext(out, true);
1082 ctxt.setEncoding(enc);
1083 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001084 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001085 }
1086 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001087 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001088 }
1089
1090 /*
1091 /**********************************************************
1092 /* Generator factories, old (as per [Issue-25]
1093 /**********************************************************
1094 */
1095
1096 /**
1097 * Method for constructing JSON generator for writing JSON content
1098 * using specified output stream.
1099 * Encoding to use must be specified, and needs to be one of available
1100 * types (as per JSON specification).
1101 *<p>
1102 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1103 * so that generator will NOT close the output stream when
1104 * {@link JsonGenerator#close} is called (unless auto-closing
1105 * feature,
1106 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1107 * is enabled).
1108 * Using application needs to close it explicitly if this is the case.
1109 *<p>
1110 * Note: there are formats that use fixed encoding (like most binary data formats)
1111 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001112 *
1113 * @param out OutputStream to use for writing JSON content
1114 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001115 *
1116 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001117 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001118 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001119 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001120 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001121 }
1122
1123 /**
1124 * Method for constructing JSON generator for writing JSON content
1125 * using specified Writer.
1126 *<p>
1127 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1128 * so that generator will NOT close the Reader when
1129 * {@link JsonGenerator#close} is called (unless auto-closing
1130 * feature,
1131 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1132 * Using application needs to close it explicitly.
1133 *
1134 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001135 *
1136 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001137 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001138 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001139 public JsonGenerator createJsonGenerator(Writer out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001140 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001141 }
1142
1143 /**
1144 * Convenience method for constructing generator that uses default
1145 * encoding of the format (UTF-8 for JSON and most other data formats).
1146 *<p>
1147 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001148 *
1149 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001150 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001151 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001152 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001153 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001154 }
1155
1156 /**
1157 * Method for constructing JSON generator for writing JSON content
1158 * to specified file, overwriting contents it might have (or creating
1159 * it if such file does not yet exist).
1160 * Encoding to use must be specified, and needs to be one of available
1161 * types (as per JSON specification).
1162 *<p>
1163 * Underlying stream <b>is owned</b> by the generator constructed,
1164 * i.e. generator will handle closing of file when
1165 * {@link JsonGenerator#close} is called.
1166 *
1167 * @param f File to write contents to
1168 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001169 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001170 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001171 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001172 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001173 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001174 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001175 }
1176
1177 /*
1178 /**********************************************************
1179 /* Factory methods used by factory for creating parser instances,
1180 /* overridable by sub-classes
1181 /**********************************************************
1182 */
1183
1184 /**
1185 * Overridable factory method that actually instantiates desired parser
1186 * given {@link InputStream} and context object.
1187 *<p>
1188 * This method is specifically designed to remain
1189 * compatible between minor versions so that sub-classes can count
1190 * on it being called as expected. That is, it is part of official
1191 * interface from sub-class perspective, although not a public
1192 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001193 *
1194 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001195 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001196 protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001197 // As per [JACKSON-259], may want to fully disable canonicalization:
1198 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
Tatu Salorantadccffbe2015-02-05 21:17:57 -08001199 _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001200 }
1201
1202 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001203 * Overridable factory method that actually instantiates parser
1204 * using given {@link Reader} object for reading content.
1205 *<p>
1206 * This method is specifically designed to remain
1207 * compatible between minor versions so that sub-classes can count
1208 * on it being called as expected. That is, it is part of official
1209 * interface from sub-class perspective, although not a public
1210 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001211 *
1212 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001213 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001214 protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001215 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001216 _rootCharSymbols.makeChild(_factoryFeatures));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001217 }
1218
1219 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001220 * Overridable factory method that actually instantiates parser
Tatu64aa9d22014-04-18 14:07:38 -07001221 * using given <code>char[]</code> object for accessing content.
1222 *
1223 * @since 2.4
1224 */
1225 protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
1226 boolean recyclable) throws IOException {
1227 return new ReaderBasedJsonParser(ctxt, _parserFeatures, null, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001228 _rootCharSymbols.makeChild(_factoryFeatures),
1229 data, offset, offset+len, recyclable);
Tatu64aa9d22014-04-18 14:07:38 -07001230 }
1231
1232 /**
1233 * Overridable factory method that actually instantiates parser
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001234 * using given {@link Reader} object for reading content
1235 * passed as raw byte array.
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 */
Tatu64aa9d22014-04-18 14:07:38 -07001243 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001244 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001245 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
Tatu Salorantadccffbe2015-02-05 21:17:57 -08001246 _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001247 }
1248
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001249 /*
1250 /**********************************************************
1251 /* Factory methods used by factory for creating generator instances,
1252 /* overridable by sub-classes
1253 /**********************************************************
1254 */
1255
1256 /**
1257 * Overridable factory method that actually instantiates generator for
1258 * given {@link Writer} and context object.
1259 *<p>
1260 * This method is specifically designed to remain
1261 * compatible between minor versions so that sub-classes can count
1262 * on it being called as expected. That is, it is part of official
1263 * interface from sub-class perspective, although not a public
1264 * method available to users of factory implementations.
1265 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001266 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001267 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001268 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(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
1280 /**
1281 * Overridable factory method that actually instantiates generator for
1282 * given {@link OutputStream} and context object, using UTF-8 encoding.
1283 *<p>
1284 * This method is specifically designed to remain
1285 * compatible between minor versions so that sub-classes can count
1286 * on it being called as expected. That is, it is part of official
1287 * interface from sub-class perspective, although not a public
1288 * method available to users of factory implementations.
1289 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001290 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001291 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1292 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001293 if (_characterEscapes != null) {
1294 gen.setCharacterEscapes(_characterEscapes);
1295 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001296 SerializableString rootSep = _rootValueSeparator;
1297 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1298 gen.setRootValueSeparator(rootSep);
1299 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001300 return gen;
1301 }
1302
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001303 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1304 {
1305 // note: this should not get called any more (caller checks, dispatches)
1306 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1307 return new UTF8Writer(ctxt, out);
1308 }
1309 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1310 return new OutputStreamWriter(out, enc.getJavaName());
1311 }
Tatu Saloranta896000f2014-04-19 12:53:40 -07001312
1313 /*
1314 /**********************************************************
1315 /* Internal factory methods, decorator handling
1316 /**********************************************************
1317 */
1318
1319 /**
1320 * @since 2.4
1321 */
1322 protected final InputStream _decorate(InputStream in, IOContext ctxt) throws IOException {
1323 if (_inputDecorator != null) {
1324 InputStream in2 = _inputDecorator.decorate(ctxt, in);
1325 if (in2 != null) {
1326 return in2;
1327 }
1328 }
1329 return in;
1330 }
1331
1332 /**
1333 * @since 2.4
1334 */
1335 protected final Reader _decorate(Reader in, IOContext ctxt) throws IOException {
1336 if (_inputDecorator != null) {
1337 Reader in2 = _inputDecorator.decorate(ctxt, in);
1338 if (in2 != null) {
1339 return in2;
1340 }
1341 }
1342 return in;
1343 }
1344
1345 /**
1346 * @since 2.4
1347 */
1348 protected final OutputStream _decorate(OutputStream out, IOContext ctxt) throws IOException {
1349 if (_outputDecorator != null) {
1350 OutputStream out2 = _outputDecorator.decorate(ctxt, out);
1351 if (out2 != null) {
1352 return out2;
1353 }
1354 }
1355 return out;
1356 }
1357
1358 /**
1359 * @since 2.4
1360 */
1361 protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException {
1362 if (_outputDecorator != null) {
1363 Writer out2 = _outputDecorator.decorate(ctxt, out);
1364 if (out2 != null) {
1365 return out2;
1366 }
1367 }
1368 return out;
1369 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001370
1371 /*
1372 /**********************************************************
1373 /* Internal factory methods, other
1374 /**********************************************************
1375 */
1376
1377 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001378 * Method used by factory to create buffer recycler instances
1379 * for parsers and generators.
1380 *<p>
1381 * Note: only public to give access for <code>ObjectMapper</code>
1382 */
1383 public BufferRecycler _getBufferRecycler()
1384 {
1385 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1386 BufferRecycler br = (ref == null) ? null : ref.get();
1387
1388 if (br == null) {
1389 br = new BufferRecycler();
1390 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1391 }
1392 return br;
1393 }
1394
1395 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001396 * Overridable factory method that actually instantiates desired
1397 * context object.
1398 */
1399 protected IOContext _createContext(Object srcRef, boolean resourceManaged) {
1400 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1401 }
1402
1403 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001404 * Helper methods used for constructing an optimal stream for
1405 * parsers to use, when input is to be read from an URL.
1406 * This helps when reading file content via URL.
1407 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001408 protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001409 if ("file".equals(url.getProtocol())) {
1410 /* Can not do this if the path refers
1411 * to a network drive on windows. This fixes the problem;
1412 * might not be needed on all platforms (NFS?), but should not
1413 * matter a lot: performance penalty of extra wrapping is more
1414 * relevant when accessing local file system.
1415 */
1416 String host = url.getHost();
1417 if (host == null || host.length() == 0) {
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001418 // [Issue#48]: Let's try to avoid probs with URL encoded stuff
1419 String path = url.getPath();
1420 if (path.indexOf('%') < 0) {
1421 return new FileInputStream(url.getPath());
1422
1423 }
1424 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001425 }
1426 }
1427 return url.openStream();
1428 }
1429}