001package Torello.Java.Additional; 002 003import javax.json.*; 004import java.lang.reflect.*; 005import java.math.*; 006 007import java.util.function.Function; 008 009import static javax.json.JsonValue.ValueType.*; 010import static Torello.Java.Additional.JFlag.*; 011import static Torello.Java.Additional.RJInternal.*; 012 013/** 014 * Utilities that build on the J2EE Standard Release JSON Parsing Tool providing additional 015 * help with converting JSON Data into Java Data Types. 016 * 017 * <EMBED CLASS='external-html' DATA-FILE-ID=GLASS_FISH_NOTE> 018 * <EMBED CLASS='external-html' DATA-FILE-ID=READ_JSON> 019 * 020 * @see Json 021 * @see JsonObject 022 * @see JsonArray 023 */ 024@Torello.JavaDoc.StaticFunctional 025@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="JSON_JDHBI") 026public class ReadJSON 027{ 028 // This is a static class. Has no program state. 029 private ReadJSON() { } 030 031 032 // ******************************************************************************************** 033 // ******************************************************************************************** 034 // Integer from JsonArray 035 // ******************************************************************************************** 036 // ******************************************************************************************** 037 038 039 /** 040 * Retrieve a {@link JsonArray} element, and transform it to an {@code 'int'} primitive 041 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=int DATA-M=intValueExact> 042 * @param ja Any instance of {@link JsonArray} 043 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 044 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA> 045 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 046 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 047 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 048 * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX> 049 * @see RJInternal#GET(JsonArray, int, Class, Function) 050 * @see JsonNumber#intValueExact() 051 */ 052 @SuppressWarnings("cast") 053 public static int getInt(JsonArray ja, int index) 054 { return GET(ja, index, int.class, JsonNumber::intValueExact); } 055 056 /** 057 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Integer}. 058 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 059 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE='java.lang.Integer' DATA-M=intValueExact> 060 * @param ja Any instance of {@link JsonArray} 061 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 062 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JA> 063 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 064 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 065 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 066 * @see RJInternal#GET(JsonArray, int, Function, Class) 067 * @see JsonNumber#intValueExact() 068 */ 069 public static Integer getINTEGER(JsonArray ja, int index) 070 { return GET(ja, index, JsonNumber::intValueExact, Integer.class); } 071 072 /** 073 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Integer}. 074 * <EMBED CLASS=defs DATA-TYPE='java.lang.Integer' DATA-PTYPE=int DATA-THIS=getINTEGER 075 * DATA-JTYPE=JsonNumber> 076 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 077 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JA> 078 * @param ja Any instance of {@link JsonArray} 079 * @param index The array index containing the element to retrieve. 080 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 081 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 082 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 083 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 084 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 085 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 086 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 087 * @see RJInternal#GET(JsonArray, int, int, Number, Class, Function, Function) 088 * @see JsonNumber#intValueExact() 089 * @see JsonNumber#intValue() 090 */ 091 public static Integer getINTEGER(JsonArray ja, int index, int FLAGS, int defaultValue) 092 { 093 return GET( 094 ja, index, FLAGS, defaultValue, 095 Integer.class, JsonNumber::intValueExact, JsonNumber::intValue 096 ); 097 } 098 099 /** 100 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 101 * {@code java.lang.Integer}, with either a user-provided parser, or the standard java integer 102 * parser 103 * <EMBED CLASS=defs DATA-TYPE='java.lang.Integer' DATA-PTYPE=int DATA-THIS=parseINTEGER 104 * DATA-JTYPE=JsonString DATA-PARSER='Integer.parseInt'> 105 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 106 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JA> 107 * @param ja Any instance of {@link JsonArray} 108 * @param index The array index containing the {@link JsonString} element to retrieve. 109 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 110 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 111 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 112 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 113 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 114 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 115 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 116 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 117 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 118 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 119 */ 120 public static Integer parseINTEGER( 121 JsonArray ja, int index, int FLAGS, int defaultValue, 122 Function<String, Integer> optionalParser 123 ) 124 { 125 return PARSE( 126 ja, index, FLAGS, defaultValue, Integer.class, optionalParser, 127 BigDecimal::intValueExact, BigDecimal::intValue 128 ); 129 } 130 131 132 // ******************************************************************************************** 133 // ******************************************************************************************** 134 // Integer from JsonObject 135 // ******************************************************************************************** 136 // ******************************************************************************************** 137 138 139 /** 140 * Extract a {@link JsonObject} property, and transform it to an {@code 'int'} primitive 141 * <EMBED CLASS=defs DATA-TYPE=int DATA-JTYPE=JsonNumber DATA-M=intValueExact> 142 * @param jo Any instance of {@link JsonObject} 143 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 144 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO> 145 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR> 146 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 147 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 148 * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX> 149 * @see RJInternal#GET(JsonObject, String, Class, Function) 150 * @see JsonNumber#intValueExact() 151 */ 152 @SuppressWarnings("cast") 153 public static int getInt(JsonObject jo, String propertyName) 154 { return GET(jo, propertyName, int.class, JsonNumber::intValueExact); } 155 156 /** 157 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Integer}. 158 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 159 * <EMBED CLASS=defs DATA-TYPE='java.lang.Integer' DATA-JTYPE=JsonNumber DATA-M=intValueExact> 160 * @param jo Any instance of {@link JsonObject}. 161 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 162 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 163 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JO> 164 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 165 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 166 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 167 * @see RJInternal#GET(JsonObject, String, boolean, Function, Class) 168 * @see JsonNumber#intValueExact() 169 */ 170 public static Integer getINTEGER(JsonObject jo, String propertyName, boolean isOptional) 171 { return GET(jo, propertyName, isOptional, JsonNumber::intValueExact, Integer.class); } 172 173 /** 174 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Integer}. 175 * <EMBED CLASS=defs DATA-TYPE='java.lang.Integer' DATA-PTYPE=int DATA-THIS=getINTEGER 176 * DATA-JTYPE=JsonNumber> 177 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 178 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JO> 179 * @param jo Any instance of {@link JsonObject} 180 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 181 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 182 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 183 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 184 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 185 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 186 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 187 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 188 * @see RJInternal#GET(JsonObject, String, int, Number, Class, Function, Function) 189 * @see JsonNumber#intValueExact() 190 * @see JsonNumber#intValue() 191 */ 192 public static Integer getINTEGER 193 (JsonObject jo, String propertyName, int FLAGS, int defaultValue) 194 { 195 return GET( 196 jo, propertyName, FLAGS, defaultValue, 197 Integer.class, JsonNumber::intValueExact, JsonNumber::intValue 198 ); 199 } 200 201 /** 202 * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to 203 * a {@code java.lang.Integer}, with either a user-provided parser, or the standard java 204 * integer parser 205 * <EMBED CLASS=defs DATA-TYPE='java.lang.Integer' DATA-PTYPE=int DATA-THIS=parseINTEGER 206 * DATA-JTYPE=JsonString DATA-PARSER='Integer.parseInt'> 207 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 208 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JO> 209 * @param jo Any instance of {@link JsonObject} 210 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 211 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 212 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 213 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 214 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 215 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 216 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 217 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 218 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 219 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 220 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 221 */ 222 public static Integer parseINTEGER( 223 JsonObject jo, String propertyName, int FLAGS, int defaultValue, 224 Function<String, Integer> optionalParser 225 ) 226 { 227 return PARSE( 228 jo, propertyName, FLAGS, defaultValue, Integer.class, optionalParser, 229 BigDecimal::intValueExact, BigDecimal::intValue 230 ); 231 } 232 233 234 // ******************************************************************************************** 235 // ******************************************************************************************** 236 // Long from JsonArray 237 // ******************************************************************************************** 238 // ******************************************************************************************** 239 240 241 /** 242 * Retrieve a {@link JsonArray} element, and transform it to a {@code 'long'} primitive 243 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=long DATA-M=longValueExact> 244 * @param ja Any instance of {@link JsonArray} 245 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 246 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA> 247 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 248 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 249 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 250 * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX> 251 * @see RJInternal#GET(JsonArray, int, Class, Function) 252 * @see JsonNumber#longValueExact() 253 */ 254 @SuppressWarnings("cast") 255 public static long getLong(JsonArray ja, int index) 256 { return GET(ja, index, long.class, JsonNumber::longValueExact); } 257 258 /** 259 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Long}. 260 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 261 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE='java.lang.Long' DATA-M=longValueExact> 262 * @param ja Any instance of {@link JsonArray} 263 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 264 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JA> 265 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 266 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 267 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 268 * @see RJInternal#GET(JsonArray, int, Function, Class) 269 * @see JsonNumber#longValueExact() 270 */ 271 public static Long getLONG(JsonArray ja, int index) 272 { return GET(ja, index, JsonNumber::longValueExact, Long.class); } 273 274 /** 275 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Long}. 276 * <EMBED CLASS=defs DATA-TYPE='java.lang.Long' DATA-PTYPE=long DATA-THIS=getLONG 277 * DATA-JTYPE=JsonNumber> 278 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 279 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JA> 280 * @param ja Any instance of {@link JsonArray} 281 * @param index The array index containing the element to retrieve. 282 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 283 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 284 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 285 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 286 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 287 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 288 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 289 * @see RJInternal#GET(JsonArray, int, int, Number, Class, Function, Function) 290 * @see JsonNumber#longValueExact() 291 * @see JsonNumber#longValue() 292 */ 293 public static Long getLONG(JsonArray ja, int index, int FLAGS, long defaultValue) 294 { 295 return GET( 296 ja, index, FLAGS, defaultValue, 297 Long.class, JsonNumber::longValueExact, JsonNumber::longValue 298 ); 299 } 300 301 /** 302 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 303 * {@code java.lang.Long}, with either a user-provided parser, or the standard java 304 * long-integer parser 305 * <EMBED CLASS=defs DATA-TYPE='java.lang.Long' DATA-PTYPE=long DATA-THIS=parseLONG 306 * DATA-JTYPE=JsonString DATA-PARSER='Long.parseLong'> 307 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 308 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JA> 309 * @param ja Any instance of {@link JsonArray} 310 * @param index The array index containing the {@link JsonString} element to retrieve. 311 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 312 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 313 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 314 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 315 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 316 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 317 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 318 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 319 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 320 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 321 */ 322 public static Long parseLONG( 323 JsonArray ja, int index, int FLAGS, long defaultValue, 324 Function<String, Long> optionalParser 325 ) 326 { 327 return PARSE( 328 ja, index, FLAGS, defaultValue, Long.class, optionalParser, 329 BigDecimal::longValueExact, BigDecimal::longValue 330 ); 331 } 332 333 334 // ******************************************************************************************** 335 // ******************************************************************************************** 336 // Long from JsonObject 337 // ******************************************************************************************** 338 // ******************************************************************************************** 339 340 341 /** 342 * Extract a {@link JsonObject} property, and transform it to a {@code 'long'} primitive 343 * <EMBED CLASS=defs DATA-TYPE=long DATA-JTYPE=JsonNumber DATA-M=longValueExact> 344 * @param jo Any instance of {@link JsonObject} 345 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 346 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO> 347 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR> 348 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 349 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 350 * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX> 351 * @see RJInternal#GET(JsonObject, String, Class, Function) 352 * @see JsonNumber#longValueExact() 353 */ 354 @SuppressWarnings("cast") 355 public static long getLong(JsonObject jo, String propertyName) 356 { return GET(jo, propertyName, long.class, JsonNumber::longValueExact); } 357 358 /** 359 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Long}. 360 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 361 * <EMBED CLASS=defs DATA-TYPE='java.lang.Long' DATA-JTYPE=JsonNumber DATA-M=longValueExact> 362 * @param jo Any instance of {@link JsonObject}. 363 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 364 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 365 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JO> 366 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 367 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 368 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 369 * @see RJInternal#GET(JsonObject, String, boolean, Function, Class) 370 * @see JsonNumber#longValueExact() 371 */ 372 public static Long getLONG(JsonObject jo, String propertyName, boolean isOptional) 373 { return GET(jo, propertyName, isOptional, JsonNumber::longValueExact, Long.class); } 374 375 /** 376 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Long}. 377 * <EMBED CLASS=defs DATA-TYPE='java.lang.Long' DATA-PTYPE=int DATA-THIS=getLONG 378 * DATA-JTYPE=JsonNumber> 379 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 380 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JO> 381 * @param jo Any instance of {@link JsonObject} 382 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 383 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 384 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 385 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 386 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 387 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 388 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 389 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 390 * @see RJInternal#GET(JsonObject, String, int, Number, Class, Function, Function) 391 * @see JsonNumber#longValueExact() 392 * @see JsonNumber#longValue() 393 */ 394 public static Long getLONG 395 (JsonObject jo, String propertyName, int FLAGS, long defaultValue) 396 { 397 return GET( 398 jo, propertyName, FLAGS, defaultValue, 399 Long.class, JsonNumber::longValueExact, JsonNumber::longValue 400 ); 401 } 402 403 /** 404 * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to 405 * a {@code java.lang.Long}, with either a user-provided parser, or the standard java 406 * long-integer parser 407 * <EMBED CLASS=defs DATA-TYPE='java.lang.Long' DATA-PTYPE=long DATA-THIS=parseLONG 408 * DATA-JTYPE=JsonString DATA-PARSER='Long.parseLong'> 409 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 410 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JO> 411 * @param jo Any instance of {@link JsonObject} 412 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 413 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 414 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 415 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 416 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 417 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 418 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 419 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 420 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 421 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 422 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 423 */ 424 public static Long parseLONG( 425 JsonObject jo, String propertyName, int FLAGS, long defaultValue, 426 Function<String, Long> optionalParser 427 ) 428 { 429 return PARSE( 430 jo, propertyName, FLAGS, defaultValue, Long.class, optionalParser, 431 BigDecimal::longValueExact, BigDecimal::longValue 432 ); 433 } 434 435 436 // ******************************************************************************************** 437 // ******************************************************************************************** 438 // Short from JsonArray 439 // ******************************************************************************************** 440 // ******************************************************************************************** 441 442 443 /** 444 * Retrieve a {@link JsonArray} element, and transform it to a {@code 'short'} primitive 445 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=short DATA-M=shortValueExact> 446 * @param ja Any instance of {@link JsonArray} 447 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 448 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA> 449 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 450 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 451 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 452 * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX> 453 * @see RJInternal#GET(JsonArray, int, Class, Function) 454 * @see JsonNumber#bigDecimalValue() 455 */ 456 @SuppressWarnings("cast") 457 public static short getShort(JsonArray ja, int index) 458 { return GET(ja, index, short.class, jn -> jn.bigDecimalValue().shortValueExact()); } 459 460 /** 461 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Short}. 462 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 463 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE='java.lang.Short' DATA-M=shortValueExact> 464 * @param ja Any instance of {@link JsonArray} 465 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 466 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JA> 467 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 468 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 469 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 470 * @see RJInternal#GET(JsonArray, int, Function, Class) 471 * @see JsonNumber#bigDecimalValue() 472 */ 473 public static Short getSHORT(JsonArray ja, int index) 474 { return GET(ja, index, jn -> jn.bigDecimalValue().shortValueExact(), Short.class); } 475 476 /** 477 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Short}. 478 * <EMBED CLASS=defs DATA-TYPE='java.lang.Short' DATA-PTYPE=short DATA-THIS=getSHORT 479 * DATA-JTYPE=JsonNumber> 480 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 481 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JA> 482 * @param ja Any instance of {@link JsonArray} 483 * @param index The array index containing the element to retrieve. 484 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 485 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 486 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 487 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 488 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 489 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 490 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 491 * @see RJInternal#GET(JsonArray, int, int, Number, Class, Function, Function) 492 * @see JsonNumber#bigDecimalValue() 493 */ 494 public static Short getSHORT(JsonArray ja, int index, int FLAGS, short defaultValue) 495 { 496 return GET( 497 ja, index, FLAGS, defaultValue, Short.class, 498 jn -> jn.bigDecimalValue().shortValueExact(), 499 jn -> jn.bigDecimalValue().shortValue() 500 ); 501 } 502 503 /** 504 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 505 * {@code java.lang.Short}, with either a user-provided parser, or the standard java 506 * short-integer parser 507 * <EMBED CLASS=defs DATA-TYPE='java.lang.Short' DATA-PTYPE=short DATA-THIS=parseSHORT 508 * DATA-JTYPE=JsonString DATA-PARSER='Short.parseShort'> 509 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 510 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JA> 511 * @param ja Any instance of {@link JsonArray} 512 * @param index The array index containing the {@link JsonString} element to retrieve. 513 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 514 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 515 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 516 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 517 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 518 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 519 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 520 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 521 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 522 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 523 */ 524 public static Short parseSHORT( 525 JsonArray ja, int index, int FLAGS, short defaultValue, 526 Function<String, Short> optionalParser 527 ) 528 { 529 return PARSE( 530 ja, index, FLAGS, defaultValue, Short.class, optionalParser, 531 BigDecimal::shortValueExact, BigDecimal::shortValue 532 ); 533 } 534 535 536 // ******************************************************************************************** 537 // ******************************************************************************************** 538 // Short from JsonObject 539 // ******************************************************************************************** 540 // ******************************************************************************************** 541 542 543 /** 544 * Extract a {@link JsonObject} property, and transform it to a {@code 'short'} primitive 545 * <EMBED CLASS=defs DATA-TYPE=short DATA-JTYPE=JsonNumber DATA-M=shortValueExact> 546 * @param jo Any instance of {@link JsonObject} 547 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 548 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO> 549 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR> 550 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 551 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 552 * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX> 553 * @see RJInternal#GET(JsonObject, String, Class, Function) 554 * @see JsonNumber#bigDecimalValue() 555 */ 556 @SuppressWarnings("cast") 557 public static short getShort(JsonObject jo, String propertyName) 558 { return GET(jo, propertyName, short.class, jn -> jn.bigDecimalValue().shortValueExact()); } 559 560 /** 561 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Short}. 562 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 563 * <EMBED CLASS=defs DATA-TYPE='java.lang.Short' DATA-JTYPE=JsonNumber DATA-M=shortValueExact> 564 * @param jo Any instance of {@link JsonObject}. 565 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 566 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 567 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JO> 568 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 569 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 570 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 571 * @see RJInternal#GET(JsonObject, String, boolean, Function, Class) 572 * @see JsonNumber#bigDecimalValue() 573 */ 574 public static Short getSHORT(JsonObject jo, String propertyName, boolean isOptional) 575 { 576 return GET( 577 jo, propertyName, isOptional, jn -> jn.bigDecimalValue().shortValueExact(), 578 Short.class 579 ); 580 } 581 582 /** 583 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Short}. 584 * <EMBED CLASS=defs DATA-TYPE='java.lang.Short' DATA-PTYPE=int DATA-THIS=getSHORT 585 * DATA-JTYPE=JsonNumber> 586 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 587 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JO> 588 * @param jo Any instance of {@link JsonObject} 589 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 590 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 591 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 592 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 593 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 594 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 595 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 596 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 597 * @see RJInternal#GET(JsonObject, String, int, Number, Class, Function, Function) 598 * @see JsonNumber#bigDecimalValue() 599 */ 600 public static Short getSHORT 601 (JsonObject jo, String propertyName, int FLAGS, short defaultValue) 602 { 603 return GET( 604 jo, propertyName, FLAGS, defaultValue, Short.class, 605 jn -> jn.bigDecimalValue().shortValueExact(), 606 jn -> jn.bigDecimalValue().shortValue() 607 ); 608 } 609 610 /** 611 * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to 612 * a {@code java.lang.Short}, with either a user-provided parser, or the standard java 613 * short-integer parser 614 * <EMBED CLASS=defs DATA-TYPE='java.lang.Short' DATA-PTYPE=short DATA-THIS=parseSHORT 615 * DATA-JTYPE=JsonString DATA-PARSER='Short.parseShort'> 616 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 617 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JO> 618 * @param jo Any instance of {@link JsonObject} 619 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 620 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 621 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 622 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 623 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 624 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 625 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 626 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 627 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 628 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 629 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 630 */ 631 public static Short parseSHORT( 632 JsonObject jo, String propertyName, int FLAGS, short defaultValue, 633 Function<String, Short> optionalParser 634 ) 635 { 636 return PARSE( 637 jo, propertyName, FLAGS, defaultValue, Short.class, optionalParser, 638 BigDecimal::shortValueExact, BigDecimal::shortValue 639 ); 640 } 641 642 643 // ******************************************************************************************** 644 // ******************************************************************************************** 645 // Byte from JsonArray 646 // ******************************************************************************************** 647 // ******************************************************************************************** 648 649 650 /** 651 * Retrieve a {@link JsonArray} element, and transform it to a {@code 'byte'} primitive 652 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=byte DATA-M=byteValueExact> 653 * @param ja Any instance of {@link JsonArray} 654 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 655 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA> 656 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 657 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 658 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 659 * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX> 660 * @see RJInternal#GET(JsonArray, int, Class, Function) 661 * @see JsonNumber#bigDecimalValue() 662 */ 663 @SuppressWarnings("cast") 664 public static byte getByte(JsonArray ja, int index) 665 { return GET(ja, index, byte.class, jn -> jn.bigDecimalValue().byteValueExact()); } 666 667 /** 668 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Byte}. 669 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 670 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE='java.lang.Byte' DATA-M=byteValueExact> 671 * @param ja Any instance of {@link JsonArray} 672 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 673 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JA> 674 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 675 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 676 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 677 * @see RJInternal#GET(JsonArray, int, Function, Class) 678 * @see JsonNumber#bigDecimalValue() 679 */ 680 public static Byte getBYTE(JsonArray ja, int index) 681 { return GET(ja, index, jn -> jn.bigDecimalValue().byteValueExact(), Byte.class); } 682 683 /** 684 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Byte}. 685 * <EMBED CLASS=defs DATA-TYPE='java.lang.Byte' DATA-PTYPE=byte DATA-THIS=getBYTE 686 * DATA-JTYPE=JsonNumber> 687 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 688 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JA> 689 * @param ja Any instance of {@link JsonArray} 690 * @param index The array index containing the element to retrieve. 691 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 692 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 693 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 694 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 695 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 696 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 697 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 698 * @see RJInternal#GET(JsonArray, int, int, Number, Class, Function, Function) 699 * @see JsonNumber#bigDecimalValue() 700 */ 701 public static Byte getBYTE(JsonArray ja, int index, int FLAGS, byte defaultValue) 702 { 703 return GET( 704 ja, index, FLAGS, defaultValue, Byte.class, 705 jn -> jn.bigDecimalValue().byteValueExact(), 706 jn -> jn.bigDecimalValue().byteValue() 707 ); 708 } 709 710 /** 711 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 712 * {@code java.lang.Byte}, with either a user-provided parser, or the standard java 713 * byte parser 714 * <EMBED CLASS=defs DATA-TYPE='java.lang.Byte' DATA-PTYPE=byte DATA-THIS=parseBYTE 715 * DATA-JTYPE=JsonString DATA-PARSER='Byte.parseByte'> 716 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 717 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JA> 718 * @param ja Any instance of {@link JsonArray} 719 * @param index The array index containing the {@link JsonString} element to retrieve. 720 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 721 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 722 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 723 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 724 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 725 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 726 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 727 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 728 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 729 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 730 */ 731 public static Byte parseBYTE( 732 JsonArray ja, int index, int FLAGS, byte defaultValue, 733 Function<String, Byte> optionalParser 734 ) 735 { 736 return PARSE( 737 ja, index, FLAGS, defaultValue, Byte.class, optionalParser, 738 BigDecimal::byteValueExact, BigDecimal::byteValue 739 ); 740 } 741 742 743 // ******************************************************************************************** 744 // ******************************************************************************************** 745 // Byte from JsonObject 746 // ******************************************************************************************** 747 // ******************************************************************************************** 748 749 750 /** 751 * Extract a {@link JsonObject} property, and transform it to a {@code 'byte'} primitive 752 * <EMBED CLASS=defs DATA-TYPE=byte DATA-JTYPE=JsonNumber DATA-M=byteValueExact> 753 * @param jo Any instance of {@link JsonObject} 754 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 755 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO> 756 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR> 757 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 758 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 759 * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX> 760 * @see RJInternal#GET(JsonObject, String, Class, Function) 761 * @see JsonNumber#bigDecimalValue() 762 */ 763 @SuppressWarnings("cast") 764 public static byte getByte(JsonObject jo, String propertyName) 765 { return GET(jo, propertyName, byte.class, jn -> jn.bigDecimalValue().byteValueExact()); } 766 767 /** 768 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Byte}. 769 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 770 * <EMBED CLASS=defs DATA-TYPE='java.lang.Byte' DATA-JTYPE=JsonNumber DATA-M=byteValueExact> 771 * @param jo Any instance of {@link JsonObject}. 772 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 773 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 774 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JO> 775 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 776 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 777 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 778 * @see RJInternal#GET(JsonObject, String, boolean, Function, Class) 779 * @see JsonNumber#bigDecimalValue() 780 */ 781 public static Byte getBYTE(JsonObject jo, String propertyName, boolean isOptional) 782 { 783 return GET( 784 jo, propertyName, isOptional, jn -> jn.bigDecimalValue().byteValueExact(), 785 Byte.class 786 ); 787 } 788 789 /** 790 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Byte}. 791 * <EMBED CLASS=defs DATA-TYPE='java.lang.Byte' DATA-PTYPE=int DATA-THIS=getBYTE 792 * DATA-JTYPE=JsonNumber> 793 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 794 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JO> 795 * @param jo Any instance of {@link JsonObject} 796 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 797 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 798 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 799 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 800 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 801 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 802 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 803 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 804 * @see RJInternal#GET(JsonObject, String, int, Number, Class, Function, Function) 805 * @see JsonNumber#bigDecimalValue() 806 */ 807 public static Byte getBYTE 808 (JsonObject jo, String propertyName, int FLAGS, byte defaultValue) 809 { 810 return GET( 811 jo, propertyName, FLAGS, defaultValue, Byte.class, 812 jn -> jn.bigDecimalValue().byteValueExact(), 813 jn -> jn.bigDecimalValue().byteValue() 814 ); 815 } 816 817 /** 818 * Retrieve a {@link JsonObject} element containing a {@link JsonString}, and transform it to 819 * a {@code java.lang.Byte}, with either a user-provided parser, or the standard java 820 * byte parser 821 * <EMBED CLASS=defs DATA-TYPE='java.lang.Byte' DATA-PTYPE=byte DATA-THIS=parseBYTE 822 * DATA-JTYPE=JsonString DATA-PARSER='Byte.parseByte'> 823 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 824 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JO> 825 * @param jo Any instance of {@link JsonObject} 826 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 827 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 828 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 829 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 830 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 831 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 832 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 833 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 834 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 835 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 836 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 837 */ 838 public static Byte parseBYTE( 839 JsonObject jo, String propertyName, int FLAGS, byte defaultValue, 840 Function<String, Byte> optionalParser 841 ) 842 { 843 return PARSE( 844 jo, propertyName, FLAGS, defaultValue, Byte.class, optionalParser, 845 BigDecimal::byteValueExact, BigDecimal::byteValue 846 ); 847 } 848 849 850 // ******************************************************************************************** 851 // ******************************************************************************************** 852 // Double from JsonArray 853 // ******************************************************************************************** 854 // ******************************************************************************************** 855 856 857 /** 858 * Retrieve a {@link JsonArray} element, and transform it to a {@code 'double'} primitive 859 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=double DATA-M=doubleValue 860 * DATA-INFINITY=Double> 861 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 862 * @param ja Any instance of {@link JsonArray} 863 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 864 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA> 865 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 866 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 867 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 868 * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX> 869 * @see RJInternal#GET(JsonArray, int, Class, Function) 870 * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber) 871 */ 872 @SuppressWarnings("cast") 873 public static double getDouble(JsonArray ja, int index) 874 { return GET(ja, index, double.class, RJInternal::DOUBLE_WITH_CHECK); } 875 876 /** 877 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Double}. 878 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 879 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE='java.lang.Double' DATA-M=doubleValue 880 * DATA-INFINITY=Double> 881 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 882 * @param ja Any instance of {@link JsonArray} 883 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 884 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JA> 885 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 886 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 887 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 888 * @see RJInternal#GET(JsonArray, int, Function, Class) 889 * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber) 890 */ 891 public static Double getDOUBLE(JsonArray ja, int index) 892 { return GET(ja, index, RJInternal::DOUBLE_WITH_CHECK, Double.class); } 893 894 /** 895 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Double}. 896 * <EMBED CLASS=defs DATA-TYPE='java.lang.Double' DATA-JTYPE=JsonNumber DATA-M=doubleValue 897 * DATA-PTYPE=double DATA-THIS=getDOUBLE> 898 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 899 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JA> 900 * @param ja Any instance of {@link JsonArray} 901 * @param index The array index containing the element to retrieve. 902 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 903 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 904 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 905 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 906 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 907 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 908 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 909 * @see RJInternal#GET(JsonArray, int, int, Number, Class, Function, Function) 910 * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber) 911 * @see JsonNumber#bigDecimalValue() 912 */ 913 public static Double getDOUBLE(JsonArray ja, int index, int FLAGS, double defaultValue) 914 { 915 return GET( 916 ja, index, FLAGS, defaultValue, Double.class, 917 RJInternal::DOUBLE_WITH_CHECK, jn -> jn.bigDecimalValue().doubleValue() 918 ); 919 } 920 921 /** 922 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 923 * {@code java.lang.Double}, with either a user-provided parser, or the standard java 924 * double parser 925 * <EMBED CLASS=defs DATA-TYPE='java.lang.Double' DATA-PTYPE=double DATA-THIS=parseDOUBLE 926 * DATA-JTYPE=JsonString DATA-PARSER='Double.parseDouble'> 927 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 928 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JA> 929 * @param ja Any instance of {@link JsonArray} 930 * @param index The array index containing the {@link JsonString} element to retrieve. 931 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 932 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 933 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 934 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 935 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 936 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 937 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 938 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 939 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 940 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 941 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 942 */ 943 public static Double parseDOUBLE( 944 JsonArray ja, int index, int FLAGS, double defaultValue, 945 Function<String, Double> optionalParser 946 ) 947 { 948 return PARSE( 949 ja, index, FLAGS, defaultValue, Double.class, optionalParser, 950 RJInternal::DOUBLE_WITH_CHECK, BigDecimal::doubleValue 951 ); 952 } 953 954 955 // ******************************************************************************************** 956 // ******************************************************************************************** 957 // Double from JsonObject 958 // ******************************************************************************************** 959 // ******************************************************************************************** 960 961 962 /** 963 * Extract a {@link JsonObject} property, and transform it to a {@code 'double'} primitive 964 * <EMBED CLASS=defs DATA-TYPE=double DATA-JTYPE=JsonNumber DATA-M=doubleValue 965 * DATA-INFINITY=Double> 966 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 967 * @param jo Any instance of {@link JsonObject} 968 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 969 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO> 970 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR> 971 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 972 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 973 * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX> 974 * @see RJInternal#GET(JsonObject, String, Class, Function) 975 * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber) 976 */ 977 @SuppressWarnings("cast") 978 public static double getDouble(JsonObject jo, String propertyName) 979 { return GET(jo, propertyName, double.class, RJInternal::DOUBLE_WITH_CHECK); } 980 981 /** 982 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Double}. 983 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 984 * <EMBED CLASS=defs DATA-TYPE='java.lang.Double' DATA-JTYPE=JsonNumber DATA-M=doubleValue 985 * DATA-INFINITY=Double> 986 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 987 * @param jo Any instance of {@link JsonObject}. 988 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 989 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 990 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JO> 991 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 992 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 993 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 994 * @see RJInternal#GET(JsonObject, String, boolean, Function, Class) 995 * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber) 996 */ 997 public static Double getDOUBLE(JsonObject jo, String propertyName, boolean isOptional) 998 { return GET(jo, propertyName, isOptional, RJInternal::DOUBLE_WITH_CHECK, Double.class); } 999 1000 /** 1001 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Double}. 1002 * <EMBED CLASS=defs DATA-TYPE='java.lang.Double' DATA-THIS=getDOUBLE DATA-PTYPE=double 1003 * DATA-M=doubleValue DATA-JTYPE=JsonNumber> 1004 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 1005 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JO> 1006 * @param jo Any instance of {@link JsonObject} 1007 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1008 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1009 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1010 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 1011 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 1012 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX> 1013 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1014 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1015 * @see RJInternal#GET(JsonObject, String, int, Number, Class, Function, Function) 1016 * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber) 1017 * @see JsonNumber#bigDecimalValue() 1018 */ 1019 public static Double getDOUBLE 1020 (JsonObject jo, String propertyName, int FLAGS, double defaultValue) 1021 { 1022 return GET( 1023 jo, propertyName, FLAGS, defaultValue, Double.class, 1024 RJInternal::DOUBLE_WITH_CHECK, jn -> jn.bigDecimalValue().doubleValue() 1025 ); 1026 } 1027 1028 /** 1029 * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to 1030 * a {@code java.lang.Double}, with either a user-provided parser, or the standard java 1031 * double parser 1032 * <EMBED CLASS=defs DATA-TYPE='java.lang.Double' DATA-PTYPE=double DATA-THIS=parseDOUBLE 1033 * DATA-JTYPE=JsonString DATA-PARSER='Double.parseDouble'> 1034 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 1035 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JO> 1036 * @param jo Any instance of {@link JsonObject} 1037 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1038 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1039 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1040 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 1041 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 1042 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 1043 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 1044 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 1045 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1046 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1047 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 1048 */ 1049 public static Double parseDOUBLE( 1050 JsonObject jo, String propertyName, int FLAGS, double defaultValue, 1051 Function<String, Double> optionalParser 1052 ) 1053 { 1054 return PARSE( 1055 jo, propertyName, FLAGS, defaultValue, Double.class, optionalParser, 1056 RJInternal::DOUBLE_WITH_CHECK, BigDecimal::doubleValue 1057 ); 1058 } 1059 1060 1061 // ******************************************************************************************** 1062 // ******************************************************************************************** 1063 // Float from JsonArray 1064 // ******************************************************************************************** 1065 // ******************************************************************************************** 1066 1067 1068 /** 1069 * Retrieve a {@link JsonArray} element, and transform it to a {@code 'float'} primitive 1070 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=float DATA-M=floatValue 1071 * DATA-INFINITY=Float> 1072 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 1073 * @param ja Any instance of {@link JsonArray} 1074 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 1075 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA> 1076 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 1077 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 1078 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 1079 * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX> 1080 * @see RJInternal#GET(JsonArray, int, Class, Function) 1081 * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber) 1082 */ 1083 @SuppressWarnings("cast") 1084 public static float getFloat(JsonArray ja, int index) 1085 { return GET(ja, index, float.class, RJInternal::FLOAT_WITH_CHECK); } 1086 1087 /** 1088 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Float}. 1089 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 1090 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE='java.lang.Float' DATA-M=floatValue 1091 * DATA-INFINITY=Float> 1092 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 1093 * @param ja Any instance of {@link JsonArray} 1094 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 1095 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JA> 1096 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 1097 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 1098 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 1099 * @see RJInternal#GET(JsonArray, int, Function, Class) 1100 * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber) 1101 */ 1102 public static Float getFLOAT(JsonArray ja, int index) 1103 { return GET(ja, index, RJInternal::FLOAT_WITH_CHECK, Float.class); } 1104 1105 /** 1106 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Float}. 1107 * <EMBED CLASS=defs DATA-TYPE='java.lang.Float' DATA-PTYPE=float DATA-THIS=getFLOAT 1108 * DATA-JTYPE=JsonNumber DATA-M=floatValue> 1109 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 1110 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JA> 1111 * @param ja Any instance of {@link JsonArray} 1112 * @param index The array index containing the element to retrieve. 1113 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1114 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1115 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 1116 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 1117 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 1118 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 1119 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 1120 * @see RJInternal#GET(JsonArray, int, int, Number, Class, Function, Function) 1121 * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber) 1122 * @see JsonNumber#bigDecimalValue() 1123 */ 1124 public static Float getFLOAT(JsonArray ja, int index, int FLAGS, float defaultValue) 1125 { 1126 return GET( 1127 ja, index, FLAGS, defaultValue, Float.class, 1128 RJInternal::FLOAT_WITH_CHECK, jn -> jn.bigDecimalValue().floatValue() 1129 ); 1130 } 1131 1132 /** 1133 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 1134 * {@code java.lang.Float}, with either a user-provided parser, or the standard java 1135 * float parser 1136 * <EMBED CLASS=defs DATA-TYPE='java.lang.Float' DATA-PTYPE=float DATA-THIS=parseFLOAT 1137 * DATA-JTYPE=JsonString DATA-PARSER='Float.parseFloat'> 1138 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 1139 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JA> 1140 * @param ja Any instance of {@link JsonArray} 1141 * @param index The array index containing the {@link JsonString} element to retrieve. 1142 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1143 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1144 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 1145 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 1146 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 1147 * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 1148 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 1149 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 1150 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 1151 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 1152 */ 1153 public static Float parseFLOAT( 1154 JsonArray ja, int index, int FLAGS, float defaultValue, 1155 Function<String, Float> optionalParser 1156 ) 1157 { 1158 return PARSE( 1159 ja, index, FLAGS, defaultValue, Float.class, optionalParser, 1160 RJInternal::FLOAT_WITH_CHECK, BigDecimal::floatValue 1161 ); 1162 } 1163 1164 1165 // ******************************************************************************************** 1166 // ******************************************************************************************** 1167 // Float from JsonObject 1168 // ******************************************************************************************** 1169 // ******************************************************************************************** 1170 1171 1172 /** 1173 * Extract a {@link JsonObject} property, and transform it to a {@code 'float'} primitive 1174 * <EMBED CLASS=defs DATA-TYPE=float DATA-JTYPE=JsonNumber DATA-M=floatValue 1175 * DATA-INFINITY=Float> 1176 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 1177 * @param jo Any instance of {@link JsonObject} 1178 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1179 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO> 1180 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR> 1181 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 1182 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 1183 * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX> 1184 * @see RJInternal#GET(JsonObject, String, Class, Function) 1185 * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber) 1186 */ 1187 @SuppressWarnings("cast") 1188 public static float getFloat(JsonObject jo, String propertyName) 1189 { return GET(jo, propertyName, float.class, RJInternal::FLOAT_WITH_CHECK); } 1190 1191 /** 1192 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Float}. 1193 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 1194 * <EMBED CLASS=defs DATA-TYPE='java.lang.Float' DATA-JTYPE=JsonNumber DATA-M=floatValue 1195 * DATA-INFINITY=Float> 1196 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT> 1197 * @param jo Any instance of {@link JsonObject}. 1198 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1199 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 1200 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JO> 1201 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 1202 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF> 1203 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 1204 * @see RJInternal#GET(JsonObject, String, boolean, Function, Class) 1205 * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber) 1206 */ 1207 public static Float getFLOAT(JsonObject jo, String propertyName, boolean isOptional) 1208 { return GET(jo, propertyName, isOptional, RJInternal::FLOAT_WITH_CHECK, Float.class); } 1209 1210 /** 1211 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Float}. 1212 * <EMBED CLASS=defs DATA-TYPE='java.lang.Float' DATA-PTYPE=int DATA-THIS=getFLOAT 1213 * DATA-JTYPE=JsonNumber DATA-M=doubleValue DATA-INFINITY=Double> 1214 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC> 1215 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_JO> 1216 * @param jo Any instance of {@link JsonObject} 1217 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1218 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1219 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1220 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 1221 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 1222 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 1223 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1224 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1225 * @see RJInternal#GET(JsonObject, String, int, Number, Class, Function, Function) 1226 * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber) 1227 * @see JsonNumber#bigDecimalValue() 1228 */ 1229 public static Float getFLOAT 1230 (JsonObject jo, String propertyName, int FLAGS, float defaultValue) 1231 { 1232 return GET( 1233 jo, propertyName, FLAGS, defaultValue, Float.class, 1234 RJInternal::FLOAT_WITH_CHECK, jn -> jn.bigDecimalValue().floatValue() 1235 ); 1236 } 1237 1238 /** 1239 * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to 1240 * a {@code java.lang.Float}, with either a user-provided parser, or the standard java 1241 * float parser 1242 * <EMBED CLASS=defs DATA-TYPE='java.lang.Float' DATA-PTYPE=float DATA-THIS=parseFLOAT 1243 * DATA-JTYPE=JsonString DATA-PARSER='Float.parseFloat'> 1244 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DESC_STR> 1245 * <EMBED CLASS='external-html' DATA-FILE-ID=JRF_XTRA_STR_JO> 1246 * @param jo Any instance of {@link JsonObject} 1247 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1248 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1249 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1250 * @param parser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 1251 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 1252 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 1253 * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> 1254 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 1255 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1256 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1257 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 1258 */ 1259 public static Float parseFLOAT( 1260 JsonObject jo, String propertyName, int FLAGS, float defaultValue, 1261 Function<String, Float> parser 1262 ) 1263 { 1264 return PARSE( 1265 jo, propertyName, FLAGS, defaultValue, Float.class, parser, 1266 RJInternal::FLOAT_WITH_CHECK, BigDecimal::floatValue 1267 ); 1268 } 1269 1270 1271 // ******************************************************************************************** 1272 // ******************************************************************************************** 1273 // Number from JsonArray 1274 // ******************************************************************************************** 1275 // ******************************************************************************************** 1276 1277 1278 /** 1279 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Number}. 1280 * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE='java.lang.Number'> 1281 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NUMBER> 1282 * @param ja Any instance of {@link JsonArray} 1283 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 1284 * @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=JR_TON_JA> 1285 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_TON_JA> 1286 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 1287 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 1288 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNAEX> 1289 * @see JsonValue#getValueType() 1290 * @see RJInternal#convertToNumber(JsonNumber) 1291 */ 1292 public static Number getNUMBER(JsonArray ja, int index, boolean throwOnNull) 1293 { 1294 // This will throw an IndexOutOfBoundsException if the index is out of bounds. 1295 JsonValue jv = ja.get(index); 1296 1297 switch (jv.getValueType()) 1298 { 1299 case NULL: 1300 1301 // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry 1302 // about what the meaning of "null" should be. 1303 1304 if (throwOnNull) throw new JsonNullArrException(ja, index, NUMBER, Number.class); 1305 else return null; 1306 1307 case NUMBER: return convertToNumber((JsonNumber) jv); 1308 1309 // The JsonValue at the specified array-index does not contain a JsonString. 1310 default: throw new JsonTypeArrException(ja, index, NUMBER, jv, Number.class); 1311 } 1312 } 1313 1314 /** 1315 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Number}. 1316 * <EMBED CLASS=defs DATA-TYPE='java.lang.Number' DATA-JTYPE=JsonNumber> 1317 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NUMBER> 1318 * @param ja Any instance of {@link JsonArray} 1319 * @param index The array index containing the element to retrieve. 1320 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1321 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1322 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 1323 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 1324 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 1325 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 1326 * @see RJInternal#GET(JsonArray, int, int, Number, Class, Function, Function) 1327 * @see RJInternal#convertToNumber(JsonNumber) 1328 */ 1329 public static Number getNUMBER(JsonArray ja, int index, int FLAGS, Number defaultValue) 1330 { return GET(ja, index, FLAGS, defaultValue, Number.class, RJInternal::convertToNumber, null); } 1331 1332 /** 1333 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 1334 * {@code java.lang.Number}, with either a user-provided parser, or the standard java parser 1335 * <EMBED CLASS=defs DATA-TYPE='java.lang.Number' DATA-JTYPE=JsonString 1336 * DATA-PARSER='new BigDecimal'> 1337 * <BR /><BR />If {@code 'parser'} is passed null, the default parser is used, which is just 1338 * the {@code java.math.BigDecimal} constructor which accepts a {@code String}. What is done 1339 * with the parsed {@code BigDecimal} instance is explained below. 1340 * <BR /><BR /> 1341 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NUM_PARSE> 1342 * @param ja Any instance of {@link JsonArray} 1343 * @param index The array index containing the {@link JsonString} element to retrieve. 1344 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1345 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1346 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 1347 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 1348 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 1349 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 1350 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 1351 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 1352 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 1353 * @see RJInternal#convertToNumber(String) 1354 */ 1355 public static Number parseNUMBER( 1356 JsonArray ja, int index, int FLAGS, Number defaultValue, 1357 Function<String, Number> optionalParser 1358 ) 1359 { 1360 return PARSE( 1361 ja, index, FLAGS, defaultValue, Number.class, optionalParser, 1362 RJInternal::convertToNumber, null /* Not Needed, convertToNumber won't throw */ 1363 ); 1364 } 1365 1366 1367 // ******************************************************************************************** 1368 // ******************************************************************************************** 1369 // Number from JsonObject 1370 // ******************************************************************************************** 1371 // ******************************************************************************************** 1372 1373 1374 /** 1375 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Number}. 1376 * <EMBED CLASS=defs DATA-TYPE='java.lang.Number' DATA-JTYPE=JsonNumber> 1377 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NUMBER> 1378 * @param jo Any instance of {@link JsonObject}. 1379 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1380 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 1381 * @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=JR_TON_JO> 1382 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_TON_JO> 1383 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 1384 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 1385 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNOEX> 1386 * @see JsonValue#getValueType() 1387 * @see RJInternal#convertToNumber(JsonNumber) 1388 */ 1389 public static Number getNUMBER 1390 (JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull) 1391 { 1392 if (! jo.containsKey(propertyName)) 1393 { 1394 if (isOptional) return null; 1395 else throw new JsonPropMissingException(jo, propertyName, NUMBER, Number.class); 1396 } 1397 1398 JsonValue jv = jo.get(propertyName); 1399 1400 switch (jv.getValueType()) 1401 { 1402 case NULL: 1403 1404 // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry 1405 // about what the meaning of "null" should be. 1406 1407 if (throwOnNull) throw new JsonNullObjException 1408 (jo, propertyName, NUMBER, Number.class); 1409 1410 else return null; 1411 1412 case NUMBER: return convertToNumber((JsonNumber) jv); 1413 1414 // The JsonObject propertydoes not contain a JsonNumber. 1415 default: throw new JsonTypeObjException 1416 (jo, propertyName, NUMBER, jv, Number.class); 1417 } 1418 } 1419 1420 /** 1421 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Number}. 1422 * <EMBED CLASS=defs DATA-TYPE='java.lang.Number' DATA-JTYPE=JsonNumber> 1423 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NUMBER> 1424 * @param jo Any instance of {@link JsonObject} 1425 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1426 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1427 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1428 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 1429 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 1430 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1431 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1432 * @see RJInternal#GET(JsonObject, String, int, Number, Class, Function, Function) 1433 * @see RJInternal#convertToNumber(JsonNumber) 1434 */ 1435 public static Number getNUMBER 1436 (JsonObject jo, String propertyName, int FLAGS, Number defaultValue) 1437 { 1438 return GET 1439 (jo, propertyName, FLAGS, defaultValue, Number.class, RJInternal::convertToNumber, null); 1440 } 1441 1442 /** 1443 * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to 1444 * a {@code java.lang.Number}, with either a user-provided parser, or the standard java 1445 * parser 1446 * <EMBED CLASS=defs DATA-TYPE='java.lang.Number' DATA-JTYPE=JsonString 1447 * DATA-PARSER='new BigDecimal'> 1448 * <BR /><BR />If {@code 'parser'} is passed null, the default parser is used, which is just 1449 * the {@code java.math.BigDecimal} constructor which accepts a {@code String}. What is done 1450 * with the parsed {@code BigDecimal} instance is explained below. 1451 * <BR /><BR /> 1452 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NUM_PARSE> 1453 * @param jo Any instance of {@link JsonObject} 1454 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1455 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1456 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1457 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 1458 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 1459 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 1460 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 1461 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1462 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1463 * @see RJInternal#PARSE(JsonObject, String, int, Number, Class, Function, Function, Function) 1464 * @see RJInternal#convertToNumber(String) 1465 */ 1466 public static Number parseNUMBER( 1467 JsonObject jo, String propertyName, int FLAGS, Number defaultValue, 1468 Function<String, Number> optionalParser 1469 ) 1470 { 1471 return PARSE( 1472 jo, propertyName, FLAGS, defaultValue, Number.class, optionalParser, 1473 RJInternal::convertToNumber, null /* Not Needed, convertToNumber won't throw */ 1474 ); 1475 } 1476 1477 1478 // ******************************************************************************************** 1479 // ******************************************************************************************** 1480 // Boolean from a JsonArray 1481 // ******************************************************************************************** 1482 // ******************************************************************************************** 1483 1484 1485 /** 1486 * Retrieve a {@link JsonArray} element, and transform it to a {@code 'boolean'} primitive 1487 * <EMBED CLASS=defs DATA-JTYPE='JSON-BOOLEAN' DATA-TYPE=boolean> 1488 * @param ja Any instance of {@link JsonArray} 1489 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 1490 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA> 1491 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 1492 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 1493 * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX> 1494 * @see #getBOOLEAN(JsonArray, int) 1495 */ 1496 public static boolean getBoolean(JsonArray ja, int index) 1497 { 1498 // This method (defined below) allows for null-returns, but "getBoolean" does not. 1499 // "getBOOLEAN" will only return null if the actual array location 'index' actually has 1500 // the word 'null' listed. 1501 1502 Boolean ret = getBOOLEAN(ja, index); 1503 1504 // In that case, throw the Null-Primitive Exception 1505 if (ret == null) throw new JsonNullPrimitiveArrException 1506 (ja, index, TRUE, boolean.class); 1507 1508 return ret; 1509 } 1510 1511 /** 1512 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Boolean}. 1513 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 1514 * <EMBED CLASS=defs DATA-JTYPE='JSON-BOOLEAN' DATA-TYPE='java.lang.Boolean'> 1515 * @param ja Any instance of {@link JsonArray} 1516 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 1517 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JA> 1518 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 1519 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 1520 * @see JsonValue#getValueType() 1521 * @see JsonValue.ValueType#TRUE 1522 * @see JsonValue.ValueType#FALSE 1523 * @see JsonValue#TRUE 1524 * @see JsonValue#FALSE 1525 */ 1526 public static Boolean getBOOLEAN(JsonArray ja, int index) 1527 { 1528 // This will throw an IndexOutOfBoundsException if the index is out of bounds. 1529 JsonValue jv = ja.get(index); 1530 1531 switch (jv.getValueType()) 1532 { 1533 // This method 'getBOOLEAN' allows for null-returns. If Json-Null, return Java-Null. 1534 case NULL: return null; 1535 1536 // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science 1537 case TRUE: return true; 1538 case FALSE: return false; 1539 1540 // The JsonValue at the specified array-index does not contain one of the two 1541 // Json-Boolean Types. Throw an exception. 1542 1543 default: throw new JsonTypeArrException(ja, index, TRUE, jv, Boolean.class); 1544 } 1545 } 1546 1547 /** 1548 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.Boolean}. 1549 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 1550 * <EMBED CLASS=defs DATA-JTYPE='JSON-BOOLEAN' DATA-TYPE='java.lang.Boolean'> 1551 * @param ja Any instance of {@link JsonArray} 1552 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 1553 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1554 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1555 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JA> 1556 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 1557 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1558 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1559 * @see JsonValue#getValueType() 1560 * @see JsonValue.ValueType#TRUE 1561 * @see JsonValue.ValueType#FALSE 1562 * @see JsonValue#TRUE 1563 * @see JsonValue#FALSE 1564 */ 1565 public static Boolean getBOOLEAN(JsonArray ja, int index, int FLAGS, boolean defaultValue) 1566 { 1567 // When TRUE, the index provided turned out to be outside of the bounds of the array. The 1568 // IndexOutOfBounds "handler" (the method called here) will check the FLAGS, and: 1569 // 1570 // 1) return the defaultValue (if Requested by 'FLAGS' for IOOBEX) 1571 // 2) return null (if Requested by 'FLAGS' for IOOBEX) 1572 // 3) throw IndexOutOfBoundsException 1573 1574 if (index >= ja.size()) return IOOBEX(ja, index, defaultValue, FLAGS); 1575 1576 JsonValue jv = ja.get(index); // Throw an IndexOutOfBoundsException 1577 1578 switch (jv.getValueType()) 1579 { 1580 // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullArrException 'handler' 1581 // will do one of the following: 1582 // 1583 // 1) return the defaultValue (if Requested by 'FLAGS' for JNAEX) 1584 // 2) return null (if Requested by 'FLAGS' for JNAEX) 1585 // 3) throw JsonNullArrException 1586 1587 case NULL: return JNAEX(ja, index, defaultValue, FLAGS, TRUE, Boolean.class); 1588 1589 // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science 1590 case TRUE: return true; 1591 case FALSE: return false; 1592 1593 // The JsonValue at the specified array-index does not contain one of the two 1594 // Json-Boolean Types. The "JsonTypeArrException Handler" will do one of these: 1595 // 1596 // 1) return the defaultValue (if Requested by 'FLAGS' for JTAEX) 1597 // 2) return null (if Requested by 'FLAGS' for JTAEX) 1598 // 3) throw JsonTypeArrException 1599 1600 default: return JTAEX(ja, index, defaultValue, FLAGS, TRUE, jv, Boolean.class); 1601 } 1602 } 1603 1604 /** 1605 * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to a 1606 * {@code java.lang.Boolean}, with either a user-provided parser, or the standard java 1607 * boolean parser 1608 * <EMBED CLASS=defs DATA-TYPE='java.lang.Boolean' DATA-PTYPE=boolean 1609 * DATA-JTYPE=JsonString DATA-PARSER='Boolean.parseBoolean'> 1610 * @param ja Any instance of {@link JsonArray} 1611 * @param index The array index containing the {@link JsonString} element to retrieve. 1612 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1613 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1614 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 1615 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JA> 1616 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 1617 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPAEX> 1618 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> 1619 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> 1620 */ 1621 public static Boolean parseBOOLEAN( 1622 JsonArray ja, int index, int FLAGS, boolean defaultValue, 1623 Function<String, Boolean> optionalParser 1624 ) 1625 { 1626 // When TRUE, the index provided turned out to be outside of the bounds of the array. The 1627 // IndexOutOfBounds "handler" (the method called here) will check the FLAGS, and: 1628 // 1629 // 1) return the defaultValue (if Requested by 'FLAGS' for IOOBEX) 1630 // 2) return null (if Requested by 'FLAGS' for IOOBEX) 1631 // 3) throw IndexOutOfBoundsException 1632 1633 if (index >= ja.size()) return IOOBEX(ja, index, defaultValue, FLAGS); 1634 1635 JsonValue jv = ja.get(index); 1636 1637 switch (jv.getValueType()) 1638 { 1639 // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullArrException 'handler' 1640 // will do one of the following: 1641 // 1642 // 1) return the defaultValue (if Requested by 'FLAGS' for JNAEX) 1643 // 2) return null (if Requested by 'FLAGS' for JNAEX) 1644 // 3) throw JsonNullArrException 1645 1646 case NULL: return JNAEX(ja, index, defaultValue, FLAGS, STRING, Boolean.class); 1647 1648 case STRING: 1649 1650 String s = ((JsonString) jv).getString(); 1651 1652 // NOTE: This isn't actually an "Exception Case", and if the user hasn't made 1653 // a request, the empty-string is passed to whatever parser is configured 1654 1655 if (s.length() == 0) 1656 { 1657 if ((FLAGS & RETURN_NULL_ON_0LEN_STR) != 0) return null; 1658 if ((FLAGS & RETURN_DEFVAL_ON_0LEN_STR) != 0) return defaultValue; 1659 if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; 1660 if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; 1661 } 1662 1663 try 1664 { 1665 return (optionalParser != null) 1666 ? optionalParser.apply(s) 1667 : Boolean.parseBoolean(s.trim()); 1668 } 1669 1670 // HANDLER STRIKES AGAIN! - but this time for "JsonStrParseArrException" 1671 // RETURNS: null, or defaultValue, (otherwise throws JsonStrParseArrException) 1672 1673 catch (Exception e) 1674 { return JSPAEX(e, ja, index, defaultValue, FLAGS, jv, Boolean.class); } 1675 1676 // The JsonValue at the specified array-index does not contain an JsonString. 1677 // The "JsonTypeArrException Handler" will do one of these: 1678 // 1679 // 1) return the defaultValue (if Requested by 'FLAGS' for JTAEX) 1680 // 2) return null (if Requested by 'FLAGS' for JTAEX) 1681 // 3) throw JsonTypeArrException 1682 1683 default: return JTAEX(ja, index, defaultValue, FLAGS, STRING, jv, Boolean.class); 1684 } 1685 } 1686 1687 1688 // ******************************************************************************************** 1689 // ******************************************************************************************** 1690 // Boolean from a JsonObject 1691 // ******************************************************************************************** 1692 // ******************************************************************************************** 1693 1694 1695 /** 1696 * Extract a {@link JsonObject} property, and transform it to a {@code 'boolean'} primitive 1697 * <EMBED CLASS=defs DATA-TYPE=boolean DATA-JTYPE='JSON-BOOLEAN'> 1698 * @param jo Any instance of {@link JsonObject} 1699 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1700 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO> 1701 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR> 1702 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 1703 * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX> 1704 * @see #getBOOLEAN(JsonObject, String, boolean) 1705 */ 1706 public static boolean getBoolean(JsonObject jo, String propertyName) 1707 { 1708 // Since 'false' is passed, a null return-value will mean that the property was actually 1709 // set to null in the JsonObject itself. (It *IS NOT* missing, it is present, but rather 1710 // declared null) 1711 1712 Boolean ret = getBOOLEAN(jo, propertyName, false); 1713 1714 if (ret == null) throw new JsonNullPrimitiveObjException 1715 (jo, propertyName, TRUE, boolean.class); 1716 1717 // NOTE: Java's Type-Stuff can automatically convert Boolean -> boolean 1718 else return ret; 1719 } 1720 1721 /** 1722 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.Boolean}. 1723 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 1724 * <EMBED CLASS=defs DATA-TYPE='java.lang.Boolean' DATA-JTYPE='JSON-BOOLEAN'> 1725 * @param jo Any instance of {@link JsonObject}. 1726 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1727 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 1728 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_JO> 1729 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 1730 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 1731 * @see JsonValue#getValueType() 1732 * @see JsonValue.ValueType#TRUE 1733 * @see JsonValue.ValueType#FALSE 1734 * @see JsonValue#TRUE 1735 * @see JsonValue#FALSE 1736 */ 1737 public static Boolean getBOOLEAN(JsonObject jo, String propertyName, boolean isOptional) 1738 { 1739 if (! jo.containsKey(propertyName)) 1740 { 1741 if (isOptional) return null; 1742 1743 throw new JsonPropMissingException(jo, propertyName, TRUE, Boolean.class); 1744 } 1745 1746 JsonValue jv = jo.get(propertyName); 1747 1748 switch (jv.getValueType()) 1749 { 1750 // This method 'getBOOLEAN' allows for null-returns. If Json-Null, return Java-Null. 1751 case NULL: return null; 1752 1753 // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science 1754 case TRUE: return true; 1755 case FALSE: return false; 1756 1757 // The JsonValue at the specified array-index does not contain one of the two 1758 // Json-Boolean Types. Throw an exception. 1759 1760 default: throw new JsonTypeObjException 1761 (jo, propertyName, TRUE, jv, Boolean.class); 1762 } 1763 } 1764 1765 /** 1766 * Retrieve a {@link JsonObject} property, and transform it to a {@code java.lang.Boolean}. 1767 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_NULLNOTE> 1768 * <EMBED CLASS=defs DATA-JTYPE='JSON-BOOLEAN' DATA-TYPE='java.lang.Boolean'> 1769 * @param jo Any instance of {@link JsonObject} 1770 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1771 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1772 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1773 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_RET_JO> 1774 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 1775 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1776 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1777 * @see JsonValue#getValueType() 1778 * @see JsonValue.ValueType#TRUE 1779 * @see JsonValue.ValueType#FALSE 1780 * @see JsonValue#TRUE 1781 * @see JsonValue#FALSE 1782 */ 1783 public static Boolean getBOOLEAN 1784 (JsonObject jo, String propertyName, int FLAGS, boolean defaultValue) 1785 { 1786 JsonValue jv = jo.get(propertyName); 1787 1788 // When TRUE, the user-specified 'property' (named by 'propertyName') isn't actually one 1789 // of the listed properties inside the JsonObject. The JsonPropMissingException "handler" 1790 // (the method called here) will check the FLAGS, and: 1791 // 1792 // 1) return the defaultValue (if Requested by 'FLAGS' for JPMEX) 1793 // 2) return null (if Requested by 'FLAGS' for JPMEX) 1794 // 3) throw JsonPropMissingException 1795 1796 if (jv == null) return JPMEX(jo, propertyName, defaultValue, FLAGS, TRUE, Boolean.class); 1797 1798 switch (jv.getValueType()) 1799 { 1800 // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullObjException 'handler' 1801 // will do one of the following: 1802 // 1803 // 1) return the defaultValue (if Requested by 'FLAGS' for JNOEX) 1804 // 2) return null (if Requested by 'FLAGS' for JNOEX) 1805 // 3) throw JsonNullArrException 1806 1807 case NULL: return JNOEX(jo, propertyName, defaultValue, FLAGS, TRUE, Boolean.class); 1808 1809 // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science 1810 case TRUE: return true; 1811 case FALSE: return false; 1812 1813 // The property contains a JsonValue that is not one of the two 1814 // Json-Boolean Types. Throw an exception. 1815 1816 // The JsonValue of 'propertyName' does not contain an JsonString. 1817 // The "JsonTypeObjException Handler" will do one of these: 1818 // 1819 // 1) return the defaultValue (if Requested by 'FLAGS' for JTOEX) 1820 // 2) return null (if Requested by 'FLAGS' for JTOEX) 1821 // 3) throw JsonTypeObjException 1822 1823 default: return JTOEX(jo, propertyName, defaultValue, FLAGS, TRUE, jv, Boolean.class); 1824 } 1825 } 1826 1827 /** 1828 * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to 1829 * a {@code java.lang.Boolean}, with either a user-provided parser, or the standard java 1830 * boolean parser 1831 * <EMBED CLASS=defs DATA-TYPE='java.lang.Float' DATA-PTYPE=float 1832 * DATA-JTYPE=JsonString DATA-PARSER='Float.parseFloat'> 1833 * @param jo Any instance of {@link JsonObject} 1834 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1835 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 1836 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRF_DEFV> 1837 * @param parser <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSER> 1838 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRF_PARSERET_JO> 1839 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 1840 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JSPOEX> 1841 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> 1842 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> 1843 */ 1844 public static Boolean parseBOOLEAN( 1845 JsonObject jo, String propertyName, int FLAGS, boolean defaultValue, 1846 Function<String, Boolean> parser 1847 ) 1848 { 1849 JsonValue jv = jo.get(propertyName); 1850 1851 // When TRUE, the user-specified 'property' (named by 'propertyName') isn't actually one 1852 // of the listed properties inside the JsonObject. The JsonPropMissingException "handler" 1853 // (the method called here) will check the FLAGS, and: 1854 // 1855 // 1) return the defaultValue (if Requested by 'FLAGS' for JPMEX) 1856 // 2) return null (if Requested by 'FLAGS' for JPMEX) 1857 // 3) throw JsonPropMissingException 1858 1859 if (jv == null) return JPMEX(jo, propertyName, defaultValue, FLAGS, STRING, Boolean.class); 1860 1861 switch (jv.getValueType()) 1862 { 1863 // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullObjException 'handler' 1864 // will do one of the following: 1865 // 1866 // 1) return the defaultValue (if Requested by 'FLAGS' for JNOEX) 1867 // 2) return null (if Requested by 'FLAGS' for JNOEX) 1868 // 3) throw JsonNullArrException 1869 1870 case NULL: return JNOEX(jo, propertyName, defaultValue, FLAGS, STRING, Boolean.class); 1871 1872 case STRING: 1873 1874 String s = ((JsonString) jv).getString(); 1875 1876 // NOTE: This isn't actually an "Exception Case", and if the user hasn't made 1877 // a request, the empty-string is passed to whatever parser is configured 1878 1879 if (s.length() == 0) 1880 { 1881 if ((FLAGS & RETURN_NULL_ON_0LEN_STR) != 0) return null; 1882 if ((FLAGS & RETURN_DEFVAL_ON_0LEN_STR) != 0) return defaultValue; 1883 if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; 1884 if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; 1885 } 1886 1887 try 1888 { return (parser != null) ? parser.apply(s) : Boolean.parseBoolean(s.trim()); } 1889 1890 // HANDLER STRIKES AGAIN! - but this time for "JsonStrParseObjException" 1891 // RETURNS: null, or defaultValue, (otherwise throws JsonStrParseObjException) 1892 1893 catch (Exception e) 1894 { return JSPOEX(e, jo, propertyName, defaultValue, FLAGS, jv, Boolean.class); } 1895 1896 // The JsonValue of 'propertyName' does not contain an JsonString. 1897 // The "JsonTypeObjException Handler" will do one of these: 1898 // 1899 // 1) return the defaultValue (if Requested by 'FLAGS' for JTOEX) 1900 // 2) return null (if Requested by 'FLAGS' for JTOEX) 1901 // 3) throw JsonTypeObjException 1902 1903 default: return JTOEX(jo, propertyName, defaultValue, FLAGS, STRING, jv, Boolean.class); 1904 } 1905 } 1906 1907 1908 // ******************************************************************************************** 1909 // ******************************************************************************************** 1910 // String 1911 // ******************************************************************************************** 1912 // ******************************************************************************************** 1913 1914 1915 /** 1916 * Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.String}. 1917 * <EMBED CLASS=defs DATA-JTYPE=JsonString DATA-TYPE='java.lang.String'> 1918 * @param ja Any instance of {@link JsonArray} 1919 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 1920 * @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=JR_TON_JA> 1921 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_TON_JA> 1922 * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} 1923 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 1924 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNAEX> 1925 * @see JsonValue#getValueType() 1926 * @see JsonValue.ValueType#STRING 1927 */ 1928 public static String getString(JsonArray ja, int index, boolean throwOnNull) 1929 { 1930 // This will throw an IndexOutOfBoundsException if the index is out of bounds. 1931 JsonValue jv = ja.get(index); 1932 1933 switch (jv.getValueType()) 1934 { 1935 case NULL: 1936 1937 // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry 1938 // about what the meaning of "null" should be. 1939 1940 if (throwOnNull) throw new JsonNullArrException(ja, index, STRING, String.class); 1941 else return null; 1942 1943 case STRING: return ((JsonString) jv).getString(); 1944 1945 // The JsonValue at the specified array-index does not contain a JsonString. 1946 default: throw new JsonTypeArrException(ja, index, STRING, jv, String.class); 1947 } 1948 } 1949 1950 /** 1951 * Extract a {@link JsonObject} property, and transform it to a {@code java.lang.String}. 1952 * <EMBED CLASS=defs DATA-TYPE='java.lang.String' DATA-JTYPE=JsonString> 1953 * @param jo Any instance of {@link JsonObject}. 1954 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 1955 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 1956 * @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=JR_TON_JO> 1957 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_TON_JO> 1958 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 1959 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 1960 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNOEX> 1961 * @see JsonValue#getValueType() 1962 * @see JsonValue.ValueType#STRING 1963 */ 1964 public static String getString 1965 (JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull) 1966 { 1967 if (! jo.containsKey(propertyName)) 1968 { 1969 if (isOptional) return null; 1970 1971 throw new JsonPropMissingException(jo, propertyName, STRING, String.class); 1972 } 1973 1974 JsonValue jv = jo.get(propertyName); 1975 1976 switch (jv.getValueType()) 1977 { 1978 case NULL: 1979 1980 // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry 1981 // about what the meaning of "null" should be. 1982 1983 if (throwOnNull) throw new JsonNullObjException 1984 (jo, propertyName, STRING, String.class); 1985 1986 else return null; 1987 1988 case STRING: return ((JsonString) jv).getString(); 1989 1990 // The JsonObject propertydoes not contain a JsonString. 1991 default: throw new JsonTypeObjException(jo, propertyName, STRING, jv, String.class); 1992 } 1993 } 1994 1995 1996 // ******************************************************************************************** 1997 // ******************************************************************************************** 1998 // ReadJSON.XL 1999 // ******************************************************************************************** 2000 // ******************************************************************************************** 2001 2002 2003 /** 2004 * More utilities for parsing and converting JSON Data into Java Data Types. 2005 * 2006 * <EMBED CLASS='external-html' DATA-FILE-ID=GLASS_FISH_NOTE> 2007 * <EMBED CLASS='external-html' DATA-FILE-ID=READ_JSON_XL> 2008 * 2009 * @see Json 2010 * @see JsonObject 2011 * @see JsonArray 2012 */ 2013 @Torello.JavaDoc.StaticFunctional 2014 @Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="JSON_JDHBI") 2015 public static class XL 2016 { 2017 private XL() { } 2018 2019 2020 // **************************************************************************************** 2021 // **************************************************************************************** 2022 // JsonArray index location to Number 2023 // **************************************************************************************** 2024 // **************************************************************************************** 2025 2026 2027 /** 2028 * <EMBED CLASS=defs DATA-TYPE=Integer DATA-PARSER='Integer.parseInt'> 2029 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2030 * @param ja Any {@link JsonArray} 2031 * @param i Any index into the {@code JsonArray} 2032 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2033 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2034 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2035 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2036 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2037 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2038 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2039 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2040 * @see #getINTEGER(JsonArray, int, int, int) 2041 * @see #parseINTEGER(JsonArray, int, int, int, Function) 2042 */ 2043 public static Integer getINTEGER( 2044 JsonArray ja, int i, int FLAGS, int defaultValue, 2045 Function<String, Integer> optionalParser 2046 ) 2047 { 2048 JsonValue.ValueType t; 2049 2050 return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) 2051 ? ReadJSON.getINTEGER(ja, i, FLAGS, defaultValue) 2052 : ReadJSON.parseINTEGER(ja, i, FLAGS, defaultValue, optionalParser); 2053 } 2054 2055 /** 2056 * <EMBED CLASS=defs DATA-TYPE=Long DATA-PARSER='Long.parseLong'> 2057 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2058 * @param ja Any {@link JsonArray} 2059 * @param i Any index into the {@code JsonArray} 2060 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2061 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2062 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2063 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2064 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2065 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2066 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2067 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2068 * @see #getLONG(JsonArray, int, int, long) 2069 * @see #parseLONG(JsonArray, int, int, long, Function) 2070 */ 2071 public static Long getLONG( 2072 JsonArray ja, int i, int FLAGS, long defaultValue, 2073 Function<String, Long> optionalParser 2074 ) 2075 { 2076 JsonValue.ValueType t; 2077 2078 return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) 2079 ? ReadJSON.getLONG(ja, i, FLAGS, defaultValue) 2080 : ReadJSON.parseLONG(ja, i, FLAGS, defaultValue, optionalParser); 2081 } 2082 2083 /** 2084 * <EMBED CLASS=defs DATA-TYPE=Short DATA-PARSER='Short.parseShort'> 2085 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2086 * @param ja Any {@link JsonArray} 2087 * @param i Any index into the {@code JsonArray} 2088 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2089 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2090 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2091 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2092 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2093 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2094 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2095 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2096 * @see #getSHORT(JsonArray, int, int, short) 2097 * @see #parseSHORT(JsonArray, int, int, short, Function) 2098 */ 2099 public static Short getSHORT( 2100 JsonArray ja, int i, int FLAGS, short defaultValue, 2101 Function<String, Short> optionalParser 2102 ) 2103 { 2104 JsonValue.ValueType t; 2105 2106 return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) 2107 ? ReadJSON.getSHORT(ja, i, FLAGS, defaultValue) 2108 : ReadJSON.parseSHORT(ja, i, FLAGS, defaultValue, optionalParser); 2109 } 2110 2111 /** 2112 * <EMBED CLASS=defs DATA-TYPE=Byte DATA-PARSER='Byte.parseByte'> 2113 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2114 * @param ja Any {@link JsonArray} 2115 * @param i Any index into the {@code JsonArray} 2116 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2117 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2118 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2119 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2120 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2121 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2122 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2123 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2124 * @see #getBYTE(JsonArray, int, int, byte) 2125 * @see #parseBYTE(JsonArray, int, int, byte, Function) 2126 */ 2127 public static Byte getBYTE( 2128 JsonArray ja, int i, int FLAGS, byte defaultValue, 2129 Function<String, Byte> optionalParser 2130 ) 2131 { 2132 JsonValue.ValueType t; 2133 2134 return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) 2135 ? ReadJSON.getBYTE(ja, i, FLAGS, defaultValue) 2136 : ReadJSON.parseBYTE(ja, i, FLAGS, defaultValue, optionalParser); 2137 } 2138 2139 /** 2140 * <EMBED CLASS=defs DATA-TYPE=Double DATA-PARSER='Double.parseDouble'> 2141 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2142 * @param ja Any {@link JsonArray} 2143 * @param i Any index into the {@code JsonArray} 2144 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2145 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2146 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2147 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2148 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2149 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2150 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2151 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2152 * @see #getDOUBLE(JsonArray, int, int, double) 2153 * @see #parseDOUBLE(JsonArray, int, int, double, Function) 2154 */ 2155 public static Double getDOUBLE( 2156 JsonArray ja, int i, int FLAGS, double defaultValue, 2157 Function<String, Double> optionalParser 2158 ) 2159 { 2160 JsonValue.ValueType t; 2161 2162 return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) 2163 ? ReadJSON.getDOUBLE(ja, i, FLAGS, defaultValue) 2164 : ReadJSON.parseDOUBLE(ja, i, FLAGS, defaultValue, optionalParser); 2165 } 2166 2167 /** 2168 * <EMBED CLASS=defs DATA-TYPE=Float DATA-PARSER='Float.parseFloat'> 2169 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2170 * @param ja Any {@link JsonArray} 2171 * @param i Any index into the {@code JsonArray} 2172 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2173 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2174 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2175 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2176 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2177 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2178 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2179 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2180 * @see #getFLOAT(JsonArray, int, int, float) 2181 * @see #parseFLOAT(JsonArray, int, int, float, Function) 2182 */ 2183 public static Float getFLOAT( 2184 JsonArray ja, int i, int FLAGS, float defaultValue, 2185 Function<String, Float> optionalParser 2186 ) 2187 { 2188 JsonValue.ValueType t; 2189 2190 return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) 2191 ? ReadJSON.getFLOAT(ja, i, FLAGS, defaultValue) 2192 : ReadJSON.parseFLOAT(ja, i, FLAGS, defaultValue, optionalParser); 2193 } 2194 2195 /** 2196 * <EMBED CLASS=defs DATA-TYPE=Number DATA-PARSER='new BigDecimal'> 2197 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2198 * @param ja Any {@link JsonArray} 2199 * @param i Any index into the {@code JsonArray} 2200 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2201 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2202 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2203 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2204 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2205 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2206 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2207 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2208 * @see #getNUMBER(JsonArray, int, int, Number) 2209 * @see #parseNUMBER(JsonArray, int, int, Number, Function) 2210 */ 2211 public static Number getNUMBER( 2212 JsonArray ja, int i, int FLAGS, Number defaultValue, 2213 Function<String, Number> optionalParser 2214 ) 2215 { 2216 JsonValue.ValueType t; 2217 2218 return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) 2219 ? ReadJSON.getNUMBER(ja, i, FLAGS, defaultValue) 2220 : ReadJSON.parseNUMBER(ja, i, FLAGS, defaultValue, optionalParser); 2221 } 2222 2223 /** 2224 * <EMBED CLASS=defs DATA-TYPE=Boolean DATA-PARSER='Boolean.parseBoolean'> 2225 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JA> 2226 * @param ja Any {@link JsonArray} 2227 * @param i Any index into the {@code JsonArray} 2228 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2229 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2230 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2231 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JA> 2232 * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> 2233 * @throws JsonStrParseArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPAEX> 2234 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNAEX> 2235 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTAEX> 2236 * @see #getBOOLEAN(JsonArray, int, int, boolean) 2237 * @see #parseBOOLEAN(JsonArray, int, int, boolean, Function) 2238 */ 2239 public static Boolean getBOOLEAN( 2240 JsonArray ja, int i, int FLAGS, boolean defaultValue, 2241 Function<String, Boolean> optionalParser 2242 ) 2243 { 2244 JsonValue.ValueType t; 2245 2246 return ( (i >= ja.size()) 2247 || ((t=ja.get(i).getValueType()) == TRUE) 2248 || (t == FALSE) 2249 || (t != STRING) 2250 ) 2251 ? ReadJSON.getBOOLEAN(ja, i, FLAGS, defaultValue) 2252 : ReadJSON.parseBOOLEAN(ja, i, FLAGS, defaultValue, optionalParser); 2253 } 2254 2255 2256 // **************************************************************************************** 2257 // **************************************************************************************** 2258 // JsonObject Property to Number 2259 // **************************************************************************************** 2260 // **************************************************************************************** 2261 2262 2263 /** 2264 * <EMBED CLASS=defs DATA-TYPE=Integer DATA-PARSER='Integer.parseInt'> 2265 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2266 * @param jo Any {@link JsonObject} 2267 * @param propertyName Any of the properties defined in the {@code JsonObject} 2268 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2269 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2270 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2271 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2272 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2273 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2274 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2275 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2276 * @see #getINTEGER(JsonObject, String, int, int) 2277 * @see #parseINTEGER(JsonObject, String, int, int, Function) 2278 */ 2279 public static Integer getINTEGER( 2280 JsonObject jo, String propertyName, int FLAGS, int defaultValue, 2281 Function<String, Integer> optionalParser 2282 ) 2283 { 2284 JsonValue.ValueType t; 2285 2286 return ( (! jo.containsKey(propertyName)) 2287 || ((t = jo.get(propertyName).getValueType()) == NUMBER) 2288 || (t != STRING) 2289 ) 2290 ? ReadJSON.getINTEGER(jo, propertyName, FLAGS, defaultValue) 2291 : ReadJSON.parseINTEGER(jo, propertyName, FLAGS, defaultValue, optionalParser); 2292 } 2293 2294 /** 2295 * <EMBED CLASS=defs DATA-TYPE=Long DATA-PARSER='Long.parseLong'> 2296 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2297 * @param jo Any {@link JsonObject} 2298 * @param propertyName Any of the properties defined in the {@code JsonObject} 2299 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2300 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2301 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2302 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2303 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2304 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2305 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2306 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2307 * @see #getLONG(JsonObject, String, int, long) 2308 * @see #parseLONG(JsonObject, String, int, long, Function) 2309 */ 2310 public static Long getLONG( 2311 JsonObject jo, String propertyName, int FLAGS, long defaultValue, 2312 Function<String, Long> optionalParser 2313 ) 2314 { 2315 JsonValue.ValueType t; 2316 2317 return ( (! jo.containsKey(propertyName)) 2318 || ((t = jo.get(propertyName).getValueType()) == NUMBER) 2319 || (t != STRING) 2320 ) 2321 ? ReadJSON.getLONG(jo, propertyName, FLAGS, defaultValue) 2322 : ReadJSON.parseLONG(jo, propertyName, FLAGS, defaultValue, optionalParser); 2323 } 2324 2325 /** 2326 * <EMBED CLASS=defs DATA-TYPE=Short DATA-PARSER='Short.parseShort'> 2327 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2328 * @param jo Any {@link JsonObject} 2329 * @param propertyName Any of the properties defined in the {@code JsonObject} 2330 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2331 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2332 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2333 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2334 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2335 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2336 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2337 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2338 * @see #getSHORT(JsonObject, String, int, short) 2339 * @see #parseSHORT(JsonObject, String, int, short, Function) 2340 */ 2341 public static Short getSHORT( 2342 JsonObject jo, String propertyName, int FLAGS, short defaultValue, 2343 Function<String, Short> optionalParser 2344 ) 2345 { 2346 JsonValue.ValueType t; 2347 2348 return ( (! jo.containsKey(propertyName)) 2349 || ((t = jo.get(propertyName).getValueType()) == NUMBER) 2350 || (t != STRING) 2351 ) 2352 ? ReadJSON.getSHORT(jo, propertyName, FLAGS, defaultValue) 2353 : ReadJSON.parseSHORT(jo, propertyName, FLAGS, defaultValue, optionalParser); 2354 } 2355 2356 /** 2357 * <EMBED CLASS=defs DATA-TYPE=Byte DATA-PARSER='Byte.parseByte'> 2358 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2359 * @param jo Any {@link JsonObject} 2360 * @param propertyName Any of the properties defined in the {@code JsonObject} 2361 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2362 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2363 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2364 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2365 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2366 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2367 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2368 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2369 * @see #getBYTE(JsonObject, String, int, byte) 2370 * @see #parseBYTE(JsonObject, String, int, byte, Function) 2371 */ 2372 public static Byte getBYTE( 2373 JsonObject jo, String propertyName, int FLAGS, byte defaultValue, 2374 Function<String, Byte> optionalParser 2375 ) 2376 { 2377 JsonValue.ValueType t; 2378 2379 return ( (! jo.containsKey(propertyName)) 2380 || ((t = jo.get(propertyName).getValueType()) == NUMBER) 2381 || (t != STRING) 2382 ) 2383 ? ReadJSON.getBYTE(jo, propertyName, FLAGS, defaultValue) 2384 : ReadJSON.parseBYTE(jo, propertyName, FLAGS, defaultValue, optionalParser); 2385 } 2386 2387 /** 2388 * <EMBED CLASS=defs DATA-TYPE=Double DATA-PARSER='Double.parseDouble'> 2389 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2390 * @param jo Any {@link JsonObject} 2391 * @param propertyName Any of the properties defined in the {@code JsonObject} 2392 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2393 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2394 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2395 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2396 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2397 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2398 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2399 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2400 * @see #getDOUBLE(JsonObject, String, int, double) 2401 * @see #parseDOUBLE(JsonObject, String, int, double, Function) 2402 */ 2403 public static Double getDOUBLE( 2404 JsonObject jo, String propertyName, int FLAGS, double defaultValue, 2405 Function<String, Double> optionalParser 2406 ) 2407 { 2408 JsonValue.ValueType t; 2409 2410 return ( (! jo.containsKey(propertyName)) 2411 || ((t = jo.get(propertyName).getValueType()) == NUMBER) 2412 || (t != STRING) 2413 ) 2414 ? ReadJSON.getDOUBLE(jo, propertyName, FLAGS, defaultValue) 2415 : ReadJSON.parseDOUBLE(jo, propertyName, FLAGS, defaultValue, optionalParser); 2416 } 2417 2418 /** 2419 * <EMBED CLASS=defs DATA-TYPE=Float DATA-PARSER='Float.parseFloat'> 2420 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2421 * @param jo Any {@link JsonObject} 2422 * @param propertyName Any of the properties defined in the {@code JsonObject} 2423 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2424 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2425 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2426 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2427 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2428 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2429 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2430 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2431 * @see #getFLOAT(JsonObject, String, int, float) 2432 * @see #parseFLOAT(JsonObject, String, int, float, Function) 2433 */ 2434 public static Float getFLOAT( 2435 JsonObject jo, String propertyName, int FLAGS, float defaultValue, 2436 Function<String, Float> optionalParser 2437 ) 2438 { 2439 JsonValue.ValueType t; 2440 2441 return ( (! jo.containsKey(propertyName)) 2442 || ((t = jo.get(propertyName).getValueType()) == NUMBER) 2443 || (t != STRING) 2444 ) 2445 ? ReadJSON.getFLOAT(jo, propertyName, FLAGS, defaultValue) 2446 : ReadJSON.parseFLOAT(jo, propertyName, FLAGS, defaultValue, optionalParser); 2447 } 2448 2449 /** 2450 * <EMBED CLASS=defs DATA-TYPE=Number DATA-PARSER='new BigDecimal'> 2451 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2452 * @param jo Any {@link JsonObject} 2453 * @param propertyName Any of the properties defined in the {@code JsonObject} 2454 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2455 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2456 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2457 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2458 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2459 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2460 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2461 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2462 * @see #getNUMBER(JsonObject, String, int, Number) 2463 * @see #parseNUMBER(JsonObject, String, int, Number, Function) 2464 */ 2465 public static Number getNUMBER( 2466 JsonObject jo, String propertyName, int FLAGS, Number defaultValue, 2467 Function<String, Number> optionalParser 2468 ) 2469 { 2470 JsonValue.ValueType t; 2471 2472 return ( (! jo.containsKey(propertyName)) 2473 || ((t = jo.get(propertyName).getValueType()) == NUMBER) 2474 || (t != STRING) 2475 ) 2476 ? ReadJSON.getNUMBER(jo, propertyName, FLAGS, defaultValue) 2477 : ReadJSON.parseNUMBER(jo, propertyName, FLAGS, defaultValue, optionalParser); 2478 } 2479 2480 /** 2481 * <EMBED CLASS=defs DATA-TYPE=Boolean DATA-PARSER='Boolean.parseBoolean'> 2482 * <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DESC_JO> 2483 * @param jo Any {@link JsonObject} 2484 * @param propertyName Any of the properties defined in the {@code JsonObject} 2485 * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} 2486 * @param defaultValue <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_DEFVAL> 2487 * @param optionalParser <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_PARSER> 2488 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_RET_JO> 2489 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> 2490 * @throws JsonStrParseObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JSPOEX> 2491 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JNOEX> 2492 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRXL_JTOEX> 2493 * @see #getBOOLEAN(JsonObject, String, int, boolean) 2494 * @see #parseBOOLEAN(JsonObject, String, int, boolean, Function) 2495 */ 2496 public static Boolean getBOOLEAN( 2497 JsonObject jo, String propertyName, int FLAGS, boolean defaultValue, 2498 Function<String, Boolean> optionalParser 2499 ) 2500 { 2501 JsonValue.ValueType t; 2502 2503 return ( (! jo.containsKey(propertyName)) 2504 || ((t = jo.get(propertyName).getValueType()) == TRUE) 2505 || (t == FALSE) 2506 || (t != STRING) 2507 ) 2508 ? ReadJSON.getBOOLEAN(jo, propertyName, FLAGS, defaultValue) 2509 : ReadJSON.parseBOOLEAN(jo, propertyName, FLAGS, defaultValue, optionalParser); 2510 } 2511 2512 2513 // **************************************************************************************** 2514 // **************************************************************************************** 2515 // Object 2516 // **************************************************************************************** 2517 // **************************************************************************************** 2518 2519 2520 /** 2521 * Retrieve a {@link JsonArray} element, and transform it to a Java {@code Object} (POJO). 2522 * <EMBED CLASS='external-HTML' DATA-FILE-ID=JR_PC_NOTE> 2523 * <EMBED CLASS=defs DATA-JTYPE=JsonObject DATA-TYPE='Type Parameter T'> 2524 * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=JR_TYPE_T> 2525 * @param ja Any instance of {@link JsonArray} 2526 * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA> 2527 * @param c <EMBED CLASS='external-html' DATA-FILE-ID=JR_CLASS> 2528 * @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=JR_TON_JA> 2529 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_TON_JA> 2530 * @throws IndexOutOfBoundsException If {@code 'index'} is out of bounds of {@code 'ja'} 2531 * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX> 2532 * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNAEX> 2533 * @throws JsonException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JEX_REFL> 2534 */ 2535 public static <T> T getObject(JsonArray ja, int index, Class<T> c, boolean throwOnNull) 2536 { 2537 // This will throw an IndexOutOfBoundsException if the index is out of bounds. 2538 JsonValue jv = ja.get(index); 2539 2540 switch (jv.getValueType()) 2541 { 2542 case NULL: 2543 2544 // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry 2545 // about what the meaning of "null" should be. 2546 2547 if (throwOnNull) throw new JsonNullArrException(ja, index, OBJECT, c); 2548 else return getObject(null, c); 2549 2550 case OBJECT: return getObject((JsonObject) jv, c); 2551 2552 // The JsonValue at the specified array-index does not contain a JsonObject. 2553 default: throw new JsonTypeArrException(ja, index, OBJECT, jv, c); 2554 } 2555 } 2556 2557 /** 2558 * Extract a {@link JsonObject} property, and transform it to a Java {@code Object} (POJO). 2559 * <EMBED CLASS='external-HTML' DATA-FILE-ID=JR_PC_NOTE> 2560 * <EMBED CLASS=defs DATA-TYPE='Type Parameter T' DATA-JTYPE=JsonObject> 2561 * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=JR_TYPE_T> 2562 * @param jo Any instance of {@link JsonObject}. 2563 * @param propertyName <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO> 2564 * @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=JR_ISOPT_JO> 2565 * @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=JR_TON_JO> 2566 * @return <EMBED CLASS='external-html' DATA-FILE-ID=JR_RET_TON_JO> 2567 * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX> 2568 * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX> 2569 * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNOEX> 2570 * @throws JsonException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JEX_REFL> 2571 */ 2572 public static <T> T getObject( 2573 JsonObject jo, String propertyName, Class<T> c, boolean isOptional, 2574 boolean throwOnNull 2575 ) 2576 { 2577 if (! jo.containsKey(propertyName)) 2578 { 2579 if (isOptional) return null; 2580 2581 throw new JsonPropMissingException(jo, propertyName, OBJECT, c); 2582 } 2583 2584 JsonValue jv = jo.get(propertyName); 2585 2586 switch (jv.getValueType()) 2587 { 2588 case NULL: 2589 2590 // This is simple-stuff (not rocket-science). "Type Mapping" Code has to 2591 // worry about what the meaning of "null" should be. 2592 2593 if (throwOnNull) throw new JsonNullObjException(jo, propertyName, OBJECT, c); 2594 2595 return getObject(null, c); 2596 2597 case OBJECT: return getObject((JsonObject) jv, c); 2598 2599 // The JsonObject propertydoes not contain a JsonObject. 2600 default: throw new JsonTypeObjException(jo, propertyName, OBJECT, jv, c); 2601 } 2602 } 2603 2604 /** 2605 * This class contains a lot of the reason / impetus for writing {@code 'ReadJSON'}. This 2606 * does converts a {@link JsonObject} into a Java-Object. The actual binding must be 2607 * implemented by the programmer - because the class-type that is passed to this method 2608 * (parameter {@code 'c'}) must have a constructor accepting this {@code JsonObject}. 2609 * 2610 * <EMBED CLASS='external-html' DATA-FILE-ID=JR_PC_NOTE> 2611 * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=JR_TYPE_T> 2612 * 2613 * @param jo This may be any {@link JsonObject} which can be bound to {@code 'c'}. 2614 * <BR /><BR /><B>NOTE:</B> This parameter may be null, and if it is, null is passed to the 2615 * {@code 'c'} constructor. 2616 * 2617 * @param c <EMBED CLASS='external-html' DATA-FILE-ID=JR_CLASS> 2618 * @return An instance of the class {@code 'T'}, which is specified by {@code 'c'} 2619 * @throws JsonException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JEX_REFL> 2620 */ 2621 public static <T> T getObject(JsonObject jo, Class<T> c) 2622 { 2623 Constructor<T> ctor = null; 2624 2625 try 2626 { 2627 // This just gets a "Constructor" using Reflection. The main point is that the 2628 // Constructor must have exactly one parameter - and that parameter must have a 2629 // type "JsonObject" (basic java.lang.reflect stuff) 2630 // 2631 //System.out.println("c.getName:():" + c.getName()); 2632 2633 ctor = c.getDeclaredConstructor(JsonObject.class); 2634 } 2635 catch (Exception e) 2636 { 2637 if (c.getEnclosingClass() != null) 2638 { 2639 int modifiers = c.getModifiers(); 2640 2641 if ((! Modifier.isStatic(modifiers)) || (! Modifier.isPublic(modifiers))) 2642 2643 throw new JsonException( 2644 "Unable to retrieve POJO Constructor for class: " + 2645 "[" + c.getName() + "]\n" + 2646 "Your class appears to be a Nested-Class, however it has not been " + 2647 "declared public and static. There is no way to retrieve a " + 2648 "1-Argument JsonObject Constructor for Nested-Type's unless the " + 2649 "type has been declared BOTH static AND public.\n" + 2650 "See Exception.getCause() for details.", 2651 e 2652 ); 2653 } 2654 2655 else throw new JsonException( 2656 "Unable to retrieve POJO Constructor for class: [" + c.getName() + "]\n" + 2657 "Do you have a one-argument, public, constructor for this class?\n" + 2658 "Does it accept a JsonObject in its parameter list?\n" + 2659 "See Exception.getCause() for details.", 2660 e 2661 ); 2662 } 2663 2664 // If the user has requested a class that doesn't have that kind of constructor, then 2665 // there is no way to build the object. Throw an exception. 2666 2667 if (ctor == null) throw new JsonException( 2668 "The class which was passed to parameter 'c' [" + c.getName() + "] does not " + 2669 "appear to have a constructor with precisely one parameter of type JsonObject." 2670 ); 2671 2672 // Call that constructor using the parameter 'jo', and return that instance / object 2673 try 2674 { return ctor.newInstance(jo); } 2675 2676 // NOTE: There are *MANY* possible Exception's that may be thrown by reflective 2677 // operations like those above. Furthermore, they are *ALL* checked exceptions. 2678 // The code below wraps those exceptions into an UN-CHECKED / RuntimeException 2679 // (JsonException) 2680 2681 catch (Exception e) 2682 { 2683 throw new JsonException( 2684 "Unable to instantiate class: [" + c.getName() + "] using provided " + 2685 "Java-Object\n" + 2686 "See Exception.getCause() for details.", 2687 e 2688 ); 2689 } 2690 } 2691 } 2692}