1 /* Jackson JSON-processor. 2 * 3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi 4 */ 5 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. 30 */ 31 NOT_AVAILABLE(null, JsonTokenId.ID_NOT_AVAILABLE), 32 33 /** 34 * START_OBJECT is returned when encountering '{' 35 * which signals starting of an Object value. 36 */ 37 START_OBJECT("{", JsonTokenId.ID_START_OBJECT), 38 39 /** 40 * END_OBJECT is returned when encountering '}' 41 * which signals ending of an Object value 42 */ 43 END_OBJECT("}", JsonTokenId.ID_END_OBJECT), 44 45 /** 46 * START_ARRAY is returned when encountering '[' 47 * which signals starting of an Array value 48 */ 49 START_ARRAY("[", JsonTokenId.ID_START_ARRAY), 50 51 /** 52 * END_ARRAY is returned when encountering ']' 53 * which signals ending of an Array value 54 */ 55 END_ARRAY("]", JsonTokenId.ID_END_ARRAY), 56 57 /** 58 * FIELD_NAME is returned when a String token is encountered 59 * as a field name (same lexical value, different function) 60 */ 61 FIELD_NAME(null, JsonTokenId.ID_FIELD_NAME), 62 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 71 * <code>JsonNode</code>-based JSON trees, Maps, Lists and such). 72 */ 73 VALUE_EMBEDDED_OBJECT(null, JsonTokenId.ID_EMBEDDED_OBJECT), 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 */ 80 VALUE_STRING(null, JsonTokenId.ID_STRING), 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 * or, for binary formats, is indicated as integral number 88 * by internal representation). 89 */ 90 VALUE_NUMBER_INT(null, JsonTokenId.ID_NUMBER_INT), 91 92 /** 93 * VALUE_NUMBER_FLOAT is returned when a numeric token other 94 * than integer is encountered: that is, a number that does 95 * have floating point or exponent marker in it, in addition 96 * to one or more digits (or, for non-textual formats, 97 * has internal floating-point representation). 98 */ 99 VALUE_NUMBER_FLOAT(null, JsonTokenId.ID_NUMBER_FLOAT), 100 101 /** 102 * VALUE_TRUE is returned when encountering literal "true" in 103 * value context 104 */ 105 VALUE_TRUE("true", JsonTokenId.ID_TRUE), 106 107 /** 108 * VALUE_FALSE is returned when encountering literal "false" in 109 * value context 110 */ 111 VALUE_FALSE("false", JsonTokenId.ID_FALSE), 112 113 /** 114 * VALUE_NULL is returned when encountering literal "null" in 115 * value context 116 */ 117 VALUE_NULL("null", JsonTokenId.ID_NULL), 118 ; 119 120 final String _serialized; 121 122 final char[] _serializedChars; 123 124 final byte[] _serializedBytes; 125 126 final int _id; 127 128 final boolean _isStructStart, _isStructEnd; 129 130 final boolean _isNumber; 131 132 final boolean _isBoolean; 133 134 final boolean _isScalar; 135 136 /** 137 * @param token representation for this token, if there is a 138 * single static representation; null otherwise 139 */ JsonToken(String token, int id)140 JsonToken(String token, int id) 141 { 142 if (token == null) { 143 _serialized = null; 144 _serializedChars = null; 145 _serializedBytes = null; 146 } else { 147 _serialized = token; 148 _serializedChars = token.toCharArray(); 149 // It's all in ascii, can just case... 150 int len = _serializedChars.length; 151 _serializedBytes = new byte[len]; 152 for (int i = 0; i < len; ++i) { 153 _serializedBytes[i] = (byte) _serializedChars[i]; 154 } 155 } 156 _id = id; 157 158 _isBoolean = (id == JsonTokenId.ID_FALSE || id == JsonTokenId.ID_TRUE); 159 _isNumber = (id == JsonTokenId.ID_NUMBER_INT || id == JsonTokenId.ID_NUMBER_FLOAT); 160 161 _isStructStart = (id == JsonTokenId.ID_START_OBJECT || id == JsonTokenId.ID_START_ARRAY); 162 _isStructEnd = (id == JsonTokenId.ID_END_OBJECT || id == JsonTokenId.ID_END_ARRAY); 163 164 _isScalar = !_isStructStart && !_isStructEnd 165 && (id != JsonTokenId.ID_FIELD_NAME) 166 && (id != JsonTokenId.ID_NOT_AVAILABLE); 167 } 168 id()169 public final int id() { return _id; } 170 asString()171 public final String asString() { return _serialized; } asCharArray()172 public final char[] asCharArray() { return _serializedChars; } asByteArray()173 public final byte[] asByteArray() { return _serializedBytes; } 174 isNumeric()175 public final boolean isNumeric() { return _isNumber; } 176 177 /** 178 * Accessor that is functionally equivalent to: 179 * <code> 180 * this == JsonToken.START_OBJECT || this == JsonToken.START_ARRAY 181 * </code> 182 * 183 * @since 2.3 184 */ isStructStart()185 public final boolean isStructStart() { return _isStructStart; } 186 187 /** 188 * Accessor that is functionally equivalent to: 189 * <code> 190 * this == JsonToken.END_OBJECT || this == JsonToken.END_ARRAY 191 * </code> 192 * 193 * @since 2.3 194 */ isStructEnd()195 public final boolean isStructEnd() { return _isStructEnd; } 196 197 /** 198 * Method that can be used to check whether this token represents 199 * a valid non-structured value. This means all tokens other than 200 * Object/Array start/end markers all field names. 201 */ isScalarValue()202 public final boolean isScalarValue() { return _isScalar; } isBoolean()203 public final boolean isBoolean() { return _isBoolean; } 204 } 205