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