jackson-core

repository·3.x·Indexed 25 days ago

https://github.com/fasterxml/jackson-core

Low-level, incremental streaming parser and generator abstractions that serve as the foundational layer for the Jackson data processing ecosystem. Provides JsonParser and JsonGenerator for reading and writing JSON, with support for input/output processing limits and error reporting configuration in versions 2.15 and 2.16+.

Tokens
1.1K
Snippets
4
Records
9
Agent score
32%

What's inside jackson-core

  1. Read and write JSON using JsonParser and JsonGenerator

    3.x

    Incremental (streaming) processing is performed using two main abstractions:

    • JsonParser: Used for reading JSON data. Instances are constructed by a JsonFactory.
    • JsonGenerator: Used for writing JSON data. Instances are constructed by a JsonFactory.

    Note: While the core abstractions are not JSON-specific, the default implementation provided in this package handles the JSON format.

  2. Initialize a JsonFactory

    3.x

    The JsonFactory is a reusable and thread-safe instance used to create parsers and generators. Since Jackson 2.10, it is recommended to use the builder pattern for configuration.

    If you are already using the jackson-databind package, you can retrieve the factory from an existing ObjectMapper using tokenStreamFactory().

    // Builder-style since 2.10:
    JsonFactory factory = JsonFactory.builder()
    // configure, if necessary:
         .enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)
         .build();
    
    // Alternatively, from an ObjectMapper:
    JsonFactory factory = objectMapper.tokenStreamFactory();
  3. Install jackson-core via Maven

    3.x

    Depending on the version of Jackson you are using, the groupId and package names differ. Use the following dependencies for Maven projects:

    Jackson 3.x

    • Group ID: tools.jackson.core
    • Artifact ID: jackson-core

    Jackson 2.x

    • Group ID: com.fasterxml.jackson.core
    • Artifact ID: jackson-core
    <dependency> <!-- Jackson 3.x -->
        <groupId>tools.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency> <!-- Jackson 2.x -->
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.21.5</version>
    </dependency>
  4. Configure input and output processing limits

    3.x

    Starting with Jackson 2.15, you can configure limits to reduce the risk of excessive memory usage or processing costs. These limits are configured on a per-JsonFactory basis using StreamReadConstraints and StreamWriteConstraints.

    JsonFactory f = JsonFactory.builder()
      .streamReadConstraints(StreamReadConstraints.builder().maxDocumentLength(10_000_000L).build())
      .streamReadConstraints(StreamReadConstraints.builder().maxNumberLength(250).build())
      .streamWriteConstraints(StreamWriteConstraints.builder().maxNestingDepth(2000).build())
      .build();
  5. Configure error reporting limits

    3.x

    Starting with Jackson 2.16, you can configure how much content is included in error messages to prevent massive error strings.

    JsonFactory f = JsonFactory.builder()
     .errorReportConfiguration(ErrorReportConfiguration.builder()
       .maxErrorTokenLength(1004)
       .maxRawContentLength(2008)
       .build()
     ).build();
  6. Check JDK and Android compatibility

    3.x

    Ensure your environment meets the minimum requirements for the version of jackson-core you are using.

    JDK Requirements

    • 2.14 and above: JDK 8+
    • 3.0 and above: JDK 17+

    Android Requirements

    • 2.14 and above: Android SDK 26+
    • 3.0 and above: Android SDK 26+
  7. Reference: Input parsing limits

    3.x
    Configurable limits for input decoding (available in Jackson 2.15+). Lengths are expressed in bytes or characters depending on the source. Note that limits are not necessarily imposed with 100% accuracy (e.g., a value slightly over the limit might not trigger an exception).