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