source: josm/trunk/src/javax/json/JsonValue.java@ 9619

Last change on this file since 9619 was 6756, checked in by Don-vip, 10 years ago

fix #9590 - replace org.json with GPL-compliant jsonp + remove mention of old world image removed in r1680

File size: 8.0 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2011-2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12 * or packager/legal/LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41package javax.json;
42
43/**
44 * <code>JsonValue</code> represents an immutable JSON value.
45 *
46 *
47 * <p>A JSON value is one of the following:
48 * an object ({@link JsonObject}), an array ({@link JsonArray}),
49 * a number ({@link JsonNumber}), a string ({@link JsonString}),
50 * {@code true} ({@link JsonValue#TRUE JsonValue.TRUE}), {@code false}
51 * ({@link JsonValue#FALSE JsonValue.FALSE}),
52 * or {@code null} ({@link JsonValue#NULL JsonValue.NULL}).
53 *
54 * @author Jitendra Kotamraju
55 */
56public interface JsonValue {
57
58 /**
59 * Indicates the type of a {@link JsonValue} object.
60 */
61 enum ValueType {
62 /**
63 * JSON array.
64 */
65 ARRAY,
66
67 /**
68 * JSON object.
69 */
70 OBJECT,
71
72 /**
73 * JSON string.
74 */
75 STRING,
76
77 /**
78 * JSON number.
79 */
80 NUMBER,
81
82 /**
83 * JSON true.
84 */
85 TRUE,
86
87 /**
88 * JSON false.
89 */
90 FALSE,
91
92 /**
93 * JSON null.
94 */
95 NULL
96 }
97
98 /**
99 * JSON null value.
100 */
101 static final JsonValue NULL = new JsonValue() {
102 @Override
103 public ValueType getValueType() {
104 return ValueType.NULL;
105 }
106
107 /**
108 * Compares the specified object with this {@link JsonValue#NULL}
109 * object for equality. Returns {@code true} if and only if the
110 * specified object is also a {@code JsonValue}, and their
111 * {@link #getValueType()} objects are <i>equal</i>.
112 *
113 * @param obj the object to be compared for equality with this
114 * {@code JsonValue}
115 * @return {@code true} if the specified object is equal to this
116 * {@code JsonValue}
117 */
118 @Override
119 public boolean equals(Object obj) {
120 if (obj instanceof JsonValue) {
121 return getValueType().equals(((JsonValue)obj).getValueType());
122 }
123 return false;
124 }
125
126 /**
127 * Returns the hash code value for this {@link JsonValue#NULL} object.
128 * The hash code of the {@link JsonValue#NULL} object is defined to be
129 * its {@link #getValueType()} object's hash code.
130 *
131 * @return the hash code value for this JsonString object
132 */
133 @Override
134 public int hashCode() {
135 return ValueType.NULL.hashCode();
136 }
137
138 /**
139 * Returns a "null" string.
140 *
141 * @return "null"
142 */
143 @Override
144 public String toString() {
145 return "null";
146 }
147 };
148
149 /**
150 * JSON true value.
151 */
152 static final JsonValue TRUE = new JsonValue() {
153 @Override
154 public ValueType getValueType() {
155 return ValueType.TRUE;
156 }
157
158 /**
159 * Compares the specified object with this {@link JsonValue#TRUE}
160 * object for equality. Returns {@code true} if and only if the
161 * specified object is also a JsonValue, and their
162 * {@link #getValueType()} objects are <i>equal</i>.
163 *
164 * @param obj the object to be compared for equality with this JsonValue.
165 * @return {@code true} if the specified object is equal to this JsonValue.
166 */
167 @Override
168 public boolean equals(Object obj) {
169 if (obj instanceof JsonValue) {
170 return getValueType().equals(((JsonValue)obj).getValueType());
171 }
172 return false;
173 }
174
175 /**
176 * Returns the hash code value for this {@link JsonValue#TRUE} object.
177 * The hash code of the {@link JsonValue#TRUE} object is defined to be
178 * its {@link #getValueType()} object's hash code.
179 *
180 * @return the hash code value for this JsonString object
181 */
182 @Override
183 public int hashCode() {
184 return ValueType.TRUE.hashCode();
185 }
186
187 /**
188 * Returns "true" string
189 *
190 * @return "true"
191 */
192 @Override
193 public String toString() {
194 return "true";
195 }
196 };
197
198 /**
199 * JSON false value
200 */
201 static final JsonValue FALSE = new JsonValue() {
202 @Override
203 public ValueType getValueType() {
204 return ValueType.FALSE;
205 }
206
207 /**
208 * Compares the specified object with this {@link JsonValue#FALSE}
209 * object for equality. Returns {@code true} if and only if the
210 * specified object is also a JsonValue, and their
211 * {@link #getValueType()} objects are <i>equal</i>.
212 *
213 * @param obj the object to be compared for equality with this JsonValue
214 * @return {@code true} if the specified object is equal to this JsonValue
215 */
216 @Override
217 public boolean equals(Object obj) {
218 if (obj instanceof JsonValue) {
219 return getValueType().equals(((JsonValue)obj).getValueType());
220 }
221 return false;
222 }
223
224 /**
225 * Returns the hash code value for this {@link JsonValue#FALSE} object.
226 * The hash code of the {@link JsonValue#FALSE} object is defined to be
227 * its {@link #getValueType()} object's hash code.
228 *
229 * @return the hash code value for this JsonString object
230 */
231 @Override
232 public int hashCode() {
233 return ValueType.FALSE.hashCode();
234 }
235
236 /**
237 * Returns "false" string
238 *
239 * @return "false"
240 */
241 @Override
242 public String toString() {
243 return "false";
244 }
245 };
246
247 /**
248 * Returns the value type of this JSON value.
249 *
250 * @return JSON value type
251 */
252 ValueType getValueType();
253
254 /**
255 * Returns JSON text for this JSON value.
256 *
257 * @return JSON text
258 */
259 @Override
260 String toString();
261
262}
Note: See TracBrowser for help on using the repository browser.