blob: 5ac418574f8ac458d3f93677777cbd6c128c17a3 [file] [log] [blame]
Tatu Saloranta68bb83d2013-04-19 10:41:15 -07001/* Jackson JSON-processor.
2 *
3 * Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
4 */
5
Tatu Salorantaf15531c2011-12-22 23:00:40 -08006package com.fasterxml.jackson.core;
7
8/**
9 * Enumeration for basic token types used for returning results
10 * of parsing JSON content.
11 */
12public 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 Salorantaf15531c2011-12-22 23:00:40 -080030 */
Tatu Saloranta066463a2013-09-22 19:57:25 -070031 NOT_AVAILABLE(null, JsonTokenId.ID_NOT_AVAILABLE),
Tatu Salorantaf15531c2011-12-22 23:00:40 -080032
33 /**
34 * START_OBJECT is returned when encountering '{'
35 * which signals starting of an Object value.
36 */
Tatu Saloranta066463a2013-09-22 19:57:25 -070037 START_OBJECT("{", JsonTokenId.ID_START_OBJECT),
Tatu Salorantaf15531c2011-12-22 23:00:40 -080038
39 /**
Lukasz Kryger545d1622013-02-20 22:55:02 +000040 * END_OBJECT is returned when encountering '}'
Tatu Salorantaf15531c2011-12-22 23:00:40 -080041 * which signals ending of an Object value
42 */
Tatu Saloranta066463a2013-09-22 19:57:25 -070043 END_OBJECT("}", JsonTokenId.ID_END_OBJECT),
44
Tatu Salorantaf15531c2011-12-22 23:00:40 -080045 /**
Lukasz Kryger545d1622013-02-20 22:55:02 +000046 * START_ARRAY is returned when encountering '['
Tatu Salorantaf15531c2011-12-22 23:00:40 -080047 * which signals starting of an Array value
48 */
Tatu Saloranta066463a2013-09-22 19:57:25 -070049 START_ARRAY("[", JsonTokenId.ID_START_ARRAY),
Tatu Salorantaf15531c2011-12-22 23:00:40 -080050
51 /**
Lukasz Kryger545d1622013-02-20 22:55:02 +000052 * END_ARRAY is returned when encountering ']'
Tatu Salorantaf15531c2011-12-22 23:00:40 -080053 * which signals ending of an Array value
54 */
Tatu Saloranta066463a2013-09-22 19:57:25 -070055 END_ARRAY("]", JsonTokenId.ID_END_ARRAY),
Tatu Salorantaf15531c2011-12-22 23:00:40 -080056
57 /**
58 * FIELD_NAME is returned when a String token is encountered
59 * as a field name (same lexical value, different function)
60 */
Tatu Saloranta066463a2013-09-22 19:57:25 -070061 FIELD_NAME(null, JsonTokenId.ID_FIELD_NAME),
62
Tatu Salorantaf15531c2011-12-22 23:00:40 -080063 /**
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 Saloranta01ba0532012-03-09 18:21:05 -080071 * <code>JsonNode</code>-based JSON trees, Maps, Lists and such).
Tatu Salorantaf15531c2011-12-22 23:00:40 -080072 */
Tatu Saloranta066463a2013-09-22 19:57:25 -070073 VALUE_EMBEDDED_OBJECT(null, JsonTokenId.ID_EMBEDDED_OBJECT),
Tatu Salorantaf15531c2011-12-22 23:00:40 -080074
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 Saloranta066463a2013-09-22 19:57:25 -070080 VALUE_STRING(null, JsonTokenId.ID_STRING),
Tatu Salorantaf15531c2011-12-22 23:00:40 -080081
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 Saloranta066463a2013-09-22 19:57:25 -070088 VALUE_NUMBER_INT(null, JsonTokenId.ID_NUMBER_INT),
Tatu Salorantaf15531c2011-12-22 23:00:40 -080089
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 Saloranta066463a2013-09-22 19:57:25 -070096 VALUE_NUMBER_FLOAT(null, JsonTokenId.ID_NUMBER_FLOAT),
97
Tatu Salorantaf15531c2011-12-22 23:00:40 -080098 /**
99 * VALUE_TRUE is returned when encountering literal "true" in
100 * value context
101 */
Tatu Saloranta066463a2013-09-22 19:57:25 -0700102 VALUE_TRUE("true", JsonTokenId.ID_TRUE),
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800103
104 /**
105 * VALUE_FALSE is returned when encountering literal "false" in
106 * value context
107 */
Tatu Saloranta066463a2013-09-22 19:57:25 -0700108 VALUE_FALSE("false", JsonTokenId.ID_FALSE),
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800109
110 /**
111 * VALUE_NULL is returned when encountering literal "null" in
112 * value context
113 */
Tatu Saloranta066463a2013-09-22 19:57:25 -0700114 VALUE_NULL("null", JsonTokenId.ID_NULL),
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800115 ;
Tatu Saloranta066463a2013-09-22 19:57:25 -0700116
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800117 final String _serialized;
118
119 final char[] _serializedChars;
120
121 final byte[] _serializedBytes;
122
Tatu Saloranta066463a2013-09-22 19:57:25 -0700123 final int _id;
124
Tatu Salorantaa663c692013-09-21 14:33:29 -0700125 final boolean _isStructStart, _isStructEnd;
126
Tatu Salorantaa663c692013-09-21 14:33:29 -0700127 final boolean _isNumber;
Tatu Saloranta8b9b4cd2013-09-22 18:08:47 -0700128
129 final boolean _isBoolean;
130
131 final boolean _isScalar;
132
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800133 /**
Tatu Saloranta10c3ec82012-09-05 19:38:49 -0700134 * @param token representation for this token, if there is a
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800135 * single static representation; null otherwise
136 */
Tatu Saloranta066463a2013-09-22 19:57:25 -0700137 JsonToken(String token, int id)
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800138 {
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 Saloranta066463a2013-09-22 19:57:25 -0700153 _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 Saloranta8b9b4cd2013-09-22 18:08:47 -0700157
Tatu Saloranta066463a2013-09-22 19:57:25 -0700158 _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 Salorantaf15531c2011-12-22 23:00:40 -0800164 }
165
Tatu Saloranta066463a2013-09-22 19:57:25 -0700166 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 Salorantaf15531c2011-12-22 23:00:40 -0800171
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800172 public final boolean isNumeric() { return _isNumber; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800173
174 /**
Tatu Saloranta066463a2013-09-22 19:57:25 -0700175 * Accessor that is functionally equivalent to:
176 * <code>
177 * this == JsonToken.START_OBJECT || this == JsonToken.START_ARRAY
178 * </code>
179 *
Tatu Salorantaa663c692013-09-21 14:33:29 -0700180 * @since 2.3
181 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800182 public final boolean isStructStart() { return _isStructStart; }
Tatu Salorantaa663c692013-09-21 14:33:29 -0700183
184 /**
Tatu Saloranta066463a2013-09-22 19:57:25 -0700185 * Accessor that is functionally equivalent to:
186 * <code>
187 * this == JsonToken.END_OBJECT || this == JsonToken.END_ARRAY
188 * </code>
189 *
Tatu Salorantaa663c692013-09-21 14:33:29 -0700190 * @since 2.3
191 */
Tatu Saloranta32e4e912014-01-26 19:59:28 -0800192 public final boolean isStructEnd() { return _isStructEnd; }
Tatu Saloranta066463a2013-09-22 19:57:25 -0700193
Tatu Salorantaa663c692013-09-21 14:33:29 -0700194 /**
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800195 * 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 Saloranta32e4e912014-01-26 19:59:28 -0800199 public final boolean isScalarValue() { return _isScalar; }
200 public final boolean isBoolean() { return _isBoolean; }
Tatu Salorantaf15531c2011-12-22 23:00:40 -0800201}