FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ParseState gets more information about the type of token. · run2000/JSON-java@1f610fc · GitHub

Commit 1f610fc

Browse files
committed
ParseState gets more information about the type of token.
This makes the stream reader code easier in some cases. Also adds some meaningful information directly to the state enum that callers can use.
1 parent 942e8e8 commit 1f610fc

1 file changed

Lines changed: 31 additions & 38 deletions

File tree

‎stream/JSONStreamReader.java‎

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,60 +71,67 @@ of this software and associated documentation files (the "Software"), to deal
7171
*/
7272
public final class JSONStreamReader {
7373

74+
private static final int NONE = 0;
75+
private static final int INTERNAL = 1;
76+
private static final int VALUE = 2;
77+
private static final int BEGIN_STRUCTURE = 4;
78+
private static final int END_STRUCTURE = 8;
79+
7480
/**
7581
* States of the internal state machine. Tokens returned from the
7682
* {@link JSONStreamReader#nextState() nextState()} loop are a subset of
7783
* these states.
7884
*/
7985
public enum ParseState {
8086
/** <em>Internal state</em> -- before the DOCUMENT state */
81-
INIT,
87+
INIT(INTERNAL),
8288
/** Start a JSON document */
83-
DOCUMENT,
89+
DOCUMENT(NONE),
8490
/** Start a JSON object */
85-
OBJECT,
91+
OBJECT(BEGIN_STRUCTURE),
8692
/** End a JSON object */
87-
END_OBJECT,
93+
END_OBJECT(END_STRUCTURE),
8894
/** Start a JSON array */
89-
ARRAY,
95+
ARRAY(BEGIN_STRUCTURE),
9096
/** End a JSON array */
91-
END_ARRAY,
97+
END_ARRAY(END_STRUCTURE),
9298
/** <em>Internal state</em> -- between a KEY and *_VALUE state */
93-
KEY_SEPARATOR,
99+
KEY_SEPARATOR(INTERNAL),
94100
/** <em>Internal state</em> -- after a *_VALUE state */
95-
VALUE_SEPARATOR,
101+
VALUE_SEPARATOR(INTERNAL),
96102
/** A key of a JSON object */
97-
KEY,
103+
KEY(NONE),
98104
/** The {@code JSONObject.Null} value */
99-
NULL_VALUE,
105+
NULL_VALUE(VALUE),
100106
/** The {@code Boolean.TRUE} or {@code Boolean.FALSE} value */
101-
BOOLEAN_VALUE,
107+
BOOLEAN_VALUE(VALUE),
102108
/** A {@code Number} value */
103-
NUMBER_VALUE,
109+
NUMBER_VALUE(VALUE),
104110
/** A {@code String} value */
105-
STRING_VALUE,
111+
STRING_VALUE(VALUE),
106112
/** End a JSON document */
107-
END_DOCUMENT;
113+
END_DOCUMENT(NONE);
114+
115+
private final int type;
108116

109-
ParseState() {
117+
ParseState(int type) {
118+
this.type = type;
110119
}
111120

112121
public boolean isInternal() {
113-
return this == INIT || this == KEY_SEPARATOR
114-
|| this == VALUE_SEPARATOR;
122+
return this.type == INTERNAL;
115123
}
116124

117125
public boolean isValue() {
118-
return this == NULL_VALUE || this == BOOLEAN_VALUE
119-
|| this == NUMBER_VALUE || this == STRING_VALUE;
126+
return this.type == VALUE;
120127
}
121128

122129
public boolean isBeginStructure() {
123-
return this == OBJECT || this == ARRAY;
130+
return this.type == BEGIN_STRUCTURE;
124131
}
125132

126133
public boolean isEndStructure() {
127-
return this == END_OBJECT || this == END_ARRAY;
134+
return this.type == END_STRUCTURE;
128135
}
129136
}
130137

@@ -436,18 +443,9 @@ public String nextKey() throws JSONException {
436443
* @return an Object of the type described above
437444
*/
438445
public Object nextValue() throws JSONException {
439-
if (objectStack.isEmpty()) {
446+
if (objectStack.isEmpty() || !state.isValue()) {
440447
throw new JSONParseException("Invalid state", lexer.parsePosition());
441448
}
442-
switch(state) {
443-
case NULL_VALUE:
444-
case BOOLEAN_VALUE:
445-
case NUMBER_VALUE:
446-
case STRING_VALUE:
447-
break;
448-
default:
449-
throw new JSONParseException("Invalid state", lexer.parsePosition());
450-
}
451449

452450
Token token = objectStack.pop();
453451
switch(token) {
@@ -818,13 +816,8 @@ public ParseState currentState() {
818816
* @return the closing ParseState of the object or array
819817
*/
820818
public ParseState skipToEndStructure() throws JSONException {
821-
switch(state) {
822-
case NULL_VALUE:
823-
case BOOLEAN_VALUE:
824-
case NUMBER_VALUE:
825-
case STRING_VALUE:
826-
skipValue();
827-
break;
819+
if(state.isValue()) {
820+
skipValue();
828821
}
829822

830823
final int stackDepth = getStackDepth();

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL