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