Package org.springframework.batch.item.json

# Class JsonItemReader<T>

java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
org.springframework.batch.item.json.JsonItemReader<T>

Type Parameters:
  `T` - the type of json objects to read

All Implemented Interfaces:
  `ResourceAwareItemReaderItemStream<T>`, `ItemReader<T>`, `ItemStream`, `ItemStreamReader<T>`

public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements ResourceAwareItemReaderItemStream<T>
`ItemStreamReader` implementation that reads Json objects from a `Resource` having the following format:

`[ { // JSON object }, { // JSON object } ]`

The implementation is not thread-safe.

Since:
  4.1
Author:
  Mahmoud Ben Hassine

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `JsonItemReader()`
  Create a new `JsonItemReader` instance.
  `JsonItemReader(org.springframework.core.io.Resource resource, JsonObjectReader<T> jsonObjectReader)`
  Create a new `JsonItemReader` instance.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `doClose()`
  Close the resources opened in `AbstractItemCountingItemStreamItemReader.doOpen()`.
  `protected void`
  `doOpen()`
  Open resources necessary to start reading input.
  `protected T`
  `doRead()`
  Read next item from input.
  `void`
  `setJsonObjectReader(JsonObjectReader<T> jsonObjectReader)`
  Set the `JsonObjectReader` to use to read and map Json fragments to domain objects.
  `void`
  `setResource(org.springframework.core.io.Resource resource)`
  `void`
  `setStrict(boolean strict)`
  In strict mode the reader will throw an exception on `AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)` if the input resource does not exist.

  ### Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader

  `close, getCurrentItemCount, isSaveState, jumpToItem, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, update`

  ### Methods inherited from class org.springframework.batch.item.ItemStreamSupport

  `getExecutionContextKey, setExecutionContextName, setName`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.springframework.batch.item.ItemReader

  `read`

  ### Methods inherited from interface org.springframework.batch.item.ItemStream

  `close, open, update`

- ## Constructor Details

  - ### JsonItemReader

    public JsonItemReader(org.springframework.core.io.Resource resource, JsonObjectReader<T> jsonObjectReader)
    Create a new `JsonItemReader` instance.
    Parameters:
      `resource` - the input json resource
      `jsonObjectReader` - the json object reader to use

  - ### JsonItemReader

    public JsonItemReader()
    Create a new `JsonItemReader` instance.

- ## Method Details

  - ### setJsonObjectReader

    public void setJsonObjectReader(JsonObjectReader<T> jsonObjectReader)
    Set the `JsonObjectReader` to use to read and map Json fragments to domain objects.
    Parameters:
      `jsonObjectReader` - the json object reader to use

  - ### setStrict

    public void setStrict(boolean strict)
    In strict mode the reader will throw an exception on `AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)` if the input resource does not exist.
    Parameters:
      `strict` - true by default

  - ### setResource

    public void setResource(org.springframework.core.io.Resource resource)
    Specified by:
      `setResource` in interface `ResourceAwareItemReaderItemStream<T>`

  - ### doRead

    @Nullable protected T doRead() throws Exception
    Description copied from class: `AbstractItemCountingItemStreamItemReader`
    Read next item from input.
    Specified by:
      `doRead` in class `AbstractItemCountingItemStreamItemReader<T>`
    Returns:
      an item or `null` if the data source is exhausted
    Throws:
      `Exception` - Allows subclasses to throw checked exceptions for interpretation by the framework

  - ### doOpen

    protected void doOpen() throws Exception
    Description copied from class: `AbstractItemCountingItemStreamItemReader`
    Open resources necessary to start reading input.
    Specified by:
      `doOpen` in class `AbstractItemCountingItemStreamItemReader<T>`
    Throws:
      `Exception` - Allows subclasses to throw checked exceptions for interpretation by the framework

  - ### doClose

    protected void doClose() throws Exception
    Description copied from class: `AbstractItemCountingItemStreamItemReader`
    Close the resources opened in `AbstractItemCountingItemStreamItemReader.doOpen()`.
    Specified by:
      `doClose` in class `AbstractItemCountingItemStreamItemReader<T>`
    Throws:
      `Exception` - Allows subclasses to throw checked exceptions for interpretation by the framework