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