| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 942e8e8 commit 1f610fc
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,60 +71,67 @@ of this software and associated documentation files (the "Software"), to deal | |||
| 71 | 71 | */ | |
| 72 | 72 | public final class JSONStreamReader { | |
| 73 | 73 | ||
| 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 | + | ||
| 74 | 80 | /** | |
| 75 | 81 | * States of the internal state machine. Tokens returned from the | |
| 76 | 82 | * {@link JSONStreamReader#nextState() nextState()} loop are a subset of | |
| 77 | 83 | * these states. | |
| 78 | 84 | */ | |
| 79 | 85 | public enum ParseState { | |
| 80 | 86 | /** <em>Internal state</em> -- before the DOCUMENT state */ | |
| 81 | - INIT, | ||
| 87 | + INIT(INTERNAL), | ||
| 82 | 88 | /** Start a JSON document */ | |
| 83 | - DOCUMENT, | ||
| 89 | + DOCUMENT(NONE), | ||
| 84 | 90 | /** Start a JSON object */ | |
| 85 | - OBJECT, | ||
| 91 | + OBJECT(BEGIN_STRUCTURE), | ||
| 86 | 92 | /** End a JSON object */ | |
| 87 | - END_OBJECT, | ||
| 93 | + END_OBJECT(END_STRUCTURE), | ||
| 88 | 94 | /** Start a JSON array */ | |
| 89 | - ARRAY, | ||
| 95 | + ARRAY(BEGIN_STRUCTURE), | ||
| 90 | 96 | /** End a JSON array */ | |
| 91 | - END_ARRAY, | ||
| 97 | + END_ARRAY(END_STRUCTURE), | ||
| 92 | 98 | /** <em>Internal state</em> -- between a KEY and *_VALUE state */ | |
| 93 | - KEY_SEPARATOR, | ||
| 99 | + KEY_SEPARATOR(INTERNAL), | ||
| 94 | 100 | /** <em>Internal state</em> -- after a *_VALUE state */ | |
| 95 | - VALUE_SEPARATOR, | ||
| 101 | + VALUE_SEPARATOR(INTERNAL), | ||
| 96 | 102 | /** A key of a JSON object */ | |
| 97 | - KEY, | ||
| 103 | + KEY(NONE), | ||
| 98 | 104 | /** The {@code JSONObject.Null} value */ | |
| 99 | - NULL_VALUE, | ||
| 105 | + NULL_VALUE(VALUE), | ||
| 100 | 106 | /** The {@code Boolean.TRUE} or {@code Boolean.FALSE} value */ | |
| 101 | - BOOLEAN_VALUE, | ||
| 107 | + BOOLEAN_VALUE(VALUE), | ||
| 102 | 108 | /** A {@code Number} value */ | |
| 103 | - NUMBER_VALUE, | ||
| 109 | + NUMBER_VALUE(VALUE), | ||
| 104 | 110 | /** A {@code String} value */ | |
| 105 | - STRING_VALUE, | ||
| 111 | + STRING_VALUE(VALUE), | ||
| 106 | 112 | /** End a JSON document */ | |
| 107 | - END_DOCUMENT; | ||
| 113 | + END_DOCUMENT(NONE); | ||
| 114 | + | ||
| 115 | + private final int type; | ||
| 108 | 116 | ||
| 109 | - ParseState() { | ||
| 117 | + ParseState(int type) { | ||
| 118 | + this.type = type; | ||
| 110 | 119 | } | |
| 111 | 120 | ||
| 112 | 121 | public boolean isInternal() { | |
| 113 | - return this == INIT || this == KEY_SEPARATOR | ||
| 114 | - || this == VALUE_SEPARATOR; | ||
| 122 | + return this.type == INTERNAL; | ||
| 115 | 123 | } | |
| 116 | 124 | ||
| 117 | 125 | public boolean isValue() { | |
| 118 | - return this == NULL_VALUE || this == BOOLEAN_VALUE | ||
| 119 | - || this == NUMBER_VALUE || this == STRING_VALUE; | ||
| 126 | + return this.type == VALUE; | ||
| 120 | 127 | } | |
| 121 | 128 | ||
| 122 | 129 | public boolean isBeginStructure() { | |
| 123 | - return this == OBJECT || this == ARRAY; | ||
| 130 | + return this.type == BEGIN_STRUCTURE; | ||
| 124 | 131 | } | |
| 125 | 132 | ||
| 126 | 133 | public boolean isEndStructure() { | |
| 127 | - return this == END_OBJECT || this == END_ARRAY; | ||
| 134 | + return this.type == END_STRUCTURE; | ||
| 128 | 135 | } | |
| 129 | 136 | } | |
| 130 | 137 | ||
@@ -436,18 +443,9 @@ public String nextKey() throws JSONException { | |||
| 436 | 443 | * @return an Object of the type described above | |
| 437 | 444 | */ | |
| 438 | 445 | public Object nextValue() throws JSONException { | |
| 439 | - if (objectStack.isEmpty()) { | ||
| 446 | + if (objectStack.isEmpty() || !state.isValue()) { | ||
| 440 | 447 | throw new JSONParseException("Invalid state", lexer.parsePosition()); | |
| 441 | 448 | } | |
| 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 | - } | ||
| 451 | 449 | ||
| 452 | 450 | Token token = objectStack.pop(); | |
| 453 | 451 | switch(token) { | |
@@ -818,13 +816,8 @@ public ParseState currentState() { | |||
| 818 | 816 | * @return the closing ParseState of the object or array | |
| 819 | 817 | */ | |
| 820 | 818 | 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(); | ||
| 828 | 821 | } | |
| 829 | 822 | ||
| 830 | 823 | final int stackDepth = getStackDepth(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments