Tatu Saloranta | 68bb83d | 2013-04-19 10:41:15 -0700 | [diff] [blame] | 1 | /* Jackson JSON-processor. |
| 2 | * |
| 3 | * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi |
| 4 | */ |
| 5 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 6 | package com.fasterxml.jackson.core; |
| 7 | |
| 8 | /** |
| 9 | * Enumeration for basic token types used for returning results |
| 10 | * of parsing JSON content. |
| 11 | */ |
| 12 | public enum JsonToken |
| 13 | { |
| 14 | /* Some notes on implementation: |
| 15 | * |
| 16 | * - Entries are to be ordered such that start/end array/object |
| 17 | * markers come first, then field name marker (if any), and |
| 18 | * finally scalar value tokens. This is assumed by some |
| 19 | * typing checks. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * NOT_AVAILABLE can be returned if {@link JsonParser} |
| 24 | * implementation can not currently return the requested |
| 25 | * token (usually next one), or even if any will be |
| 26 | * available, but that may be able to determine this in |
| 27 | * future. This is the case with non-blocking parsers -- |
| 28 | * they can not block to wait for more data to parse and |
| 29 | * must return something. |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 30 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 31 | NOT_AVAILABLE(null, JsonTokenId.ID_NOT_AVAILABLE), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * START_OBJECT is returned when encountering '{' |
| 35 | * which signals starting of an Object value. |
| 36 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 37 | START_OBJECT("{", JsonTokenId.ID_START_OBJECT), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Lukasz Kryger | 545d162 | 2013-02-20 22:55:02 +0000 | [diff] [blame] | 40 | * END_OBJECT is returned when encountering '}' |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 41 | * which signals ending of an Object value |
| 42 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 43 | END_OBJECT("}", JsonTokenId.ID_END_OBJECT), |
| 44 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 45 | /** |
Lukasz Kryger | 545d162 | 2013-02-20 22:55:02 +0000 | [diff] [blame] | 46 | * START_ARRAY is returned when encountering '[' |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 47 | * which signals starting of an Array value |
| 48 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 49 | START_ARRAY("[", JsonTokenId.ID_START_ARRAY), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
Lukasz Kryger | 545d162 | 2013-02-20 22:55:02 +0000 | [diff] [blame] | 52 | * END_ARRAY is returned when encountering ']' |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 53 | * which signals ending of an Array value |
| 54 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 55 | END_ARRAY("]", JsonTokenId.ID_END_ARRAY), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * FIELD_NAME is returned when a String token is encountered |
| 59 | * as a field name (same lexical value, different function) |
| 60 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 61 | FIELD_NAME(null, JsonTokenId.ID_FIELD_NAME), |
| 62 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Placeholder token returned when the input source has a concept |
| 65 | * of embedded Object that are not accessible as usual structure |
| 66 | * (of starting with {@link #START_OBJECT}, having values, ending with |
| 67 | * {@link #END_OBJECT}), but as "raw" objects. |
| 68 | *<p> |
| 69 | * Note: this token is never returned by regular JSON readers, but |
| 70 | * only by readers that expose other kinds of source (like |
Tatu Saloranta | 01ba053 | 2012-03-09 18:21:05 -0800 | [diff] [blame] | 71 | * <code>JsonNode</code>-based JSON trees, Maps, Lists and such). |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 72 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 73 | VALUE_EMBEDDED_OBJECT(null, JsonTokenId.ID_EMBEDDED_OBJECT), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * VALUE_STRING is returned when a String token is encountered |
| 77 | * in value context (array element, field value, or root-level |
| 78 | * stand-alone value) |
| 79 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 80 | VALUE_STRING(null, JsonTokenId.ID_STRING), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * VALUE_NUMBER_INT is returned when an integer numeric token is |
| 84 | * encountered in value context: that is, a number that does |
| 85 | * not have floating point or exponent marker in it (consists |
| 86 | * only of an optional sign, followed by one or more digits) |
| 87 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 88 | VALUE_NUMBER_INT(null, JsonTokenId.ID_NUMBER_INT), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * VALUE_NUMBER_INT is returned when a numeric token other |
| 92 | * that is not an integer is encountered: that is, a number that does |
| 93 | * have floating point or exponent marker in it, in addition |
| 94 | * to one or more digits. |
| 95 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 96 | VALUE_NUMBER_FLOAT(null, JsonTokenId.ID_NUMBER_FLOAT), |
| 97 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 98 | /** |
| 99 | * VALUE_TRUE is returned when encountering literal "true" in |
| 100 | * value context |
| 101 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 102 | VALUE_TRUE("true", JsonTokenId.ID_TRUE), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * VALUE_FALSE is returned when encountering literal "false" in |
| 106 | * value context |
| 107 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 108 | VALUE_FALSE("false", JsonTokenId.ID_FALSE), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * VALUE_NULL is returned when encountering literal "null" in |
| 112 | * value context |
| 113 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 114 | VALUE_NULL("null", JsonTokenId.ID_NULL), |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 115 | ; |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 116 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 117 | final String _serialized; |
| 118 | |
| 119 | final char[] _serializedChars; |
| 120 | |
| 121 | final byte[] _serializedBytes; |
| 122 | |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 123 | final int _id; |
| 124 | |
Tatu Saloranta | a663c69 | 2013-09-21 14:33:29 -0700 | [diff] [blame] | 125 | final boolean _isStructStart, _isStructEnd; |
| 126 | |
Tatu Saloranta | a663c69 | 2013-09-21 14:33:29 -0700 | [diff] [blame] | 127 | final boolean _isNumber; |
Tatu Saloranta | 8b9b4cd | 2013-09-22 18:08:47 -0700 | [diff] [blame] | 128 | |
| 129 | final boolean _isBoolean; |
| 130 | |
| 131 | final boolean _isScalar; |
| 132 | |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 133 | /** |
Tatu Saloranta | 10c3ec8 | 2012-09-05 19:38:49 -0700 | [diff] [blame] | 134 | * @param token representation for this token, if there is a |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 135 | * single static representation; null otherwise |
| 136 | */ |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 137 | JsonToken(String token, int id) |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 138 | { |
| 139 | if (token == null) { |
| 140 | _serialized = null; |
| 141 | _serializedChars = null; |
| 142 | _serializedBytes = null; |
| 143 | } else { |
| 144 | _serialized = token; |
| 145 | _serializedChars = token.toCharArray(); |
| 146 | // It's all in ascii, can just case... |
| 147 | int len = _serializedChars.length; |
| 148 | _serializedBytes = new byte[len]; |
| 149 | for (int i = 0; i < len; ++i) { |
| 150 | _serializedBytes[i] = (byte) _serializedChars[i]; |
| 151 | } |
| 152 | } |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 153 | _id = id; |
| 154 | |
| 155 | _isBoolean = (id == JsonTokenId.ID_FALSE || id == JsonTokenId.ID_TRUE); |
| 156 | _isNumber = (id == JsonTokenId.ID_NUMBER_INT || id == JsonTokenId.ID_NUMBER_FLOAT); |
Tatu Saloranta | 8b9b4cd | 2013-09-22 18:08:47 -0700 | [diff] [blame] | 157 | |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 158 | _isStructStart = (id == JsonTokenId.ID_START_OBJECT || id == JsonTokenId.ID_START_ARRAY); |
| 159 | _isStructEnd = (id == JsonTokenId.ID_END_OBJECT || id == JsonTokenId.ID_END_ARRAY); |
| 160 | |
| 161 | _isScalar = !_isStructStart && !_isStructEnd |
| 162 | && (id != JsonTokenId.ID_FIELD_NAME) |
| 163 | && (id != JsonTokenId.ID_NOT_AVAILABLE); |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 166 | public final int id() { return _id; } |
| 167 | |
| 168 | public final String asString() { return _serialized; } |
| 169 | public final char[] asCharArray() { return _serializedChars; } |
| 170 | public final byte[] asByteArray() { return _serializedBytes; } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 171 | |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 172 | public final boolean isNumeric() { return _isNumber; } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 173 | |
| 174 | /** |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 175 | * Accessor that is functionally equivalent to: |
| 176 | * <code> |
| 177 | * this == JsonToken.START_OBJECT || this == JsonToken.START_ARRAY |
| 178 | * </code> |
| 179 | * |
Tatu Saloranta | a663c69 | 2013-09-21 14:33:29 -0700 | [diff] [blame] | 180 | * @since 2.3 |
| 181 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 182 | public final boolean isStructStart() { return _isStructStart; } |
Tatu Saloranta | a663c69 | 2013-09-21 14:33:29 -0700 | [diff] [blame] | 183 | |
| 184 | /** |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 185 | * Accessor that is functionally equivalent to: |
| 186 | * <code> |
| 187 | * this == JsonToken.END_OBJECT || this == JsonToken.END_ARRAY |
| 188 | * </code> |
| 189 | * |
Tatu Saloranta | a663c69 | 2013-09-21 14:33:29 -0700 | [diff] [blame] | 190 | * @since 2.3 |
| 191 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 192 | public final boolean isStructEnd() { return _isStructEnd; } |
Tatu Saloranta | 066463a | 2013-09-22 19:57:25 -0700 | [diff] [blame] | 193 | |
Tatu Saloranta | a663c69 | 2013-09-21 14:33:29 -0700 | [diff] [blame] | 194 | /** |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 195 | * Method that can be used to check whether this token represents |
| 196 | * a valid non-structured value. This means all tokens other than |
| 197 | * Object/Array start/end markers all field names. |
| 198 | */ |
Tatu Saloranta | 32e4e91 | 2014-01-26 19:59:28 -0800 | [diff] [blame] | 199 | public final boolean isScalarValue() { return _isScalar; } |
| 200 | public final boolean isBoolean() { return _isBoolean; } |
Tatu Saloranta | f15531c | 2011-12-22 23:00:40 -0800 | [diff] [blame] | 201 | } |