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