Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1 | /* Jackson JSON-processor. |
| 2 | * |
| 3 | * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 4 | */ |
| 5 | package com.fasterxml.jackson.core; |
| 6 | |
| 7 | import java.io.*; |
| 8 | import java.lang.ref.SoftReference; |
| 9 | import java.net.URL; |
| 10 | |
| 11 | import com.fasterxml.jackson.core.format.InputAccessor; |
| 12 | import com.fasterxml.jackson.core.format.MatchStrength; |
| 13 | import com.fasterxml.jackson.core.io.*; |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 14 | import com.fasterxml.jackson.core.json.*; |
Cowtowncoder | 3a00c86 | 2015-02-04 22:30:30 -0800 | [diff] [blame] | 15 | import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 16 | import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer; |
| 17 | import com.fasterxml.jackson.core.util.BufferRecycler; |
Tatu Saloranta | e6dfc69 | 2012-09-28 15:34:05 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 19 | |
| 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 Saloranta | e92d04f | 2013-07-24 22:58:08 -0700 | [diff] [blame] | 40 | @SuppressWarnings("resource") |
Tatu Saloranta | 95f76a4 | 2012-10-05 13:04:23 -0700 | [diff] [blame] | 41 | public class JsonFactory |
| 42 | implements Versioned, |
| 43 | java.io.Serializable // since 2.1 (for Android, mostly) |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 44 | { |
Tatu Saloranta | dccffbe | 2015-02-05 21:17:57 -0800 | [diff] [blame] | 45 | private static final long serialVersionUID = 1; // since 2.6.0 |
Tatu Saloranta | 95f76a4 | 2012-10-05 13:04:23 -0700 | [diff] [blame] | 46 | |
Tatu Saloranta | 7b796a8 | 2013-04-27 10:18:30 -0700 | [diff] [blame] | 47 | /* |
| 48 | /********************************************************** |
| 49 | /* Helper types |
| 50 | /********************************************************** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 51 | */ |
Tatu Saloranta | e92d04f | 2013-07-24 22:58:08 -0700 | [diff] [blame] | 52 | |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Enumeration that defines all on/off features that can only be |
| 55 | * changed for {@link JsonFactory}. |
| 56 | */ |
| 57 | public enum Feature { |
| 58 | |
| 59 | // // // Symbol handling (interning etc) |
| 60 | |
| 61 | /** |
| 62 | * Feature that determines whether JSON object field names are |
| 63 | * to be canonicalized using {@link String#intern} or not: |
| 64 | * if enabled, all field names will be intern()ed (and caller |
| 65 | * can count on this being true for all such names); if disabled, |
| 66 | * no intern()ing is done. There may still be basic |
| 67 | * canonicalization (that is, same String will be used to represent |
| 68 | * all identical object property names for a single document). |
| 69 | *<p> |
| 70 | * Note: this setting only has effect if |
| 71 | * {@link #CANONICALIZE_FIELD_NAMES} is true -- otherwise no |
| 72 | * canonicalization of any sort is done. |
| 73 | *<p> |
| 74 | * This setting is enabled by default. |
| 75 | */ |
| 76 | INTERN_FIELD_NAMES(true), |
| 77 | |
| 78 | /** |
| 79 | * Feature that determines whether JSON object field names are |
| 80 | * to be canonicalized (details of how canonicalization is done |
| 81 | * then further specified by |
| 82 | * {@link #INTERN_FIELD_NAMES}). |
| 83 | *<p> |
| 84 | * This setting is enabled by default. |
| 85 | */ |
Tatu Saloranta | 383bc8f | 2014-05-24 00:47:07 -0700 | [diff] [blame] | 86 | CANONICALIZE_FIELD_NAMES(true), |
| 87 | |
| 88 | /** |
| 89 | * Feature that determines what happens if we encounter a case in symbol |
| 90 | * handling where number of hash collisions exceeds a safety threshold |
| 91 | * -- which almost certainly means a denial-of-service attack via generated |
| 92 | * duplicate hash codes. |
| 93 | * If feature is enabled, an {@link IllegalStateException} is |
| 94 | * thrown to indicate the suspected denial-of-service attack; if disabled, processing continues but |
| 95 | * canonicalization (and thereby <code>intern()</code>ing) is disabled) as protective |
| 96 | * measure. |
| 97 | *<p> |
| 98 | * This setting is enabled by default. |
| 99 | * |
| 100 | * @since 2.4 |
| 101 | */ |
| 102 | FAIL_ON_SYMBOL_HASH_OVERFLOW(true), |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 103 | |
Tatu Saloranta | 8891c0c | 2015-04-23 22:47:05 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Feature that determines whether we will use {@link BufferRecycler} with |
| 106 | * {@link ThreadLocal} and {@link SoftReference}, for efficient reuse of |
| 107 | * underlying input/output buffers. |
| 108 | * This usually makes sense on normal J2SE/J2EE server-side processing; |
| 109 | * but may not make sense on platforms where {@link SoftReference} handling |
| 110 | * is broken (like Android), or if there are retention issues due to |
| 111 | * {@link ThreadLocal} (see |
| 112 | * <a href="https://github.com/FasterXML/jackson-core/issues/189">Issue #189</a> |
| 113 | * for a possible case) |
| 114 | * |
| 115 | * @since 2.6 |
| 116 | */ |
| 117 | USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING(true) |
| 118 | |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 119 | ; |
| 120 | |
| 121 | /** |
| 122 | * Whether feature is enabled or disabled by default. |
| 123 | */ |
| 124 | private final boolean _defaultState; |
| 125 | |
| 126 | /** |
| 127 | * Method that calculates bit set (flags) of all features that |
| 128 | * are enabled by default. |
| 129 | */ |
Tatu Saloranta | e15b9a8 | 2014-01-24 21:01:04 -0800 | [diff] [blame] | 130 | public static int collectDefaults() { |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 131 | int flags = 0; |
| 132 | for (Feature f : values()) { |
Tatu Saloranta | e15b9a8 | 2014-01-24 21:01:04 -0800 | [diff] [blame] | 133 | if (f.enabledByDefault()) { flags |= f.getMask(); } |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 134 | } |
| 135 | return flags; |
| 136 | } |
| 137 | |
Tatu Saloranta | e15b9a8 | 2014-01-24 21:01:04 -0800 | [diff] [blame] | 138 | private Feature(boolean defaultState) { _defaultState = defaultState; } |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 139 | |
| 140 | public boolean enabledByDefault() { return _defaultState; } |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 141 | public boolean enabledIn(int flags) { return (flags & getMask()) != 0; } |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 142 | public int getMask() { return (1 << ordinal()); } |
Tatu Saloranta | 7b796a8 | 2013-04-27 10:18:30 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | /********************************************************** |
| 147 | /* Constants |
| 148 | /********************************************************** |
| 149 | */ |
| 150 | |
| 151 | /** |
| 152 | * Name used to identify JSON format |
| 153 | * (and returned by {@link #getFormatName()} |
| 154 | */ |
| 155 | public final static String FORMAT_NAME_JSON = "JSON"; |
| 156 | |
| 157 | /** |
| 158 | * Bitfield (set of flags) of all factory features that are enabled by default. |
| 159 | */ |
| 160 | protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults(); |
| 161 | |
| 162 | /** |
| 163 | * Bitfield (set of flags) of all parser features that are enabled |
| 164 | * by default. |
| 165 | */ |
| 166 | protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults(); |
| 167 | |
| 168 | /** |
| 169 | * Bitfield (set of flags) of all generator features that are enabled |
| 170 | * by default. |
| 171 | */ |
| 172 | protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults(); |
| 173 | |
| 174 | private final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR; |
| 175 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 176 | /* |
| 177 | /********************************************************** |
| 178 | /* Buffer, symbol table management |
| 179 | /********************************************************** |
| 180 | */ |
| 181 | |
| 182 | /** |
Tatu Saloranta | 10c3ec8 | 2012-09-05 19:38:49 -0700 | [diff] [blame] | 183 | * This <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference} |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 184 | * to a {@link BufferRecycler} used to provide a low-cost |
| 185 | * buffer recycling between reader and writer instances. |
| 186 | */ |
| 187 | final protected static ThreadLocal<SoftReference<BufferRecycler>> _recyclerRef |
| 188 | = new ThreadLocal<SoftReference<BufferRecycler>>(); |
| 189 | |
| 190 | /** |
| 191 | * Each factory comes equipped with a shared root symbol table. |
| 192 | * It should not be linked back to the original blueprint, to |
| 193 | * avoid contents from leaking between factories. |
| 194 | */ |
Tatu Saloranta | 95f76a4 | 2012-10-05 13:04:23 -0700 | [diff] [blame] | 195 | protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot(); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Alternative to the basic symbol table, some stream-based |
| 199 | * parsers use different name canonicalization method. |
| 200 | *<p> |
| 201 | * TODO: should clean up this; looks messy having 2 alternatives |
| 202 | * with not very clear differences. |
Tatu Saloranta | dccffbe | 2015-02-05 21:17:57 -0800 | [diff] [blame] | 203 | * |
| 204 | * @since 2.6.0 |
| 205 | */ |
| 206 | protected final transient ByteQuadsCanonicalizer _byteSymbolCanonicalizer = ByteQuadsCanonicalizer.createRoot(); |
| 207 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 208 | /* |
| 209 | /********************************************************** |
| 210 | /* Configuration |
| 211 | /********************************************************** |
| 212 | */ |
| 213 | |
| 214 | /** |
| 215 | * Object that implements conversion functionality between |
| 216 | * Java objects and JSON content. For base JsonFactory implementation |
| 217 | * usually not set by default, but can be explicitly set. |
| 218 | * Sub-classes (like @link org.codehaus.jackson.map.MappingJsonFactory} |
| 219 | * usually provide an implementation. |
| 220 | */ |
| 221 | protected ObjectCodec _objectCodec; |
| 222 | |
| 223 | /** |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 224 | * Currently enabled factory features. |
| 225 | */ |
| 226 | protected int _factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS; |
| 227 | |
| 228 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 229 | * Currently enabled parser features. |
| 230 | */ |
| 231 | protected int _parserFeatures = DEFAULT_PARSER_FEATURE_FLAGS; |
| 232 | |
| 233 | /** |
| 234 | * Currently enabled generator features. |
| 235 | */ |
| 236 | protected int _generatorFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS; |
| 237 | |
| 238 | /** |
| 239 | * Definition of custom character escapes to use for generators created |
| 240 | * by this factory, if any. If null, standard data format specific |
| 241 | * escapes are used. |
| 242 | */ |
| 243 | protected CharacterEscapes _characterEscapes; |
| 244 | |
| 245 | /** |
| 246 | * Optional helper object that may decorate input sources, to do |
| 247 | * additional processing on input during parsing. |
| 248 | */ |
| 249 | protected InputDecorator _inputDecorator; |
| 250 | |
| 251 | /** |
| 252 | * Optional helper object that may decorate output object, to do |
| 253 | * additional processing on output during content generation. |
| 254 | */ |
| 255 | protected OutputDecorator _outputDecorator; |
Tatu Saloranta | e6dfc69 | 2012-09-28 15:34:05 -0700 | [diff] [blame] | 256 | |
| 257 | /** |
| 258 | * Separator used between root-level values, if any; null indicates |
| 259 | * "do not add separator". |
| 260 | * Default separator is a single space character. |
| 261 | * |
| 262 | * @since 2.1 |
| 263 | */ |
| 264 | protected SerializableString _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | /********************************************************** |
| 268 | /* Construction |
| 269 | /********************************************************** |
| 270 | */ |
| 271 | |
| 272 | /** |
| 273 | * Default constructor used to create factory instances. |
| 274 | * Creation of a factory instance is a light-weight operation, |
| 275 | * but it is still a good idea to reuse limited number of |
| 276 | * factory instances (and quite often just a single instance): |
| 277 | * factories are used as context for storing some reused |
| 278 | * processing objects (such as symbol tables parsers use) |
| 279 | * and this reuse only works within context of a single |
| 280 | * factory instance. |
| 281 | */ |
Gonçalo Silva | 2bba5dd | 2014-01-25 17:06:21 +0000 | [diff] [blame] | 282 | public JsonFactory() { this(null); } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 283 | |
| 284 | public JsonFactory(ObjectCodec oc) { _objectCodec = oc; } |
| 285 | |
Tatu Saloranta | 378ecdc | 2012-08-04 16:27:27 -0700 | [diff] [blame] | 286 | /** |
Tatu Saloranta | 7b796a8 | 2013-04-27 10:18:30 -0700 | [diff] [blame] | 287 | * Constructor used when copy()ing a factory instance. |
| 288 | * |
| 289 | * @since 2.2.1 |
| 290 | */ |
| 291 | protected JsonFactory(JsonFactory src, ObjectCodec codec) |
| 292 | { |
| 293 | _objectCodec = null; |
| 294 | _factoryFeatures = src._factoryFeatures; |
| 295 | _parserFeatures = src._parserFeatures; |
| 296 | _generatorFeatures = src._generatorFeatures; |
| 297 | _characterEscapes = src._characterEscapes; |
| 298 | _inputDecorator = src._inputDecorator; |
| 299 | _outputDecorator = src._outputDecorator; |
| 300 | _rootValueSeparator = src._rootValueSeparator; |
| 301 | |
| 302 | /* 27-Apr-2013, tatu: How about symbol table; should we try to |
| 303 | * reuse shared symbol tables? Could be more efficient that way; |
| 304 | * although can slightly add to concurrency overhead. |
| 305 | */ |
| 306 | } |
| 307 | |
| 308 | /** |
Tatu Saloranta | 378ecdc | 2012-08-04 16:27:27 -0700 | [diff] [blame] | 309 | * Method for constructing a new {@link JsonFactory} that has |
| 310 | * the same settings as this instance, but is otherwise |
| 311 | * independent (i.e. nothing is actually shared, symbol tables |
| 312 | * are separate). |
| 313 | * Note that {@link ObjectCodec} reference is not copied but is |
| 314 | * set to null; caller typically needs to set it after calling |
Tatu Saloranta | 7b796a8 | 2013-04-27 10:18:30 -0700 | [diff] [blame] | 315 | * this method. Reason for this is that the codec is used for |
| 316 | * callbacks, and assumption is that there is strict 1-to-1 |
| 317 | * mapping between codec, factory. Caller has to, then, explicitly |
| 318 | * set codec after making the copy. |
Tatu Saloranta | 378ecdc | 2012-08-04 16:27:27 -0700 | [diff] [blame] | 319 | * |
| 320 | * @since 2.1 |
| 321 | */ |
| 322 | public JsonFactory copy() |
| 323 | { |
| 324 | _checkInvalidCopy(JsonFactory.class); |
Tatu Saloranta | 7b796a8 | 2013-04-27 10:18:30 -0700 | [diff] [blame] | 325 | // as per above, do clear ObjectCodec |
| 326 | return new JsonFactory(this, null); |
Tatu Saloranta | 378ecdc | 2012-08-04 16:27:27 -0700 | [diff] [blame] | 327 | } |
Tatu Saloranta | 7b796a8 | 2013-04-27 10:18:30 -0700 | [diff] [blame] | 328 | |
Tatu Saloranta | 378ecdc | 2012-08-04 16:27:27 -0700 | [diff] [blame] | 329 | /** |
| 330 | * @since 2.1 |
| 331 | * @param exp |
| 332 | */ |
| 333 | protected void _checkInvalidCopy(Class<?> exp) |
| 334 | { |
| 335 | if (getClass() != exp) { |
| 336 | throw new IllegalStateException("Failed copy(): "+getClass().getName() |
| 337 | +" (version: "+version()+") does not override copy(); it has to"); |
| 338 | } |
| 339 | } |
Tatu Saloranta | 95f76a4 | 2012-10-05 13:04:23 -0700 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | /********************************************************** |
| 343 | /* Serializable overrides |
| 344 | /********************************************************** |
| 345 | */ |
| 346 | |
| 347 | /** |
| 348 | * Method that we need to override to actually make restoration go |
| 349 | * through constructors etc. |
| 350 | * Also: must be overridden by sub-classes as well. |
| 351 | */ |
| 352 | protected Object readResolve() { |
Tatu Saloranta | f7741c5 | 2013-04-27 11:06:30 -0700 | [diff] [blame] | 353 | return new JsonFactory(this, _objectCodec); |
Tatu Saloranta | 95f76a4 | 2012-10-05 13:04:23 -0700 | [diff] [blame] | 354 | } |
Tatu Saloranta | a9e5c9f | 2013-08-28 19:39:59 -0700 | [diff] [blame] | 355 | |
| 356 | /* |
| 357 | /********************************************************** |
| 358 | /* Capability introspection |
| 359 | /********************************************************** |
| 360 | */ |
| 361 | |
| 362 | /** |
| 363 | * Introspection method that higher-level functionality may call |
| 364 | * to see whether underlying data format requires a stable ordering |
| 365 | * of object properties or not. |
| 366 | * This is usually used for determining |
| 367 | * whether to force a stable ordering (like alphabetic ordering by name) |
| 368 | * if no ordering if explicitly specified. |
| 369 | *<p> |
| 370 | * Default implementation returns <code>false</code> as JSON does NOT |
| 371 | * require stable ordering. Formats that require ordering include positional |
| 372 | * textual formats like <code>CSV</code>, and schema-based binary formats |
| 373 | * like <code>Avro</code>. |
| 374 | * |
| 375 | * @since 2.3 |
| 376 | */ |
Tatu | 475a99b | 2014-04-22 14:32:50 -0700 | [diff] [blame] | 377 | public boolean requiresPropertyOrdering() { return false; } |
Tatu Saloranta | b883544 | 2013-09-14 12:12:57 -0700 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * Introspection method that higher-level functionality may call |
| 381 | * to see whether underlying data format can read and write binary |
| 382 | * data natively; that is, embeded it as-is without using encodings |
| 383 | * such as Base64. |
| 384 | *<p> |
| 385 | * Default implementation returns <code>false</code> as JSON does not |
| 386 | * support native access: all binary content must use Base64 encoding. |
| 387 | * Most binary formats (like Smile and Avro) support native binary content. |
| 388 | * |
| 389 | * @since 2.3 |
| 390 | */ |
Tatu | 475a99b | 2014-04-22 14:32:50 -0700 | [diff] [blame] | 391 | public boolean canHandleBinaryNatively() { return false; } |
Tatu Saloranta | b883544 | 2013-09-14 12:12:57 -0700 | [diff] [blame] | 392 | |
Tatu | 475a99b | 2014-04-22 14:32:50 -0700 | [diff] [blame] | 393 | /** |
| 394 | * Introspection method that can be used by base factory to check |
| 395 | * whether access using <code>char[]</code> is something that actual |
| 396 | * parser implementations can take advantage of, over having to |
| 397 | * use {@link java.io.Reader}. Sub-types are expected to override |
| 398 | * definition; default implementation (suitable for JSON) alleges |
| 399 | * that optimization are possible; and thereby is likely to try |
| 400 | * to access {@link java.lang.String} content by first copying it into |
| 401 | * recyclable intermediate buffer. |
| 402 | * |
| 403 | * @since 2.4 |
| 404 | */ |
| 405 | public boolean canUseCharArrays() { return true; } |
Cowtowncoder | 2e262b9 | 2015-05-27 17:32:17 -0700 | [diff] [blame] | 406 | |
| 407 | /** |
| 408 | * Method for accessing kind of {@link FormatFeature} that a parser |
| 409 | * {@link JsonParser} produced by this factory would accept, if any; |
| 410 | * <code>null</code> returned if none. |
| 411 | * |
| 412 | * @since 2.6 |
| 413 | */ |
| 414 | public Class<? extends FormatFeature> getFormatReadFeatureType() { |
| 415 | return null; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Method for accessing kind of {@link FormatFeature} that a parser |
| 420 | * {@link JsonGenerator} produced by this factory would accept, if any; |
| 421 | * <code>null</code> returned if none. |
| 422 | * |
| 423 | * @since 2.6 |
| 424 | */ |
| 425 | public Class<? extends FormatFeature> getFormatWriteFeatureType() { |
| 426 | return null; |
| 427 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 428 | /* |
| 429 | /********************************************************** |
Cowtowncoder | 2e262b9 | 2015-05-27 17:32:17 -0700 | [diff] [blame] | 430 | /* Format detection functionality |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 431 | /********************************************************** |
| 432 | */ |
| 433 | |
| 434 | /** |
Tatu Saloranta | 1bdf026 | 2012-08-24 14:43:17 -0700 | [diff] [blame] | 435 | * Method that can be used to quickly check whether given schema |
| 436 | * is something that parsers and/or generators constructed by this |
| 437 | * factory could use. Note that this means possible use, at the level |
| 438 | * of data format (i.e. schema is for same data format as parsers and |
| 439 | * generators this factory constructs); individual schema instances |
| 440 | * may have further usage restrictions. |
| 441 | * |
| 442 | * @since 2.1 |
| 443 | */ |
| 444 | public boolean canUseSchema(FormatSchema schema) { |
Tatu Saloranta | 244ddd1 | 2016-05-14 15:11:04 -0700 | [diff] [blame] | 445 | if (schema == null){ |
| 446 | return false; |
| 447 | } |
Tatu Saloranta | 1bdf026 | 2012-08-24 14:43:17 -0700 | [diff] [blame] | 448 | String ourFormat = getFormatName(); |
| 449 | return (ourFormat != null) && ourFormat.equals(schema.getSchemaType()); |
| 450 | } |
| 451 | |
| 452 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 453 | * Method that returns short textual id identifying format |
| 454 | * this factory supports. |
| 455 | *<p> |
| 456 | * Note: sub-classes should override this method; default |
| 457 | * implementation will return null for all sub-classes |
| 458 | */ |
| 459 | public String getFormatName() |
| 460 | { |
| 461 | /* Somewhat nasty check: since we can't make this abstract |
| 462 | * (due to backwards compatibility concerns), need to prevent |
| 463 | * format name "leakage" |
| 464 | */ |
| 465 | if (getClass() == JsonFactory.class) { |
| 466 | return FORMAT_NAME_JSON; |
| 467 | } |
| 468 | return null; |
| 469 | } |
| 470 | |
Cowtowncoder | 2e262b9 | 2015-05-27 17:32:17 -0700 | [diff] [blame] | 471 | /** |
| 472 | * Convenience method for trying to determine whether input via given accessor |
| 473 | * is of format type supported by this factory. |
| 474 | */ |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 475 | public MatchStrength hasFormat(InputAccessor acc) throws IOException |
| 476 | { |
| 477 | // since we can't keep this abstract, only implement for "vanilla" instance |
| 478 | if (getClass() == JsonFactory.class) { |
| 479 | return hasJSONFormat(acc); |
| 480 | } |
| 481 | return null; |
| 482 | } |
| 483 | |
Tatu Saloranta | ad2df5f | 2012-08-22 20:55:49 -0700 | [diff] [blame] | 484 | /** |
| 485 | * Method that can be called to determine if a custom |
| 486 | * {@link ObjectCodec} is needed for binding data parsed |
| 487 | * using {@link JsonParser} constructed by this factory |
| 488 | * (which typically also implies the same for serialization |
| 489 | * with {@link JsonGenerator}). |
| 490 | * |
| 491 | * @return True if custom codec is needed with parsers and |
| 492 | * generators created by this factory; false if a general |
| 493 | * {@link ObjectCodec} is enough |
| 494 | * |
| 495 | * @since 2.1 |
| 496 | */ |
| 497 | public boolean requiresCustomCodec() { |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Helper method that can be called to determine if content accessed |
| 503 | * using given accessor seems to be JSON content. |
| 504 | */ |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 505 | protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException |
| 506 | { |
| 507 | return ByteSourceJsonBootstrapper.hasJSONFormat(acc); |
Tatu Saloranta | ad2df5f | 2012-08-22 20:55:49 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 510 | /* |
| 511 | /********************************************************** |
| 512 | /* Versioned |
| 513 | /********************************************************** |
| 514 | */ |
| 515 | |
Tatu Saloranta | 68d79dd | 2013-01-10 21:13:46 -0800 | [diff] [blame] | 516 | @Override |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 517 | public Version version() { |
Tatu Saloranta | 68d79dd | 2013-01-10 21:13:46 -0800 | [diff] [blame] | 518 | return PackageVersion.VERSION; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* |
| 522 | /********************************************************** |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 523 | /* Configuration, factory features |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 524 | /********************************************************** |
| 525 | */ |
| 526 | |
| 527 | /** |
| 528 | * Method for enabling or disabling specified parser feature |
| 529 | * (check {@link JsonParser.Feature} for list of features) |
| 530 | */ |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 531 | public final JsonFactory configure(JsonFactory.Feature f, boolean state) { |
| 532 | return state ? enable(f) : disable(f); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Method for enabling specified parser feature |
| 537 | * (check {@link JsonFactory.Feature} for list of features) |
| 538 | */ |
| 539 | public JsonFactory enable(JsonFactory.Feature f) { |
| 540 | _factoryFeatures |= f.getMask(); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 541 | return this; |
| 542 | } |
| 543 | |
| 544 | /** |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 545 | * Method for disabling specified parser features |
| 546 | * (check {@link JsonFactory.Feature} for list of features) |
| 547 | */ |
| 548 | public JsonFactory disable(JsonFactory.Feature f) { |
| 549 | _factoryFeatures &= ~f.getMask(); |
| 550 | return this; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Checked whether specified parser feature is enabled. |
| 555 | */ |
| 556 | public final boolean isEnabled(JsonFactory.Feature f) { |
| 557 | return (_factoryFeatures & f.getMask()) != 0; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | /********************************************************** |
| 562 | /* Configuration, parser configuration |
| 563 | /********************************************************** |
| 564 | */ |
| 565 | |
| 566 | /** |
| 567 | * Method for enabling or disabling specified parser feature |
| 568 | * (check {@link JsonParser.Feature} for list of features) |
| 569 | */ |
| 570 | public final JsonFactory configure(JsonParser.Feature f, boolean state) { |
| 571 | return state ? enable(f) : disable(f); |
| 572 | } |
| 573 | |
| 574 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 575 | * Method for enabling specified parser feature |
| 576 | * (check {@link JsonParser.Feature} for list of features) |
| 577 | */ |
| 578 | public JsonFactory enable(JsonParser.Feature f) { |
| 579 | _parserFeatures |= f.getMask(); |
| 580 | return this; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Method for disabling specified parser features |
| 585 | * (check {@link JsonParser.Feature} for list of features) |
| 586 | */ |
| 587 | public JsonFactory disable(JsonParser.Feature f) { |
| 588 | _parserFeatures &= ~f.getMask(); |
| 589 | return this; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Checked whether specified parser feature is enabled. |
| 594 | */ |
| 595 | public final boolean isEnabled(JsonParser.Feature f) { |
| 596 | return (_parserFeatures & f.getMask()) != 0; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Method for getting currently configured input decorator (if any; |
| 601 | * there is no default decorator). |
| 602 | */ |
| 603 | public InputDecorator getInputDecorator() { |
| 604 | return _inputDecorator; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Method for overriding currently configured input decorator |
| 609 | */ |
| 610 | public JsonFactory setInputDecorator(InputDecorator d) { |
| 611 | _inputDecorator = d; |
| 612 | return this; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | /********************************************************** |
| 617 | /* Configuration, generator settings |
| 618 | /********************************************************** |
| 619 | */ |
| 620 | |
| 621 | /** |
| 622 | * Method for enabling or disabling specified generator feature |
| 623 | * (check {@link JsonGenerator.Feature} for list of features) |
| 624 | */ |
| 625 | public final JsonFactory configure(JsonGenerator.Feature f, boolean state) { |
Tatu | 0735190 | 2012-01-19 13:12:13 -0800 | [diff] [blame] | 626 | return state ? enable(f) : disable(f); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | |
| 630 | /** |
| 631 | * Method for enabling specified generator features |
| 632 | * (check {@link JsonGenerator.Feature} for list of features) |
| 633 | */ |
| 634 | public JsonFactory enable(JsonGenerator.Feature f) { |
| 635 | _generatorFeatures |= f.getMask(); |
| 636 | return this; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Method for disabling specified generator feature |
| 641 | * (check {@link JsonGenerator.Feature} for list of features) |
| 642 | */ |
| 643 | public JsonFactory disable(JsonGenerator.Feature f) { |
| 644 | _generatorFeatures &= ~f.getMask(); |
| 645 | return this; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Check whether specified generator feature is enabled. |
| 650 | */ |
| 651 | public final boolean isEnabled(JsonGenerator.Feature f) { |
| 652 | return (_generatorFeatures & f.getMask()) != 0; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Method for accessing custom escapes factory uses for {@link JsonGenerator}s |
| 657 | * it creates. |
| 658 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 659 | public CharacterEscapes getCharacterEscapes() { return _characterEscapes; } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 660 | |
| 661 | /** |
| 662 | * Method for defining custom escapes factory uses for {@link JsonGenerator}s |
| 663 | * it creates. |
| 664 | */ |
| 665 | public JsonFactory setCharacterEscapes(CharacterEscapes esc) { |
| 666 | _characterEscapes = esc; |
| 667 | return this; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Method for getting currently configured output decorator (if any; |
| 672 | * there is no default decorator). |
| 673 | */ |
| 674 | public OutputDecorator getOutputDecorator() { |
| 675 | return _outputDecorator; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Method for overriding currently configured output decorator |
| 680 | */ |
| 681 | public JsonFactory setOutputDecorator(OutputDecorator d) { |
| 682 | _outputDecorator = d; |
| 683 | return this; |
| 684 | } |
Tatu Saloranta | e6dfc69 | 2012-09-28 15:34:05 -0700 | [diff] [blame] | 685 | |
| 686 | /** |
| 687 | * Method that allows overriding String used for separating root-level |
| 688 | * JSON values (default is single space character) |
| 689 | * |
| 690 | * @param sep Separator to use, if any; null means that no separator is |
| 691 | * automatically added |
| 692 | * |
| 693 | * @since 2.1 |
| 694 | */ |
| 695 | public JsonFactory setRootValueSeparator(String sep) { |
| 696 | _rootValueSeparator = (sep == null) ? null : new SerializedString(sep); |
| 697 | return this; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * @since 2.1 |
| 702 | */ |
| 703 | public String getRootValueSeparator() { |
| 704 | return (_rootValueSeparator == null) ? null : _rootValueSeparator.getValue(); |
| 705 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 706 | |
| 707 | /* |
| 708 | /********************************************************** |
| 709 | /* Configuration, other |
| 710 | /********************************************************** |
| 711 | */ |
| 712 | |
| 713 | /** |
| 714 | * Method for associating a {@link ObjectCodec} (typically |
Tatu Saloranta | d77350e | 2011-12-22 23:13:13 -0800 | [diff] [blame] | 715 | * a <code>com.fasterxml.jackson.databind.ObjectMapper</code>) |
| 716 | * with this factory (and more importantly, parsers and generators |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 717 | * it constructs). This is needed to use data-binding methods |
| 718 | * of {@link JsonParser} and {@link JsonGenerator} instances. |
| 719 | */ |
| 720 | public JsonFactory setCodec(ObjectCodec oc) { |
| 721 | _objectCodec = oc; |
| 722 | return this; |
| 723 | } |
| 724 | |
| 725 | public ObjectCodec getCodec() { return _objectCodec; } |
| 726 | |
| 727 | /* |
| 728 | /********************************************************** |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 729 | /* Parser factories (new ones, as per [Issue-25]) |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 730 | /********************************************************** |
| 731 | */ |
| 732 | |
| 733 | /** |
| 734 | * Method for constructing JSON parser instance to parse |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 735 | * contents of specified file. |
| 736 | * |
| 737 | *<p> |
| 738 | * Encoding is auto-detected from contents according to JSON |
| 739 | * specification recommended mechanism. Json specification |
| 740 | * supports only UTF-8, UTF-16 and UTF-32 as valid encodings, |
| 741 | * so auto-detection implemented only for this charsets. |
| 742 | * For other charsets use {@link #createParser(java.io.Reader)}. |
| 743 | * |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 744 | *<p> |
| 745 | * Underlying input stream (needed for reading contents) |
| 746 | * will be <b>owned</b> (and managed, i.e. closed as need be) by |
| 747 | * the parser, since caller has no access to it. |
| 748 | * |
| 749 | * @param f File that contains JSON content to parse |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 750 | * |
| 751 | * @since 2.1 |
| 752 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 753 | public JsonParser createParser(File f) throws IOException, JsonParseException { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 754 | // true, since we create InputStream from File |
| 755 | IOContext ctxt = _createContext(f, true); |
| 756 | InputStream in = new FileInputStream(f); |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 757 | return _createParser(_decorate(in, ctxt), ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Method for constructing JSON parser instance to parse |
| 762 | * contents of resource reference by given URL. |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 763 | * |
| 764 | *<p> |
| 765 | * Encoding is auto-detected from contents according to JSON |
| 766 | * specification recommended mechanism. Json specification |
| 767 | * supports only UTF-8, UTF-16 and UTF-32 as valid encodings, |
| 768 | * so auto-detection implemented only for this charsets. |
| 769 | * For other charsets use {@link #createParser(java.io.Reader)}. |
| 770 | * |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 771 | *<p> |
| 772 | * Underlying input stream (needed for reading contents) |
| 773 | * will be <b>owned</b> (and managed, i.e. closed as need be) by |
| 774 | * the parser, since caller has no access to it. |
| 775 | * |
| 776 | * @param url URL pointing to resource that contains JSON content to parse |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 777 | * |
| 778 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 779 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 780 | public JsonParser createParser(URL url) throws IOException, JsonParseException { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 781 | // true, since we create InputStream from URL |
| 782 | IOContext ctxt = _createContext(url, true); |
| 783 | InputStream in = _optimizedStreamFromURL(url); |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 784 | return _createParser(_decorate(in, ctxt), ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Method for constructing JSON parser instance to parse |
| 789 | * the contents accessed via specified input stream. |
| 790 | *<p> |
| 791 | * The input stream will <b>not be owned</b> by |
| 792 | * the parser, it will still be managed (i.e. closed if |
| 793 | * end-of-stream is reacher, or parser close method called) |
| 794 | * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE} |
| 795 | * is enabled. |
| 796 | *<p> |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 797 | * |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 798 | * Note: no encoding argument is taken since it can always be |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 799 | * auto-detected as suggested by JSON RFC. Json specification |
| 800 | * supports only UTF-8, UTF-16 and UTF-32 as valid encodings, |
| 801 | * so auto-detection implemented only for this charsets. |
| 802 | * For other charsets use {@link #createParser(java.io.Reader)}. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 803 | * |
| 804 | * @param in InputStream to use for reading JSON content to parse |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 805 | * |
| 806 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 807 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 808 | public JsonParser createParser(InputStream in) throws IOException, JsonParseException { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 809 | IOContext ctxt = _createContext(in, false); |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 810 | return _createParser(_decorate(in, ctxt), ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 811 | } |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 812 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 813 | /** |
| 814 | * Method for constructing parser for parsing |
| 815 | * the contents accessed via specified Reader. |
| 816 | <p> |
| 817 | * The read stream will <b>not be owned</b> by |
| 818 | * the parser, it will still be managed (i.e. closed if |
| 819 | * end-of-stream is reacher, or parser close method called) |
| 820 | * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE} |
| 821 | * is enabled. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 822 | * |
| 823 | * @param r Reader to use for reading JSON content to parse |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 824 | * |
| 825 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 826 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 827 | public JsonParser createParser(Reader r) throws IOException, JsonParseException { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 828 | // false -> we do NOT own Reader (did not create it) |
| 829 | IOContext ctxt = _createContext(r, false); |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 830 | return _createParser(_decorate(r, ctxt), ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Method for constructing parser for parsing |
| 835 | * the contents of given byte array. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 836 | * |
| 837 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 838 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 839 | public JsonParser createParser(byte[] data) throws IOException, JsonParseException { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 840 | IOContext ctxt = _createContext(data, true); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 841 | if (_inputDecorator != null) { |
| 842 | InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length); |
| 843 | if (in != null) { |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 844 | return _createParser(in, ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 845 | } |
| 846 | } |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 847 | return _createParser(data, 0, data.length, ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Method for constructing parser for parsing |
| 852 | * the contents of given byte array. |
| 853 | * |
| 854 | * @param data Buffer that contains data to parse |
| 855 | * @param offset Offset of the first data byte within buffer |
| 856 | * @param len Length of contents to parse within buffer |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 857 | * |
| 858 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 859 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 860 | public JsonParser createParser(byte[] data, int offset, int len) throws IOException, JsonParseException { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 861 | IOContext ctxt = _createContext(data, true); |
| 862 | // [JACKSON-512]: allow wrapping with InputDecorator |
| 863 | if (_inputDecorator != null) { |
| 864 | InputStream in = _inputDecorator.decorate(ctxt, data, offset, len); |
| 865 | if (in != null) { |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 866 | return _createParser(in, ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 867 | } |
| 868 | } |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 869 | return _createParser(data, offset, len, ctxt); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Method for constructing parser for parsing |
| 874 | * contents of given String. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 875 | * |
| 876 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 877 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 878 | public JsonParser createParser(String content) throws IOException, JsonParseException { |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 879 | final int strLen = content.length(); |
| 880 | // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char) |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 881 | if ((_inputDecorator != null) || (strLen > 0x8000) || !canUseCharArrays()) { |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 882 | // easier to just wrap in a Reader than extend InputDecorator; or, if content |
| 883 | // is too long for us to copy it over |
| 884 | return createParser(new StringReader(content)); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 885 | } |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 886 | IOContext ctxt = _createContext(content, true); |
Tatu Saloranta | 84dc184 | 2014-04-18 21:07:57 -0700 | [diff] [blame] | 887 | char[] buf = ctxt.allocTokenBuffer(strLen); |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 888 | content.getChars(0, strLen, buf, 0); |
| 889 | return _createParser(buf, 0, strLen, ctxt, true); |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Method for constructing parser for parsing |
| 894 | * contents of given char array. |
| 895 | * |
| 896 | * @since 2.4 |
| 897 | */ |
| 898 | public JsonParser createParser(char[] content) throws IOException { |
| 899 | return createParser(content, 0, content.length); |
| 900 | } |
| 901 | |
| 902 | /** |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 903 | * Method for constructing parser for parsing contents of given char array. |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 904 | * |
| 905 | * @since 2.4 |
| 906 | */ |
| 907 | public JsonParser createParser(char[] content, int offset, int len) throws IOException { |
| 908 | if (_inputDecorator != null) { // easier to just wrap in a Reader than extend InputDecorator |
| 909 | return createParser(new CharArrayReader(content, offset, len)); |
| 910 | } |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 911 | return _createParser(content, offset, len, _createContext(content, true), |
| 912 | // important: buffer is NOT recyclable, as it's from caller |
| 913 | false); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 916 | /** |
| 917 | * @since 2.8 |
| 918 | */ |
| 919 | public JsonParser createParser(DataInput in) throws IOException { |
| 920 | IOContext ctxt = _createContext(in, false); |
| 921 | return _createParser(_decorate(in, ctxt), ctxt); |
| 922 | } |
| 923 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 924 | /* |
| 925 | /********************************************************** |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 926 | /* Parser factories (old ones, pre-2.2) |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 927 | /********************************************************** |
| 928 | */ |
| 929 | |
| 930 | /** |
| 931 | * Method for constructing JSON parser instance to parse |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 932 | * contents of specified file. |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 933 | *<p> |
| 934 | * Encoding is auto-detected from contents according to JSON |
| 935 | * specification recommended mechanism. Json specification |
| 936 | * supports only UTF-8, UTF-16 and UTF-32 as valid encodings, |
| 937 | * so auto-detection implemented only for this charsets. |
| 938 | * For other charsets use {@link #createParser(java.io.Reader)}. |
| 939 | * |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 940 | *<p> |
| 941 | * Underlying input stream (needed for reading contents) |
| 942 | * will be <b>owned</b> (and managed, i.e. closed as need be) by |
| 943 | * the parser, since caller has no access to it. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 944 | * |
| 945 | * @param f File that contains JSON content to parse |
| 946 | * |
| 947 | * @deprecated Since 2.2, use {@link #createParser(File)} instead. |
| 948 | */ |
| 949 | @Deprecated |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 950 | public JsonParser createJsonParser(File f) throws IOException, JsonParseException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 951 | return createParser(f); |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * Method for constructing JSON parser instance to parse |
| 956 | * contents of resource reference by given URL. |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 957 | * |
| 958 | *<p> |
| 959 | * Encoding is auto-detected from contents according to JSON |
| 960 | * specification recommended mechanism. Json specification |
| 961 | * supports only UTF-8, UTF-16 and UTF-32 as valid encodings, |
| 962 | * so auto-detection implemented only for this charsets. |
| 963 | * For other charsets use {@link #createParser(java.io.Reader)}. |
| 964 | * |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 965 | *<p> |
| 966 | * Underlying input stream (needed for reading contents) |
| 967 | * will be <b>owned</b> (and managed, i.e. closed as need be) by |
| 968 | * the parser, since caller has no access to it. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 969 | * |
| 970 | * @param url URL pointing to resource that contains JSON content to parse |
| 971 | * |
| 972 | * @deprecated Since 2.2, use {@link #createParser(URL)} instead. |
| 973 | */ |
| 974 | @Deprecated |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 975 | public JsonParser createJsonParser(URL url) throws IOException, JsonParseException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 976 | return createParser(url); |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * Method for constructing JSON parser instance to parse |
| 981 | * the contents accessed via specified input stream. |
| 982 | *<p> |
| 983 | * The input stream will <b>not be owned</b> by |
| 984 | * the parser, it will still be managed (i.e. closed if |
| 985 | * end-of-stream is reacher, or parser close method called) |
| 986 | * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE} |
| 987 | * is enabled. |
| 988 | *<p> |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 989 | * |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 990 | * Note: no encoding argument is taken since it can always be |
Dmitry Spikhalskiy | 5590fcf | 2015-11-29 20:27:14 +0300 | [diff] [blame] | 991 | * auto-detected as suggested by JSON RFC. Json specification |
| 992 | * supports only UTF-8, UTF-16 and UTF-32 as valid encodings, |
| 993 | * so auto-detection implemented only for this charsets. |
| 994 | * For other charsets use {@link #createParser(java.io.Reader)}. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 995 | * |
| 996 | * @param in InputStream to use for reading JSON content to parse |
| 997 | * |
| 998 | * @deprecated Since 2.2, use {@link #createParser(InputStream)} instead. |
| 999 | */ |
| 1000 | @Deprecated |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1001 | public JsonParser createJsonParser(InputStream in) throws IOException, JsonParseException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1002 | return createParser(in); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * Method for constructing parser for parsing |
| 1007 | * the contents accessed via specified Reader. |
| 1008 | <p> |
| 1009 | * The read stream will <b>not be owned</b> by |
| 1010 | * the parser, it will still be managed (i.e. closed if |
| 1011 | * end-of-stream is reacher, or parser close method called) |
| 1012 | * if (and only if) {@link com.fasterxml.jackson.core.JsonParser.Feature#AUTO_CLOSE_SOURCE} |
| 1013 | * is enabled. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1014 | * |
| 1015 | * @param r Reader to use for reading JSON content to parse |
| 1016 | * |
| 1017 | * @deprecated Since 2.2, use {@link #createParser(Reader)} instead. |
| 1018 | */ |
| 1019 | @Deprecated |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1020 | public JsonParser createJsonParser(Reader r) throws IOException, JsonParseException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1021 | return createParser(r); |
| 1022 | } |
| 1023 | |
| 1024 | /** |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1025 | * Method for constructing parser for parsing the contents of given byte array. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1026 | * |
| 1027 | * @deprecated Since 2.2, use {@link #createParser(byte[])} instead. |
| 1028 | */ |
| 1029 | @Deprecated |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1030 | public JsonParser createJsonParser(byte[] data) throws IOException, JsonParseException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1031 | return createParser(data); |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * Method for constructing parser for parsing |
| 1036 | * the contents of given byte array. |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1037 | * |
| 1038 | * @param data Buffer that contains data to parse |
| 1039 | * @param offset Offset of the first data byte within buffer |
| 1040 | * @param len Length of contents to parse within buffer |
| 1041 | * |
| 1042 | * @deprecated Since 2.2, use {@link #createParser(byte[],int,int)} instead. |
| 1043 | */ |
| 1044 | @Deprecated |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1045 | public JsonParser createJsonParser(byte[] data, int offset, int len) throws IOException, JsonParseException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1046 | return createParser(data, offset, len); |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * Method for constructing parser for parsing |
| 1051 | * contents of given String. |
| 1052 | * |
| 1053 | * @deprecated Since 2.2, use {@link #createParser(String)} instead. |
| 1054 | */ |
| 1055 | @Deprecated |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1056 | public JsonParser createJsonParser(String content) throws IOException, JsonParseException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1057 | return createParser(content); |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | /********************************************************** |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1062 | /* Generator factories, new (as per [Issue-25] |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1063 | /********************************************************** |
| 1064 | */ |
| 1065 | |
| 1066 | /** |
| 1067 | * Method for constructing JSON generator for writing JSON content |
| 1068 | * using specified output stream. |
| 1069 | * Encoding to use must be specified, and needs to be one of available |
| 1070 | * types (as per JSON specification). |
| 1071 | *<p> |
| 1072 | * Underlying stream <b>is NOT owned</b> by the generator constructed, |
| 1073 | * so that generator will NOT close the output stream when |
| 1074 | * {@link JsonGenerator#close} is called (unless auto-closing |
| 1075 | * feature, |
| 1076 | * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} |
| 1077 | * is enabled). |
| 1078 | * Using application needs to close it explicitly if this is the case. |
| 1079 | *<p> |
| 1080 | * Note: there are formats that use fixed encoding (like most binary data formats) |
| 1081 | * and that ignore passed in encoding. |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1082 | * |
| 1083 | * @param out OutputStream to use for writing JSON content |
| 1084 | * @param enc Character encoding to use |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1085 | * |
| 1086 | * @since 2.1 |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1087 | */ |
| 1088 | public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc) |
| 1089 | throws IOException |
| 1090 | { |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1091 | // false -> we won't manage the stream unless explicitly directed to |
| 1092 | IOContext ctxt = _createContext(out, false); |
| 1093 | ctxt.setEncoding(enc); |
| 1094 | if (enc == JsonEncoding.UTF8) { |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1095 | return _createUTF8Generator(_decorate(out, ctxt), ctxt); |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1096 | } |
| 1097 | Writer w = _createWriter(out, enc, ctxt); |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1098 | return _createGenerator(_decorate(w, ctxt), ctxt); |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /** |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1102 | * Convenience method for constructing generator that uses default |
| 1103 | * encoding of the format (UTF-8 for JSON and most other data formats). |
| 1104 | *<p> |
| 1105 | * Note: there are formats that use fixed encoding (like most binary data formats). |
| 1106 | * |
| 1107 | * @since 2.1 |
| 1108 | */ |
| 1109 | public JsonGenerator createGenerator(OutputStream out) throws IOException { |
| 1110 | return createGenerator(out, JsonEncoding.UTF8); |
| 1111 | } |
| 1112 | |
| 1113 | /** |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1114 | * Method for constructing JSON generator for writing JSON content |
| 1115 | * using specified Writer. |
| 1116 | *<p> |
| 1117 | * Underlying stream <b>is NOT owned</b> by the generator constructed, |
| 1118 | * so that generator will NOT close the Reader when |
| 1119 | * {@link JsonGenerator#close} is called (unless auto-closing |
| 1120 | * feature, |
| 1121 | * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled). |
| 1122 | * Using application needs to close it explicitly. |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1123 | * |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1124 | * @since 2.1 |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1125 | * |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1126 | * @param w Writer to use for writing JSON content |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1127 | */ |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1128 | public JsonGenerator createGenerator(Writer w) throws IOException { |
| 1129 | IOContext ctxt = _createContext(w, false); |
| 1130 | return _createGenerator(_decorate(w, ctxt), ctxt); |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * Method for constructing JSON generator for writing JSON content |
| 1135 | * to specified file, overwriting contents it might have (or creating |
| 1136 | * it if such file does not yet exist). |
| 1137 | * Encoding to use must be specified, and needs to be one of available |
| 1138 | * types (as per JSON specification). |
| 1139 | *<p> |
| 1140 | * Underlying stream <b>is owned</b> by the generator constructed, |
| 1141 | * i.e. generator will handle closing of file when |
| 1142 | * {@link JsonGenerator#close} is called. |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1143 | * |
| 1144 | * @param f File to write contents to |
| 1145 | * @param enc Character encoding to use |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1146 | * |
| 1147 | * @since 2.1 |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1148 | */ |
Tatu Saloranta | b0a2e19 | 2014-04-10 09:36:07 -0700 | [diff] [blame] | 1149 | public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1150 | { |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1151 | OutputStream out = new FileOutputStream(f); |
| 1152 | // true -> yes, we have to manage the stream since we created it |
| 1153 | IOContext ctxt = _createContext(out, true); |
| 1154 | ctxt.setEncoding(enc); |
| 1155 | if (enc == JsonEncoding.UTF8) { |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1156 | return _createUTF8Generator(_decorate(out, ctxt), ctxt); |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1157 | } |
| 1158 | Writer w = _createWriter(out, enc, ctxt); |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1159 | return _createGenerator(_decorate(w, ctxt), ctxt); |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Tatu Saloranta | 38b3ef1 | 2016-05-03 18:08:31 -0700 | [diff] [blame] | 1162 | /** |
| 1163 | * Method for constructing generator for writing content using specified |
| 1164 | * {@link DataOutput} instance. |
| 1165 | * |
| 1166 | * @since 2.8 |
| 1167 | */ |
| 1168 | public JsonGenerator createGenerator(DataOutput out, JsonEncoding enc) throws IOException { |
| 1169 | return createGenerator(_createDataOutputWrapper(out), enc); |
| 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * Convenience method for constructing generator that uses default |
| 1174 | * encoding of the format (UTF-8 for JSON and most other data formats). |
| 1175 | *<p> |
| 1176 | * Note: there are formats that use fixed encoding (like most binary data formats). |
| 1177 | * |
| 1178 | * @since 2.8 |
| 1179 | */ |
| 1180 | public JsonGenerator createGenerator(DataOutput out) throws IOException { |
| 1181 | return createGenerator(_createDataOutputWrapper(out), JsonEncoding.UTF8); |
| 1182 | } |
| 1183 | |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1184 | /* |
| 1185 | /********************************************************** |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 1186 | /* Generator factories, old (pre-2.2) |
Tatu Saloranta | feaabd1 | 2012-07-22 23:03:41 -0700 | [diff] [blame] | 1187 | /********************************************************** |
| 1188 | */ |
| 1189 | |
| 1190 | /** |
| 1191 | * Method for constructing JSON generator for writing JSON content |
| 1192 | * using specified output stream. |
| 1193 | * Encoding to use must be specified, and needs to be one of available |
| 1194 | * types (as per JSON specification). |
| 1195 | *<p> |
| 1196 | * Underlying stream <b>is NOT owned</b> by the generator constructed, |
| 1197 | * so that generator will NOT close the output stream when |
| 1198 | * {@link JsonGenerator#close} is called (unless auto-closing |
| 1199 | * feature, |
| 1200 | * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} |
| 1201 | * is enabled). |
| 1202 | * Using application needs to close it explicitly if this is the case. |
| 1203 | *<p> |
| 1204 | * Note: there are formats that use fixed encoding (like most binary data formats) |
| 1205 | * and that ignore passed in encoding. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1206 | * |
| 1207 | * @param out OutputStream to use for writing JSON content |
| 1208 | * @param enc Character encoding to use |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1209 | * |
| 1210 | * @deprecated Since 2.2, use {@link #createGenerator(OutputStream, JsonEncoding)} instead. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1211 | */ |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1212 | @Deprecated |
Tatu Saloranta | b0a2e19 | 2014-04-10 09:36:07 -0700 | [diff] [blame] | 1213 | public JsonGenerator createJsonGenerator(OutputStream out, JsonEncoding enc) throws IOException { |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1214 | return createGenerator(out, enc); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * Method for constructing JSON generator for writing JSON content |
| 1219 | * using specified Writer. |
| 1220 | *<p> |
| 1221 | * Underlying stream <b>is NOT owned</b> by the generator constructed, |
| 1222 | * so that generator will NOT close the Reader when |
| 1223 | * {@link JsonGenerator#close} is called (unless auto-closing |
| 1224 | * feature, |
| 1225 | * {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is enabled). |
| 1226 | * Using application needs to close it explicitly. |
| 1227 | * |
| 1228 | * @param out Writer to use for writing JSON content |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1229 | * |
| 1230 | * @deprecated Since 2.2, use {@link #createGenerator(Writer)} instead. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1231 | */ |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1232 | @Deprecated |
Tatu Saloranta | b0a2e19 | 2014-04-10 09:36:07 -0700 | [diff] [blame] | 1233 | public JsonGenerator createJsonGenerator(Writer out) throws IOException { |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1234 | return createGenerator(out); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * Convenience method for constructing generator that uses default |
| 1239 | * encoding of the format (UTF-8 for JSON and most other data formats). |
| 1240 | *<p> |
| 1241 | * Note: there are formats that use fixed encoding (like most binary data formats). |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1242 | * |
| 1243 | * @deprecated Since 2.2, use {@link #createGenerator(OutputStream)} instead. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1244 | */ |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1245 | @Deprecated |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1246 | public JsonGenerator createJsonGenerator(OutputStream out) throws IOException { |
Tatu Saloranta | 4ab04ce | 2012-11-15 20:42:16 -0800 | [diff] [blame] | 1247 | return createGenerator(out, JsonEncoding.UTF8); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1248 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1249 | |
| 1250 | /* |
| 1251 | /********************************************************** |
| 1252 | /* Factory methods used by factory for creating parser instances, |
| 1253 | /* overridable by sub-classes |
| 1254 | /********************************************************** |
| 1255 | */ |
| 1256 | |
| 1257 | /** |
| 1258 | * Overridable factory method that actually instantiates desired parser |
| 1259 | * given {@link InputStream} and context object. |
| 1260 | *<p> |
| 1261 | * This method is specifically designed to remain |
| 1262 | * compatible between minor versions so that sub-classes can count |
| 1263 | * on it being called as expected. That is, it is part of official |
| 1264 | * interface from sub-class perspective, although not a public |
| 1265 | * method available to users of factory implementations. |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 1266 | * |
| 1267 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1268 | */ |
Tatu Saloranta | b0a2e19 | 2014-04-10 09:36:07 -0700 | [diff] [blame] | 1269 | protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1270 | // As per [JACKSON-259], may want to fully disable canonicalization: |
| 1271 | return new ByteSourceJsonBootstrapper(ctxt, in).constructParser(_parserFeatures, |
Tatu Saloranta | dccffbe | 2015-02-05 21:17:57 -0800 | [diff] [blame] | 1272 | _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures); |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1276 | * Overridable factory method that actually instantiates parser |
| 1277 | * using given {@link Reader} object for reading content. |
| 1278 | *<p> |
| 1279 | * This method is specifically designed to remain |
| 1280 | * compatible between minor versions so that sub-classes can count |
| 1281 | * on it being called as expected. That is, it is part of official |
| 1282 | * interface from sub-class perspective, although not a public |
| 1283 | * method available to users of factory implementations. |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 1284 | * |
| 1285 | * @since 2.1 |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1286 | */ |
Tatu Saloranta | b0a2e19 | 2014-04-10 09:36:07 -0700 | [diff] [blame] | 1287 | protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1288 | return new ReaderBasedJsonParser(ctxt, _parserFeatures, r, _objectCodec, |
Tatu Saloranta | 0541b3b | 2014-05-23 20:20:46 -0700 | [diff] [blame] | 1289 | _rootCharSymbols.makeChild(_factoryFeatures)); |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1293 | * Overridable factory method that actually instantiates parser |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 1294 | * using given <code>char[]</code> object for accessing content. |
| 1295 | * |
| 1296 | * @since 2.4 |
| 1297 | */ |
| 1298 | protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt, |
| 1299 | boolean recyclable) throws IOException { |
| 1300 | return new ReaderBasedJsonParser(ctxt, _parserFeatures, null, _objectCodec, |
Tatu Saloranta | 0541b3b | 2014-05-23 20:20:46 -0700 | [diff] [blame] | 1301 | _rootCharSymbols.makeChild(_factoryFeatures), |
| 1302 | data, offset, offset+len, recyclable); |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Overridable factory method that actually instantiates parser |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1307 | * using given {@link Reader} object for reading content |
| 1308 | * passed as raw byte array. |
| 1309 | *<p> |
| 1310 | * This method is specifically designed to remain |
| 1311 | * compatible between minor versions so that sub-classes can count |
| 1312 | * on it being called as expected. That is, it is part of official |
| 1313 | * interface from sub-class perspective, although not a public |
| 1314 | * method available to users of factory implementations. |
| 1315 | */ |
Tatu | 64aa9d2 | 2014-04-18 14:07:38 -0700 | [diff] [blame] | 1316 | protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1317 | { |
Tatu Saloranta | 6819492 | 2012-11-15 20:34:29 -0800 | [diff] [blame] | 1318 | return new ByteSourceJsonBootstrapper(ctxt, data, offset, len).constructParser(_parserFeatures, |
Tatu Saloranta | dccffbe | 2015-02-05 21:17:57 -0800 | [diff] [blame] | 1319 | _objectCodec, _byteSymbolCanonicalizer, _rootCharSymbols, _factoryFeatures); |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 1322 | /** |
| 1323 | * @since 2.8 |
| 1324 | */ |
| 1325 | protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOException |
| 1326 | { |
| 1327 | // 13-May-2016, tatu: Need to take care not to accidentally create JSON parser for |
| 1328 | // non-JSON input. So, bit unclean but... |
| 1329 | String format = getFormatName(); |
Tatu Saloranta | cd5b6ff | 2016-07-21 14:38:42 -0700 | [diff] [blame] | 1330 | if (format != FORMAT_NAME_JSON) { // NOTE: only ensure override; full equality NOT needed |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 1331 | throw new UnsupportedOperationException(String.format( |
| 1332 | "InputData source not (yet?) support for this format (%s)", format)); |
| 1333 | } |
Tatu Saloranta | 155babc | 2016-05-17 16:13:19 -0700 | [diff] [blame] | 1334 | // Also: while we can't do full bootstrapping (due to read-ahead limitations), should |
| 1335 | // at least handle possible UTF-8 BOM |
| 1336 | int firstByte = ByteSourceJsonBootstrapper.skipUTF8BOM(input); |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 1337 | ByteQuadsCanonicalizer can = _byteSymbolCanonicalizer.makeChild(_factoryFeatures); |
| 1338 | return new UTF8DataInputJsonParser(ctxt, _parserFeatures, input, |
Tatu Saloranta | 155babc | 2016-05-17 16:13:19 -0700 | [diff] [blame] | 1339 | _objectCodec, can, firstByte); |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1342 | /* |
| 1343 | /********************************************************** |
| 1344 | /* Factory methods used by factory for creating generator instances, |
| 1345 | /* overridable by sub-classes |
| 1346 | /********************************************************** |
| 1347 | */ |
| 1348 | |
| 1349 | /** |
| 1350 | * Overridable factory method that actually instantiates generator for |
| 1351 | * given {@link Writer} and context object. |
| 1352 | *<p> |
| 1353 | * This method is specifically designed to remain |
| 1354 | * compatible between minor versions so that sub-classes can count |
| 1355 | * on it being called as expected. That is, it is part of official |
| 1356 | * interface from sub-class perspective, although not a public |
| 1357 | * method available to users of factory implementations. |
| 1358 | */ |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1359 | protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 1360 | { |
Tatu Saloranta | e6dfc69 | 2012-09-28 15:34:05 -0700 | [diff] [blame] | 1361 | WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt, |
| 1362 | _generatorFeatures, _objectCodec, out); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1363 | if (_characterEscapes != null) { |
| 1364 | gen.setCharacterEscapes(_characterEscapes); |
| 1365 | } |
Tatu Saloranta | e6dfc69 | 2012-09-28 15:34:05 -0700 | [diff] [blame] | 1366 | SerializableString rootSep = _rootValueSeparator; |
| 1367 | if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) { |
| 1368 | gen.setRootValueSeparator(rootSep); |
| 1369 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1370 | return gen; |
| 1371 | } |
| 1372 | |
| 1373 | /** |
| 1374 | * Overridable factory method that actually instantiates generator for |
| 1375 | * given {@link OutputStream} and context object, using UTF-8 encoding. |
| 1376 | *<p> |
| 1377 | * This method is specifically designed to remain |
| 1378 | * compatible between minor versions so that sub-classes can count |
| 1379 | * on it being called as expected. That is, it is part of official |
| 1380 | * interface from sub-class perspective, although not a public |
| 1381 | * method available to users of factory implementations. |
| 1382 | */ |
Tatu Saloranta | eeb2fbd | 2012-05-28 22:00:08 -0700 | [diff] [blame] | 1383 | protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException { |
Tatu Saloranta | e6dfc69 | 2012-09-28 15:34:05 -0700 | [diff] [blame] | 1384 | UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt, |
| 1385 | _generatorFeatures, _objectCodec, out); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1386 | if (_characterEscapes != null) { |
| 1387 | gen.setCharacterEscapes(_characterEscapes); |
| 1388 | } |
Tatu Saloranta | e6dfc69 | 2012-09-28 15:34:05 -0700 | [diff] [blame] | 1389 | SerializableString rootSep = _rootValueSeparator; |
| 1390 | if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) { |
| 1391 | gen.setRootValueSeparator(rootSep); |
| 1392 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1393 | return gen; |
| 1394 | } |
| 1395 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1396 | protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException |
| 1397 | { |
| 1398 | // note: this should not get called any more (caller checks, dispatches) |
| 1399 | if (enc == JsonEncoding.UTF8) { // We have optimized writer for UTF-8 |
| 1400 | return new UTF8Writer(ctxt, out); |
| 1401 | } |
| 1402 | // not optimal, but should do unless we really care about UTF-16/32 encoding speed |
| 1403 | return new OutputStreamWriter(out, enc.getJavaName()); |
| 1404 | } |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1405 | |
| 1406 | /* |
| 1407 | /********************************************************** |
| 1408 | /* Internal factory methods, decorator handling |
| 1409 | /********************************************************** |
| 1410 | */ |
| 1411 | |
| 1412 | /** |
| 1413 | * @since 2.4 |
| 1414 | */ |
| 1415 | protected final InputStream _decorate(InputStream in, IOContext ctxt) throws IOException { |
| 1416 | if (_inputDecorator != null) { |
| 1417 | InputStream in2 = _inputDecorator.decorate(ctxt, in); |
| 1418 | if (in2 != null) { |
| 1419 | return in2; |
| 1420 | } |
| 1421 | } |
| 1422 | return in; |
| 1423 | } |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 1424 | |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1425 | /** |
| 1426 | * @since 2.4 |
| 1427 | */ |
| 1428 | protected final Reader _decorate(Reader in, IOContext ctxt) throws IOException { |
| 1429 | if (_inputDecorator != null) { |
| 1430 | Reader in2 = _inputDecorator.decorate(ctxt, in); |
| 1431 | if (in2 != null) { |
| 1432 | return in2; |
| 1433 | } |
| 1434 | } |
| 1435 | return in; |
| 1436 | } |
| 1437 | |
| 1438 | /** |
Tatu Saloranta | d7cbb64 | 2016-05-13 13:23:04 -0700 | [diff] [blame] | 1439 | * @since 2.8 |
| 1440 | */ |
| 1441 | protected final DataInput _decorate(DataInput in, IOContext ctxt) throws IOException { |
| 1442 | if (_inputDecorator != null) { |
| 1443 | DataInput in2 = _inputDecorator.decorate(ctxt, in); |
| 1444 | if (in2 != null) { |
| 1445 | return in2; |
| 1446 | } |
| 1447 | } |
| 1448 | return in; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1452 | * @since 2.4 |
| 1453 | */ |
| 1454 | protected final OutputStream _decorate(OutputStream out, IOContext ctxt) throws IOException { |
| 1455 | if (_outputDecorator != null) { |
| 1456 | OutputStream out2 = _outputDecorator.decorate(ctxt, out); |
| 1457 | if (out2 != null) { |
| 1458 | return out2; |
| 1459 | } |
| 1460 | } |
| 1461 | return out; |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * @since 2.4 |
| 1466 | */ |
| 1467 | protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException { |
| 1468 | if (_outputDecorator != null) { |
| 1469 | Writer out2 = _outputDecorator.decorate(ctxt, out); |
| 1470 | if (out2 != null) { |
| 1471 | return out2; |
| 1472 | } |
| 1473 | } |
| 1474 | return out; |
| 1475 | } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1476 | |
| 1477 | /* |
| 1478 | /********************************************************** |
| 1479 | /* Internal factory methods, other |
| 1480 | /********************************************************** |
| 1481 | */ |
| 1482 | |
| 1483 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1484 | * Method used by factory to create buffer recycler instances |
| 1485 | * for parsers and generators. |
| 1486 | *<p> |
| 1487 | * Note: only public to give access for <code>ObjectMapper</code> |
| 1488 | */ |
| 1489 | public BufferRecycler _getBufferRecycler() |
| 1490 | { |
Tatu Saloranta | 8891c0c | 2015-04-23 22:47:05 -0700 | [diff] [blame] | 1491 | /* 23-Apr-2015, tatu: Let's allow disabling of buffer recycling |
| 1492 | * scheme, for cases where it is considered harmful (possibly |
| 1493 | * on Android, for example) |
| 1494 | */ |
| 1495 | if (isEnabled(Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING)) { |
| 1496 | SoftReference<BufferRecycler> ref = _recyclerRef.get(); |
happyelf | 833de41 | 2016-07-23 11:10:21 +0800 | [diff] [blame^] | 1497 | BufferRecycler br = (ref == null) ? null : ref.get(); |
Tatu Saloranta | 8891c0c | 2015-04-23 22:47:05 -0700 | [diff] [blame] | 1498 | |
| 1499 | if (br == null) { |
| 1500 | br = new BufferRecycler(); |
| 1501 | _recyclerRef.set(new SoftReference<BufferRecycler>(br)); |
| 1502 | } |
happyelf | 833de41 | 2016-07-23 11:10:21 +0800 | [diff] [blame^] | 1503 | return br; |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1504 | } |
happyelf | 833de41 | 2016-07-23 11:10:21 +0800 | [diff] [blame^] | 1505 | return new BufferRecycler(); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1506 | } |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 1507 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1508 | /** |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1509 | * Overridable factory method that actually instantiates desired |
| 1510 | * context object. |
| 1511 | */ |
| 1512 | protected IOContext _createContext(Object srcRef, boolean resourceManaged) { |
| 1513 | return new IOContext(_getBufferRecycler(), srcRef, resourceManaged); |
| 1514 | } |
Tatu Saloranta | d302ae0 | 2015-12-27 21:53:15 -0800 | [diff] [blame] | 1515 | |
Tatu Saloranta | 896000f | 2014-04-19 12:53:40 -0700 | [diff] [blame] | 1516 | /** |
Tatu Saloranta | 38b3ef1 | 2016-05-03 18:08:31 -0700 | [diff] [blame] | 1517 | * @since 2.8 |
| 1518 | */ |
| 1519 | protected OutputStream _createDataOutputWrapper(DataOutput out) { |
| 1520 | return new DataOutputAsStream(out); |
| 1521 | } |
| 1522 | |
| 1523 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1524 | * Helper methods used for constructing an optimal stream for |
| 1525 | * parsers to use, when input is to be read from an URL. |
| 1526 | * This helps when reading file content via URL. |
| 1527 | */ |
Tatu Saloranta | efc2367 | 2013-12-30 19:09:39 -0800 | [diff] [blame] | 1528 | protected InputStream _optimizedStreamFromURL(URL url) throws IOException { |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1529 | if ("file".equals(url.getProtocol())) { |
| 1530 | /* Can not do this if the path refers |
| 1531 | * to a network drive on windows. This fixes the problem; |
| 1532 | * might not be needed on all platforms (NFS?), but should not |
| 1533 | * matter a lot: performance penalty of extra wrapping is more |
| 1534 | * relevant when accessing local file system. |
| 1535 | */ |
| 1536 | String host = url.getHost(); |
| 1537 | if (host == null || host.length() == 0) { |
Tatu Saloranta | 38b3ef1 | 2016-05-03 18:08:31 -0700 | [diff] [blame] | 1538 | // [core#48]: Let's try to avoid probs with URL encoded stuff |
Tatu Saloranta | 61d5bdd | 2013-01-11 19:02:01 -0800 | [diff] [blame] | 1539 | String path = url.getPath(); |
| 1540 | if (path.indexOf('%') < 0) { |
| 1541 | return new FileInputStream(url.getPath()); |
| 1542 | |
| 1543 | } |
| 1544 | // otherwise, let's fall through and let URL decoder do its magic |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | return url.openStream(); |
| 1548 | } |
| 1549 | } |