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