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