001/* 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.parser.json; 021 022/** 023 * This is the generalization of anything that is a "value" 024 * element in a JSON structure. This could be a JSON object, 025 * a JSON array, a scalar value (number, string, boolean), 026 * or a null. 027 * 028 */ 029public abstract class JsonLikeValue { 030 031 public enum ValueType { 032 ARRAY, OBJECT, SCALAR, NULL 033 }; 034 035 public enum ScalarType { 036 NUMBER, STRING, BOOLEAN 037 } 038 039 public abstract ValueType getJsonType (); 040 041 public abstract ScalarType getDataType (); 042 043 public abstract Object getValue (); 044 045 public boolean isArray () { 046 return this.getJsonType() == ValueType.ARRAY; 047 } 048 049 public boolean isObject () { 050 return this.getJsonType() == ValueType.OBJECT; 051 } 052 053 public boolean isScalar () { 054 return this.getJsonType() == ValueType.SCALAR; 055 } 056 057 public boolean isString () { 058 return this.getJsonType() == ValueType.SCALAR && this.getDataType() == ScalarType.STRING; 059 } 060 061 public boolean isNumber () { 062 return this.getJsonType() == ValueType.SCALAR && this.getDataType() == ScalarType.NUMBER; 063 } 064 065 public boolean isNull () { 066 return this.getJsonType() == ValueType.NULL; 067 } 068 069 public JsonLikeArray getAsArray () { 070 return null; 071 } 072 public JsonLikeObject getAsObject () { 073 return null; 074 } 075 public String getAsString () { 076 return this.toString(); 077 } 078 public Number getAsNumber () { 079 return this.isNumber() ? (Number)this.getValue() : null; 080 } 081 public boolean getAsBoolean () { 082 return !isNull(); 083 } 084 085 public static JsonLikeArray asArray (JsonLikeValue element) { 086 if (element != null) { 087 return element.getAsArray(); 088 } 089 return null; 090 } 091 public static JsonLikeObject asObject (JsonLikeValue element) { 092 if (element != null) { 093 return element.getAsObject(); 094 } 095 return null; 096 } 097 public static String asString (JsonLikeValue element) { 098 if (element != null) { 099 return element.getAsString(); 100 } 101 return null; 102 } 103 public static boolean asBoolean (JsonLikeValue element) { 104 if (element != null) { 105 return element.getAsBoolean(); 106 } 107 return false; 108 } 109 110 111 public static final JsonLikeValue NULL = new JsonLikeValue() { 112 @Override 113 public ValueType getJsonType() { 114 return ValueType.NULL; 115 } 116 117 @Override 118 public ScalarType getDataType() { 119 return null; 120 } 121 122 @Override 123 public Object getValue() { 124 return null; 125 } 126 127 @Override 128 public boolean equals (Object obj) { 129 if (this == obj){ 130 return true; 131 } 132 if (obj instanceof JsonLikeValue) { 133 return getJsonType().equals(((JsonLikeValue)obj).getJsonType()); 134 } 135 return false; 136 } 137 138 @Override 139 public int hashCode() { 140 return "null".hashCode(); 141 } 142 143 @Override 144 public String toString() { 145 return "null"; 146 } 147 }; 148 149 public static final JsonLikeValue TRUE = new JsonLikeValue() { 150 @Override 151 public ValueType getJsonType() { 152 return ValueType.SCALAR; 153 } 154 155 @Override 156 public ScalarType getDataType() { 157 return ScalarType.BOOLEAN; 158 } 159 160 @Override 161 public Object getValue() { 162 return Boolean.TRUE; 163 } 164 165 @Override 166 public boolean equals(Object obj) { 167 if (this == obj){ 168 return true; 169 } 170 if (obj instanceof JsonLikeValue) { 171 return getJsonType().equals(((JsonLikeValue)obj).getJsonType()) 172 && getDataType().equals(((JsonLikeValue)obj).getDataType()) 173 && toString().equals(((JsonLikeValue)obj).toString()); 174 } 175 return false; 176 } 177 178 @Override 179 public int hashCode() { 180 return "true".hashCode(); 181 } 182 183 @Override 184 public String toString() { 185 return "true"; 186 } 187 }; 188 189 public static final JsonLikeValue FALSE = new JsonLikeValue() { 190 @Override 191 public ValueType getJsonType() { 192 return ValueType.SCALAR; 193 } 194 195 @Override 196 public ScalarType getDataType() { 197 return ScalarType.BOOLEAN; 198 } 199 200 @Override 201 public Object getValue() { 202 return Boolean.FALSE; 203 } 204 205 @Override 206 public boolean equals(Object obj) { 207 if (this == obj){ 208 return true; 209 } 210 if (obj instanceof JsonLikeValue) { 211 return getJsonType().equals(((JsonLikeValue)obj).getJsonType()) 212 && getDataType().equals(((JsonLikeValue)obj).getDataType()) 213 && toString().equals(((JsonLikeValue)obj).toString()); 214 } 215 return false; 216 } 217 218 @Override 219 public int hashCode() { 220 return "false".hashCode(); 221 } 222 223 @Override 224 public String toString() { 225 return "false"; 226 } 227 }; 228}