Class JsonReader
- All Implemented Interfaces:
AutoCloseable
,Iterable<MapValue>
As an iteration proceeds it may return any number of valid MapValue
instances before it encounters a problem in the input stream such as a bad
object or unexpected end of file. It is up to the application to handle
error conditions. There is no mechanism for restarting in the middle of
a file or string. If a JsonParseException
is thrown it will
contain the location of the exception in the parse stream to help isolate
problems.
An instance of this class can only be used to create a single iterator. This is to ensure that the Iterator can be closed explicitly by the caller. If an attempt is made to create an additional iterator IllegalArgumentException is thrown.
Instances of this class must be closed when done to free parser resources. It implements AutoCloseable to assist but in the event it is not called inside a try-with-resources block an explicit close() should be used.
-
Constructor Summary
ConstructorDescriptionJsonReader
(File file, JsonOptions options) Creates an iterator over JSON objects in the specified file.JsonReader
(InputStream input, JsonOptions options) Creates an iterator over JSON objects provided by the InputStream.JsonReader
(Reader input, JsonOptions options) Creates an iterator over JSON objects provided by the Reader.JsonReader
(String jsonString, JsonOptions options) Creates an iterator over JSON objects in a string. -
Method Summary
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
JsonReader
Creates an iterator over JSON objects in the specified file.- Parameters:
file
- the file to be usedoptions
- if desired, this can be null
-
JsonReader
Creates an iterator over JSON objects in a string.- Parameters:
jsonString
- the String to useoptions
- if desired, this can be null
-
JsonReader
Creates an iterator over JSON objects provided by the InputStream. The caller is responsible for closing the InputStream if necessary.- Parameters:
input
- the InputStream to useoptions
- if desired, this can be null
-
JsonReader
Creates an iterator over JSON objects provided by the Reader. The caller is responsible for closing the Reader if necessary.- Parameters:
input
- the Reader to useoptions
- if desired, this can be null
-
-
Method Details
-
iterator
Returns an Iterator<MapValue> over the source provided. This method can only be called once for a given instance of JsonReader. An additional call will result in IllegalArgumentException being thrown.- Specified by:
iterator
in interfaceIterable<MapValue>
- Throws:
IllegalArgumentException
- if called twice
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
getLocationOffset
public long getLocationOffset()Returns the byte offset into the JSON being processed if available or -1 if not- Returns:
- the offset or -1 if not available in the underlying data being parsed
-