blob: 57c5f2f15966265c56679e3022ba4992b2cf7b17 [file] [log] [blame]
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001/* Jackson JSON-processor.
2 *
3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
Tatu Salorantaf15531c2011-12-22 23:00:40 -08004 */
5package com.fasterxml.jackson.core;
6
7import java.io.*;
8import java.lang.ref.SoftReference;
9import java.net.URL;
10
11import com.fasterxml.jackson.core.format.InputAccessor;
12import com.fasterxml.jackson.core.format.MatchStrength;
13import com.fasterxml.jackson.core.io.*;
Tatu07351902012-01-19 13:12:13 -080014import com.fasterxml.jackson.core.json.*;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080015import com.fasterxml.jackson.core.sym.BytesToNameCanonicalizer;
16import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
17import com.fasterxml.jackson.core.util.BufferRecycler;
Tatu Salorantae6dfc692012-09-28 15:34:05 -070018import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080019
20/**
21 * The main factory class of Jackson package, used to configure and
22 * construct reader (aka parser, {@link JsonParser})
23 * and writer (aka generator, {@link JsonGenerator})
24 * instances.
25 *<p>
26 * Factory instances are thread-safe and reusable after configuration
27 * (if any). Typically applications and services use only a single
28 * globally shared factory instance, unless they need differently
29 * configured factories. Factory reuse is important if efficiency matters;
30 * most recycling of expensive construct is done on per-factory basis.
31 *<p>
32 * Creation of a factory instance is a light-weight operation,
33 * and since there is no need for pluggable alternative implementations
34 * (as there is no "standard" JSON processor API to implement),
35 * the default constructor is used for constructing factory
36 * instances.
37 *
38 * @author Tatu Saloranta
39 */
Tatu Salorantae92d04f2013-07-24 22:58:08 -070040@SuppressWarnings("resource")
Tatu Saloranta95f76a42012-10-05 13:04:23 -070041public class JsonFactory
42 implements Versioned,
43 java.io.Serializable // since 2.1 (for Android, mostly)
Tatu Salorantaf15531c2011-12-22 23:00:40 -080044{
45 /**
Tatu64aa9d22014-04-18 14:07:38 -070046 * Computed for Jackson 2.4.0 release
Tatu Saloranta95f76a42012-10-05 13:04:23 -070047 */
Tatu64aa9d22014-04-18 14:07:38 -070048 private static final long serialVersionUID = 3306684576057132431L;
Tatu Saloranta95f76a42012-10-05 13:04:23 -070049
Tatu Saloranta7b796a82013-04-27 10:18:30 -070050 /*
51 /**********************************************************
52 /* Helper types
53 /**********************************************************
Tatu Salorantaf15531c2011-12-22 23:00:40 -080054 */
Tatu Salorantae92d04f2013-07-24 22:58:08 -070055
Tatu07351902012-01-19 13:12:13 -080056 /**
57 * Enumeration that defines all on/off features that can only be
58 * changed for {@link JsonFactory}.
59 */
60 public enum Feature {
61
62 // // // Symbol handling (interning etc)
63
64 /**
65 * Feature that determines whether JSON object field names are
66 * to be canonicalized using {@link String#intern} or not:
67 * if enabled, all field names will be intern()ed (and caller
68 * can count on this being true for all such names); if disabled,
69 * no intern()ing is done. There may still be basic
70 * canonicalization (that is, same String will be used to represent
71 * all identical object property names for a single document).
72 *<p>
73 * Note: this setting only has effect if
74 * {@link #CANONICALIZE_FIELD_NAMES} is true -- otherwise no
75 * canonicalization of any sort is done.
76 *<p>
77 * This setting is enabled by default.
78 */
79 INTERN_FIELD_NAMES(true),
80
81 /**
82 * Feature that determines whether JSON object field names are
83 * to be canonicalized (details of how canonicalization is done
84 * then further specified by
85 * {@link #INTERN_FIELD_NAMES}).
86 *<p>
87 * This setting is enabled by default.
88 */
89 CANONICALIZE_FIELD_NAMES(true)
90
91 ;
92
93 /**
94 * Whether feature is enabled or disabled by default.
95 */
96 private final boolean _defaultState;
97
98 /**
99 * Method that calculates bit set (flags) of all features that
100 * are enabled by default.
101 */
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800102 public static int collectDefaults() {
Tatu07351902012-01-19 13:12:13 -0800103 int flags = 0;
104 for (Feature f : values()) {
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800105 if (f.enabledByDefault()) { flags |= f.getMask(); }
Tatu07351902012-01-19 13:12:13 -0800106 }
107 return flags;
108 }
109
Tatu Salorantae15b9a82014-01-24 21:01:04 -0800110 private Feature(boolean defaultState) { _defaultState = defaultState; }
Tatu07351902012-01-19 13:12:13 -0800111
112 public boolean enabledByDefault() { return _defaultState; }
Tatu07351902012-01-19 13:12:13 -0800113 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
Tatu07351902012-01-19 13:12:13 -0800114 public int getMask() { return (1 << ordinal()); }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700115 }
116
117 /*
118 /**********************************************************
119 /* Constants
120 /**********************************************************
121 */
122
123 /**
124 * Name used to identify JSON format
125 * (and returned by {@link #getFormatName()}
126 */
127 public final static String FORMAT_NAME_JSON = "JSON";
128
129 /**
130 * Bitfield (set of flags) of all factory features that are enabled by default.
131 */
132 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
133
134 /**
135 * Bitfield (set of flags) of all parser features that are enabled
136 * by default.
137 */
138 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
139
140 /**
141 * Bitfield (set of flags) of all generator features that are enabled
142 * by default.
143 */
144 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
145
146 private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
147
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800148 /*
149 /**********************************************************
150 /* Buffer, symbol table management
151 /**********************************************************
152 */
153
154 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700155 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference}
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800156 * to a {@link BufferRecycler} used to provide a low-cost
157 * buffer recycling between reader and writer instances.
158 */
159 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
160 = new ThreadLocal<SoftReference<BufferRecycler>>();
161
162 /**
163 * Each factory comes equipped with a shared root symbol table.
164 * It should not be linked back to the original blueprint, to
165 * avoid contents from leaking between factories.
166 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700167 protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800168
169 /**
170 * Alternative to the basic symbol table, some stream-based
171 * parsers use different name canonicalization method.
172 *<p>
173 * TODO: should clean up this; looks messy having 2 alternatives
174 * with not very clear differences.
175 */
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700176 protected final transient BytesToNameCanonicalizer _rootByteSymbols = BytesToNameCanonicalizer.createRoot();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800177
178 /*
179 /**********************************************************
180 /* Configuration
181 /**********************************************************
182 */
183
184 /**
185 * Object that implements conversion functionality between
186 * Java objects and JSON content. For base JsonFactory implementation
187 * usually not set by default, but can be explicitly set.
188 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
189 * usually provide an implementation.
190 */
191 protected ObjectCodec _objectCodec;
192
193 /**
Tatu07351902012-01-19 13:12:13 -0800194 * Currently enabled factory features.
195 */
196 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
197
198 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800199 * Currently enabled parser features.
200 */
201 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
202
203 /**
204 * Currently enabled generator features.
205 */
206 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
207
208 /**
209 * Definition of custom character escapes to use for generators created
210 * by this factory, if any. If null, standard data format specific
211 * escapes are used.
212 */
213 protected CharacterEscapes _characterEscapes;
214
215 /**
216 * Optional helper object that may decorate input sources, to do
217 * additional processing on input during parsing.
218 */
219 protected InputDecorator _inputDecorator;
220
221 /**
222 * Optional helper object that may decorate output object, to do
223 * additional processing on output during content generation.
224 */
225 protected OutputDecorator _outputDecorator;
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700226
227 /**
228 * Separator used between root-level values, if any; null indicates
229 * "do not add separator".
230 * Default separator is a single space character.
231 *
232 * @since 2.1
233 */
234 protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800235
236 /*
237 /**********************************************************
238 /* Construction
239 /**********************************************************
240 */
241
242 /**
243 * Default constructor used to create factory instances.
244 * Creation of a factory instance is a light-weight operation,
245 * but it is still a good idea to reuse limited number of
246 * factory instances (and quite often just a single instance):
247 * factories are used as context for storing some reused
248 * processing objects (such as symbol tables parsers use)
249 * and this reuse only works within context of a single
250 * factory instance.
251 */
Gonçalo Silva2bba5dd2014-01-25 17:06:21 +0000252 public JsonFactory() { this(null); }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800253
254 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
255
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700256 /**
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700257 * Constructor used when copy()ing a factory instance.
258 *
259 * @since 2.2.1
260 */
261 protected JsonFactory(JsonFactory src, ObjectCodec codec)
262 {
263 _objectCodec = null;
264 _factoryFeatures = src._factoryFeatures;
265 _parserFeatures = src._parserFeatures;
266 _generatorFeatures = src._generatorFeatures;
267 _characterEscapes = src._characterEscapes;
268 _inputDecorator = src._inputDecorator;
269 _outputDecorator = src._outputDecorator;
270 _rootValueSeparator = src._rootValueSeparator;
271
272 /* 27-Apr-2013, tatu: How about symbol table; should we try to
273 * reuse shared symbol tables? Could be more efficient that way;
274 * although can slightly add to concurrency overhead.
275 */
276 }
277
278 /**
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700279 * Method for constructing a new {@link JsonFactory} that has
280 * the same settings as this instance, but is otherwise
281 * independent (i.e. nothing is actually shared, symbol tables
282 * are separate).
283 * Note that {@link ObjectCodec} reference is not copied but is
284 * set to null; caller typically needs to set it after calling
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700285 * this method. Reason for this is that the codec is used for
286 * callbacks, and assumption is that there is strict 1-to-1
287 * mapping between codec, factory. Caller has to, then, explicitly
288 * set codec after making the copy.
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700289 *
290 * @since 2.1
291 */
292 public JsonFactory copy()
293 {
294 _checkInvalidCopy(JsonFactory.class);
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700295 // as per above, do clear ObjectCodec
296 return new JsonFactory(this, null);
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700297 }
Tatu Saloranta7b796a82013-04-27 10:18:30 -0700298
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700299 /**
300 * @since 2.1
301 * @param exp
302 */
303 protected void _checkInvalidCopy(Class<?> exp)
304 {
305 if (getClass() != exp) {
306 throw new IllegalStateException("Failed copy(): "+getClass().getName()
307 +" (version: "+version()+") does not override copy(); it has to");
308 }
309 }
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700310
311 /*
312 /**********************************************************
313 /* Serializable overrides
314 /**********************************************************
315 */
316
317 /**
318 * Method that we need to override to actually make restoration go
319 * through constructors etc.
320 * Also: must be overridden by sub-classes as well.
321 */
322 protected Object readResolve() {
Tatu Salorantaf7741c52013-04-27 11:06:30 -0700323 return new JsonFactory(this, _objectCodec);
Tatu Saloranta95f76a42012-10-05 13:04:23 -0700324 }
Tatu Salorantaa9e5c9f2013-08-28 19:39:59 -0700325
326 /*
327 /**********************************************************
328 /* Capability introspection
329 /**********************************************************
330 */
331
332 /**
333 * Introspection method that higher-level functionality may call
334 * to see whether underlying data format requires a stable ordering
335 * of object properties or not.
336 * This is usually used for determining
337 * whether to force a stable ordering (like alphabetic ordering by name)
338 * if no ordering if explicitly specified.
339 *<p>
340 * Default implementation returns <code>false</code> as JSON does NOT
341 * require stable ordering. Formats that require ordering include positional
342 * textual formats like <code>CSV</code>, and schema-based binary formats
343 * like <code>Avro</code>.
344 *
345 * @since 2.3
346 */
347 public boolean requiresPropertyOrdering() {
348 return false;
349 }
Tatu Salorantab8835442013-09-14 12:12:57 -0700350
351 /**
352 * Introspection method that higher-level functionality may call
353 * to see whether underlying data format can read and write binary
354 * data natively; that is, embeded it as-is without using encodings
355 * such as Base64.
356 *<p>
357 * Default implementation returns <code>false</code> as JSON does not
358 * support native access: all binary content must use Base64 encoding.
359 * Most binary formats (like Smile and Avro) support native binary content.
360 *
361 * @since 2.3
362 */
Tatu Saloranta67a3b192013-09-14 20:34:04 -0700363 public boolean canHandleBinaryNatively() {
Tatu Salorantab8835442013-09-14 12:12:57 -0700364 return false;
365 }
366
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800367 /*
368 /**********************************************************
369 /* Format detection functionality (since 1.8)
370 /**********************************************************
371 */
372
373 /**
Tatu Saloranta1bdf0262012-08-24 14:43:17 -0700374 * Method that can be used to quickly check whether given schema
375 * is something that parsers and/or generators constructed by this
376 * factory could use. Note that this means possible use, at the level
377 * of data format (i.e. schema is for same data format as parsers and
378 * generators this factory constructs); individual schema instances
379 * may have further usage restrictions.
380 *
381 * @since 2.1
382 */
383 public boolean canUseSchema(FormatSchema schema) {
384 String ourFormat = getFormatName();
385 return (ourFormat != null) && ourFormat.equals(schema.getSchemaType());
386 }
387
388 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800389 * Method that returns short textual id identifying format
390 * this factory supports.
391 *<p>
392 * Note: sub-classes should override this method; default
393 * implementation will return null for all sub-classes
394 */
395 public String getFormatName()
396 {
397 /* Somewhat nasty check: since we can't make this abstract
398 * (due to backwards compatibility concerns), need to prevent
399 * format name "leakage"
400 */
401 if (getClass() == JsonFactory.class) {
402 return FORMAT_NAME_JSON;
403 }
404 return null;
405 }
406
407 public MatchStrength hasFormat(InputAccessor acc) throws IOException
408 {
409 // since we can't keep this abstract, only implement for "vanilla" instance
410 if (getClass() == JsonFactory.class) {
411 return hasJSONFormat(acc);
412 }
413 return null;
414 }
415
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700416 /**
417 * Method that can be called to determine if a custom
418 * {@link ObjectCodec} is needed for binding data parsed
419 * using {@link JsonParser} constructed by this factory
420 * (which typically also implies the same for serialization
421 * with {@link JsonGenerator}).
422 *
423 * @return True if custom codec is needed with parsers and
424 * generators created by this factory; false if a general
425 * {@link ObjectCodec} is enough
426 *
427 * @since 2.1
428 */
429 public boolean requiresCustomCodec() {
430 return false;
431 }
432
433 /**
434 * Helper method that can be called to determine if content accessed
435 * using given accessor seems to be JSON content.
436 */
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800437 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
438 {
439 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
Tatu Salorantaad2df5f2012-08-22 20:55:49 -0700440 }
441
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800442 /*
443 /**********************************************************
444 /* Versioned
445 /**********************************************************
446 */
447
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800448 @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800449 public Version version() {
Tatu Saloranta68d79dd2013-01-10 21:13:46 -0800450 return PackageVersion.VERSION;
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800451 }
452
453 /*
454 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800455 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800456 /**********************************************************
457 */
458
459 /**
460 * Method for enabling or disabling specified parser feature
461 * (check {@link JsonParser.Feature} for list of features)
462 */
Tatu07351902012-01-19 13:12:13 -0800463 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
464 return state ? enable(f) : disable(f);
465 }
466
467 /**
468 * Method for enabling specified parser feature
469 * (check {@link JsonFactory.Feature} for list of features)
470 */
471 public JsonFactory enable(JsonFactory.Feature f) {
472 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800473 return this;
474 }
475
476 /**
Tatu07351902012-01-19 13:12:13 -0800477 * Method for disabling specified parser features
478 * (check {@link JsonFactory.Feature} for list of features)
479 */
480 public JsonFactory disable(JsonFactory.Feature f) {
481 _factoryFeatures &= ~f.getMask();
482 return this;
483 }
484
485 /**
486 * Checked whether specified parser feature is enabled.
487 */
488 public final boolean isEnabled(JsonFactory.Feature f) {
489 return (_factoryFeatures & f.getMask()) != 0;
490 }
491
492 /*
493 /**********************************************************
494 /* Configuration, parser configuration
495 /**********************************************************
496 */
497
498 /**
499 * Method for enabling or disabling specified parser feature
500 * (check {@link JsonParser.Feature} for list of features)
501 */
502 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
503 return state ? enable(f) : disable(f);
504 }
505
506 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800507 * Method for enabling specified parser feature
508 * (check {@link JsonParser.Feature} for list of features)
509 */
510 public JsonFactory enable(JsonParser.Feature f) {
511 _parserFeatures |= f.getMask();
512 return this;
513 }
514
515 /**
516 * Method for disabling specified parser features
517 * (check {@link JsonParser.Feature} for list of features)
518 */
519 public JsonFactory disable(JsonParser.Feature f) {
520 _parserFeatures &= ~f.getMask();
521 return this;
522 }
523
524 /**
525 * Checked whether specified parser feature is enabled.
526 */
527 public final boolean isEnabled(JsonParser.Feature f) {
528 return (_parserFeatures & f.getMask()) != 0;
529 }
530
531 /**
532 * Method for getting currently configured input decorator (if any;
533 * there is no default decorator).
534 */
535 public InputDecorator getInputDecorator() {
536 return _inputDecorator;
537 }
538
539 /**
540 * Method for overriding currently configured input decorator
541 */
542 public JsonFactory setInputDecorator(InputDecorator d) {
543 _inputDecorator = d;
544 return this;
545 }
546
547 /*
548 /**********************************************************
549 /* Configuration, generator settings
550 /**********************************************************
551 */
552
553 /**
554 * Method for enabling or disabling specified generator feature
555 * (check {@link JsonGenerator.Feature} for list of features)
556 */
557 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800558 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800559 }
560
561
562 /**
563 * Method for enabling specified generator features
564 * (check {@link JsonGenerator.Feature} for list of features)
565 */
566 public JsonFactory enable(JsonGenerator.Feature f) {
567 _generatorFeatures |= f.getMask();
568 return this;
569 }
570
571 /**
572 * Method for disabling specified generator feature
573 * (check {@link JsonGenerator.Feature} for list of features)
574 */
575 public JsonFactory disable(JsonGenerator.Feature f) {
576 _generatorFeatures &= ~f.getMask();
577 return this;
578 }
579
580 /**
581 * Check whether specified generator feature is enabled.
582 */
583 public final boolean isEnabled(JsonGenerator.Feature f) {
584 return (_generatorFeatures & f.getMask()) != 0;
585 }
586
587 /**
588 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
589 * it creates.
590 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800591 public CharacterEscapes getCharacterEscapes() { return _characterEscapes; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800592
593 /**
594 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
595 * it creates.
596 */
597 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
598 _characterEscapes = esc;
599 return this;
600 }
601
602 /**
603 * Method for getting currently configured output decorator (if any;
604 * there is no default decorator).
605 */
606 public OutputDecorator getOutputDecorator() {
607 return _outputDecorator;
608 }
609
610 /**
611 * Method for overriding currently configured output decorator
612 */
613 public JsonFactory setOutputDecorator(OutputDecorator d) {
614 _outputDecorator = d;
615 return this;
616 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -0700617
618 /**
619 * Method that allows overriding String used for separating root-level
620 * JSON values (default is single space character)
621 *
622 * @param sep Separator to use, if any; null means that no separator is
623 * automatically added
624 *
625 * @since 2.1
626 */
627 public JsonFactory setRootValueSeparator(String sep) {
628 _rootValueSeparator = (sep == null) ? null : new SerializedString(sep);
629 return this;
630 }
631
632 /**
633 * @since 2.1
634 */
635 public String getRootValueSeparator() {
636 return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue();
637 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800638
639 /*
640 /**********************************************************
641 /* Configuration, other
642 /**********************************************************
643 */
644
645 /**
646 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800647 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
648 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800649 * it constructs). This is needed to use data-binding methods
650 * of {@link JsonParser} and {@link JsonGenerator} instances.
651 */
652 public JsonFactory setCodec(ObjectCodec oc) {
653 _objectCodec = oc;
654 return this;
655 }
656
657 public ObjectCodec getCodec() { return _objectCodec; }
658
659 /*
660 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700661 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800662 /**********************************************************
663 */
664
665 /**
666 * Method for constructing JSON parser instance to parse
667 * contents of specified file. Encoding is auto-detected
668 * from contents according to JSON specification recommended
669 * mechanism.
670 *<p>
671 * Underlying input stream (needed for reading contents)
672 * will be <b>owned</b> (and managed, i.e. closed as need be) by
673 * the parser, since caller has no access to it.
674 *
675 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700676 *
677 * @since 2.1
678 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800679 public JsonParser createParser(File f) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800680 // true, since we create InputStream from File
681 IOContext ctxt = _createContext(f, true);
682 InputStream in = new FileInputStream(f);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700683 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800684 }
685
686 /**
687 * Method for constructing JSON parser instance to parse
688 * contents of resource reference by given URL.
689 * Encoding is auto-detected
690 * from contents according to JSON specification recommended
691 * mechanism.
692 *<p>
693 * Underlying input stream (needed for reading contents)
694 * will be <b>owned</b> (and managed, i.e. closed as need be) by
695 * the parser, since caller has no access to it.
696 *
697 * @param url URL pointing to resource that contains JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800698 *
699 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800700 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800701 public JsonParser createParser(URL url) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800702 // true, since we create InputStream from URL
703 IOContext ctxt = _createContext(url, true);
704 InputStream in = _optimizedStreamFromURL(url);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700705 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800706 }
707
708 /**
709 * Method for constructing JSON parser instance to parse
710 * the contents accessed via specified input stream.
711 *<p>
712 * The input stream will <b>not be owned</b> by
713 * the parser, it will still be managed (i.e. closed if
714 * end-of-stream is reacher, or parser close method called)
715 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
716 * is enabled.
717 *<p>
718 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700719 * auto-detected as suggested by JSON RFC.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800720 *
721 * @param in InputStream to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800722 *
723 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800724 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800725 public JsonParser createParser(InputStream in) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800726 IOContext ctxt = _createContext(in, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700727 return _createParser(_decorate(in, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800728 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700729
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800730 /**
731 * Method for constructing parser for parsing
732 * the contents accessed via specified Reader.
733 <p>
734 * The read stream will <b>not be owned</b> by
735 * the parser, it will still be managed (i.e. closed if
736 * end-of-stream is reacher, or parser close method called)
737 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
738 * is enabled.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800739 *
740 * @param r Reader to use for reading JSON content to parse
Tatu Saloranta68194922012-11-15 20:34:29 -0800741 *
742 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800743 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800744 public JsonParser createParser(Reader r) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800745 // false -> we do NOT own Reader (did not create it)
746 IOContext ctxt = _createContext(r, false);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700747 return _createParser(_decorate(r, ctxt), ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800748 }
749
750 /**
751 * Method for constructing parser for parsing
752 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800753 *
754 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800755 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800756 public JsonParser createParser(byte[] data) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800757 IOContext ctxt = _createContext(data, true);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800758 if (_inputDecorator != null) {
759 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
760 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700761 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800762 }
763 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700764 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800765 }
766
767 /**
768 * Method for constructing parser for parsing
769 * the contents of given byte array.
770 *
771 * @param data Buffer that contains data to parse
772 * @param offset Offset of the first data byte within buffer
773 * @param len Length of contents to parse within buffer
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(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800778 IOContext ctxt = _createContext(data, true);
779 // [JACKSON-512]: allow wrapping with InputDecorator
780 if (_inputDecorator != null) {
781 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
782 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700783 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800784 }
785 }
Tatu Saloranta68194922012-11-15 20:34:29 -0800786 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800787 }
788
789 /**
790 * Method for constructing parser for parsing
791 * contents of given String.
Tatu Saloranta68194922012-11-15 20:34:29 -0800792 *
793 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800794 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800795 public JsonParser createParser(String content) throws IOException, JsonParseException {
Tatu64aa9d22014-04-18 14:07:38 -0700796 final int strLen = content.length();
797 // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char)
798 if (_inputDecorator != null || strLen > 0x8000) {
799 // easier to just wrap in a Reader than extend InputDecorator; or, if content
800 // is too long for us to copy it over
801 return createParser(new StringReader(content));
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800802 }
Tatu64aa9d22014-04-18 14:07:38 -0700803 IOContext ctxt = _createContext(content, true);
Tatu Saloranta84dc1842014-04-18 21:07:57 -0700804 char[] buf = ctxt.allocTokenBuffer(strLen);
Tatu64aa9d22014-04-18 14:07:38 -0700805 content.getChars(0, strLen, buf, 0);
806 return _createParser(buf, 0, strLen, ctxt, true);
807 }
808
809 /**
810 * Method for constructing parser for parsing
811 * contents of given char array.
812 *
813 * @since 2.4
814 */
815 public JsonParser createParser(char[] content) throws IOException {
816 return createParser(content, 0, content.length);
817 }
818
819 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -0700820 * Method for constructing parser for parsing contents of given char array.
Tatu64aa9d22014-04-18 14:07:38 -0700821 *
822 * @since 2.4
823 */
824 public JsonParser createParser(char[] content, int offset, int len) throws IOException {
825 if (_inputDecorator != null) { // easier to just wrap in a Reader than extend InputDecorator
826 return createParser(new CharArrayReader(content, offset, len));
827 }
Tatu Saloranta896000f2014-04-19 12:53:40 -0700828 return _createParser(content, offset, len, _createContext(content, true),
829 // important: buffer is NOT recyclable, as it's from caller
830 false);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800831 }
832
833 /*
834 /**********************************************************
Tatu Saloranta68194922012-11-15 20:34:29 -0800835 /* Parser factories (old ones, as per [Issue-25])
836 /**********************************************************
837 */
838
839 /**
840 * Method for constructing JSON parser instance to parse
841 * contents of specified file. Encoding is auto-detected
842 * from contents according to JSON specification recommended
843 * mechanism.
844 *<p>
845 * Underlying input stream (needed for reading contents)
846 * will be <b>owned</b> (and managed, i.e. closed as need be) by
847 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800848 *
849 * @param f File that contains JSON content to parse
850 *
851 * @deprecated Since 2.2, use {@link #createParser(File)} instead.
852 */
853 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800854 public JsonParser createJsonParser(File f) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800855 return createParser(f);
856 }
857
858 /**
859 * Method for constructing JSON parser instance to parse
860 * contents of resource reference by given URL.
861 * Encoding is auto-detected
862 * from contents according to JSON specification recommended
863 * mechanism.
864 *<p>
865 * Underlying input stream (needed for reading contents)
866 * will be <b>owned</b> (and managed, i.e. closed as need be) by
867 * the parser, since caller has no access to it.
Tatu Saloranta68194922012-11-15 20:34:29 -0800868 *
869 * @param url URL pointing to resource that contains JSON content to parse
870 *
871 * @deprecated Since 2.2, use {@link #createParser(URL)} instead.
872 */
873 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800874 public JsonParser createJsonParser(URL url) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800875 return createParser(url);
876 }
877
878 /**
879 * Method for constructing JSON parser instance to parse
880 * the contents accessed via specified input stream.
881 *<p>
882 * The input stream will <b>not be owned</b> by
883 * the parser, it will still be managed (i.e. closed if
884 * end-of-stream is reacher, or parser close method called)
885 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
886 * is enabled.
887 *<p>
888 * Note: no encoding argument is taken since it can always be
889 * auto-detected as suggested by JSON RFC.
Tatu Saloranta68194922012-11-15 20:34:29 -0800890 *
891 * @param in InputStream to use for reading JSON content to parse
892 *
893 * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead.
894 */
895 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800896 public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800897 return createParser(in);
898 }
899
900 /**
901 * Method for constructing parser for parsing
902 * the contents accessed via specified Reader.
903 <p>
904 * The read stream will <b>not be owned</b> by
905 * the parser, it will still be managed (i.e. closed if
906 * end-of-stream is reacher, or parser close method called)
907 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
908 * is enabled.
Tatu Saloranta68194922012-11-15 20:34:29 -0800909 *
910 * @param r Reader to use for reading JSON content to parse
911 *
912 * @deprecated Since 2.2, use {@link #createParser(Reader)} instead.
913 */
914 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800915 public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800916 return createParser(r);
917 }
918
919 /**
Tatu Salorantaefc23672013-12-30 19:09:39 -0800920 * Method for constructing parser for parsing the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800921 *
922 * @deprecated Since 2.2, use {@link #createParser(byte[])} instead.
923 */
924 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800925 public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800926 return createParser(data);
927 }
928
929 /**
930 * Method for constructing parser for parsing
931 * the contents of given byte array.
Tatu Saloranta68194922012-11-15 20:34:29 -0800932 *
933 * @param data Buffer that contains data to parse
934 * @param offset Offset of the first data byte within buffer
935 * @param len Length of contents to parse within buffer
936 *
937 * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead.
938 */
939 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800940 public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800941 return createParser(data, offset, len);
942 }
943
944 /**
945 * Method for constructing parser for parsing
946 * contents of given String.
947 *
948 * @deprecated Since 2.2, use {@link #createParser(String)} instead.
949 */
950 @Deprecated
Tatu Salorantaefc23672013-12-30 19:09:39 -0800951 public JsonParser createJsonParser(String content) throws IOException, JsonParseException {
Tatu Saloranta68194922012-11-15 20:34:29 -0800952 return createParser(content);
953 }
954
955 /*
956 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700957 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800958 /**********************************************************
959 */
960
961 /**
962 * Method for constructing JSON generator for writing JSON content
963 * using specified output stream.
964 * Encoding to use must be specified, and needs to be one of available
965 * types (as per JSON specification).
966 *<p>
967 * Underlying stream <b>is NOT owned</b> by the generator constructed,
968 * so that generator will NOT close the output stream when
969 * {@link JsonGenerator#close} is called (unless auto-closing
970 * feature,
971 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
972 * is enabled).
973 * Using application needs to close it explicitly if this is the case.
974 *<p>
975 * Note: there are formats that use fixed encoding (like most binary data formats)
976 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700977 *
978 * @param out OutputStream to use for writing JSON content
979 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800980 *
981 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700982 */
983 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
984 throws IOException
985 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800986 // false -> we won't manage the stream unless explicitly directed to
987 IOContext ctxt = _createContext(out, false);
988 ctxt.setEncoding(enc);
989 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -0700990 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800991 }
992 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -0700993 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700994 }
995
996 /**
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -0800997 * Convenience method for constructing generator that uses default
998 * encoding of the format (UTF-8 for JSON and most other data formats).
999 *<p>
1000 * Note: there are formats that use fixed encoding (like most binary data formats).
1001 *
1002 * @since 2.1
1003 */
1004 public JsonGenerator createGenerator(OutputStream out) throws IOException {
1005 return createGenerator(out, JsonEncoding.UTF8);
1006 }
1007
1008 /**
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001009 * Method for constructing JSON generator for writing JSON content
1010 * using specified Writer.
1011 *<p>
1012 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1013 * so that generator will NOT close the Reader when
1014 * {@link JsonGenerator#close} is called (unless auto-closing
1015 * feature,
1016 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1017 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001018 *
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001019 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001020 *
Tatu Saloranta896000f2014-04-19 12:53:40 -07001021 * @param w Writer to use for writing JSON content
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001022 */
Tatu Saloranta896000f2014-04-19 12:53:40 -07001023 public JsonGenerator createGenerator(Writer w) throws IOException {
1024 IOContext ctxt = _createContext(w, false);
1025 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001026 }
1027
1028 /**
1029 * Method for constructing JSON generator for writing JSON content
1030 * to specified file, overwriting contents it might have (or creating
1031 * it if such file does not yet exist).
1032 * Encoding to use must be specified, and needs to be one of available
1033 * types (as per JSON specification).
1034 *<p>
1035 * Underlying stream <b>is owned</b> by the generator constructed,
1036 * i.e. generator will handle closing of file when
1037 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001038 *
1039 * @param f File to write contents to
1040 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001041 *
1042 * @since 2.1
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001043 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001044 public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001045 {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001046 OutputStream out = new FileOutputStream(f);
1047 // true -> yes, we have to manage the stream since we created it
1048 IOContext ctxt = _createContext(out, true);
1049 ctxt.setEncoding(enc);
1050 if (enc == JsonEncoding.UTF8) {
Tatu Saloranta896000f2014-04-19 12:53:40 -07001051 return _createUTF8Generator(_decorate(out, ctxt), ctxt);
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001052 }
1053 Writer w = _createWriter(out, enc, ctxt);
Tatu Saloranta896000f2014-04-19 12:53:40 -07001054 return _createGenerator(_decorate(w, ctxt), ctxt);
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001055 }
1056
1057 /*
1058 /**********************************************************
1059 /* Generator factories, old (as per [Issue-25]
1060 /**********************************************************
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 Salorantaf15531c2011-12-22 23:00:40 -08001079 *
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 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001084 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001085 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001086 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001087 return createGenerator(out, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001088 }
1089
1090 /**
1091 * Method for constructing JSON generator for writing JSON content
1092 * using specified Writer.
1093 *<p>
1094 * Underlying stream <b>is NOT owned</b> by the generator constructed,
1095 * so that generator will NOT close the Reader when
1096 * {@link JsonGenerator#close} is called (unless auto-closing
1097 * feature,
1098 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
1099 * Using application needs to close it explicitly.
1100 *
1101 * @param out Writer to use for writing JSON content
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001102 *
1103 * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001104 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001105 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001106 public JsonGenerator createJsonGenerator(Writer out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001107 return createGenerator(out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001108 }
1109
1110 /**
1111 * Convenience method for constructing generator that uses default
1112 * encoding of the format (UTF-8 for JSON and most other data formats).
1113 *<p>
1114 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001115 *
1116 * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001117 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001118 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001119 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001120 return createGenerator(out, JsonEncoding.UTF8);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001121 }
1122
1123 /**
1124 * Method for constructing JSON generator for writing JSON content
1125 * to specified file, overwriting contents it might have (or creating
1126 * it if such file does not yet exist).
1127 * Encoding to use must be specified, and needs to be one of available
1128 * types (as per JSON specification).
1129 *<p>
1130 * Underlying stream <b>is owned</b> by the generator constructed,
1131 * i.e. generator will handle closing of file when
1132 * {@link JsonGenerator#close} is called.
1133 *
1134 * @param f File to write contents to
1135 * @param enc Character encoding to use
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001136 *
1137 *
1138 * @deprecated Since 2.2, use {@link #createGenerator(File,JsonEncoding)} instead.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001139 */
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001140 @Deprecated
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001141 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc) throws IOException {
Tatu Saloranta4ab04ce2012-11-15 20:42:16 -08001142 return createGenerator(f, enc);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001143 }
1144
1145 /*
1146 /**********************************************************
1147 /* Factory methods used by factory for creating parser instances,
1148 /* overridable by sub-classes
1149 /**********************************************************
1150 */
1151
1152 /**
1153 * Overridable factory method that actually instantiates desired parser
1154 * given {@link InputStream} and context object.
1155 *<p>
1156 * This method is specifically designed to remain
1157 * compatible between minor versions so that sub-classes can count
1158 * on it being called as expected. That is, it is part of official
1159 * interface from sub-class perspective, although not a public
1160 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001161 *
1162 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001163 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001164 protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001165 // As per [JACKSON-259], may want to fully disable canonicalization:
1166 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
1167 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1168 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1169 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001170 }
1171
1172 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001173 * Overridable factory method that actually instantiates parser
1174 * using given {@link Reader} object for reading content.
1175 *<p>
1176 * This method is specifically designed to remain
1177 * compatible between minor versions so that sub-classes can count
1178 * on it being called as expected. That is, it is part of official
1179 * interface from sub-class perspective, although not a public
1180 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001181 *
1182 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001183 */
Tatu Salorantab0a2e192014-04-10 09:36:07 -07001184 protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
Tatu Saloranta68194922012-11-15 20:34:29 -08001185 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
1186 _rootCharSymbols.makeChild(isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1187 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES)));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001188 }
1189
1190 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001191 * Overridable factory method that actually instantiates parser
Tatu64aa9d22014-04-18 14:07:38 -07001192 * using given <code>char[]</code> object for accessing content.
1193 *
1194 * @since 2.4
1195 */
1196 protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt,
1197 boolean recyclable) throws IOException {
1198 return new ReaderBasedJsonParser(ctxt, _parserFeatures, null, _objectCodec,
1199 _rootCharSymbols.makeChild(isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1200 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES)),
1201 data, offset, offset+len,
1202 // false -> caller-provided, not handled by BufferRecycler
1203 recyclable);
1204 }
1205
1206 /**
1207 * Overridable factory method that actually instantiates parser
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001208 * using given {@link Reader} object for reading content
1209 * passed as raw byte array.
1210 *<p>
1211 * This method is specifically designed to remain
1212 * compatible between minor versions so that sub-classes can count
1213 * on it being called as expected. That is, it is part of official
1214 * interface from sub-class perspective, although not a public
1215 * method available to users of factory implementations.
1216 */
Tatu64aa9d22014-04-18 14:07:38 -07001217 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001218 {
Tatu Saloranta68194922012-11-15 20:34:29 -08001219 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
1220 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1221 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1222 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001223 }
1224
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001225 /*
1226 /**********************************************************
1227 /* Factory methods used by factory for creating generator instances,
1228 /* overridable by sub-classes
1229 /**********************************************************
1230 */
1231
1232 /**
1233 * Overridable factory method that actually instantiates generator for
1234 * given {@link Writer} and context object.
1235 *<p>
1236 * This method is specifically designed to remain
1237 * compatible between minor versions so that sub-classes can count
1238 * on it being called as expected. That is, it is part of official
1239 * interface from sub-class perspective, although not a public
1240 * method available to users of factory implementations.
1241 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001242 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001243 {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001244 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1245 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001246 if (_characterEscapes != null) {
1247 gen.setCharacterEscapes(_characterEscapes);
1248 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001249 SerializableString rootSep = _rootValueSeparator;
1250 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1251 gen.setRootValueSeparator(rootSep);
1252 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001253 return gen;
1254 }
1255
1256 /**
1257 * Overridable factory method that actually instantiates generator for
1258 * given {@link OutputStream} and context object, using UTF-8 encoding.
1259 *<p>
1260 * This method is specifically designed to remain
1261 * compatible between minor versions so that sub-classes can count
1262 * on it being called as expected. That is, it is part of official
1263 * interface from sub-class perspective, although not a public
1264 * method available to users of factory implementations.
1265 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001266 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001267 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1268 _generatorFeatures, _objectCodec, out);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001269 if (_characterEscapes != null) {
1270 gen.setCharacterEscapes(_characterEscapes);
1271 }
Tatu Salorantae6dfc692012-09-28 15:34:05 -07001272 SerializableString rootSep = _rootValueSeparator;
1273 if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
1274 gen.setRootValueSeparator(rootSep);
1275 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001276 return gen;
1277 }
1278
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001279 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1280 {
1281 // note: this should not get called any more (caller checks, dispatches)
1282 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1283 return new UTF8Writer(ctxt, out);
1284 }
1285 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1286 return new OutputStreamWriter(out, enc.getJavaName());
1287 }
Tatu Saloranta896000f2014-04-19 12:53:40 -07001288
1289 /*
1290 /**********************************************************
1291 /* Internal factory methods, decorator handling
1292 /**********************************************************
1293 */
1294
1295 /**
1296 * @since 2.4
1297 */
1298 protected final InputStream _decorate(InputStream in, IOContext ctxt) throws IOException {
1299 if (_inputDecorator != null) {
1300 InputStream in2 = _inputDecorator.decorate(ctxt, in);
1301 if (in2 != null) {
1302 return in2;
1303 }
1304 }
1305 return in;
1306 }
1307
1308 /**
1309 * @since 2.4
1310 */
1311 protected final Reader _decorate(Reader in, IOContext ctxt) throws IOException {
1312 if (_inputDecorator != null) {
1313 Reader in2 = _inputDecorator.decorate(ctxt, in);
1314 if (in2 != null) {
1315 return in2;
1316 }
1317 }
1318 return in;
1319 }
1320
1321 /**
1322 * @since 2.4
1323 */
1324 protected final OutputStream _decorate(OutputStream out, IOContext ctxt) throws IOException {
1325 if (_outputDecorator != null) {
1326 OutputStream out2 = _outputDecorator.decorate(ctxt, out);
1327 if (out2 != null) {
1328 return out2;
1329 }
1330 }
1331 return out;
1332 }
1333
1334 /**
1335 * @since 2.4
1336 */
1337 protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException {
1338 if (_outputDecorator != null) {
1339 Writer out2 = _outputDecorator.decorate(ctxt, out);
1340 if (out2 != null) {
1341 return out2;
1342 }
1343 }
1344 return out;
1345 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001346
1347 /*
1348 /**********************************************************
1349 /* Internal factory methods, other
1350 /**********************************************************
1351 */
1352
1353 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001354 * Method used by factory to create buffer recycler instances
1355 * for parsers and generators.
1356 *<p>
1357 * Note: only public to give access for <code>ObjectMapper</code>
1358 */
1359 public BufferRecycler _getBufferRecycler()
1360 {
1361 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1362 BufferRecycler br = (ref == null) ? null : ref.get();
1363
1364 if (br == null) {
1365 br = new BufferRecycler();
1366 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1367 }
1368 return br;
1369 }
1370
1371 /**
Tatu Saloranta896000f2014-04-19 12:53:40 -07001372 * Overridable factory method that actually instantiates desired
1373 * context object.
1374 */
1375 protected IOContext _createContext(Object srcRef, boolean resourceManaged) {
1376 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1377 }
1378
1379 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001380 * Helper methods used for constructing an optimal stream for
1381 * parsers to use, when input is to be read from an URL.
1382 * This helps when reading file content via URL.
1383 */
Tatu Salorantaefc23672013-12-30 19:09:39 -08001384 protected InputStream _optimizedStreamFromURL(URL url) throws IOException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001385 if ("file".equals(url.getProtocol())) {
1386 /* Can not do this if the path refers
1387 * to a network drive on windows. This fixes the problem;
1388 * might not be needed on all platforms (NFS?), but should not
1389 * matter a lot: performance penalty of extra wrapping is more
1390 * relevant when accessing local file system.
1391 */
1392 String host = url.getHost();
1393 if (host == null || host.length() == 0) {
Tatu Saloranta61d5bdd2013-01-11 19:02:01 -08001394 // [Issue#48]: Let's try to avoid probs with URL encoded stuff
1395 String path = url.getPath();
1396 if (path.indexOf('%') < 0) {
1397 return new FileInputStream(url.getPath());
1398
1399 }
1400 // otherwise, let's fall through and let URL decoder do its magic
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001401 }
1402 }
1403 return url.openStream();
1404 }
1405}