blob: c85652fd53efcc588a71b5d4414b435f1191c04a [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) {
445 String ourFormat = getFormatName();
446 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
447 }
448
449 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800450 * Method that returns short textual id identifying format
451 * this factory supports.
452 *<p>
453 * Note: sub-classes should override this method; default
454 * implementation will return null for all sub-classes
455 */
456 public String getFormatName()
457 {
458 /* Somewhat nasty check: since we can't make this abstract
459 * (due to backwards compatibility concerns), need to prevent
460 * format name "leakage"
461 */
462 if (getClass() == JsonFactory.class) {
463 return FORMAT_NAME_JSON;
464 }
465 return null;
466 }
467
Cowtowncoder2e262b92015-05-27 17:32:17 -0700468 /**
469 * Convenience method for trying to determine whether input via given accessor
470 * is of format type supported by this factory.
471 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800472 public MatchStrength hasFormat(InputAccessor acc) throws IOException
473 {
474 // since we can't keep this abstract, only implement for "vanilla" instance
475 if (getClass() == JsonFactory.class) {
476 return hasJSONFormat(acc);
477 }
478 return null;
479 }
480
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700481 /**
482 * Method that can be called to determine if a custom
483 * {@link ObjectCodec} is needed for binding data parsed
484 * using {@link JsonParser} constructed by this factory
485 * (which typically also implies the same for serialization
486 * with {@link JsonGenerator}).
487 *
488 * @return True if custom codec is needed with parsers and
489 * generators created by this factory; false if a general
490 * {@link ObjectCodec} is enough
491 *
492 * @since 2.1
493 */
494 public boolean requiresCustomCodec() {
495 return false;
496 }
497
498 /**
499 * Helper method that can be called to determine if content accessed
500 * using given accessor seems to be JSON content.
501 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800502 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
503 {
504 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700505 }
506
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800507 /*
508 /**********************************************************
509 /* Versioned
510 /**********************************************************
511 */
512
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800513 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800514 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800515 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800516 }
517
518 /*
519 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800520 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800521 /**********************************************************
522 */
523
524 /**
525 * Method for enabling or disabling specified parser feature
526 * (check {@link JsonParser.Feature} for list of features)
527 */
Tatu07351902012-01-19 13:12:13 -0800528 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
529 return state ? enable(f) : disable(f);
530 }
531
532 /**
533 * Method for enabling specified parser feature
534 * (check {@link JsonFactory.Feature} for list of features)
535 */
536 public JsonFactory enable(JsonFactory.Feature f) {
537 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800538 return this;
539 }
540
541 /**
Tatu07351902012-01-19 13:12:13 -0800542 * Method for disabling specified parser features
543 * (check {@link JsonFactory.Feature} for list of features)
544 */
545 public JsonFactory disable(JsonFactory.Feature f) {
546 _factoryFeatures &= ~f.getMask();
547 return this;
548 }
549
550 /**
551 * Checked whether specified parser feature is enabled.
552 */
553 public final boolean isEnabled(JsonFactory.Feature f) {
554 return (_factoryFeatures & f.getMask()) != 0;
555 }
556
557 /*
558 /**********************************************************
559 /* Configuration, parser configuration
560 /**********************************************************
561 */
562
563 /**
564 * Method for enabling or disabling specified parser feature
565 * (check {@link JsonParser.Feature} for list of features)
566 */
567 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
568 return state ? enable(f) : disable(f);
569 }
570
571 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800572 * Method for enabling specified parser feature
573 * (check {@link JsonParser.Feature} for list of features)
574 */
575 public JsonFactory enable(JsonParser.Feature f) {
576 _parserFeatures |= f.getMask();
577 return this;
578 }
579
580 /**
581 * Method for disabling specified parser features
582 * (check {@link JsonParser.Feature} for list of features)
583 */
584 public JsonFactory disable(JsonParser.Feature f) {
585 _parserFeatures &= ~f.getMask();
586 return this;
587 }
588
589 /**
590 * Checked whether specified parser feature is enabled.
591 */
592 public final boolean isEnabled(JsonParser.Feature f) {
593 return (_parserFeatures & f.getMask()) != 0;
594 }
595
596 /**
597 * Method for getting currently configured input decorator (if any;
598 * there is no default decorator).
599 */
600 public InputDecorator getInputDecorator() {
601 return _inputDecorator;
602 }
603
604 /**
605 * Method for overriding currently configured input decorator
606 */
607 public JsonFactory setInputDecorator(InputDecorator d) {
608 _inputDecorator = d;
609 return this;
610 }
611
612 /*
613 /**********************************************************
614 /* Configuration, generator settings
615 /**********************************************************
616 */
617
618 /**
619 * Method for enabling or disabling specified generator feature
620 * (check {@link JsonGenerator.Feature} for list of features)
621 */
622 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800623 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800624 }
625
626
627 /**
628 * Method for enabling specified generator features
629 * (check {@link JsonGenerator.Feature} for list of features)
630 */
631 public JsonFactory enable(JsonGenerator.Feature f) {
632 _generatorFeatures |= f.getMask();
633 return this;
634 }
635
636 /**
637 * Method for disabling specified generator feature
638 * (check {@link JsonGenerator.Feature} for list of features)
639 */
640 public JsonFactory disable(JsonGenerator.Feature f) {
641 _generatorFeatures &= ~f.getMask();
642 return this;
643 }
644
645 /**
646 * Check whether specified generator feature is enabled.
647 */
648 public final boolean isEnabled(JsonGenerator.Feature f) {
649 return (_generatorFeatures & f.getMask()) != 0;
650 }
651
652 /**
653 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
654 * it creates.
655 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800656 public CharacterEscapes getCharacterEscapes() { return _characterEscapes; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800657
658 /**
659 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
660 * it creates.
661 */
662 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
663 _characterEscapes = esc;
664 return this;
665 }
666
667 /**
668 * Method for getting currently configured output decorator (if any;
669 * there is no default decorator).
670 */
671 public OutputDecorator getOutputDecorator() {
672 return _outputDecorator;
673 }
674
675 /**
676 * Method for overriding currently configured output decorator
677 */
678 public JsonFactory setOutputDecorator(OutputDecorator d) {
679 _outputDecorator = d;
680 return this;
681 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700682
683 /**
684 * Method that allows overriding String used for separating root-level
685 * JSON values (default is single space character)
686 *
687 * @param sep Separator to use, if any; null means that no separator is
688 * automatically added
689 *
690 * @since 2.1
691 */
692 public JsonFactory setRootValueSeparator(String sep) {
693 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
694 return this;
695 }
696
697 /**
698 * @since 2.1
699 */
700 public String getRootValueSeparator() {
701 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
702 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800703
704 /*
705 /**********************************************************
706 /* Configuration, other
707 /**********************************************************
708 */
709
710 /**
711 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800712 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
713 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800714 * it constructs). This is needed to use data-binding methods
715 * of {@link JsonParser} and {@link JsonGenerator} instances.
716 */
717 public JsonFactory setCodec(ObjectCodec oc) {
718 _objectCodec = oc;
719 return this;
720 }
721
722 public ObjectCodec getCodec() { return _objectCodec; }
723
724 /*
725 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700726 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800727 /**********************************************************
728 */
729
730 /**
731 * Method for constructing JSON parser instance to parse
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300732 * contents of specified file.
733 *
734 *<p>
735 * Encoding is auto-detected from contents according to JSON
736 * specification recommended mechanism. Json specification
737 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
738 * so auto-detection implemented only for this charsets.
739 * For other charsets use {@link #createParser(java.io.Reader)}.
740 *
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800741 *<p>
742 * Underlying input stream (needed for reading contents)
743 * will be <b>owned</b> (and managed, i.e. closed as need be) by
744 * the parser, since caller has no access to it.
745 *
746 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700747 *
748 * @since 2.1
749 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800750 public JsonParser createParser(File f) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800751 // true, since we create InputStream from File
752 IOContext ctxt = _createContext(f, true);
753 InputStream in = new FileInputStream(f);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700754 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800755 }
756
757 /**
758 * Method for constructing JSON parser instance to parse
759 * contents of resource reference by given URL.
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300760 *
761 *<p>
762 * Encoding is auto-detected from contents according to JSON
763 * specification recommended mechanism. Json specification
764 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
765 * so auto-detection implemented only for this charsets.
766 * For other charsets use {@link #createParser(java.io.Reader)}.
767 *
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800768 *<p>
769 * Underlying input stream (needed for reading contents)
770 * will be <b>owned</b> (and managed, i.e. closed as need be) by
771 * the parser, since caller has no access to it.
772 *
773 * @param url URL pointing to resource that contains 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(URL url) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800778 // true, since we create InputStream from URL
779 IOContext ctxt = _createContext(url, true);
780 InputStream in = _optimizedStreamFromURL(url);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700781 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800782 }
783
784 /**
785 * Method for constructing JSON parser instance to parse
786 * the contents accessed via specified input stream.
787 *<p>
788 * The input stream will <b>not be owned</b> by
789 * the parser, it will still be managed (i.e. closed if
790 * end-of-stream is reacher, or parser close method called)
791 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
792 * is enabled.
793 *<p>
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300794 *
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800795 * Note: no encoding argument is taken since it can always be
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300796 * auto-detected as suggested by JSON RFC. Json specification
797 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
798 * so auto-detection implemented only for this charsets.
799 * For other charsets use {@link #createParser(java.io.Reader)}.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800800 *
801 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800802 *
803 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800804 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800805 public JsonParser createParser(InputStream in) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800806 IOContext ctxt = _createContext(in, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700807 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800808 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700809
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800810 /**
811 * Method for constructing parser for parsing
812 * the contents accessed via specified Reader.
813 <p>
814 * The read stream will <b>not be owned</b> by
815 * the parser, it will still be managed (i.e. closed if
816 * end-of-stream is reacher, or parser close method called)
817 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
818 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800819 *
820 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800821 *
822 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800823 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800824 public JsonParser createParser(Reader r) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800825 // false -> we do NOT own Reader (did not create it)
826 IOContext ctxt = _createContext(r, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700827 return _createParser(_decorate(r, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800828 }
829
830 /**
831 * Method for constructing parser for parsing
832 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800833 *
834 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800835 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800836 public JsonParser createParser(byte[] data) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800837 IOContext ctxt = _createContext(data, true);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800838 if (_inputDecorator != null) {
839 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
840 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700841 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800842 }
843 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700844 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800845 }
846
847 /**
848 * Method for constructing parser for parsing
849 * the contents of given byte array.
850 *
851 * @param data Buffer that contains data to parse
852 * @param offset Offset of the first data byte within buffer
853 * @param len Length of contents to parse within buffer
Tatu Saloranta68194922012-11-15 20:34:29 -0800854 *
855 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800856 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800857 public JsonParser createParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800858 IOContext ctxt = _createContext(data, true);
859 // [JACKSON-512]: allow wrapping with InputDecorator
860 if (_inputDecorator != null) {
861 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
862 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700863 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800864 }
865 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800866 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800867 }
868
869 /**
870 * Method for constructing parser for parsing
871 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800872 *
873 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800874 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800875 public JsonParser createParser(String content) throws IOException, JsonParseException {
Tatu64aa9d22014-04-18 14:07:38 -0700876 final int strLen = content.length();
877 // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char)
Tatu Salorantad7cbb642016-05-13 13:23:04 -0700878 if ((_inputDecorator != null) || (strLen > 0x8000) || !canUseCharArrays()) {
Tatu64aa9d22014-04-18 14:07:38 -0700879 // easier to just wrap in a Reader than extend InputDecorator; or, if content
880 // is too long for us to copy it over
881 return createParser(new StringReader(content));
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800882 }
Tatu64aa9d22014-04-18 14:07:38 -0700883 IOContext ctxt = _createContext(content, true);
Tatu Saloranta84dc1842014-04-18 21:07:57 -0700884 char[] buf = ctxt.allocTokenBuffer(strLen);
Tatu64aa9d22014-04-18 14:07:38 -0700885 content.getChars(0, strLen, buf, 0);
886 return _createParser(buf, 0, strLen, ctxt, true);
887 }
888
889 /**
890 * Method for constructing parser for parsing
891 * contents of given char array.
892 *
893 * @since 2.4
894 */
895 public JsonParser createParser(char[] content) throws IOException {
896 return createParser(content, 0, content.length);
897 }
898
899 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -0700900 * Method for constructing parser for parsing contents of given char array.
Tatu64aa9d22014-04-18 14:07:38 -0700901 *
902 * @since 2.4
903 */
904 public JsonParser createParser(char[] content, int offset, int len) throws IOException {
905 if (_inputDecorator != null) { // easier to just wrap in a Reader than extend InputDecorator
906 return createParser(new CharArrayReader(content, offset, len));
907 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700908 return _createParser(content, offset, len, _createContext(content, true),
909 // important: buffer is NOT recyclable, as it's from caller
910 false);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800911 }
912
Tatu Salorantad7cbb642016-05-13 13:23:04 -0700913 /**
914 * @since 2.8
915 */
916 public JsonParser createParser(DataInput in) throws IOException {
917 IOContext ctxt = _createContext(in, false);
918 return _createParser(_decorate(in, ctxt), ctxt);
919 }
920
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800921 /*
922 /**********************************************************
Tatu Salorantad302ae02015-12-27 21:53:15 -0800923 /* Parser factories (old ones, pre-2.2)
Tatu Saloranta68194922012-11-15 20:34:29 -0800924 /**********************************************************
925 */
926
927 /**
928 * Method for constructing JSON parser instance to parse
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300929 * contents of specified file.
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300930 *<p>
931 * Encoding is auto-detected from contents according to JSON
932 * specification recommended mechanism. Json specification
933 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
934 * so auto-detection implemented only for this charsets.
935 * For other charsets use {@link #createParser(java.io.Reader)}.
936 *
Tatu Saloranta68194922012-11-15 20:34:29 -0800937 *<p>
938 * Underlying input stream (needed for reading contents)
939 * will be <b>owned</b> (and managed, i.e. closed as need be) by
940 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800941 *
942 * @param f File that contains JSON content to parse
943 *
944 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
945 */
946 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800947 public JsonParser createJsonParser(File f) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800948 return createParser(f);
949 }
950
951 /**
952 * Method for constructing JSON parser instance to parse
953 * contents of resource reference by given URL.
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300954 *
955 *<p>
956 * Encoding is auto-detected from contents according to JSON
957 * specification recommended mechanism. Json specification
958 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
959 * so auto-detection implemented only for this charsets.
960 * For other charsets use {@link #createParser(java.io.Reader)}.
961 *
Tatu Saloranta68194922012-11-15 20:34:29 -0800962 *<p>
963 * Underlying input stream (needed for reading contents)
964 * will be <b>owned</b> (and managed, i.e. closed as need be) by
965 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800966 *
967 * @param url URL pointing to resource that contains JSON content to parse
968 *
969 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
970 */
971 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800972 public JsonParser createJsonParser(URL url) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800973 return createParser(url);
974 }
975
976 /**
977 * Method for constructing JSON parser instance to parse
978 * the contents accessed via specified input stream.
979 *<p>
980 * The input stream will <b>not be owned</b> by
981 * the parser, it will still be managed (i.e. closed if
982 * end-of-stream is reacher, or parser close method called)
983 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
984 * is enabled.
985 *<p>
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300986 *
Tatu Saloranta68194922012-11-15 20:34:29 -0800987 * Note: no encoding argument is taken since it can always be
Dmitry Spikhalskiy5590fcf2015-11-29 20:27:14 +0300988 * auto-detected as suggested by JSON RFC. Json specification
989 * supports only UTF-8, UTF-16 and UTF-32 as valid encodings,
990 * so auto-detection implemented only for this charsets.
991 * For other charsets use {@link #createParser(java.io.Reader)}.
Tatu Saloranta68194922012-11-15 20:34:29 -0800992 *
993 * @param in InputStream to use for reading JSON content to parse
994 *
995 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
996 */
997 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800998 public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800999 return createParser(in);
1000 }
1001
1002 /**
1003 * Method for constructing parser for parsing
1004 * the contents accessed via specified Reader.
1005 <p>
1006 * The read stream will <b>not be owned</b> by
1007 * the parser, it will still be managed (i.e. closed if
1008 * end-of-stream is reacher, or parser close method called)
1009 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
1010 * is enabled.
Tatu Saloranta68194922012-11-15 20:34:29 -08001011 *
1012 * @param r Reader to use for reading JSON content to parse
1013 *
1014 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
1015 */
1016 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001017 public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001018 return createParser(r);
1019 }
1020
1021 /**
Tatu Salorantaefc23672013-12-30 19:09:39 -08001022 * Method for constructing parser for parsing the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -08001023 *
1024 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
1025 */
1026 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001027 public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001028 return createParser(data);
1029 }
1030
1031 /**
1032 * Method for constructing parser for parsing
1033 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -08001034 *
1035 * @param data Buffer that contains data to parse
1036 * @param offset Offset of the first data byte within buffer
1037 * @param len Length of contents to parse within buffer
1038 *
1039 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
1040 */
1041 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001042 public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001043 return createParser(data, offset, len);
1044 }
1045
1046 /**
1047 * Method for constructing parser for parsing
1048 * contents of given String.
1049 *
1050 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
1051 */
1052 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -08001053 public JsonParser createJsonParser(String content) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001054 return createParser(content);
1055 }
1056
1057 /*
1058 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001059 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001060 /**********************************************************
1061 */
1062
1063 /**
1064 * Method for constructing JSON generator for writing JSON content
1065 * using specified output stream.
1066 * Encoding to use must be specified, and needs to be one of available
1067 * types (as per JSON specification).
1068 *<p>
1069 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1070 * so that generator will NOT close the output stream when
1071 * {@link JsonGenerator#close} is called (unless auto-closing
1072 * feature,
1073 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1074 * is enabled).
1075 * Using application needs to close it explicitly if this is the case.
1076 *<p>
1077 * Note: there are formats that use fixed encoding (like most binary data formats)
1078 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001079 *
1080 * @param out OutputStream to use for writing JSON content
1081 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001082 *
1083 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001084 */
1085 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
1086 throws IOException
1087 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001088 // false -> we won't manage the stream unless explicitly directed to
1089 IOContext ctxt = _createContext(out, false);
1090 ctxt.setEncoding(enc);
1091 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001092 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001093 }
1094 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001095 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001096 }
1097
1098 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001099 * Convenience method for constructing generator that uses default
1100 * encoding of the format (UTF-8 for JSON and most other data formats).
1101 *<p>
1102 * Note: there are formats that use fixed encoding (like most binary data formats).
1103 *
1104 * @since 2.1
1105 */
1106 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1107 return createGenerator(out, JsonEncoding.UTF8);
1108 }
1109
1110 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001111 * Method for constructing JSON generator for writing JSON content
1112 * using specified Writer.
1113 *<p>
1114 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1115 * so that generator will NOT close the Reader when
1116 * {@link JsonGenerator#close} is called (unless auto-closing
1117 * feature,
1118 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1119 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001120 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001121 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001122 *
Tatu Saloranta896000f2014-04-19 12:53:40 -07001123 * @param w Writer to use for writing JSON content
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001124 */
Tatu Saloranta896000f2014-04-19 12:53:40 -07001125 public JsonGenerator createGenerator(Writer w) throws IOException {
1126 IOContext ctxt = _createContext(w, false);
1127 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001128 }
1129
1130 /**
1131 * Method for constructing JSON generator for writing JSON content
1132 * to specified file, overwriting contents it might have (or creating
1133 * it if such file does not yet exist).
1134 * Encoding to use must be specified, and needs to be one of available
1135 * types (as per JSON specification).
1136 *<p>
1137 * Underlying stream <b>is owned</b> by the generator constructed,
1138 * i.e. generator will handle closing of file when
1139 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001140 *
1141 * @param f File to write contents to
1142 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001143 *
1144 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001145 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001146 public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001147 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001148 OutputStream out = new FileOutputStream(f);
1149 // true -> yes, we have to manage the stream since we created it
1150 IOContext ctxt = _createContext(out, true);
1151 ctxt.setEncoding(enc);
1152 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001153 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001154 }
1155 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001156 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001157 }
1158
Tatu Saloranta38b3ef12016-05-03 18:08:31 -07001159 /**
1160 * Method for constructing generator for writing content using specified
1161 * {@link DataOutput} instance.
1162 *
1163 * @since 2.8
1164 */
1165 public JsonGenerator createGenerator(DataOutput out, JsonEncoding enc) throws IOException {
1166 return createGenerator(_createDataOutputWrapper(out), enc);
1167 }
1168
1169 /**
1170 * Convenience method for constructing generator that uses default
1171 * encoding of the format (UTF-8 for JSON and most other data formats).
1172 *<p>
1173 * Note: there are formats that use fixed encoding (like most binary data formats).
1174 *
1175 * @since 2.8
1176 */
1177 public JsonGenerator createGenerator(DataOutput out) throws IOException {
1178 return createGenerator(_createDataOutputWrapper(out), JsonEncoding.UTF8);
1179 }
1180
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001181 /*
1182 /**********************************************************
Tatu Salorantad302ae02015-12-27 21:53:15 -08001183 /* Generator factories, old (pre-2.2)
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001184 /**********************************************************
1185 */
1186
1187 /**
1188 * Method for constructing JSON generator for writing JSON content
1189 * using specified output stream.
1190 * Encoding to use must be specified, and needs to be one of available
1191 * types (as per JSON specification).
1192 *<p>
1193 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1194 * so that generator will NOT close the output stream when
1195 * {@link JsonGenerator#close} is called (unless auto-closing
1196 * feature,
1197 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
1198 * is enabled).
1199 * Using application needs to close it explicitly if this is the case.
1200 *<p>
1201 * Note: there are formats that use fixed encoding (like most binary data formats)
1202 * and that ignore passed in encoding.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001203 *
1204 * @param out OutputStream to use for writing JSON content
1205 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001206 *
1207 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001208 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001209 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001210 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001211 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001212 }
1213
1214 /**
1215 * Method for constructing JSON generator for writing JSON content
1216 * using specified Writer.
1217 *<p>
1218 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1219 * so that generator will NOT close the Reader when
1220 * {@link JsonGenerator#close} is called (unless auto-closing
1221 * feature,
1222 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1223 * Using application needs to close it explicitly.
1224 *
1225 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001226 *
1227 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001228 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001229 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001230 public JsonGenerator createJsonGenerator(Writer out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001231 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001232 }
1233
1234 /**
1235 * Convenience method for constructing generator that uses default
1236 * encoding of the format (UTF-8 for JSON and most other data formats).
1237 *<p>
1238 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001239 *
1240 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001241 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001242 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001243 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001244 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001245 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001246
1247 /*
1248 /**********************************************************
1249 /* Factory methods used by factory for creating parser instances,
1250 /* overridable by sub-classes
1251 /**********************************************************
1252 */
1253
1254 /**
1255 * Overridable factory method that actually instantiates desired parser
1256 * given {@link InputStream} and context object.
1257 *<p>
1258 * This method is specifically designed to remain
1259 * compatible between minor versions so that sub-classes can count
1260 * on it being called as expected. That is, it is part of official
1261 * interface from sub-class perspective, although not a public
1262 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001263 *
1264 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001265 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001266 protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001267 // As per [JACKSON-259], may want to fully disable canonicalization:
1268 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
Tatu Salorantadccffbe2015-02-05 21:17:57 -08001269 _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001270 }
1271
1272 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001273 * Overridable factory method that actually instantiates parser
1274 * using given {@link Reader} object for reading content.
1275 *<p>
1276 * This method is specifically designed to remain
1277 * compatible between minor versions so that sub-classes can count
1278 * on it being called as expected. That is, it is part of official
1279 * interface from sub-class perspective, although not a public
1280 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001281 *
1282 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001283 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001284 protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001285 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001286 _rootCharSymbols.makeChild(_factoryFeatures));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001287 }
1288
1289 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001290 * Overridable factory method that actually instantiates parser
Tatu64aa9d22014-04-18 14:07:38 -07001291 * using given <code>char[]</code> object for accessing content.
1292 *
1293 * @since 2.4
1294 */
1295 protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
1296 boolean recyclable) throws IOException {
1297 return new ReaderBasedJsonParser(ctxt, _parserFeatures, null, _objectCodec,
Tatu Saloranta0541b3b2014-05-23 20:20:46 -07001298 _rootCharSymbols.makeChild(_factoryFeatures),
1299 data, offset, offset+len, recyclable);
Tatu64aa9d22014-04-18 14:07:38 -07001300 }
1301
1302 /**
1303 * Overridable factory method that actually instantiates parser
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001304 * using given {@link Reader} object for reading content
1305 * passed as raw byte array.
1306 *<p>
1307 * This method is specifically designed to remain
1308 * compatible between minor versions so that sub-classes can count
1309 * on it being called as expected. That is, it is part of official
1310 * interface from sub-class perspective, although not a public
1311 * method available to users of factory implementations.
1312 */
Tatu64aa9d22014-04-18 14:07:38 -07001313 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001314 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001315 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
Tatu Salorantadccffbe2015-02-05 21:17:57 -08001316 _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures);
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001317 }
1318
Tatu Salorantad7cbb642016-05-13 13:23:04 -07001319 /**
1320 * @since 2.8
1321 */
1322 protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOException
1323 {
1324 // 13-May-2016, tatu: Need to take care not to accidentally create JSON parser for
1325 // non-JSON input. So, bit unclean but...
1326 String format = getFormatName();
1327 if (format != FORMAT_NAME_JSON) {
1328 throw new UnsupportedOperationException(String.format(
1329 "InputData source not (yet?) support for this format (%s)", format));
1330 }
1331 ByteQuadsCanonicalizer can = _byteSymbolCanonicalizer.makeChild(_factoryFeatures);
1332 return new UTF8DataInputJsonParser(ctxt, _parserFeatures, input,
1333 _objectCodec, can);
1334 }
1335
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001336 /*
1337 /**********************************************************
1338 /* Factory methods used by factory for creating generator instances,
1339 /* overridable by sub-classes
1340 /**********************************************************
1341 */
1342
1343 /**
1344 * Overridable factory method that actually instantiates generator for
1345 * given {@link Writer} and context object.
1346 *<p>
1347 * This method is specifically designed to remain
1348 * compatible between minor versions so that sub-classes can count
1349 * on it being called as expected. That is, it is part of official
1350 * interface from sub-class perspective, although not a public
1351 * method available to users of factory implementations.
1352 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001353 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001354 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001355 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1356 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001357 if (_characterEscapes != null) {
1358 gen.setCharacterEscapes(_characterEscapes);
1359 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001360 SerializableString rootSep = _rootValueSeparator;
1361 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1362 gen.setRootValueSeparator(rootSep);
1363 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001364 return gen;
1365 }
1366
1367 /**
1368 * Overridable factory method that actually instantiates generator for
1369 * given {@link OutputStream} and context object, using UTF-8 encoding.
1370 *<p>
1371 * This method is specifically designed to remain
1372 * compatible between minor versions so that sub-classes can count
1373 * on it being called as expected. That is, it is part of official
1374 * interface from sub-class perspective, although not a public
1375 * method available to users of factory implementations.
1376 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001377 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001378 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1379 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001380 if (_characterEscapes != null) {
1381 gen.setCharacterEscapes(_characterEscapes);
1382 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001383 SerializableString rootSep = _rootValueSeparator;
1384 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1385 gen.setRootValueSeparator(rootSep);
1386 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001387 return gen;
1388 }
1389
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001390 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1391 {
1392 // note: this should not get called any more (caller checks, dispatches)
1393 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1394 return new UTF8Writer(ctxt, out);
1395 }
1396 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1397 return new OutputStreamWriter(out, enc.getJavaName());
1398 }
Tatu Saloranta896000f2014-04-19 12:53:40 -07001399
1400 /*
1401 /**********************************************************
1402 /* Internal factory methods, decorator handling
1403 /**********************************************************
1404 */
1405
1406 /**
1407 * @since 2.4
1408 */
1409 protected final InputStream _decorate(InputStream in, IOContext ctxt) throws IOException {
1410 if (_inputDecorator != null) {
1411 InputStream in2 = _inputDecorator.decorate(ctxt, in);
1412 if (in2 != null) {
1413 return in2;
1414 }
1415 }
1416 return in;
1417 }
Tatu Salorantad7cbb642016-05-13 13:23:04 -07001418
Tatu Saloranta896000f2014-04-19 12:53:40 -07001419 /**
1420 * @since 2.4
1421 */
1422 protected final Reader _decorate(Reader in, IOContext ctxt) throws IOException {
1423 if (_inputDecorator != null) {
1424 Reader in2 = _inputDecorator.decorate(ctxt, in);
1425 if (in2 != null) {
1426 return in2;
1427 }
1428 }
1429 return in;
1430 }
1431
1432 /**
Tatu Salorantad7cbb642016-05-13 13:23:04 -07001433 * @since 2.8
1434 */
1435 protected final DataInput _decorate(DataInput in, IOContext ctxt) throws IOException {
1436 if (_inputDecorator != null) {
1437 DataInput in2 = _inputDecorator.decorate(ctxt, in);
1438 if (in2 != null) {
1439 return in2;
1440 }
1441 }
1442 return in;
1443 }
1444
1445 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001446 * @since 2.4
1447 */
1448 protected final OutputStream _decorate(OutputStream out, IOContext ctxt) throws IOException {
1449 if (_outputDecorator != null) {
1450 OutputStream out2 = _outputDecorator.decorate(ctxt, out);
1451 if (out2 != null) {
1452 return out2;
1453 }
1454 }
1455 return out;
1456 }
1457
1458 /**
1459 * @since 2.4
1460 */
1461 protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException {
1462 if (_outputDecorator != null) {
1463 Writer out2 = _outputDecorator.decorate(ctxt, out);
1464 if (out2 != null) {
1465 return out2;
1466 }
1467 }
1468 return out;
1469 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001470
1471 /*
1472 /**********************************************************
1473 /* Internal factory methods, other
1474 /**********************************************************
1475 */
1476
1477 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001478 * Method used by factory to create buffer recycler instances
1479 * for parsers and generators.
1480 *<p>
1481 * Note: only public to give access for <code>ObjectMapper</code>
1482 */
1483 public BufferRecycler _getBufferRecycler()
1484 {
Tatu Saloranta8891c0c2015-04-23 22:47:05 -07001485 BufferRecycler br;
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001486
Tatu Saloranta8891c0c2015-04-23 22:47:05 -07001487 /* 23-Apr-2015, tatu: Let's allow disabling of buffer recycling
1488 * scheme, for cases where it is considered harmful (possibly
1489 * on Android, for example)
1490 */
1491 if (isEnabled(Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING)) {
1492 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1493 br = (ref == null) ? null : ref.get();
1494
1495 if (br == null) {
1496 br = new BufferRecycler();
1497 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1498 }
1499 } else {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001500 br = new BufferRecycler();
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001501 }
1502 return br;
1503 }
Tatu Salorantad302ae02015-12-27 21:53:15 -08001504
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001505 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001506 * Overridable factory method that actually instantiates desired
1507 * context object.
1508 */
1509 protected IOContext _createContext(Object srcRef, boolean resourceManaged) {
1510 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1511 }
Tatu Salorantad302ae02015-12-27 21:53:15 -08001512
Tatu Saloranta896000f2014-04-19 12:53:40 -07001513 /**
Tatu Saloranta38b3ef12016-05-03 18:08:31 -07001514 * @since 2.8
1515 */
1516 protected OutputStream _createDataOutputWrapper(DataOutput out) {
1517 return new DataOutputAsStream(out);
1518 }
1519
1520 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001521 * Helper methods used for constructing an optimal stream for
1522 * parsers to use, when input is to be read from an URL.
1523 * This helps when reading file content via URL.
1524 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001525 protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001526 if ("file".equals(url.getProtocol())) {
1527 /* Can not do this if the path refers
1528 * to a network drive on windows. This fixes the problem;
1529 * might not be needed on all platforms (NFS?), but should not
1530 * matter a lot: performance penalty of extra wrapping is more
1531 * relevant when accessing local file system.
1532 */
1533 String host = url.getHost();
1534 if (host == null || host.length() == 0) {
Tatu Saloranta38b3ef12016-05-03 18:08:31 -07001535 // [core#48]: Let's try to avoid probs with URL encoded stuff
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001536 String path = url.getPath();
1537 if (path.indexOf('%') < 0) {
1538 return new FileInputStream(url.getPath());
1539
1540 }
1541 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001542 }
1543 }
1544 return url.openStream();
1545 }
1546}