1 package org.json.simple.parser;
4 * ParseException explains why and where the error occurs in source JSON text.
6 * @author FangYidong<fangyidong@yahoo.com.cn>
9 public class ParseException extends Exception {
10 private static final long serialVersionUID = -7880698968187728547L;
12 public static final int ERROR_UNEXPECTED_CHAR = 0;
13 public static final int ERROR_UNEXPECTED_TOKEN = 1;
14 public static final int ERROR_UNEXPECTED_EXCEPTION = 2;
16 private int errorType;
17 private Object unexpectedObject;
20 public ParseException(int errorType){
21 this(-1, errorType, null);
24 public ParseException(int errorType, Object unexpectedObject){
25 this(-1, errorType, unexpectedObject);
28 public ParseException(int position, int errorType, Object unexpectedObject){
29 this.position = position;
30 this.errorType = errorType;
31 this.unexpectedObject = unexpectedObject;
34 public int getErrorType() {
38 public void setErrorType(int errorType) {
39 this.errorType = errorType;
43 * @see org.json.simple.parser.JSONParser#getPosition()
45 * @return The character position (starting with 0) of the input where the error occurs.
47 public int getPosition() {
51 public void setPosition(int position) {
52 this.position = position;
56 * @see org.json.simple.parser.Yytoken
58 * @return One of the following base on the value of errorType:
59 * ERROR_UNEXPECTED_CHAR java.lang.Character
60 * ERROR_UNEXPECTED_TOKEN org.json.simple.parser.Yytoken
61 * ERROR_UNEXPECTED_EXCEPTION java.lang.Exception
63 public Object getUnexpectedObject() {
64 return unexpectedObject;
67 public void setUnexpectedObject(Object unexpectedObject) {
68 this.unexpectedObject = unexpectedObject;
71 public String getMessage() {
72 StringBuffer sb = new StringBuffer();
75 case ERROR_UNEXPECTED_CHAR:
76 sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position).append(".");
78 case ERROR_UNEXPECTED_TOKEN:
79 sb.append("Unexpected token ").append(unexpectedObject).append(" at position ").append(position).append(".");
81 case ERROR_UNEXPECTED_EXCEPTION:
82 sb.append("Unexpected exception at position ").append(position).append(": ").append(unexpectedObject);
85 sb.append("Unkown error at position ").append(position).append(".");