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.URI;
022import java.net.URISyntaxException;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026
027import com.nimbusds.oauth2.sdk.ParseException;
028import net.minidev.json.JSONArray;
029
030
031/**
032 * JSON array helper methods for parsing and typed retrieval of values.
033 */
034public final class JSONArrayUtils {
035
036
037        /**
038         * Parses a JSON array.
039         *
040         * <p>Specific JSON to Java entity mapping (as per JSON Simple):
041         *
042         * <ul>
043         *     <li>JSON numbers mapped to {@code java.lang.Number}.
044         *     <li>JSON integer numbers mapped to {@code long}.
045         *     <li>JSON fraction numbers mapped to {@code double}.
046         * </ul>
047         *
048         * @param s The JSON array string to parse. Must not be {@code null}.
049         *
050         * @return The JSON array.
051         *
052         * @throws ParseException If the string cannot be parsed to a JSON
053         *                        array.
054         */
055        public static JSONArray parse(final String s)
056                throws ParseException {
057
058                Object o = JSONUtils.parseJSON(s);
059
060                if (o instanceof JSONArray)
061                        return (JSONArray)o;
062                else
063                        throw new ParseException("The JSON entity is not an array");
064        }
065
066
067        /**
068         * Converts the specified JSON array to a string list.
069         *
070         * @param jsonArray The JSON array. May be {@code null}.
071         *
072         * @return The corresponding string list, empty list if the JSON array
073         *         is {@code null} or empty.
074         */
075        public static List<String> toStringList(final JSONArray jsonArray) {
076
077                if (CollectionUtils.isEmpty(jsonArray)) {
078                        return Collections.emptyList();
079                }
080
081                List<String> stringList = new ArrayList<>(jsonArray.size());
082
083                for (Object o: jsonArray) {
084
085                        if (o == null) {
086                                continue; // skip
087                        }
088
089                        stringList.add(o.toString());
090                }
091
092                return stringList;
093        }
094
095
096        /**
097         * Converts the specified JSON array to a URI list.
098         *
099         * @param jsonArray The JSON array. May be {@code null}.
100         *
101         * @return The corresponding URI list, empty list if the JSON array is
102         *         {@code null} or empty.
103         *
104         * @throws ParseException If a JSON array item couldn't be parsed to a
105         *                        URI.
106         */
107        public static List<URI> toURIList(final JSONArray jsonArray)
108                throws ParseException {
109
110                if (CollectionUtils.isEmpty(jsonArray)) {
111                        return Collections.emptyList();
112                }
113
114                List<URI> uriList = new ArrayList<>(jsonArray.size());
115
116                for (Object o: jsonArray) {
117
118                        if (o == null) {
119                                continue; // skip
120                        }
121
122                        try {
123                                uriList.add(new URI(o.toString()));
124                        } catch (URISyntaxException e) {
125                                throw new ParseException("Illegal URI: " + e.getMessage(), e);
126                        }
127                }
128
129                return uriList;
130        }
131        
132        
133        /**
134         * Prevents public instantiation.
135         */
136        private JSONArrayUtils() {}
137}