Class JSONParserException
- java.lang.Object
-
- java.lang.Throwable
-
- java.lang.Exception
-
- com.github.tnakamot.json.parser.JSONParserException
-
- All Implemented Interfaces:
Serializable
public class JSONParserException extends Exception
Thrown when a JSON lexical analyzer fails to tokenize a given JSON text or the parser fails due to a syntax error.Instances of this class are immutable.
The error message can be customized by
JSONParserErrorHandlingOptions
. The message format is basically(name):(position): (message)
(name) is the short name of the source JSON text, which is typically a file name. If
JSONParserErrorHandlingOptions.showURI()
is true, the URI is shown instead.(position) shows the location where the error is detected within the source JSON text. If
JSONParserErrorHandlingOptions.showLineAndColumnNumber()
is false, the position in number of Unicode characters is shown (starting from zero). If it is true, the line number and the column (both starting from one) separated by ":" is shown.If
JSONParserErrorHandlingOptions.showErrorLine()
is true, a line which contains the error is shown additionally with a position marker.Here is an example of a JSON text which has an error:
{ "key": My name is JSON }
If
JSONParserErrorHandlingOptions.showURI()
is falseJSONParserErrorHandlingOptions.showLineAndColumnNumber()
is trueJSONParserErrorHandlingOptions.showErrorLine()
is false
the error message from the parser will be
hello.json:12: unknown token starting with 'M'
If
JSONParserErrorHandlingOptions.showURI()
is trueJSONParserErrorHandlingOptions.showLineAndColumnNumber()
is falseJSONParserErrorHandlingOptions.showErrorLine()
is true
the error message from the parser will be
/path/to/hello.json:12: unknown token starting with 'M' "key": My name is JSON ^
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description JSONParserException(@NotNull JSONText source, @Nullable StringLocation location, @NotNull JSONParserErrorHandlingOptions options, @NotNull String msg)
Instantiate this exception.JSONParserException(@NotNull JSONText source, @Nullable StringLocation begin, @Nullable StringLocation end, @NotNull JSONParserErrorHandlingOptions options, @NotNull String msg)
Instantiate this exception.JSONParserException(@NotNull JSONText source, @Nullable StringRange location, @NotNull JSONParserErrorHandlingOptions options, @NotNull String msg)
Instantiate this exception.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getMessage()
@Nullable StringRange
location()
Returns the location of the problem where the lexical analyzer failed to tokenize the given JSON text or the parse failed.JSONText
source()
-
Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
-
-
-
Constructor Detail
-
JSONParserException
public JSONParserException(@NotNull @NotNull JSONText source, @Nullable @Nullable StringRange location, @NotNull @NotNull JSONParserErrorHandlingOptions options, @NotNull @NotNull String msg)
Instantiate this exception.- Parameters:
source
- JSON text that has a problemlocation
- location of the problem within the source JSON textoptions
- options to change the error message format of this exceptionmsg
- error message which explains the problem
-
JSONParserException
public JSONParserException(@NotNull @NotNull JSONText source, @Nullable @Nullable StringLocation begin, @Nullable @Nullable StringLocation end, @NotNull @NotNull JSONParserErrorHandlingOptions options, @NotNull @NotNull String msg)
Instantiate this exception.- Parameters:
source
- JSON text that has a problembegin
- beginning location of the problem within the source JSON textend
- end location of the problem within the source JSON textoptions
- options to change the error message format of this exceptionmsg
- error message which explains the problem
-
JSONParserException
public JSONParserException(@NotNull @NotNull JSONText source, @Nullable @Nullable StringLocation location, @NotNull @NotNull JSONParserErrorHandlingOptions options, @NotNull @NotNull String msg)
Instantiate this exception.- Parameters:
source
- JSON text that has a problemlocation
- location of the problem within the source JSON textoptions
- options to change the error message format of this exceptionmsg
- error message which explains the problem
-
-
Method Detail
-
source
public JSONText source()
- Returns:
- the JSON text that cannot be tokenized properly.
-
location
@Nullable public @Nullable StringRange location()
Returns the location of the problem where the lexical analyzer failed to tokenize the given JSON text or the parse failed.- Returns:
- beginning location of the problem within the source JSON text
-
getMessage
public String getMessage()
- Overrides:
getMessage
in classThrowable
-
-