blob: c68317f3c65f33772fdf934cc3a512fe8be21af8 [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
Tatu Saloranta8891c0c2015-04-23 22:47:05 -0700104 /**
105 * Feature that determines whether we will use {@link BufferRecycler} with
106 * {@link ThreadLocal} and {@link SoftReference}, for efficient reuse of
107 * underlying input/output buffers.
108 * This usually makes sense on normal J2SE/J2EE server-side processing;
109 * but may not make sense on platforms where {@link SoftReference} handling
110 * is broken (like Android), or if there are retention issues due to
111 * {@link ThreadLocal} (see
112 * <a href="https://github.com/FasterXML/jackson-core/issues/189">Issue #189</a>
113 * for a possible case)
114 *
115 * @since 2.6
116 */
117 USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING(true)
118
Tatu07351902012-01-19 13:12:13 -0800119 ;
120
121 /**
122 * Whether feature is enabled or disabled by default.
123 */
124 private final boolean _defaultState;
125
126 /**
127 * Method that calculates bit set (flags) of all features that
128 * are enabled by default.
129 */
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800130 public static int collectDefaults() {
Tatu07351902012-01-19 13:12:13 -0800131 int flags = 0;
132 for (Feature f : values()) {
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800133 if (f.enabledByDefault()) { flags |= f.getMask(); }
Tatu07351902012-01-19 13:12:13 -0800134 }
135 return flags;
136 }
137
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800138 private Feature(boolean defaultState) { _defaultState = defaultState; }
Tatu07351902012-01-19 13:12:13 -0800139
140 public boolean enabledByDefault() { return _defaultState; }
Tatu07351902012-01-19 13:12:13 -0800141 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
Tatu07351902012-01-19 13:12:13 -0800142 public int getMask() { return (1 << ordinal()); }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700143 }
144
145 /*
146 /**********************************************************
147 /* Constants
148 /**********************************************************
149 */
150
151 /**
152 * Name used to identify JSON format
153 * (and returned by {@link #getFormatName()}
154 */
155 public final static String FORMAT_NAME_JSON = "JSON";
156
157 /**
158 * Bitfield (set of flags) of all factory features that are enabled by default.
159 */
160 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
161
162 /**
163 * Bitfield (set of flags) of all parser features that are enabled
164 * by default.
165 */
166 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
167
168 /**
169 * Bitfield (set of flags) of all generator features that are enabled
170 * by default.
171 */
172 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
173
174 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
175
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800176 /*
177 /**********************************************************
178 /* Buffer, symbol table management
179 /**********************************************************
180 */
181
182 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700183 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800184 * to a {@link BufferRecycler} used to provide a low-cost
185 * buffer recycling between reader and writer instances.
186 */
187 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
188 = new ThreadLocal<SoftReference<BufferRecycler>>();
189
190 /**
191 * Each factory comes equipped with a shared root symbol table.
192 * It should not be linked back to the original blueprint, to
193 * avoid contents from leaking between factories.
194 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700195 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800196
197 /**
198 * Alternative to the basic symbol table, some stream-based
199 * parsers use different name canonicalization method.
200 *<p>
201 * TODO: should clean up this; looks messy having 2 alternatives
202 * with not very clear differences.
Tatu Salorantadccffbe2015-02-05 21:17:57 -0800203 *
204 * @since 2.6.0
205 */
206 protected final transient ByteQuadsCanonicalizer _byteSymbolCanonicalizer = ByteQuadsCanonicalizer.createRoot();
207
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800208 /*
209 /**********************************************************
210 /* Configuration
211 /**********************************************************
212 */
213
214 /**
215 * Object that implements conversion functionality between
216 * Java objects and JSON content. For base JsonFactory implementation
217 * usually not set by default, but can be explicitly set.
218 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
219 * usually provide an implementation.
220 */
221 protected ObjectCodec _objectCodec;
222
223 /**
Tatu07351902012-01-19 13:12:13 -0800224 * Currently enabled factory features.
225 */
226 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
227
228 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800229 * Currently enabled parser features.
230 */
231 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
232
233 /**
234 * Currently enabled generator features.
235 */
236 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
237
238 /**
239 * Definition of custom character escapes to use for generators created
240 * by this factory, if any. If null, standard data format specific
241 * escapes are used.
242 */
243 protected CharacterEscapes _characterEscapes;
244
245 /**
246 * Optional helper object that may decorate input sources, to do
247 * additional processing on input during parsing.
248 */
249 protected InputDecorator _inputDecorator;
250
251 /**
252 * Optional helper object that may decorate output object, to do
253 * additional processing on output during content generation.
254 */
255 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700256
257 /**
258 * Separator used between root-level values, if any; null indicates
259 * "do not add separator".
260 * Default separator is a single space character.
261 *
262 * @since 2.1
263 */
264 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800265
266 /*
267 /**********************************************************
268 /* Construction
269 /**********************************************************
270 */
271
272 /**
273 * Default constructor used to create factory instances.
274 * Creation of a factory instance is a light-weight operation,
275 * but it is still a good idea to reuse limited number of
276 * factory instances (and quite often just a single instance):
277 * factories are used as context for storing some reused
278 * processing objects (such as symbol tables parsers use)
279 * and this reuse only works within context of a single
280 * factory instance.
281 */
Gonçalo Silva2bba5dd2014-01-25 17:06:21 +0000282 public JsonFactory() { this(null); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800283
284 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
285
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700286 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700287 * Constructor used when copy()ing a factory instance.
288 *
289 * @since 2.2.1
290 */
291 protected JsonFactory(JsonFactory src, ObjectCodec codec)
292 {
293 _objectCodec = null;
294 _factoryFeatures = src._factoryFeatures;
295 _parserFeatures = src._parserFeatures;
296 _generatorFeatures = src._generatorFeatures;
297 _characterEscapes = src._characterEscapes;
298 _inputDecorator = src._inputDecorator;
299 _outputDecorator = src._outputDecorator;
300 _rootValueSeparator = src._rootValueSeparator;
301
302 /* 27-Apr-2013, tatu: How about symbol table; should we try to
303 * reuse shared symbol tables? Could be more efficient that way;
304 * although can slightly add to concurrency overhead.
305 */
306 }
307
308 /**
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700309 * Method for constructing a new {@link JsonFactory} that has
310 * the same settings as this instance, but is otherwise
311 * independent (i.e. nothing is actually shared, symbol tables
312 * are separate).
313 * Note that {@link ObjectCodec} reference is not copied but is
314 * set to null; caller typically needs to set it after calling
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700315 * this method. Reason for this is that the codec is used for
316 * callbacks, and assumption is that there is strict 1-to-1
317 * mapping between codec, factory. Caller has to, then, explicitly
318 * set codec after making the copy.
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700319 *
320 * @since 2.1
321 */
322 public JsonFactory copy()
323 {
324 _checkInvalidCopy(JsonFactory.class);
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700325 // as per above, do clear ObjectCodec
326 return new JsonFactory(this, null);
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700327 }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700328
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700329 /**
330 * @since 2.1
331 * @param exp
332 */
333 protected void _checkInvalidCopy(Class<?> exp)
334 {
335 if (getClass() != exp) {
336 throw new IllegalStateException("Failed copy(): "+getClass().getName()
337 +" (version: "+version()+") does not override copy(); it has to");
338 }
339 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700340
341 /*
342 /**********************************************************
343 /* Serializable overrides
344 /**********************************************************
345 */
346
347 /**
348 * Method that we need to override to actually make restoration go
349 * through constructors etc.
350 * Also: must be overridden by sub-classes as well.
351 */
352 protected Object readResolve() {
Tatu Salorantaf7741c52013-04-27 11:06:30 -0700353 return new JsonFactory(this, _objectCodec);
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700354 }
Tatu Salorantaa9e5c9f2013-08-28 19:39:59 -0700355
356 /*
357 /**********************************************************
358 /* Capability introspection
359 /**********************************************************
360 */
361
362 /**
363 * Introspection method that higher-level functionality may call
364 * to see whether underlying data format requires a stable ordering
365 * of object properties or not.
366 * This is usually used for determining
367 * whether to force a stable ordering (like alphabetic ordering by name)
368 * if no ordering if explicitly specified.
369 *<p>
370 * Default implementation returns <code>false</code> as JSON does NOT
371 * require stable ordering. Formats that require ordering include positional
372 * textual formats like <code>CSV</code>, and schema-based binary formats
373 * like <code>Avro</code>.
374 *
375 * @since 2.3
376 */
Tatu475a99b2014-04-22 14:32:50 -0700377 public boolean requiresPropertyOrdering() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700378
379 /**
380 * Introspection method that higher-level functionality may call
381 * to see whether underlying data format can read and write binary
382 * data natively; that is, embeded it as-is without using encodings
383 * such as Base64.
384 *<p>
385 * Default implementation returns <code>false</code> as JSON does not
386 * support native access: all binary content must use Base64 encoding.
387 * Most binary formats (like Smile and Avro) support native binary content.
388 *
389 * @since 2.3
390 */
Tatu475a99b2014-04-22 14:32:50 -0700391 public boolean canHandleBinaryNatively() { return false; }
Tatu Salorantab8835442013-09-14 12:12:57 -0700392
Tatu475a99b2014-04-22 14:32:50 -0700393 /**
394 * Introspection method that can be used by base factory to check
395 * whether access using <code>char[]</code> is something that actual
396 * parser implementations can take advantage of, over having to
397 * use {@link java.io.Reader}. Sub-types are expected to override
398 * definition; default implementation (suitable for JSON) alleges
399 * that optimization are possible; and thereby is likely to try
400 * to access {@link java.lang.String} content by first copying it into
401 * recyclable intermediate buffer.
402 *
403 * @since 2.4
404 */
405 public boolean canUseCharArrays() { return true; }
Cowtowncoder2e262b92015-05-27 17:32:17 -0700406
407 /**
408 * Method for accessing kind of {@link FormatFeature} that a parser
409 * {@link JsonParser} produced by this factory would accept, if any;
410 * <code>null</code> returned if none.
411 *
412 * @since 2.6
413 */
414 public Class<? extends FormatFeature> getFormatReadFeatureType() {
415 return null;
416 }
417
418 /**
419 * Method for accessing kind of {@link FormatFeature} that a parser
420 * {@link JsonGenerator} produced by this factory would accept, if any;
421 * <code>null</code> returned if none.
422 *
423 * @since 2.6
424 */
425 public Class<? extends FormatFeature> getFormatWriteFeatureType() {
426 return null;
427 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800428 /*
429 /**********************************************************
Cowtowncoder2e262b92015-05-27 17:32:17 -0700430 /* Format detection functionality
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800431 /**********************************************************
432 */
433
434 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700435 * Method that can be used to quickly check whether given schema
436 * is something that parsers and/or generators constructed by this
437 * factory could use. Note that this means possible use, at the level
438 * of data format (i.e. schema is for same data format as parsers and
439 * generators this factory constructs); individual schema instances
440 * may have further usage restrictions.
441 *
442 * @since 2.1
443 */
444 public boolean canUseSchema(FormatSchema schema) {
Tatu Saloranta244ddd12016-05-14 15:11:04 -0700445 if (schema == null){
446 return false;
447 }
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700448 String ourFormat = getFormatName();
449 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
450 }
451
452 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800453 * Method that returns short textual id identifying format
454 * this factory supports.
455 *<p>
456 * Note: sub-classes should override this method; default
457 * implementation will return null for all sub-classes
458 */
459 public String getFormatName()
460 {
461 /* Somewhat nasty check: since we can't make this abstract
462 * (due to backwards compatibility concerns), need to prevent
463 * format name "leakage"
464 */
465 if (getClass() == JsonFactory.class) {
466 return FORMAT_NAME_JSON;
467 }
468 return null;
469 }
470
Cowtowncoder2e262b92015-05-27 17:32:17 -0700471 /**
472 * Convenience method for trying to determine whether input via given accessor
473 * is of format type supported by this factory.
474 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800475 public MatchStrength hasFormat(InputAccessor acc) throws IOException
476 {
477 // since we can't keep this abstract, only implement for "vanilla" instance
478 if (getClass() == JsonFactory.class) {
479 return hasJSONFormat(acc);
480 }
481 return null;
482 }
483
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700484 /**
485 * Method that can be called to determine if a custom
486 * {@link ObjectCodec} is needed for binding data parsed
487 * using {@link JsonParser} constructed by this factory
488 * (which typically also implies the same for serialization
489 * with {@link JsonGenerator}).
490 *
491 * @return True if custom codec is needed with parsers and
492 * generators created by this factory; false if a general
493 * {@link ObjectCodec} is enough
494 *
495 * @since 2.1
496 */
497 public boolean requiresCustomCodec() {
498 return false;
499 }
500
501 /**
502 * Helper method that can be called to determine if content accessed
503 * using given accessor seems to be JSON content.
504 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800505 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
506 {
507 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700508 }
509
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800510 /*
511 /**********************************************************
512 /* Versioned
513 /**********************************************************
514 */
515
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800516 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800517 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800518 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800519 }
520
521 /*
522 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800523 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800524 /**********************************************************
525 */
526
527 /**
528 * Method for enabling or disabling specified parser feature
529 * (check {@link JsonParser.Feature} for list of features)
530 */
Tatu07351902012-01-19 13:12:13 -0800531 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
532 return state ? enable(f) : disable(f);
533 }
534
535 /**
536 * Method for enabling specified parser feature
537 * (check {@link JsonFactory.Feature} for list of features)
538 */
539 public JsonFactory enable(JsonFactory.Feature f) {
540 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800541 return this;
542 }
543
544 /**
Tatu07351902012-01-19 13:12:13 -0800545 * Method for disabling specified parser features
546 * (check {@link JsonFactory.Feature} for list of features)
547 */
548 public JsonFactory disable(JsonFactory.Feature f) {
549 _factoryFeatures &= ~f.getMask();
550 return this;
551 }
552
553 /**
554 * Checked whether specified parser feature is enabled.
555 */
556 public final boolean isEnabled(JsonFactory.Feature f) {
557 return (_factoryFeatures & f.getMask()) != 0;
558 }
559
560 /*
561 /**********************************************************
562 /* Configuration, parser configuration
563 /**********************************************************
564 */
565
566 /**
567 * Method for enabling or disabling specified parser feature
568 * (check {@link JsonParser.Feature} for list of features)
569 */
570 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
571 return state ? enable(f) : disable(f);
572 }
573
574 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800575 * Method for enabling specified parser feature
576 * (check {@link JsonParser.Feature} for list of features)
577 */
578 public JsonFactory enable(JsonParser.Feature f) {
579 _parserFeatures |= f.getMask();
580 return this;
581 }
582
583 /**
584 * Method for disabling specified parser features
585 * (check {@link JsonParser.Feature} for list of features)
586 */
587 public JsonFactory disable(JsonParser.Feature f) {
588 _parserFeatures &= ~f.getMask();
589 return this;
590 }
591
592 /**
593 * Checked whether specified parser feature is enabled.
594 */
595 public final boolean isEnabled(JsonParser.Feature f) {
596 return (_parserFeatures & f.getMask()) != 0;
597 }
598
599 /**
600 * Method for getting currently configured input decorator (if any;
601 * there is no default decorator).
602 */
603 public InputDecorator getInputDecorator() {
604 return _inputDecorator;
605 }
606
607 /**
608 * Method for overriding currently configured input decorator
609 */
610 public JsonFactory setInputDecorator(InputDecorator d) {
611 _inputDecorator = d;
612 return this;
613 }
614
615 /*
616 /**********************************************************
617 /* Configuration, generator settings
618 /**********************************************************
619 */
620
621 /**
622 * Method for enabling or disabling specified generator feature
623 * (check {@link JsonGenerator.Feature} for list of features)
624 */
625 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800626 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800627 }
628
629
630 /**
631 * Method for enabling specified generator features
632 * (check {@link JsonGenerator.Feature} for list of features)
633 */
634 public JsonFactory enable(JsonGenerator.Feature f) {
635 _generatorFeatures |= f.getMask();
636 return this;
637 }
638
639 /**
640 * Method for disabling specified generator feature
641 * (check {@link JsonGenerator.Feature} for list of features)
642 */
643 public JsonFactory disable(JsonGenerator.Feature f) {
644 _generatorFeatures &= ~f.getMask();
645 return this;
646 }
647
648 /**
649 * Check whether specified generator feature is enabled.
650 */
651 public final boolean isEnabled(JsonGenerator.Feature f) {
652 return (_generatorFeatures & f.getMask()) != 0;
653 }
654
655 /**
656 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
657 * it creates.
658 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800659 public CharacterEscapes getCharacterEscapes() { return _characterEscapes; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800660
661 /**
662 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
663 * it creates.
664 */
665 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
666 _characterEscapes = esc;
667 return this;
668 }
669
670 /**
671 * Method for getting currently configured output decorator (if any;
672 * there is no default decorator).
673 */
674 public OutputDecorator getOutputDecorator() {
675 return _outputDecorator;
676 }
677
678 /**
679 * Method for overriding currently configured output decorator
680 */
681 public JsonFactory setOutputDecorator(OutputDecorator d) {
682 _outputDecorator = d;
683 return this;
684 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700685
686 /**
687 * Method that allows overriding String used for separating root-level
688 * JSON values (default is single space character)
689 *
690 * @param sep Separator to use, if any; null means that no separator is
691 * automatically added
692 *
693 * @since 2.1
694 */
695 public JsonFactory setRootValueSeparator(String sep) {
696 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
697 return this;
698 }
699
700 /**
701 * @since 2.1
702 */
703 public String getRootValueSeparator() {
704 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
705 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800706
707 /*
708 /**********************************************************
709 /* Configuration, other
710 /**********************************************************
711 */
712
713 /**
714 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800715 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
716 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800717 * it constructs). This is needed to use data-binding methods
718 * of {@link JsonParser} and {@link JsonGenerator} instances.
719 */
720 public JsonFactory setCodec(ObjectCodec oc) {
721 _objectCodec = oc;
722 return this;
723 }
724
725 public ObjectCodec getCodec() { return _objectCodec; }
726
727 /*
728 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700729 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800730 /**********************************************************
731 */
732
733 /**
734 * Method for constructing JSON parser instance to parse
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300735 * contents of specified file.
736 *
737 *<p>
738 * Encoding is auto-detected from contents according to JSON
739 * specification recommended mechanism. Json specification
740 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
741 * so auto-detection implemented only for this charsets.
742 * For other charsets use {@link #createParser(java.io.Reader)}.
743 *
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800744 *<p>
745 * Underlying input stream (needed for reading contents)
746 * will be <b>owned</b> (and managed, i.e. closed as need be) by
747 * the parser, since caller has no access to it.
748 *
749 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700750 *
751 * @since 2.1
752 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800753 public JsonParser createParser(File f) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800754 // true, since we create InputStream from File
755 IOContext ctxt = _createContext(f, true);
756 InputStream in = new FileInputStream(f);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700757 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800758 }
759
760 /**
761 * Method for constructing JSON parser instance to parse
762 * contents of resource reference by given URL.
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300763 *
764 *<p>
765 * Encoding is auto-detected from contents according to JSON
766 * specification recommended mechanism. Json specification
767 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
768 * so auto-detection implemented only for this charsets.
769 * For other charsets use {@link #createParser(java.io.Reader)}.
770 *
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800771 *<p>
772 * Underlying input stream (needed for reading contents)
773 * will be <b>owned</b> (and managed, i.e. closed as need be) by
774 * the parser, since caller has no access to it.
775 *
776 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800777 *
778 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800779 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800780 public JsonParser createParser(URL url) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800781 // true, since we create InputStream from URL
782 IOContext ctxt = _createContext(url, true);
783 InputStream in = _optimizedStreamFromURL(url);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700784 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800785 }
786
787 /**
788 * Method for constructing JSON parser instance to parse
789 * the contents accessed via specified input stream.
790 *<p>
791 * The input stream will <b>not be owned</b> by
792 * the parser, it will still be managed (i.e. closed if
793 * end-of-stream is reacher, or parser close method called)
794 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
795 * is enabled.
796 *<p>
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300797 *
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800798 * Note: no encoding argument is taken since it can always be
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300799 * auto-detected as suggested by JSON RFC. Json specification
800 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
801 * so auto-detection implemented only for this charsets.
802 * For other charsets use {@link #createParser(java.io.Reader)}.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800803 *
804 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800805 *
806 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800807 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800808 public JsonParser createParser(InputStream in) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800809 IOContext ctxt = _createContext(in, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700810 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800811 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700812
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800813 /**
814 * Method for constructing parser for parsing
815 * the contents accessed via specified Reader.
816 <p>
817 * The read stream will <b>not be owned</b> by
818 * the parser, it will still be managed (i.e. closed if
819 * end-of-stream is reacher, or parser close method called)
820 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
821 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800822 *
823 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800824 *
825 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800826 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800827 public JsonParser createParser(Reader r) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800828 // false -> we do NOT own Reader (did not create it)
829 IOContext ctxt = _createContext(r, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700830 return _createParser(_decorate(r, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800831 }
832
833 /**
834 * Method for constructing parser for parsing
835 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800836 *
837 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800838 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800839 public JsonParser createParser(byte[] data) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800840 IOContext ctxt = _createContext(data, true);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800841 if (_inputDecorator != null) {
842 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
843 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700844 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800845 }
846 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700847 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800848 }
849
850 /**
851 * Method for constructing parser for parsing
852 * the contents of given byte array.
853 *
854 * @param data Buffer that contains data to parse
855 * @param offset Offset of the first data byte within buffer
856 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800857 *
858 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800859 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800860 public JsonParser createParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800861 IOContext ctxt = _createContext(data, true);
862 // [JACKSON-512]: allow wrapping with InputDecorator
863 if (_inputDecorator != null) {
864 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
865 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700866 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800867 }
868 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800869 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800870 }
871
872 /**
873 * Method for constructing parser for parsing
874 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800875 *
876 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800877 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800878 public JsonParser createParser(String content) throws IOException, JsonParseException {
Tatu64aa9d22014-04-18 14:07:38 -0700879 final int strLen = content.length();
880 // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char)
Tatu Salorantad7cbb642016-05-13 13:23:04 -0700881 if ((_inputDecorator != null) || (strLen > 0x8000) || !canUseCharArrays()) {
Tatu64aa9d22014-04-18 14:07:38 -0700882 // easier to just wrap in a Reader than extend InputDecorator; or, if content
883 // is too long for us to copy it over
884 return createParser(new StringReader(content));
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800885 }
Tatu64aa9d22014-04-18 14:07:38 -0700886 IOContext ctxt = _createContext(content, true);
Tatu Saloranta84dc1842014-04-18 21:07:57 -0700887 char[] buf = ctxt.allocTokenBuffer(strLen);
Tatu64aa9d22014-04-18 14:07:38 -0700888 content.getChars(0, strLen, buf, 0);
889 return _createParser(buf, 0, strLen, ctxt, true);
890 }
891
892 /**
893 * Method for constructing parser for parsing
894 * contents of given char array.
895 *
896 * @since 2.4
897 */
898 public JsonParser createParser(char[] content) throws IOException {
899 return createParser(content, 0, content.length);
900 }
901
902 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -0700903 * Method for constructing parser for parsing contents of given char array.
Tatu64aa9d22014-04-18 14:07:38 -0700904 *
905 * @since 2.4
906 */
907 public JsonParser createParser(char[] content, int offset, int len) throws IOException {
908 if (_inputDecorator != null) { // easier to just wrap in a Reader than extend InputDecorator
909 return createParser(new CharArrayReader(content, offset, len));
910 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700911 return _createParser(content, offset, len, _createContext(content, true),
912 // important: buffer is NOT recyclable, as it's from caller
913 false);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800914 }
915
Tatu Salorantad7cbb642016-05-13 13:23:04 -0700916 /**
917 * @since 2.8
918 */
919 public JsonParser createParser(DataInput in) throws IOException {
920 IOContext ctxt = _createContext(in, false);
921 return _createParser(_decorate(in, ctxt), ctxt);
922 }
923
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800924 /*
925 /**********************************************************
Tatu Salorantad302ae02015-12-27 21:53:15 -0800926 /* Parser factories (old ones, pre-2.2)
Tatu Saloranta68194922012-11-15 20:34:29 -0800927 /**********************************************************
928 */
929
930 /**
931 * Method for constructing JSON parser instance to parse
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300932 * contents of specified file.
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300933 *<p>
934 * Encoding is auto-detected from contents according to JSON
935 * specification recommended mechanism. Json specification
936 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
937 * so auto-detection implemented only for this charsets.
938 * For other charsets use {@link #createParser(java.io.Reader)}.
939 *
Tatu Saloranta68194922012-11-15 20:34:29 -0800940 *<p>
941 * Underlying input stream (needed for reading contents)
942 * will be <b>owned</b> (and managed, i.e. closed as need be) by
943 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800944 *
945 * @param f File that contains JSON content to parse
946 *
947 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
948 */
949 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800950 public JsonParser createJsonParser(File f) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800951 return createParser(f);
952 }
953
954 /**
955 * Method for constructing JSON parser instance to parse
956 * contents of resource reference by given URL.
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300957 *
958 *<p>
959 * Encoding is auto-detected from contents according to JSON
960 * specification recommended mechanism. Json specification
961 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
962 * so auto-detection implemented only for this charsets.
963 * For other charsets use {@link #createParser(java.io.Reader)}.
964 *
Tatu Saloranta68194922012-11-15 20:34:29 -0800965 *<p>
966 * Underlying input stream (needed for reading contents)
967 * will be <b>owned</b> (and managed, i.e. closed as need be) by
968 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800969 *
970 * @param url URL pointing to resource that contains JSON content to parse
971 *
972 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
973 */
974 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800975 public JsonParser createJsonParser(URL url) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800976 return createParser(url);
977 }
978
979 /**
980 * Method for constructing JSON parser instance to parse
981 * the contents accessed via specified input stream.
982 *<p>
983 * The input stream will <b>not be owned</b> by
984 * the parser, it will still be managed (i.e. closed if
985 * end-of-stream is reacher, or parser close method called)
986 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
987 * is enabled.
988 *<p>
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300989 *
Tatu Saloranta68194922012-11-15 20:34:29 -0800990 * Note: no encoding argument is taken since it can always be
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300991 * auto-detected as suggested by JSON RFC. Json specification
992 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
993 * so auto-detection implemented only for this charsets.
994 * For other charsets use {@link #createParser(java.io.Reader)}.
Tatu Saloranta68194922012-11-15 20:34:29 -0800995 *
996 * @param in InputStream to use for reading JSON content to parse
997 *
998 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
999 */
1000 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001001 public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001002 return createParser(in);
1003 }
1004
1005 /**
1006 * Method for constructing parser for parsing
1007 * the contents accessed via specified Reader.
1008 <p>
1009 * The read stream will <b>not be owned</b> by
1010 * the parser, it will still be managed (i.e. closed if
1011 * end-of-stream is reacher, or parser close method called)
1012 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
1013 * is enabled.
Tatu Saloranta68194922012-11-15 20:34:29 -08001014 *
1015 * @param r Reader to use for reading JSON content to parse
1016 *
1017 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
1018 */
1019 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001020 public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001021 return createParser(r);
1022 }
1023
1024 /**
Tatu Salorantaefc23672013-12-30 19:09:39 -08001025 * Method for constructing parser for parsing the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -08001026 *
1027 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
1028 */
1029 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001030 public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001031 return createParser(data);
1032 }
1033
1034 /**
1035 * Method for constructing parser for parsing
1036 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -08001037 *
1038 * @param data Buffer that contains data to parse
1039 * @param offset Offset of the first data byte within buffer
1040 * @param len Length of contents to parse within buffer
1041 *
1042 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
1043 */
1044 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001045 public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001046 return createParser(data, offset, len);
1047 }
1048
1049 /**
1050 * Method for constructing parser for parsing
1051 * contents of given String.
1052 *
1053 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
1054 */
1055 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001056 public JsonParser createJsonParser(String content) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001057 return createParser(content);
1058 }
1059
1060 /*
1061 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001062 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001063 /**********************************************************
1064 */
1065
1066 /**
1067 * Method for constructing JSON generator for writing JSON content
1068 * using specified output stream.
1069 * Encoding to use must be specified, and needs to be one of available
1070 * types (as per JSON specification).
1071 *<p>
1072 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1073 * so that generator will NOT close the output stream when
1074 * {@link JsonGenerator#close} is called (unless auto-closing
1075 * feature,
1076 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1077 * is enabled).
1078 * Using application needs to close it explicitly if this is the case.
1079 *<p>
1080 * Note: there are formats that use fixed encoding (like most binary data formats)
1081 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001082 *
1083 * @param out OutputStream to use for writing JSON content
1084 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001085 *
1086 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001087 */
1088 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
1089 throws IOException
1090 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001091 // false -> we won't manage the stream unless explicitly directed to
1092 IOContext ctxt = _createContext(out, false);
1093 ctxt.setEncoding(enc);
1094 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001095 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001096 }
1097 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001098 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001099 }
1100
1101 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001102 * Convenience method for constructing generator that uses default
1103 * encoding of the format (UTF-8 for JSON and most other data formats).
1104 *<p>
1105 * Note: there are formats that use fixed encoding (like most binary data formats).
1106 *
1107 * @since 2.1
1108 */
1109 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1110 return createGenerator(out, JsonEncoding.UTF8);
1111 }
1112
1113 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001114 * Method for constructing JSON generator for writing JSON content
1115 * using specified Writer.
1116 *<p>
1117 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1118 * so that generator will NOT close the Reader when
1119 * {@link JsonGenerator#close} is called (unless auto-closing
1120 * feature,
1121 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1122 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001123 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001124 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001125 *
Tatu Saloranta896000f2014-04-19 12:53:40 -07001126 * @param w Writer to use for writing JSON content
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001127 */
Tatu Saloranta896000f2014-04-19 12:53:40 -07001128 public JsonGenerator createGenerator(Writer w) throws IOException {
1129 IOContext ctxt = _createContext(w, false);
1130 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001131 }
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.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001143 *
1144 * @param f File to write contents to
1145 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001146 *
1147 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001148 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001149 public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001150 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001151 OutputStream out = new FileOutputStream(f);
1152 // true -> yes, we have to manage the stream since we created it
1153 IOContext ctxt = _createContext(out, true);
1154 ctxt.setEncoding(enc);
1155 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001156 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001157 }
1158 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001159 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001160 }
1161
Tatu Saloranta38b3ef12016-05-03 18:08:31 -07001162 /**
1163 * Method for constructing generator for writing content using specified
1164 * {@link DataOutput} instance.
1165 *
1166 * @since 2.8
1167 */
1168 public JsonGenerator createGenerator(DataOutput out, JsonEncoding enc) throws IOException {
1169 return createGenerator(_createDataOutputWrapper(out), enc);
1170 }
1171
1172 /**
1173 * Convenience method for constructing generator that uses default
1174 * encoding of the format (UTF-8 for JSON and most other data formats).
1175 *<p>
1176 * Note: there are formats that use fixed encoding (like most binary data formats).
1177 *
1178 * @since 2.8
1179 */
1180 public JsonGenerator createGenerator(DataOutput out) throws IOException {
1181 return createGenerator(_createDataOutputWrapper(out), JsonEncoding.UTF8);
1182 }
1183
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001184 /*
1185 /**********************************************************
Tatu Salorantad302ae02015-12-27 21:53:15 -08001186 /* Generator factories, old (pre-2.2)
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001187 /**********************************************************
1188 */
1189
1190 /**
1191 * Method for constructing JSON generator for writing JSON content
1192 * using specified output stream.
1193 * Encoding to use must be specified, and needs to be one of available
1194 * types (as per JSON specification).
1195 *<p>
1196 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1197 * so that generator will NOT close the output stream when
1198 * {@link JsonGenerator#close} is called (unless auto-closing
1199 * feature,
1200 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1201 * is enabled).
1202 * Using application needs to close it explicitly if this is the case.
1203 *<p>
1204 * Note: there are formats that use fixed encoding (like most binary data formats)
1205 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001206 *
1207 * @param out OutputStream to use for writing JSON content
1208 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001209 *
1210 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001211 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001212 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001213 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001214 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001215 }
1216
1217 /**
1218 * Method for constructing JSON generator for writing JSON content
1219 * using specified Writer.
1220 *<p>
1221 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1222 * so that generator will NOT close the Reader when
1223 * {@link JsonGenerator#close} is called (unless auto-closing
1224 * feature,
1225 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1226 * Using application needs to close it explicitly.
1227 *
1228 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001229 *
1230 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001231 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001232 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001233 public JsonGenerator createJsonGenerator(Writer out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001234 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001235 }
1236
1237 /**
1238 * Convenience method for constructing generator that uses default
1239 * encoding of the format (UTF-8 for JSON and most other data formats).
1240 *<p>
1241 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001242 *
1243 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001244 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001245 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001246 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001247 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001248 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001249
1250 /*
1251 /**********************************************************
1252 /* Factory methods used by factory for creating parser instances,
1253 /* overridable by sub-classes
1254 /**********************************************************
1255 */
1256
1257 /**
1258 * Overridable factory method that actually instantiates desired parser
1259 * given {@link InputStream} and context object.
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.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001266 *
1267 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001268 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001269 protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001270 // As per [JACKSON-259], may want to fully disable canonicalization:
1271 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
Tatu Salorantadccffbe2015-02-05 21:17:57 -08001272 _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001273 }
1274
1275 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001276 * Overridable factory method that actually instantiates parser
1277 * using given {@link Reader} object for reading content.
1278 *<p>
1279 * This method is specifically designed to remain
1280 * compatible between minor versions so that sub-classes can count
1281 * on it being called as expected. That is, it is part of official
1282 * interface from sub-class perspective, although not a public
1283 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001284 *
1285 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001286 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001287 protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001288 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001289 _rootCharSymbols.makeChild(_factoryFeatures));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001290 }
1291
1292 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001293 * Overridable factory method that actually instantiates parser
Tatu64aa9d22014-04-18 14:07:38 -07001294 * using given <code>char[]</code> object for accessing content.
1295 *
1296 * @since 2.4
1297 */
1298 protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
1299 boolean recyclable) throws IOException {
1300 return new ReaderBasedJsonParser(ctxt, _parserFeatures, null, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001301 _rootCharSymbols.makeChild(_factoryFeatures),
1302 data, offset, offset+len, recyclable);
Tatu64aa9d22014-04-18 14:07:38 -07001303 }
1304
1305 /**
1306 * Overridable factory method that actually instantiates parser
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001307 * using given {@link Reader} object for reading content
1308 * passed as raw byte array.
1309 *<p>
1310 * This method is specifically designed to remain
1311 * compatible between minor versions so that sub-classes can count
1312 * on it being called as expected. That is, it is part of official
1313 * interface from sub-class perspective, although not a public
1314 * method available to users of factory implementations.
1315 */
Tatu64aa9d22014-04-18 14:07:38 -07001316 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001317 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001318 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
Tatu Salorantadccffbe2015-02-05 21:17:57 -08001319 _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001320 }
1321
Tatu Salorantad7cbb642016-05-13 13:23:04 -07001322 /**
1323 * @since 2.8
1324 */
1325 protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOException
1326 {
1327 // 13-May-2016, tatu: Need to take care not to accidentally create JSON parser for
1328 // non-JSON input. So, bit unclean but...
1329 String format = getFormatName();
1330 if (format != FORMAT_NAME_JSON) {
1331 throw new UnsupportedOperationException(String.format(
1332 "InputData source not (yet?) support for this format (%s)", format));
1333 }
1334 ByteQuadsCanonicalizer can = _byteSymbolCanonicalizer.makeChild(_factoryFeatures);
1335 return new UTF8DataInputJsonParser(ctxt, _parserFeatures, input,
1336 _objectCodec, can);
1337 }
1338
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001339 /*
1340 /**********************************************************
1341 /* Factory methods used by factory for creating generator instances,
1342 /* overridable by sub-classes
1343 /**********************************************************
1344 */
1345
1346 /**
1347 * Overridable factory method that actually instantiates generator for
1348 * given {@link Writer} and context object.
1349 *<p>
1350 * This method is specifically designed to remain
1351 * compatible between minor versions so that sub-classes can count
1352 * on it being called as expected. That is, it is part of official
1353 * interface from sub-class perspective, although not a public
1354 * method available to users of factory implementations.
1355 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001356 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001357 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001358 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1359 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001360 if (_characterEscapes != null) {
1361 gen.setCharacterEscapes(_characterEscapes);
1362 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001363 SerializableString rootSep = _rootValueSeparator;
1364 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1365 gen.setRootValueSeparator(rootSep);
1366 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001367 return gen;
1368 }
1369
1370 /**
1371 * Overridable factory method that actually instantiates generator for
1372 * given {@link OutputStream} and context object, using UTF-8 encoding.
1373 *<p>
1374 * This method is specifically designed to remain
1375 * compatible between minor versions so that sub-classes can count
1376 * on it being called as expected. That is, it is part of official
1377 * interface from sub-class perspective, although not a public
1378 * method available to users of factory implementations.
1379 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001380 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001381 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1382 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001383 if (_characterEscapes != null) {
1384 gen.setCharacterEscapes(_characterEscapes);
1385 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001386 SerializableString rootSep = _rootValueSeparator;
1387 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1388 gen.setRootValueSeparator(rootSep);
1389 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001390 return gen;
1391 }
1392
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001393 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1394 {
1395 // note: this should not get called any more (caller checks, dispatches)
1396 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1397 return new UTF8Writer(ctxt, out);
1398 }
1399 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1400 return new OutputStreamWriter(out, enc.getJavaName());
1401 }
Tatu Saloranta896000f2014-04-19 12:53:40 -07001402
1403 /*
1404 /**********************************************************
1405 /* Internal factory methods, decorator handling
1406 /**********************************************************
1407 */
1408
1409 /**
1410 * @since 2.4
1411 */
1412 protected final InputStream _decorate(InputStream in, IOContext ctxt) throws IOException {
1413 if (_inputDecorator != null) {
1414 InputStream in2 = _inputDecorator.decorate(ctxt, in);
1415 if (in2 != null) {
1416 return in2;
1417 }
1418 }
1419 return in;
1420 }
Tatu Salorantad7cbb642016-05-13 13:23:04 -07001421
Tatu Saloranta896000f2014-04-19 12:53:40 -07001422 /**
1423 * @since 2.4
1424 */
1425 protected final Reader _decorate(Reader in, IOContext ctxt) throws IOException {
1426 if (_inputDecorator != null) {
1427 Reader in2 = _inputDecorator.decorate(ctxt, in);
1428 if (in2 != null) {
1429 return in2;
1430 }
1431 }
1432 return in;
1433 }
1434
1435 /**
Tatu Salorantad7cbb642016-05-13 13:23:04 -07001436 * @since 2.8
1437 */
1438 protected final DataInput _decorate(DataInput in, IOContext ctxt) throws IOException {
1439 if (_inputDecorator != null) {
1440 DataInput in2 = _inputDecorator.decorate(ctxt, in);
1441 if (in2 != null) {
1442 return in2;
1443 }
1444 }
1445 return in;
1446 }
1447
1448 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001449 * @since 2.4
1450 */
1451 protected final OutputStream _decorate(OutputStream out, IOContext ctxt) throws IOException {
1452 if (_outputDecorator != null) {
1453 OutputStream out2 = _outputDecorator.decorate(ctxt, out);
1454 if (out2 != null) {
1455 return out2;
1456 }
1457 }
1458 return out;
1459 }
1460
1461 /**
1462 * @since 2.4
1463 */
1464 protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException {
1465 if (_outputDecorator != null) {
1466 Writer out2 = _outputDecorator.decorate(ctxt, out);
1467 if (out2 != null) {
1468 return out2;
1469 }
1470 }
1471 return out;
1472 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001473
1474 /*
1475 /**********************************************************
1476 /* Internal factory methods, other
1477 /**********************************************************
1478 */
1479
1480 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001481 * Method used by factory to create buffer recycler instances
1482 * for parsers and generators.
1483 *<p>
1484 * Note: only public to give access for <code>ObjectMapper</code>
1485 */
1486 public BufferRecycler _getBufferRecycler()
1487 {
Tatu Saloranta8891c0c2015-04-23 22:47:05 -07001488 BufferRecycler br;
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001489
Tatu Saloranta8891c0c2015-04-23 22:47:05 -07001490 /* 23-Apr-2015, tatu: Let's allow disabling of buffer recycling
1491 * scheme, for cases where it is considered harmful (possibly
1492 * on Android, for example)
1493 */
1494 if (isEnabled(Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING)) {
1495 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1496 br = (ref == null) ? null : ref.get();
1497
1498 if (br == null) {
1499 br = new BufferRecycler();
1500 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1501 }
1502 } else {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001503 br = new BufferRecycler();
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001504 }
1505 return br;
1506 }
Tatu Salorantad302ae02015-12-27 21:53:15 -08001507
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001508 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001509 * Overridable factory method that actually instantiates desired
1510 * context object.
1511 */
1512 protected IOContext _createContext(Object srcRef, boolean resourceManaged) {
1513 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1514 }
Tatu Salorantad302ae02015-12-27 21:53:15 -08001515
Tatu Saloranta896000f2014-04-19 12:53:40 -07001516 /**
Tatu Saloranta38b3ef12016-05-03 18:08:31 -07001517 * @since 2.8
1518 */
1519 protected OutputStream _createDataOutputWrapper(DataOutput out) {
1520 return new DataOutputAsStream(out);
1521 }
1522
1523 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001524 * Helper methods used for constructing an optimal stream for
1525 * parsers to use, when input is to be read from an URL.
1526 * This helps when reading file content via URL.
1527 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001528 protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001529 if ("file".equals(url.getProtocol())) {
1530 /* Can not do this if the path refers
1531 * to a network drive on windows. This fixes the problem;
1532 * might not be needed on all platforms (NFS?), but should not
1533 * matter a lot: performance penalty of extra wrapping is more
1534 * relevant when accessing local file system.
1535 */
1536 String host = url.getHost();
1537 if (host == null || host.length() == 0) {
Tatu Saloranta38b3ef12016-05-03 18:08:31 -07001538 // [core#48]: Let's try to avoid probs with URL encoded stuff
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001539 String path = url.getPath();
1540 if (path.indexOf('%') < 0) {
1541 return new FileInputStream(url.getPath());
1542
1543 }
1544 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001545 }
1546 }
1547 return url.openStream();
1548 }
1549}