blob: 5395efe4374d1d3f19be61a5bd7e40f91188f8ea [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
4 *
5 * Licensed under the License specified in file LICENSE, included with
6 * the source code and binary code bundles.
7 * You may not use this file except in compliance with the License.
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package com.fasterxml.jackson.core;
16
17import java.io.*;
18import java.lang.ref.SoftReference;
19import java.net.URL;
20
21import com.fasterxml.jackson.core.format.InputAccessor;
22import com.fasterxml.jackson.core.format.MatchStrength;
23import com.fasterxml.jackson.core.io.*;
Tatu07351902012-01-19 13:12:13 -080024import com.fasterxml.jackson.core.json.*;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080025import com.fasterxml.jackson.core.sym.BytesToNameCanonicalizer;
26import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
27import com.fasterxml.jackson.core.util.BufferRecycler;
Tatu Salorantaf15531c2011-12-22 23:00:40 -080028
29/**
30 * The main factory class of Jackson package, used to configure and
31 * construct reader (aka parser, {@link JsonParser})
32 * and writer (aka generator, {@link JsonGenerator})
33 * instances.
34 *<p>
35 * Factory instances are thread-safe and reusable after configuration
36 * (if any). Typically applications and services use only a single
37 * globally shared factory instance, unless they need differently
38 * configured factories. Factory reuse is important if efficiency matters;
39 * most recycling of expensive construct is done on per-factory basis.
40 *<p>
41 * Creation of a factory instance is a light-weight operation,
42 * and since there is no need for pluggable alternative implementations
43 * (as there is no "standard" JSON processor API to implement),
44 * the default constructor is used for constructing factory
45 * instances.
46 *
47 * @author Tatu Saloranta
48 */
49public class JsonFactory implements Versioned
50{
51 /**
52 * Name used to identify JSON format
53 * (and returned by {@link #getFormatName()}
54 */
55 public final static String FORMAT_NAME_JSON = "JSON";
56
57 /**
Tatu07351902012-01-19 13:12:13 -080058 * Bitfield (set of flags) of all factory features that are enabled by default.
59 */
60 protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
61
62 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -080063 * Bitfield (set of flags) of all parser features that are enabled
64 * by default.
65 */
Tatu07351902012-01-19 13:12:13 -080066 protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
67
Tatu Salorantaf15531c2011-12-22 23:00:40 -080068 /**
69 * Bitfield (set of flags) of all generator features that are enabled
70 * by default.
71 */
Tatu07351902012-01-19 13:12:13 -080072 protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
Tatu Salorantaf15531c2011-12-22 23:00:40 -080073
Tatu07351902012-01-19 13:12:13 -080074 /**
75 * Enumeration that defines all on/off features that can only be
76 * changed for {@link JsonFactory}.
77 */
78 public enum Feature {
79
80 // // // Symbol handling (interning etc)
81
82 /**
83 * Feature that determines whether JSON object field names are
84 * to be canonicalized using {@link String#intern} or not:
85 * if enabled, all field names will be intern()ed (and caller
86 * can count on this being true for all such names); if disabled,
87 * no intern()ing is done. There may still be basic
88 * canonicalization (that is, same String will be used to represent
89 * all identical object property names for a single document).
90 *<p>
91 * Note: this setting only has effect if
92 * {@link #CANONICALIZE_FIELD_NAMES} is true -- otherwise no
93 * canonicalization of any sort is done.
94 *<p>
95 * This setting is enabled by default.
96 */
97 INTERN_FIELD_NAMES(true),
98
99 /**
100 * Feature that determines whether JSON object field names are
101 * to be canonicalized (details of how canonicalization is done
102 * then further specified by
103 * {@link #INTERN_FIELD_NAMES}).
104 *<p>
105 * This setting is enabled by default.
106 */
107 CANONICALIZE_FIELD_NAMES(true)
108
109 ;
110
111 /**
112 * Whether feature is enabled or disabled by default.
113 */
114 private final boolean _defaultState;
115
116 /**
117 * Method that calculates bit set (flags) of all features that
118 * are enabled by default.
119 */
120 public static int collectDefaults()
121 {
122 int flags = 0;
123 for (Feature f : values()) {
124 if (f.enabledByDefault()) {
125 flags |= f.getMask();
126 }
127 }
128 return flags;
129 }
130
131 private Feature(boolean defaultState)
132 {
133 _defaultState = defaultState;
134 }
135
136 public boolean enabledByDefault() { return _defaultState; }
137
138 public boolean enabledIn(int flags) { return (flags & getMask()) != 0; }
139
140 public int getMask() { return (1 << ordinal()); }
141 }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800142 /*
143 /**********************************************************
144 /* Buffer, symbol table management
145 /**********************************************************
146 */
147
148 /**
149 * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftRerefence}
150 * to a {@link BufferRecycler} used to provide a low-cost
151 * buffer recycling between reader and writer instances.
152 */
153 final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef
154 = new ThreadLocal<SoftReference<BufferRecycler>>();
155
156 /**
157 * Each factory comes equipped with a shared root symbol table.
158 * It should not be linked back to the original blueprint, to
159 * avoid contents from leaking between factories.
160 */
161 protected CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
162
163 /**
164 * Alternative to the basic symbol table, some stream-based
165 * parsers use different name canonicalization method.
166 *<p>
167 * TODO: should clean up this; looks messy having 2 alternatives
168 * with not very clear differences.
169 */
170 protected BytesToNameCanonicalizer _rootByteSymbols = BytesToNameCanonicalizer.createRoot();
171
172 /*
173 /**********************************************************
174 /* Configuration
175 /**********************************************************
176 */
177
178 /**
179 * Object that implements conversion functionality between
180 * Java objects and JSON content. For base JsonFactory implementation
181 * usually not set by default, but can be explicitly set.
182 * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory}
183 * usually provide an implementation.
184 */
185 protected ObjectCodec _objectCodec;
186
187 /**
Tatu07351902012-01-19 13:12:13 -0800188 * Currently enabled factory features.
189 */
190 protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
191
192 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800193 * Currently enabled parser features.
194 */
195 protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
196
197 /**
198 * Currently enabled generator features.
199 */
200 protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
201
202 /**
203 * Definition of custom character escapes to use for generators created
204 * by this factory, if any. If null, standard data format specific
205 * escapes are used.
206 */
207 protected CharacterEscapes _characterEscapes;
208
209 /**
210 * Optional helper object that may decorate input sources, to do
211 * additional processing on input during parsing.
212 */
213 protected InputDecorator _inputDecorator;
214
215 /**
216 * Optional helper object that may decorate output object, to do
217 * additional processing on output during content generation.
218 */
219 protected OutputDecorator _outputDecorator;
220
221 /*
222 /**********************************************************
223 /* Construction
224 /**********************************************************
225 */
226
227 /**
228 * Default constructor used to create factory instances.
229 * Creation of a factory instance is a light-weight operation,
230 * but it is still a good idea to reuse limited number of
231 * factory instances (and quite often just a single instance):
232 * factories are used as context for storing some reused
233 * processing objects (such as symbol tables parsers use)
234 * and this reuse only works within context of a single
235 * factory instance.
236 */
237 public JsonFactory() { this(null); }
238
239 public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
240
Tatu Saloranta378ecdc2012-08-04 16:27:27 -0700241 /**
242 * Method for constructing a new {@link JsonFactory} that has
243 * the same settings as this instance, but is otherwise
244 * independent (i.e. nothing is actually shared, symbol tables
245 * are separate).
246 * Note that {@link ObjectCodec} reference is not copied but is
247 * set to null; caller typically needs to set it after calling
248 * this method.
249 *
250 * @since 2.1
251 */
252 public JsonFactory copy()
253 {
254 _checkInvalidCopy(JsonFactory.class);
255 return new JsonFactory(null);
256 }
257
258 /**
259 * @since 2.1
260 * @param exp
261 */
262 protected void _checkInvalidCopy(Class<?> exp)
263 {
264 if (getClass() != exp) {
265 throw new IllegalStateException("Failed copy(): "+getClass().getName()
266 +" (version: "+version()+") does not override copy(); it has to");
267 }
268 }
269
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800270 /*
271 /**********************************************************
272 /* Format detection functionality (since 1.8)
273 /**********************************************************
274 */
275
276 /**
277 * Method that returns short textual id identifying format
278 * this factory supports.
279 *<p>
280 * Note: sub-classes should override this method; default
281 * implementation will return null for all sub-classes
282 */
283 public String getFormatName()
284 {
285 /* Somewhat nasty check: since we can't make this abstract
286 * (due to backwards compatibility concerns), need to prevent
287 * format name "leakage"
288 */
289 if (getClass() == JsonFactory.class) {
290 return FORMAT_NAME_JSON;
291 }
292 return null;
293 }
294
295 public MatchStrength hasFormat(InputAccessor acc) throws IOException
296 {
297 // since we can't keep this abstract, only implement for "vanilla" instance
298 if (getClass() == JsonFactory.class) {
299 return hasJSONFormat(acc);
300 }
301 return null;
302 }
303
304 protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
305 {
306 return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
307 }
308
309 /*
310 /**********************************************************
311 /* Versioned
312 /**********************************************************
313 */
314
Tatu Salorantae9b48512012-04-17 10:22:07 -0700315// @Override
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800316 public Version version() {
Tatu Saloranta5a1bd992011-12-28 23:18:23 -0800317 return CoreVersion.instance.version();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800318 }
319
320 /*
321 /**********************************************************
Tatu07351902012-01-19 13:12:13 -0800322 /* Configuration, factory features
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800323 /**********************************************************
324 */
325
326 /**
327 * Method for enabling or disabling specified parser feature
328 * (check {@link JsonParser.Feature} for list of features)
329 */
Tatu07351902012-01-19 13:12:13 -0800330 public final JsonFactory configure(JsonFactory.Feature f, boolean state) {
331 return state ? enable(f) : disable(f);
332 }
333
334 /**
335 * Method for enabling specified parser feature
336 * (check {@link JsonFactory.Feature} for list of features)
337 */
338 public JsonFactory enable(JsonFactory.Feature f) {
339 _factoryFeatures |= f.getMask();
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800340 return this;
341 }
342
343 /**
Tatu07351902012-01-19 13:12:13 -0800344 * Method for disabling specified parser features
345 * (check {@link JsonFactory.Feature} for list of features)
346 */
347 public JsonFactory disable(JsonFactory.Feature f) {
348 _factoryFeatures &= ~f.getMask();
349 return this;
350 }
351
352 /**
353 * Checked whether specified parser feature is enabled.
354 */
355 public final boolean isEnabled(JsonFactory.Feature f) {
356 return (_factoryFeatures & f.getMask()) != 0;
357 }
358
359 /*
360 /**********************************************************
361 /* Configuration, parser configuration
362 /**********************************************************
363 */
364
365 /**
366 * Method for enabling or disabling specified parser feature
367 * (check {@link JsonParser.Feature} for list of features)
368 */
369 public final JsonFactory configure(JsonParser.Feature f, boolean state) {
370 return state ? enable(f) : disable(f);
371 }
372
373 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800374 * Method for enabling specified parser feature
375 * (check {@link JsonParser.Feature} for list of features)
376 */
377 public JsonFactory enable(JsonParser.Feature f) {
378 _parserFeatures |= f.getMask();
379 return this;
380 }
381
382 /**
383 * Method for disabling specified parser features
384 * (check {@link JsonParser.Feature} for list of features)
385 */
386 public JsonFactory disable(JsonParser.Feature f) {
387 _parserFeatures &= ~f.getMask();
388 return this;
389 }
390
391 /**
392 * Checked whether specified parser feature is enabled.
393 */
394 public final boolean isEnabled(JsonParser.Feature f) {
395 return (_parserFeatures & f.getMask()) != 0;
396 }
397
398 /**
399 * Method for getting currently configured input decorator (if any;
400 * there is no default decorator).
401 */
402 public InputDecorator getInputDecorator() {
403 return _inputDecorator;
404 }
405
406 /**
407 * Method for overriding currently configured input decorator
408 */
409 public JsonFactory setInputDecorator(InputDecorator d) {
410 _inputDecorator = d;
411 return this;
412 }
413
414 /*
415 /**********************************************************
416 /* Configuration, generator settings
417 /**********************************************************
418 */
419
420 /**
421 * Method for enabling or disabling specified generator feature
422 * (check {@link JsonGenerator.Feature} for list of features)
423 */
424 public final JsonFactory configure(JsonGenerator.Feature f, boolean state) {
Tatu07351902012-01-19 13:12:13 -0800425 return state ? enable(f) : disable(f);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800426 }
427
428
429 /**
430 * Method for enabling specified generator features
431 * (check {@link JsonGenerator.Feature} for list of features)
432 */
433 public JsonFactory enable(JsonGenerator.Feature f) {
434 _generatorFeatures |= f.getMask();
435 return this;
436 }
437
438 /**
439 * Method for disabling specified generator feature
440 * (check {@link JsonGenerator.Feature} for list of features)
441 */
442 public JsonFactory disable(JsonGenerator.Feature f) {
443 _generatorFeatures &= ~f.getMask();
444 return this;
445 }
446
447 /**
448 * Check whether specified generator feature is enabled.
449 */
450 public final boolean isEnabled(JsonGenerator.Feature f) {
451 return (_generatorFeatures & f.getMask()) != 0;
452 }
453
454 /**
455 * Method for accessing custom escapes factory uses for {@link JsonGenerator}s
456 * it creates.
457 */
458 public CharacterEscapes getCharacterEscapes() {
459 return _characterEscapes;
460 }
461
462 /**
463 * Method for defining custom escapes factory uses for {@link JsonGenerator}s
464 * it creates.
465 */
466 public JsonFactory setCharacterEscapes(CharacterEscapes esc) {
467 _characterEscapes = esc;
468 return this;
469 }
470
471 /**
472 * Method for getting currently configured output decorator (if any;
473 * there is no default decorator).
474 */
475 public OutputDecorator getOutputDecorator() {
476 return _outputDecorator;
477 }
478
479 /**
480 * Method for overriding currently configured output decorator
481 */
482 public JsonFactory setOutputDecorator(OutputDecorator d) {
483 _outputDecorator = d;
484 return this;
485 }
486
487 /*
488 /**********************************************************
489 /* Configuration, other
490 /**********************************************************
491 */
492
493 /**
494 * Method for associating a {@link ObjectCodec} (typically
Tatu Salorantad77350e2011-12-22 23:13:13 -0800495 * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>)
496 * with this factory (and more importantly, parsers and generators
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800497 * it constructs). This is needed to use data-binding methods
498 * of {@link JsonParser} and {@link JsonGenerator} instances.
499 */
500 public JsonFactory setCodec(ObjectCodec oc) {
501 _objectCodec = oc;
502 return this;
503 }
504
505 public ObjectCodec getCodec() { return _objectCodec; }
506
507 /*
508 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700509 /* Parser factories (new ones, as per [Issue-25])
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800510 /**********************************************************
511 */
512
513 /**
514 * Method for constructing JSON parser instance to parse
515 * contents of specified file. Encoding is auto-detected
516 * from contents according to JSON specification recommended
517 * mechanism.
518 *<p>
519 * Underlying input stream (needed for reading contents)
520 * will be <b>owned</b> (and managed, i.e. closed as need be) by
521 * the parser, since caller has no access to it.
522 *
523 * @param f File that contains JSON content to parse
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700524 *
525 * @since 2.1
526 */
527 public JsonParser createParser(File f)
528 throws IOException, JsonParseException
529 {
530 // Must delegate to old version, until 2.2
531 // TODO: change direction in 2.2; after ensuring impls support new method
532 return createJsonParser(f);
533 }
534
535 /**
536 * Method for constructing JSON parser instance to parse
537 * contents of resource reference by given URL.
538 * Encoding is auto-detected
539 * from contents according to JSON specification recommended
540 * mechanism.
541 *<p>
542 * Underlying input stream (needed for reading contents)
543 * will be <b>owned</b> (and managed, i.e. closed as need be) by
544 * the parser, since caller has no access to it.
545 *
546 * @param url URL pointing to resource that contains JSON content to parse
547 *
548 * @since 2.1
549 */
550 public JsonParser createParser(URL url)
551 throws IOException, JsonParseException
552 {
553 // Must delegate to old version, until 2.2
554 // TODO: change direction in 2.2; after ensuring impls support new method
555 return createJsonParser(url);
556 }
557
558 /**
559 * Method for constructing JSON parser instance to parse
560 * the contents accessed via specified input stream.
561 *<p>
562 * The input stream will <b>not be owned</b> by
563 * the parser, it will still be managed (i.e. closed if
564 * end-of-stream is reacher, or parser close method called)
565 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
566 * is enabled.
567 *<p>
568 * Note: no encoding argument is taken since it can always be
569 * auto-detected as suggested by JSON RFC.
570 *
571 * @param in InputStream to use for reading JSON content to parse
572 *
573 * @since 2.1
574 */
575 public JsonParser createParser(InputStream in)
576 throws IOException, JsonParseException
577 {
578 // Must delegate to old version, until 2.2
579 // TODO: change direction in 2.2; after ensuring impls support new method
580 return createJsonParser(in);
581 }
582
583 /**
584 * Method for constructing parser for parsing
585 * the contents accessed via specified Reader.
586 <p>
587 * The read stream will <b>not be owned</b> by
588 * the parser, it will still be managed (i.e. closed if
589 * end-of-stream is reacher, or parser close method called)
590 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
591 * is enabled.
592 *
593 * @param r Reader to use for reading JSON content to parse
594 *
595 * @since 2.1
596 */
597 public JsonParser createParser(Reader r)
598 throws IOException, JsonParseException
599 {
600 // Must delegate to old version, until 2.2
601 // TODO: change direction in 2.2; after ensuring impls support new method
602 return createJsonParser(r);
603 }
604
605 /**
606 * Method for constructing parser for parsing
607 * the contents of given byte array.
608 *
609 * @since 2.1
610 */
611 public JsonParser createParser(byte[] data)
612 throws IOException, JsonParseException
613 {
614 // Must delegate to old version, until 2.2
615 // TODO: change direction in 2.2; after ensuring impls support new method
616 return createJsonParser(data);
617 }
618
619 /**
620 * Method for constructing parser for parsing
621 * the contents of given byte array.
622 *
623 * @param data Buffer that contains data to parse
624 * @param offset Offset of the first data byte within buffer
625 * @param len Length of contents to parse within buffer
626 *
627 * @since 2.1
628 */
629 public JsonParser createParser(byte[] data, int offset, int len)
630 throws IOException, JsonParseException
631 {
632 // Must delegate to old version, until 2.2
633 // TODO: change direction in 2.2; after ensuring impls support new method
634 return createJsonParser(data, offset, len);
635 }
636
637 /**
638 * Method for constructing parser for parsing
639 * contents of given String.
640 *
641 * @since 2.1
642 */
643 public JsonParser createParser(String content)
644 throws IOException, JsonParseException
645 {
646 // Must delegate to old version, until 2.2
647 // TODO: change direction in 2.2; after ensuring impls support new method
648 return createJsonParser(content);
649 }
650
651 /*
652 /**********************************************************
653 /* Parser factories (old ones, as per [Issue-25])
654 /**********************************************************
655 */
656
657 /**
658 * Method for constructing JSON parser instance to parse
659 * contents of specified file. Encoding is auto-detected
660 * from contents according to JSON specification recommended
661 * mechanism.
662 *<p>
663 * Underlying input stream (needed for reading contents)
664 * will be <b>owned</b> (and managed, i.e. closed as need be) by
665 * the parser, since caller has no access to it.
666 *<p>
667 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
668 * instead, should call <code>createParser</code>.
669 *
670 * @param f File that contains JSON content to parse
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800671 */
672 public JsonParser createJsonParser(File f)
673 throws IOException, JsonParseException
674 {
675 // true, since we create InputStream from File
676 IOContext ctxt = _createContext(f, true);
677 InputStream in = new FileInputStream(f);
678 // [JACKSON-512]: allow wrapping with InputDecorator
679 if (_inputDecorator != null) {
680 in = _inputDecorator.decorate(ctxt, in);
681 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700682 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800683 }
684
685 /**
686 * Method for constructing JSON parser instance to parse
687 * contents of resource reference by given URL.
688 * Encoding is auto-detected
689 * from contents according to JSON specification recommended
690 * mechanism.
691 *<p>
692 * Underlying input stream (needed for reading contents)
693 * will be <b>owned</b> (and managed, i.e. closed as need be) by
694 * the parser, since caller has no access to it.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700695 *<p>
696 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
697 * instead, should call <code>createParser</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800698 *
699 * @param url URL pointing to resource that contains JSON content to parse
700 */
701 public JsonParser createJsonParser(URL url)
702 throws IOException, JsonParseException
703 {
704 // true, since we create InputStream from URL
705 IOContext ctxt = _createContext(url, true);
706 InputStream in = _optimizedStreamFromURL(url);
707 // [JACKSON-512]: allow wrapping with InputDecorator
708 if (_inputDecorator != null) {
709 in = _inputDecorator.decorate(ctxt, in);
710 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700711 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800712 }
713
714 /**
715 * Method for constructing JSON parser instance to parse
716 * the contents accessed via specified input stream.
717 *<p>
718 * The input stream will <b>not be owned</b> by
719 * the parser, it will still be managed (i.e. closed if
720 * end-of-stream is reacher, or parser close method called)
721 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
722 * is enabled.
723 *<p>
724 * Note: no encoding argument is taken since it can always be
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700725 * auto-detected as suggested by JSON RFC.
726 *<p>
727 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
728 * instead, should call <code>createParser</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800729 *
730 * @param in InputStream to use for reading JSON content to parse
731 */
732 public JsonParser createJsonParser(InputStream in)
733 throws IOException, JsonParseException
734 {
735 IOContext ctxt = _createContext(in, false);
736 // [JACKSON-512]: allow wrapping with InputDecorator
737 if (_inputDecorator != null) {
738 in = _inputDecorator.decorate(ctxt, in);
739 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700740 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800741 }
742
743 /**
744 * Method for constructing parser for parsing
745 * the contents accessed via specified Reader.
746 <p>
747 * The read stream will <b>not be owned</b> by
748 * the parser, it will still be managed (i.e. closed if
749 * end-of-stream is reacher, or parser close method called)
750 * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE}
751 * is enabled.
752 *<p>
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700753 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
754 * instead, should call <code>createParser</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800755 *
756 * @param r Reader to use for reading JSON content to parse
757 */
758 public JsonParser createJsonParser(Reader r)
759 throws IOException, JsonParseException
760 {
761 // false -> we do NOT own Reader (did not create it)
762 IOContext ctxt = _createContext(r, false);
763 // [JACKSON-512]: allow wrapping with InputDecorator
764 if (_inputDecorator != null) {
765 r = _inputDecorator.decorate(ctxt, r);
766 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700767 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800768 }
769
770 /**
771 * Method for constructing parser for parsing
772 * the contents of given byte array.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700773 *<p>
774 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
775 * instead, should call <code>createParser</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800776 */
777 public JsonParser createJsonParser(byte[] data)
778 throws IOException, JsonParseException
779 {
780 IOContext ctxt = _createContext(data, true);
781 // [JACKSON-512]: allow wrapping with InputDecorator
782 if (_inputDecorator != null) {
783 InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
784 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700785 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800786 }
787 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700788 return _createParser(data, 0, data.length, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800789 }
790
791 /**
792 * Method for constructing parser for parsing
793 * the contents of given byte array.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700794 *<p>
795 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
796 * instead, should call <code>createParser</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800797 *
798 * @param data Buffer that contains data to parse
799 * @param offset Offset of the first data byte within buffer
800 * @param len Length of contents to parse within buffer
801 */
802 public JsonParser createJsonParser(byte[] data, int offset, int len)
803 throws IOException, JsonParseException
804 {
805 IOContext ctxt = _createContext(data, true);
806 // [JACKSON-512]: allow wrapping with InputDecorator
807 if (_inputDecorator != null) {
808 InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
809 if (in != null) {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700810 return _createParser(in, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800811 }
812 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700813 return _createParser(data, offset, len, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800814 }
815
816 /**
817 * Method for constructing parser for parsing
818 * contents of given String.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700819 *<p>
820 * NOTE: as of 2.1, should not be used (will be deprecated in 2.2);
821 * instead, should call <code>createParser</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800822 */
823 public JsonParser createJsonParser(String content)
824 throws IOException, JsonParseException
825 {
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700826 Reader r = new StringReader(content);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800827 // true -> we own the Reader (and must close); not a big deal
828 IOContext ctxt = _createContext(r, true);
829 // [JACKSON-512]: allow wrapping with InputDecorator
830 if (_inputDecorator != null) {
831 r = _inputDecorator.decorate(ctxt, r);
832 }
Tatu Saloranta3e5ff6d2012-06-27 18:46:43 -0700833 return _createParser(r, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800834 }
835
836 /*
837 /**********************************************************
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700838 /* Generator factories, new (as per [Issue-25]
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800839 /**********************************************************
840 */
841
842 /**
843 * Method for constructing JSON generator for writing JSON content
844 * using specified output stream.
845 * Encoding to use must be specified, and needs to be one of available
846 * types (as per JSON specification).
847 *<p>
848 * Underlying stream <b>is NOT owned</b> by the generator constructed,
849 * so that generator will NOT close the output stream when
850 * {@link JsonGenerator#close} is called (unless auto-closing
851 * feature,
852 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
853 * is enabled).
854 * Using application needs to close it explicitly if this is the case.
855 *<p>
856 * Note: there are formats that use fixed encoding (like most binary data formats)
857 * and that ignore passed in encoding.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700858 *<p>
859 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
860 * instead, should call <code>createGenerator</code>.
861 *
862 * @since 2.1 Will eventually replace <code>createJsonGenerator</code> variant.
863 *
864 * @param out OutputStream to use for writing JSON content
865 * @param enc Character encoding to use
866 */
867 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
868 throws IOException
869 {
870 return createJsonGenerator(out, enc);
871 }
872
873 /**
874 * Method for constructing JSON generator for writing JSON content
875 * using specified Writer.
876 *<p>
877 * Underlying stream <b>is NOT owned</b> by the generator constructed,
878 * so that generator will NOT close the Reader when
879 * {@link JsonGenerator#close} is called (unless auto-closing
880 * feature,
881 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
882 * Using application needs to close it explicitly.
883 *<p>
884 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
885 * instead, should call <code>createGenerator</code>.
886 *
887 * @since 2.1 Will eventually replace <code>createJsonGenerator</code> variant.
888 *
889 * @param out Writer to use for writing JSON content
890 */
891 public JsonGenerator createGenerator(Writer out)
892 throws IOException
893 {
894 return createJsonGenerator(out);
895 }
896
897 /**
898 * Convenience method for constructing generator that uses default
899 * encoding of the format (UTF-8 for JSON and most other data formats).
900 *<p>
901 * Note: there are formats that use fixed encoding (like most binary data formats).
902 *<p>
903 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
904 * instead, should call <code>createGenerator</code>.
905 *
906 * @since 2.1 Will eventually replace <code>createJsonGenerator</code> variant.
907 */
908 public JsonGenerator createGenerator(OutputStream out) throws IOException {
909 return createJsonGenerator(out);
910 }
911
912 /**
913 * Method for constructing JSON generator for writing JSON content
914 * to specified file, overwriting contents it might have (or creating
915 * it if such file does not yet exist).
916 * Encoding to use must be specified, and needs to be one of available
917 * types (as per JSON specification).
918 *<p>
919 * Underlying stream <b>is owned</b> by the generator constructed,
920 * i.e. generator will handle closing of file when
921 * {@link JsonGenerator#close} is called.
922 *<p>
923 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
924 * instead, should call <code>createGenerator</code>.
925 *
926 * @since 2.1 Will eventually replace <code>createJsonGenerator</code> variant.
927 *
928 * @param f File to write contents to
929 * @param enc Character encoding to use
930 */
931 public JsonGenerator createGenerator(File f, JsonEncoding enc)
932 throws IOException
933 {
934 return createJsonGenerator(f, enc);
935 }
936
937 /*
938 /**********************************************************
939 /* Generator factories, old (as per [Issue-25]
940 /**********************************************************
941 */
942
943 /**
944 * Method for constructing JSON generator for writing JSON content
945 * using specified output stream.
946 * Encoding to use must be specified, and needs to be one of available
947 * types (as per JSON specification).
948 *<p>
949 * Underlying stream <b>is NOT owned</b> by the generator constructed,
950 * so that generator will NOT close the output stream when
951 * {@link JsonGenerator#close} is called (unless auto-closing
952 * feature,
953 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET}
954 * is enabled).
955 * Using application needs to close it explicitly if this is the case.
956 *<p>
957 * Note: there are formats that use fixed encoding (like most binary data formats)
958 * and that ignore passed in encoding.
959 *<p>
960 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
961 * instead, should call <code>createGenerator</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800962 *
963 * @param out OutputStream to use for writing JSON content
964 * @param enc Character encoding to use
965 */
966 public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc)
967 throws IOException
968 {
969 // false -> we won't manage the stream unless explicitly directed to
970 IOContext ctxt = _createContext(out, false);
971 ctxt.setEncoding(enc);
972 if (enc == JsonEncoding.UTF8) {
973 // [JACKSON-512]: allow wrapping with _outputDecorator
974 if (_outputDecorator != null) {
975 out = _outputDecorator.decorate(ctxt, out);
976 }
977 return _createUTF8JsonGenerator(out, ctxt);
978 }
979 Writer w = _createWriter(out, enc, ctxt);
980 // [JACKSON-512]: allow wrapping with _outputDecorator
981 if (_outputDecorator != null) {
982 w = _outputDecorator.decorate(ctxt, w);
983 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -0700984 return _createGenerator(w, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800985 }
986
987 /**
988 * Method for constructing JSON generator for writing JSON content
989 * using specified Writer.
990 *<p>
991 * Underlying stream <b>is NOT owned</b> by the generator constructed,
992 * so that generator will NOT close the Reader when
993 * {@link JsonGenerator#close} is called (unless auto-closing
994 * feature,
995 * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled).
996 * Using application needs to close it explicitly.
Tatu Salorantafeaabd12012-07-22 23:03:41 -0700997 *<p>
998 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
999 * instead, should call <code>createGenerator</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001000 *
1001 * @param out Writer to use for writing JSON content
1002 */
1003 public JsonGenerator createJsonGenerator(Writer out)
1004 throws IOException
1005 {
1006 IOContext ctxt = _createContext(out, false);
1007 // [JACKSON-512]: allow wrapping with _outputDecorator
1008 if (_outputDecorator != null) {
1009 out = _outputDecorator.decorate(ctxt, out);
1010 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001011 return _createGenerator(out, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001012 }
1013
1014 /**
1015 * Convenience method for constructing generator that uses default
1016 * encoding of the format (UTF-8 for JSON and most other data formats).
1017 *<p>
1018 * Note: there are formats that use fixed encoding (like most binary data formats).
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001019 *<p>
1020 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
1021 * instead, should call <code>createGenerator</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001022 */
1023 public JsonGenerator createJsonGenerator(OutputStream out) throws IOException {
1024 return createJsonGenerator(out, JsonEncoding.UTF8);
1025 }
1026
1027 /**
1028 * Method for constructing JSON generator for writing JSON content
1029 * to specified file, overwriting contents it might have (or creating
1030 * it if such file does not yet exist).
1031 * Encoding to use must be specified, and needs to be one of available
1032 * types (as per JSON specification).
1033 *<p>
1034 * Underlying stream <b>is owned</b> by the generator constructed,
1035 * i.e. generator will handle closing of file when
1036 * {@link JsonGenerator#close} is called.
Tatu Salorantafeaabd12012-07-22 23:03:41 -07001037 *<p>
1038 * NOTE: starting with 2.1, should not be used (will be deprecated in 2.2);
1039 * instead, should call <code>createGenerator</code>.
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001040 *
1041 * @param f File to write contents to
1042 * @param enc Character encoding to use
1043 */
1044 public JsonGenerator createJsonGenerator(File f, JsonEncoding enc)
1045 throws IOException
1046 {
1047 OutputStream out = new FileOutputStream(f);
1048 // true -> yes, we have to manage the stream since we created it
1049 IOContext ctxt = _createContext(out, true);
1050 ctxt.setEncoding(enc);
1051 if (enc == JsonEncoding.UTF8) {
1052 // [JACKSON-512]: allow wrapping with _outputDecorator
1053 if (_outputDecorator != null) {
1054 out = _outputDecorator.decorate(ctxt, out);
1055 }
1056 return _createUTF8JsonGenerator(out, ctxt);
1057 }
1058 Writer w = _createWriter(out, enc, ctxt);
1059 // [JACKSON-512]: allow wrapping with _outputDecorator
1060 if (_outputDecorator != null) {
1061 w = _outputDecorator.decorate(ctxt, w);
1062 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001063 return _createGenerator(w, ctxt);
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001064 }
1065
1066 /*
1067 /**********************************************************
1068 /* Factory methods used by factory for creating parser instances,
1069 /* overridable by sub-classes
1070 /**********************************************************
1071 */
1072
1073 /**
1074 * Overridable factory method that actually instantiates desired parser
1075 * given {@link InputStream} and context object.
1076 *<p>
1077 * This method is specifically designed to remain
1078 * compatible between minor versions so that sub-classes can count
1079 * on it being called as expected. That is, it is part of official
1080 * interface from sub-class perspective, although not a public
1081 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001082 *
1083 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001084 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001085 protected JsonParser _createParser(InputStream in, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001086 throws IOException, JsonParseException
1087 {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001088 /* NOTE: MUST call the deprecated method until it is deleted, just so
1089 * that override still works as expected, for now.
1090 */
1091 return _createJsonParser(in, ctxt);
1092 }
1093
1094 /**
1095 * @deprecated since 2.1 -- use {@link #_createParser(InputStream, IOContext)} instead
1096 */
1097 @Deprecated
1098 protected JsonParser _createJsonParser(InputStream in, IOContext ctxt) throws IOException, JsonParseException {
Tatu07351902012-01-19 13:12:13 -08001099 // As per [JACKSON-259], may want to fully disable canonicalization:
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001100 return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures,
Tatu07351902012-01-19 13:12:13 -08001101 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1102 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1103 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001104 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001105
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001106 /**
1107 * Overridable factory method that actually instantiates parser
1108 * using given {@link Reader} object for reading content.
1109 *<p>
1110 * This method is specifically designed to remain
1111 * compatible between minor versions so that sub-classes can count
1112 * on it being called as expected. That is, it is part of official
1113 * interface from sub-class perspective, although not a public
1114 * method available to users of factory implementations.
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001115 *
1116 * @since 2.1
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001117 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001118 protected JsonParser _createParser(Reader r, IOContext ctxt)
1119 throws IOException, JsonParseException
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001120 {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001121 /* NOTE: MUST call the deprecated method until it is deleted, just so
1122 * that override still works as expected, for now.
1123 */
1124 return _createJsonParser(r, ctxt);
1125 }
1126
1127 /**
1128 * @deprecated since 2.1 -- use {@link #_createParser(Reader, IOContext)} instead
1129 */
1130 @Deprecated
1131 protected JsonParser _createJsonParser(Reader r, IOContext ctxt) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001132 return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec,
Tatu07351902012-01-19 13:12:13 -08001133 _rootCharSymbols.makeChild(isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1134 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES)));
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001135 }
1136
1137 /**
1138 * Overridable factory method that actually instantiates parser
1139 * using given {@link Reader} object for reading content
1140 * passed as raw byte array.
1141 *<p>
1142 * This method is specifically designed to remain
1143 * compatible between minor versions so that sub-classes can count
1144 * on it being called as expected. That is, it is part of official
1145 * interface from sub-class perspective, although not a public
1146 * method available to users of factory implementations.
1147 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001148 protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt)
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001149 throws IOException, JsonParseException
1150 {
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001151 /* NOTE: MUST call the deprecated method until it is deleted, just so
1152 * that override still works as expected, for now.
1153 */
1154 return _createJsonParser(data, offset, len, ctxt);
1155 }
1156
1157 /**
1158 * @deprecated since 2.1 -- use {@link #_createParser(byte[], int, int, IOContext)} instead
1159 */
1160 @Deprecated
1161 protected JsonParser _createJsonParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException, JsonParseException {
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001162 return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures,
Tatu07351902012-01-19 13:12:13 -08001163 _objectCodec, _rootByteSymbols, _rootCharSymbols,
1164 isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES),
1165 isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001166 }
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001167
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001168 /*
1169 /**********************************************************
1170 /* Factory methods used by factory for creating generator instances,
1171 /* overridable by sub-classes
1172 /**********************************************************
1173 */
1174
1175 /**
1176 * Overridable factory method that actually instantiates generator for
1177 * given {@link Writer} and context object.
1178 *<p>
1179 * This method is specifically designed to remain
1180 * compatible between minor versions so that sub-classes can count
1181 * on it being called as expected. That is, it is part of official
1182 * interface from sub-class perspective, although not a public
1183 * method available to users of factory implementations.
1184 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001185 protected JsonGenerator _createGenerator(Writer out, IOContext ctxt)
1186 throws IOException
1187 {
1188 /* NOTE: MUST call the deprecated method until it is deleted, just so
1189 * that override still works as expected, for now.
1190 */
1191 return _createJsonGenerator(out, ctxt);
1192 }
1193
1194 /**
Tatu Salorantaec300272012-06-28 15:08:51 -07001195 * @deprecated since 2.1 -- use {@link #_createGenerator(Writer, IOContext)} instead
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001196 */
1197 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001198 protected JsonGenerator _createJsonGenerator(Writer out, IOContext ctxt)
1199 throws IOException
1200 {
1201 WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt, _generatorFeatures, _objectCodec, out);
1202 if (_characterEscapes != null) {
1203 gen.setCharacterEscapes(_characterEscapes);
1204 }
1205 return gen;
1206 }
1207
1208 /**
1209 * Overridable factory method that actually instantiates generator for
1210 * given {@link OutputStream} and context object, using UTF-8 encoding.
1211 *<p>
1212 * This method is specifically designed to remain
1213 * compatible between minor versions so that sub-classes can count
1214 * on it being called as expected. That is, it is part of official
1215 * interface from sub-class perspective, although not a public
1216 * method available to users of factory implementations.
1217 */
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001218 protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
1219 return _createUTF8JsonGenerator(out, ctxt);
1220 }
1221
1222 /**
Tatu Salorantaec300272012-06-28 15:08:51 -07001223 * @deprecated since 2.1
Tatu Salorantaeeb2fbd2012-05-28 22:00:08 -07001224 */
1225 @Deprecated
Tatu Salorantaf15531c2011-12-22 23:00:40 -08001226 protected JsonGenerator _createUTF8JsonGenerator(OutputStream out, IOContext ctxt)
1227 throws IOException
1228 {
1229 UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt, _generatorFeatures, _objectCodec, out);
1230 if (_characterEscapes != null) {
1231 gen.setCharacterEscapes(_characterEscapes);
1232 }
1233 return gen;
1234 }
1235
1236 protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
1237 {
1238 // note: this should not get called any more (caller checks, dispatches)
1239 if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8
1240 return new UTF8Writer(ctxt, out);
1241 }
1242 // not optimal, but should do unless we really care about UTF-16/32 encoding speed
1243 return new OutputStreamWriter(out, enc.getJavaName());
1244 }
1245
1246 /*
1247 /**********************************************************
1248 /* Internal factory methods, other
1249 /**********************************************************
1250 */
1251
1252 /**
1253 * Overridable factory method that actually instantiates desired
1254 * context object.
1255 */
1256 protected IOContext _createContext(Object srcRef, boolean resourceManaged)
1257 {
1258 return new IOContext(_getBufferRecycler(), srcRef, resourceManaged);
1259 }
1260
1261 /**
1262 * Method used by factory to create buffer recycler instances
1263 * for parsers and generators.
1264 *<p>
1265 * Note: only public to give access for <code>ObjectMapper</code>
1266 */
1267 public BufferRecycler _getBufferRecycler()
1268 {
1269 SoftReference<BufferRecycler> ref = _recyclerRef.get();
1270 BufferRecycler br = (ref == null) ? null : ref.get();
1271
1272 if (br == null) {
1273 br = new BufferRecycler();
1274 _recyclerRef.set(new SoftReference<BufferRecycler>(br));
1275 }
1276 return br;
1277 }
1278
1279 /**
1280 * Helper methods used for constructing an optimal stream for
1281 * parsers to use, when input is to be read from an URL.
1282 * This helps when reading file content via URL.
1283 */
1284 protected InputStream _optimizedStreamFromURL(URL url)
1285 throws IOException
1286 {
1287 if ("file".equals(url.getProtocol())) {
1288 /* Can not do this if the path refers
1289 * to a network drive on windows. This fixes the problem;
1290 * might not be needed on all platforms (NFS?), but should not
1291 * matter a lot: performance penalty of extra wrapping is more
1292 * relevant when accessing local file system.
1293 */
1294 String host = url.getHost();
1295 if (host == null || host.length() == 0) {
1296 return new FileInputStream(url.getPath());
1297 }
1298 }
1299 return url.openStream();
1300 }
1301}