001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.oauth2.sdk.util; 019 020 021import java.net.MalformedURLException; 022import java.net.URI; 023import java.net.URISyntaxException; 024import java.net.URL; 025import java.util.*; 026 027import net.minidev.json.JSONArray; 028import net.minidev.json.JSONObject; 029 030import com.nimbusds.jose.jwk.JWKSet; 031import com.nimbusds.jwt.JWTClaimsSet; 032import com.nimbusds.oauth2.sdk.ParseException; 033 034 035/** 036 * JSON object helper methods for parsing and typed retrieval of member values. 037 */ 038public final class JSONObjectUtils { 039 040 041 /** 042 * Returns {@code true} if the JSON object is defined and contains the 043 * specified key. 044 * 045 * @param jsonObject The JSON object to check. May be {@code null}. 046 * @param key The key to check. Must not be {@code null}. 047 * 048 * @return {@code true} if the JSON object is defined and contains the 049 * specified key, else {@code false}. 050 */ 051 public static boolean containsKey(final JSONObject jsonObject, final String key) { 052 053 return jsonObject != null && jsonObject.containsKey(key); 054 } 055 056 057 /** 058 * Parses a JSON object. 059 * 060 * <p>Specific JSON to Java entity mapping (as per JSON Simple): 061 * 062 * <ul> 063 * <li>JSON numbers mapped to {@code java.lang.Number}. 064 * <li>JSON integer numbers mapped to {@code long}. 065 * <li>JSON fraction numbers mapped to {@code double}. 066 * </ul> 067 * 068 * @param s The JSON object string to parse. Must not be {@code null}. 069 * 070 * @return The JSON object. 071 * 072 * @throws ParseException If the string cannot be parsed to a JSON 073 * object. 074 */ 075 public static JSONObject parse(final String s) 076 throws ParseException { 077 078 Object o = JSONUtils.parseJSON(s); 079 080 if (o instanceof JSONObject) 081 return (JSONObject)o; 082 else 083 throw new ParseException("The JSON entity is not an object"); 084 } 085 086 087 /** 088 * Parses a JSON object while keeping the order of JSON object members. 089 * 090 * <p>Specific JSON to Java entity mapping (as per JSON Simple): 091 * 092 * <ul> 093 * <li>JSON numbers mapped to {@code java.lang.Number}. 094 * <li>JSON integer numbers mapped to {@code long}. 095 * <li>JSON fraction numbers mapped to {@code double}. 096 * </ul> 097 * 098 * @param s The JSON object string to parse. Must not be {@code null}. 099 * 100 * @return The JSON object as linked hash map. 101 * 102 * @throws ParseException If the string cannot be parsed to a JSON 103 * object. 104 */ 105 public static LinkedHashMap<String,Object> parseKeepingOrder(final String s) 106 throws ParseException { 107 108 Object o = JSONUtils.parseJSONKeepingOrder(s); 109 110 if (o instanceof LinkedHashMap) 111 return (LinkedHashMap<String,Object>)o; 112 else 113 throw new ParseException("The JSON entity is not an object"); 114 } 115 116 117 /** 118 * Use {@link #parse(String)} instead. 119 * 120 * @param s The JSON object string to parse. Must not be {@code null}. 121 * 122 * @return The JSON object. 123 * 124 * @throws ParseException If the string cannot be parsed to a JSON 125 * object. 126 */ 127 @Deprecated 128 public static JSONObject parseJSONObject(final String s) 129 throws ParseException { 130 131 return parse(s); 132 } 133 134 135 /** 136 * Gets a generic member of a JSON object. 137 * 138 * @param o The JSON object. Must not be {@code null}. 139 * @param key The JSON object member key. Must not be {@code null}. 140 * @param clazz The expected class of the JSON object member value. Must 141 * not be {@code null}. 142 * 143 * @return The JSON object member value. 144 * 145 * @throws ParseException If the value is missing, {@code null} or not 146 * of the expected type. 147 */ 148 public static <T> T getGeneric(final JSONObject o, final String key, final Class<T> clazz) 149 throws ParseException { 150 151 if (! o.containsKey(key)) 152 throw new ParseException("Missing JSON object member with key " + key + ""); 153 154 Object value = o.get(key); 155 156 if (value == null) { 157 throw new ParseException("JSON object member with key " + key + " has null value"); 158 } 159 160 try { 161 return JSONUtils.to(value, clazz); 162 } catch (ParseException e) { 163 throw new ParseException("Unexpected type of JSON object member with key " + key + "", e); 164 } 165 } 166 167 168 /** 169 * Gets a boolean member of a JSON object. 170 * 171 * @param o The JSON object. Must not be {@code null}. 172 * @param key The JSON object member key. Must not be {@code null}. 173 * 174 * @return The member value. 175 * 176 * @throws ParseException If the value is missing, {@code null} or not 177 * of the expected type. 178 */ 179 public static boolean getBoolean(final JSONObject o, final String key) 180 throws ParseException { 181 182 return getGeneric(o, key, Boolean.class); 183 } 184 185 186 /** 187 * Gets a boolean member of a JSON object. 188 * 189 * @param o The JSON object. Must not be {@code null}. 190 * @param key The JSON object member key. Must not be {@code null}. 191 * @param def The default value to return if the key is not present or. 192 * the value is {@code null}. May be {@code null}. 193 * 194 * @return The member value. 195 * 196 * @throws ParseException If the value is not of the expected type. 197 */ 198 public static boolean getBoolean(final JSONObject o, final String key, final boolean def) 199 throws ParseException { 200 201 if (o.get(key) != null) { 202 return getBoolean(o, key); 203 } 204 205 return def; 206 } 207 208 209 /** 210 * Gets an number member of a JSON object as {@code int}. 211 * 212 * @param o The JSON object. Must not be {@code null}. 213 * @param key The JSON object member key. Must not be {@code null}. 214 * 215 * @return The member value. 216 * 217 * @throws ParseException If the value is missing, {@code null} or not 218 * of the expected type. 219 */ 220 public static int getInt(final JSONObject o, final String key) 221 throws ParseException { 222 223 return getGeneric(o, key, Number.class).intValue(); 224 } 225 226 227 /** 228 * Gets an number member of a JSON object as {@code int}. 229 * 230 * @param o The JSON object. Must not be {@code null}. 231 * @param key The JSON object member key. Must not be {@code null}. 232 * @param def The default value to return if the key is not present or 233 * the value is {@code null}. 234 * 235 * @return The member value. 236 * 237 * @throws ParseException If the value is not of the expected type. 238 */ 239 public static int getInt(final JSONObject o, final String key, final int def) 240 throws ParseException { 241 242 if (o.get(key) != null) { 243 return getInt(o, key); 244 } 245 246 return def; 247 } 248 249 250 /** 251 * Gets a number member of a JSON object as {@code long}. 252 * 253 * @param o The JSON object. Must not be {@code null}. 254 * @param key The JSON object member key. Must not be {@code null}. 255 * 256 * @return The member value. 257 * 258 * @throws ParseException If the value is missing, {@code null} or not 259 * of the expected type. 260 */ 261 public static long getLong(final JSONObject o, final String key) 262 throws ParseException { 263 264 return getGeneric(o, key, Number.class).longValue(); 265 } 266 267 268 /** 269 * Gets a number member of a JSON object as {@code long}. 270 * 271 * @param o The JSON object. Must not be {@code null}. 272 * @param key The JSON object member key. Must not be {@code null}. 273 * @param def The default value to return if the key is not present or 274 * the value is {@code null}. 275 * 276 * @return The member value. 277 * 278 * @throws ParseException If the value is not of the expected type. 279 */ 280 public static long getLong(final JSONObject o, final String key, final long def) 281 throws ParseException { 282 283 if (o.get(key) != null) { 284 return getLong(o, key); 285 } 286 287 return def; 288 } 289 290 291 /** 292 * Gets a number member of a JSON object {@code float}. 293 * 294 * @param o The JSON object. Must not be {@code null}. 295 * @param key The JSON object member key. Must not be {@code null}. 296 * 297 * @return The member value. 298 * 299 * @throws ParseException If the value is missing, {@code null} or not 300 * of the expected type. 301 */ 302 public static float getFloat(final JSONObject o, final String key) 303 throws ParseException { 304 305 return getGeneric(o, key, Number.class).floatValue(); 306 } 307 308 309 /** 310 * Gets a number member of a JSON object {@code float}. 311 * 312 * @param o The JSON object. Must not be {@code null}. 313 * @param key The JSON object member key. Must not be {@code null}. 314 * @param def The default value to return if the key is not present or 315 * the value is {@code null}. 316 * 317 * @return The member value. 318 * 319 * @throws ParseException If the value is not of the expected type. 320 */ 321 public static float getFloat(final JSONObject o, final String key, final float def) 322 throws ParseException { 323 324 if (o.get(key) != null) { 325 return getFloat(o, key); 326 } 327 328 return def; 329 } 330 331 332 /** 333 * Gets a number member of a JSON object as {@code double}. 334 * 335 * @param o The JSON object. Must not be {@code null}. 336 * @param key The JSON object member key. Must not be {@code null}. 337 * 338 * @return The member value. 339 * 340 * @throws ParseException If the value is missing, {@code null} or not 341 * of the expected type. 342 */ 343 public static double getDouble(final JSONObject o, final String key) 344 throws ParseException { 345 346 return getGeneric(o, key, Number.class).doubleValue(); 347 } 348 349 350 /** 351 * Gets a number member of a JSON object as {@code double}. 352 * 353 * @param o The JSON object. Must not be {@code null}. 354 * @param key The JSON object member key. Must not be {@code null}. 355 * @param def The default value to return if the key is not present or 356 * the value is {@code null}. 357 * 358 * @return The member value. 359 * 360 * @throws ParseException If the value is not of the expected type. 361 */ 362 public static double getDouble(final JSONObject o, final String key, final double def) 363 throws ParseException { 364 365 if (o.get(key) != null) { 366 return getDouble(o, key); 367 } 368 369 return def; 370 } 371 372 373 /** 374 * Gets a number member of a JSON object as {@code java.lang.Number}. 375 * 376 * @param o The JSON object. Must not be {@code null}. 377 * @param key The JSON object member key. Must not be {@code null}. 378 * 379 * @return The member value. 380 * 381 * @throws ParseException If the value is missing, {@code null} or not 382 * of the expected type. 383 */ 384 public static Number getNumber(final JSONObject o, final String key) 385 throws ParseException { 386 387 return getGeneric(o, key, Number.class); 388 } 389 390 391 /** 392 * Gets a number member of a JSON object as {@code java.lang.Number}. 393 * 394 * @param o The JSON object. Must not be {@code null}. 395 * @param key The JSON object member key. Must not be {@code null}. 396 * @param def The default value to return if the key is not present or 397 * the value is {@code null}. May be {@code null}. 398 * 399 * @return The member value. 400 * 401 * @throws ParseException If the value is not of the expected type. 402 */ 403 public static Number getNumber(final JSONObject o, final String key, final Number def) 404 throws ParseException { 405 406 if (o.get(key) != null) { 407 return getNumber(o, key); 408 } 409 410 return def; 411 } 412 413 414 /** 415 * Gets a string member of a JSON object. 416 * 417 * @param o The JSON object. Must not be {@code null}. 418 * @param key The JSON object member key. Must not be {@code null}. 419 * 420 * @return The member value. 421 * 422 * @throws ParseException If the value is missing, {@code null} or not 423 * of the expected type. 424 */ 425 public static String getString(final JSONObject o, final String key) 426 throws ParseException { 427 428 return getGeneric(o, key, String.class); 429 } 430 431 432 /** 433 * Gets a string member of a JSON object. 434 * 435 * @param o The JSON object. Must not be {@code null}. 436 * @param key The JSON object member key. Must not be {@code null}. 437 * @param def The default value to return if the key is not present or 438 * the value is {@code null}. May be {@code null}. 439 * 440 * @return The member value. 441 * 442 * @throws ParseException If the value is not of the expected type. 443 */ 444 public static String getString(final JSONObject o, final String key, final String def) 445 throws ParseException { 446 447 if (o.get(key) != null) { 448 return getString(o, key); 449 } 450 451 return def; 452 } 453 454 455 /** 456 * Gets a string member of a JSON object as an enumerated object. 457 * 458 * @param o The JSON object. Must not be {@code null}. 459 * @param key The JSON object member key. Must not be 460 * {@code null}. 461 * @param enumClass The enumeration class. Must not be {@code null}. 462 * 463 * @return The member value. 464 * 465 * @throws ParseException If the value is missing, {@code null} or not 466 * of the expected type. 467 */ 468 public static <T extends Enum<T>> T getEnum(final JSONObject o, 469 final String key, 470 final Class<T> enumClass) 471 throws ParseException { 472 473 String value = getString(o, key); 474 475 for (T en: enumClass.getEnumConstants()) { 476 477 if (en.toString().equalsIgnoreCase(value)) 478 return en; 479 } 480 481 throw new ParseException("Unexpected value of JSON object member with key " + key + ""); 482 } 483 484 485 /** 486 * Gets a string member of a JSON object as an enumerated object. 487 * 488 * @param o The JSON object. Must not be {@code null}. 489 * @param key The JSON object member key. Must not be 490 * {@code null}. 491 * @param enumClass The enumeration class. Must not be {@code null}. 492 * @param def The default value to return if the key is not 493 * present or the value is {@code null}. May be 494 * {@code null}. 495 * 496 * @return The member value. 497 * 498 * @throws ParseException If the value is not of the expected type. 499 */ 500 public static <T extends Enum<T>> T getEnum(final JSONObject o, 501 final String key, 502 final Class<T> enumClass, 503 final T def) 504 throws ParseException { 505 506 if (o.get(key) != null) { 507 return getEnum(o, key, enumClass); 508 } 509 510 return def; 511 } 512 513 514 /** 515 * Gets a string member of a JSON object as {@code java.net.URI}. 516 * 517 * @param o The JSON object. Must not be {@code null}. 518 * @param key The JSON object member key. Must not be {@code null}. 519 * 520 * @return The member value. 521 * 522 * @throws ParseException If the value is missing, {@code null} or not 523 * of the expected type. 524 */ 525 public static URI getURI(final JSONObject o, final String key) 526 throws ParseException { 527 528 try { 529 return new URI(getGeneric(o, key, String.class)); 530 531 } catch (URISyntaxException e) { 532 533 throw new ParseException(e.getMessage(), e); 534 } 535 } 536 537 538 /** 539 * Gets a string member of a JSON object as {@code java.net.URI}. 540 * 541 * @param o The JSON object. Must not be {@code null}. 542 * @param key The JSON object member key. Must not be {@code null}. 543 * @param def The default value to return if the key is not present or 544 * the value is {@code null}. May be {@code null}. 545 * 546 * @return The member value. 547 * 548 * @throws ParseException If the value is not of the expected type. 549 */ 550 public static URI getURI(final JSONObject o, final String key, final URI def) 551 throws ParseException { 552 553 if (o.get(key) != null) { 554 return getURI(o, key); 555 } 556 557 return def; 558 } 559 560 561 /** 562 * Gets a string member of a JSON object as {@code java.net.URL}. 563 * 564 * @param o The JSON object. Must not be {@code null}. 565 * @param key The JSON object member key. Must not be {@code null}. 566 * 567 * @return The member value. 568 * 569 * @throws ParseException If the value is missing, {@code null} or not 570 * of the expected type. 571 */ 572 public static URL getURL(final JSONObject o, final String key) 573 throws ParseException { 574 575 try { 576 return new URL(getGeneric(o, key, String.class)); 577 578 } catch (MalformedURLException e) { 579 580 throw new ParseException(e.getMessage(), e); 581 } 582 } 583 584 585 /** 586 * Gets a string member of a JSON object as {@code java.net.URL}. 587 * 588 * @param o The JSON object. Must not be {@code null}. 589 * @param key The JSON object member key. Must not be {@code null}. 590 * @param def The default value to return if the key is not present or 591 * the value is {@code null}. May be {@code null}. 592 * 593 * @return The member value. 594 * 595 * @throws ParseException If the value is not of the expected type. 596 */ 597 public static URL getURL(final JSONObject o, final String key, final URL def) 598 throws ParseException { 599 600 if (o.get(key) != null) { 601 return getURL(o, key); 602 } 603 604 return def; 605 } 606 607 608 /** 609 * Gets a JSON array member of a JSON object. 610 * 611 * @param o The JSON object. Must not be {@code null}. 612 * @param key The JSON object member key. Must not be {@code null}. 613 * 614 * @return The member value. 615 * 616 * @throws ParseException If the value is missing, {@code null} or not 617 * of the expected type. 618 */ 619 public static JSONArray getJSONArray(final JSONObject o, final String key) 620 throws ParseException { 621 622 List<?> list = getGeneric(o, key, List.class); 623 JSONArray jsonArray = new JSONArray(); 624 jsonArray.addAll(list); 625 return jsonArray; 626 } 627 628 629 /** 630 * Gets a JSON array member of a JSON object. 631 * 632 * @param o The JSON object. Must not be {@code null}. 633 * @param key The JSON object member key. Must not be {@code null}. 634 * @param def The default value to return if the key is not present or 635 * the value is {@code null}. May be {@code null}. 636 * 637 * @return The member value. 638 * 639 * @throws ParseException If the value is not of the expected type. 640 */ 641 public static JSONArray getJSONArray(final JSONObject o, final String key, final JSONArray def) 642 throws ParseException { 643 644 if (o.get(key) != null) { 645 return getJSONArray(o, key); 646 } 647 648 return def; 649 } 650 651 652 /** 653 * Gets a list member of a JSON object. 654 * 655 * @param o The JSON object. Must not be {@code null}. 656 * @param key The JSON object member key. Must not be {@code null}. 657 * 658 * @return The member value. 659 * 660 * @throws ParseException If the value is missing, {@code null} or not 661 * of the expected type. 662 */ 663 @SuppressWarnings("unchecked") 664 public static List<Object> getList(final JSONObject o, final String key) 665 throws ParseException { 666 667 return getGeneric(o, key, List.class); 668 } 669 670 671 /** 672 * Gets a list member of a JSON object. 673 * 674 * @param o The JSON object. Must not be {@code null}. 675 * @param key The JSON object member key. Must not be {@code null}. 676 * @param def The default value to return if the key is not present or 677 * the value is {@code null}. May be {@code null}. 678 * 679 * @return The member value. 680 * 681 * @throws ParseException If the value is not of the expected type. 682 */ 683 public static List<Object> getList(final JSONObject o, final String key, final List<Object> def) 684 throws ParseException { 685 686 if (o.get(key) != null) { 687 return getList(o, key); 688 } 689 690 return def; 691 } 692 693 694 /** 695 * Gets a string array member of a JSON object. 696 * 697 * @param o The JSON object. Must not be {@code null}. 698 * @param key The JSON object member key. Must not be {@code null}. 699 * 700 * @return The member value. 701 * 702 * @throws ParseException If the value is missing, {@code null} or not 703 * of the expected type. 704 */ 705 public static String[] getStringArray(final JSONObject o, final String key) 706 throws ParseException { 707 708 List<Object> list = getList(o, key); 709 710 try { 711 return list.toArray(new String[0]); 712 713 } catch (ArrayStoreException e) { 714 715 throw new ParseException("JSON object member with key " + key + " is not an array of strings"); 716 } 717 } 718 719 720 /** 721 * Gets a string array member of a JSON object. 722 * 723 * @param o The JSON object. Must not be {@code null}. 724 * @param key The JSON object member key. Must not be {@code null}. 725 * @param def The default value to return if the key is not present or 726 * the value is {@code null}. May be {@code null}. 727 * 728 * @return The member value. 729 * 730 * @throws ParseException If the value is not of the expected type. 731 */ 732 public static String[] getStringArray(final JSONObject o, final String key, final String[] def) 733 throws ParseException { 734 735 if (o.get(key) != null) { 736 return getStringArray(o, key); 737 } 738 739 return def; 740 } 741 742 743 /** 744 * Gets a string list member of a JSON object. 745 * 746 * @param o The JSON object. Must not be {@code null}. 747 * @param key The JSON object member key. Must not be {@code null}. 748 * 749 * @return The member value. 750 * 751 * @throws ParseException If the value is missing, {@code null} or not 752 * of the expected type. 753 */ 754 public static List<String> getStringList(final JSONObject o, final String key) 755 throws ParseException { 756 757 return Arrays.asList(getStringArray(o, key)); 758 } 759 760 761 /** 762 * Gets a string list member of a JSON object. 763 * 764 * @param o The JSON object. Must not be {@code null}. 765 * @param key The JSON object member key. Must not be {@code null}. 766 * @param def The default value to return if the key is not present or 767 * the value is {@code null}. May be {@code null}. 768 * 769 * @return The member value. 770 * 771 * @throws ParseException If the value is not of the expected type. 772 */ 773 public static List<String> getStringList(final JSONObject o, final String key, final List<String> def) 774 throws ParseException { 775 776 if (o.get(key) != null) { 777 return getStringList(o, key); 778 } 779 780 return def; 781 } 782 783 784 /** 785 * Gets a string array member of a JSON object as a string set. 786 * 787 * @param o The JSON object. Must not be {@code null}. 788 * @param key The JSON object member key. Must not be {@code null}. 789 * 790 * @return The member value. 791 * 792 * @throws ParseException If the value is missing, {@code null} or not 793 * of the expected type. 794 */ 795 public static Set<String> getStringSet(final JSONObject o, final String key) 796 throws ParseException { 797 798 List<Object> list = getList(o, key); 799 800 Set<String> set = new HashSet<>(); 801 802 for (Object item: list) { 803 804 try { 805 set.add((String)item); 806 807 } catch (Exception e) { 808 809 throw new ParseException("JSON object member with key " + key + " is not an array of strings"); 810 } 811 812 } 813 814 return set; 815 } 816 817 818 /** 819 * Gets a string array member of a JSON object as a string set. 820 * 821 * @param o The JSON object. Must not be {@code null}. 822 * @param key The JSON object member key. Must not be {@code null}. 823 * @param def The default value to return if the key is not present or 824 * the value is {@code null}. May be {@code null}. 825 * 826 * @return The member value. 827 * 828 * @throws ParseException If the value is not of the expected type. 829 */ 830 public static Set<String> getStringSet(final JSONObject o, final String key, final Set<String> def) 831 throws ParseException { 832 833 if (o.get(key) != null) { 834 return getStringSet(o, key); 835 } 836 837 return def; 838 } 839 840 841 /** 842 * Gets a JSON object member of a JSON object. 843 * 844 * @param o The JSON object. Must not be {@code null}. 845 * @param key The JSON object member key. Must not be {@code null}. 846 * 847 * @return The member value. 848 * 849 * @throws ParseException If the value is missing, {@code null} or not 850 * of the expected type. 851 */ 852 public static JSONObject getJSONObject(final JSONObject o, final String key) 853 throws ParseException { 854 855 @SuppressWarnings("unchecked") 856 Map<String, ?> mapObject = getGeneric(o, key, Map.class); 857 return new JSONObject(mapObject); 858 } 859 860 861 /** 862 * Gets a JSON object member of a JSON object. 863 * 864 * @param o The JSON object. Must not be {@code null}. 865 * @param key The JSON object member key. Must not be {@code null}. 866 * @param def The default value to return if the key is not present or 867 * the value is {@code null}. May be {@code null}. 868 * 869 * @return The member value. 870 * 871 * @throws ParseException If the value is not of the expected type. 872 */ 873 public static JSONObject getJSONObject(final JSONObject o, final String key, final JSONObject def) 874 throws ParseException { 875 876 if (o.get(key) != null) { 877 return getJSONObject(o, key); 878 } 879 880 return def; 881 } 882 883 884 /** 885 * Returns the JSON object representation of the specified JWT claims 886 * set. 887 * 888 * @param jwtClaimsSet The JWT claims set, {@code null} if not 889 * specified. 890 * 891 * @return The JSON object, {@code null} if not specified. 892 */ 893 public static JSONObject toJSONObject(final JWTClaimsSet jwtClaimsSet) { 894 895 if (jwtClaimsSet == null) { 896 return null; 897 } 898 899 if (jwtClaimsSet.getClaims().isEmpty()) { 900 return new JSONObject(); 901 } 902 903 // Serialise and parse is the safest method 904 final String json = jwtClaimsSet.toString(); 905 906 try { 907 return parse(json); 908 } catch (ParseException e) { 909 // Should never happen 910 return null; 911 } 912 } 913 914 915 /** 916 * Returns the JSON object representation of the specified JWK set. 917 * 918 * @param jwkSet The JWK set, {@code null} if not specified. 919 * 920 * @return The JSON object, {@code null} if not specified. 921 */ 922 public static JSONObject toJSONObject(final JWKSet jwkSet) { 923 924 if (jwkSet == null) { 925 return null; 926 } 927 928 // Serialise and parse is the safest method 929 final String json = jwkSet.toString(false); 930 931 try { 932 return parse(json); 933 } catch (ParseException e) { 934 // Should never happen 935 return null; 936 } 937 } 938 939 940 /** 941 * Prevents public instantiation. 942 */ 943 private JSONObjectUtils() {} 944} 945