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