001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.json;
017:
018: /**
019: * JsonValue allows you to hold any of the JSON types and to get a string
020: * version without needing to know what type you have.
021: * @author Joe Walker [joe at getahead dot ltd dot uk]
022: */
023: public abstract class JsonValue {
024: /**
025: * i.e. How do we send this from one system to another?
026: * @return A string representing the portable version of this data
027: */
028: public abstract String toExternalRepresentation();
029:
030: /**
031: * Often when dealing with a {@link JsonValue} you know which subtype it is,
032: * this allows you to assume it is a string and get the Java value quickly.
033: * @return A String representing the {@link JsonNumber} implementing this
034: * @throws ClassCastException if this is not a JsonString
035: */
036: public String getString() {
037: throw new ClassCastException(this .getClass().getSimpleName()
038: + " is not a " + JsonString.class.getSimpleName());
039: }
040:
041: /**
042: * Often when dealing with a {@link JsonValue} you know which subtype it is,
043: * this allows you to assume it is a number and get the Java value quickly.
044: * @return An int representing the {@link JsonNumber} implementing this
045: * @throws ClassCastException if this is not a JsonNumber
046: */
047: public int getInteger() {
048: throw new ClassCastException(this .getClass().getSimpleName()
049: + " is not a " + JsonNumber.class.getSimpleName());
050: }
051:
052: /**
053: * Often when dealing with a {@link JsonValue} you know which subtype it is,
054: * this allows you to assume it is a number and get the Java value quickly.
055: * @return A long representing the {@link JsonNumber} implementing this
056: * @throws ClassCastException if this is not a JsonNumber
057: */
058: public long getLong() {
059: throw new ClassCastException(this .getClass().getSimpleName()
060: + " is not a " + JsonNumber.class.getSimpleName());
061: }
062:
063: /**
064: * Often when dealing with a {@link JsonValue} you know which subtype it is,
065: * this allows you to assume it is a number and get the Java value quickly.
066: * @return A double representing the {@link JsonNumber} implementing this
067: * @throws ClassCastException if this is not a JsonNumber
068: */
069: public double getDouble() {
070: throw new ClassCastException(this .getClass().getSimpleName()
071: + " is not a " + JsonNumber.class.getSimpleName());
072: }
073:
074: /**
075: * Often when dealing with a {@link JsonValue} you know which subtype it is,
076: * this allows you to assume it is a boolean and get the Java value quickly.
077: * @return A boolean representing the {@link JsonNumber} implementing this
078: * @throws ClassCastException if this is not a JsonNumber
079: */
080: public boolean getBoolean() {
081: throw new ClassCastException(this .getClass().getSimpleName()
082: + " is not a " + JsonNumber.class.getSimpleName());
083: }
084:
085: /**
086: * Often when dealing with a {@link JsonValue} you know which subtype it is,
087: * this allows you to assume it is a JsonArray and get it quickly
088: * @return A cast of this to {@link JsonArray}
089: * @throws ClassCastException if this is not a JsonArray
090: */
091: public JsonArray getJsonArray() {
092: throw new ClassCastException(this .getClass().getSimpleName()
093: + " is not a " + JsonArray.class.getSimpleName());
094: }
095:
096: /**
097: * Often when dealing with a {@link JsonValue} you know which subtype it is,
098: * this allows you to assume it is a JsonObject and get it quickly
099: * @return A cast of this to {@link JsonObject}
100: * @throws ClassCastException if this is not a JsonObject
101: */
102: public JsonObject getJsonObject() {
103: throw new ClassCastException(this .getClass().getSimpleName()
104: + " is not a " + JsonObject.class.getSimpleName());
105: }
106: }
|