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;
026import java.util.Map;
027
028import net.minidev.json.JSONArray;
029import net.minidev.json.JSONObject;
030
031import com.nimbusds.oauth2.sdk.ParseException;
032
033
034/**
035 * JSON array helper methods for parsing and typed retrieval of values.
036 */
037public final class JSONArrayUtils {
038
039
040        /**
041         * Parses a JSON array.
042         *
043         * <p>Specific JSON to Java entity mapping (as per JSON Simple):
044         *
045         * <ul>
046         *     <li>JSON numbers mapped to {@code java.lang.Number}.
047         *     <li>JSON integer numbers mapped to {@code long}.
048         *     <li>JSON fraction numbers mapped to {@code double}.
049         * </ul>
050         *
051         * @param s The JSON array string to parse. Must not be {@code null}.
052         *
053         * @return The JSON array.
054         *
055         * @throws ParseException If the string cannot be parsed to a JSON
056         *                        array.
057         */
058        public static JSONArray parse(final String s)
059                throws ParseException {
060
061                Object o = JSONUtils.parseJSON(s);
062
063                if (o instanceof JSONArray)
064                        return (JSONArray)o;
065                else
066                        throw new ParseException("The JSON entity is not an array");
067        }
068
069
070        /**
071         * Converts the specified JSON array to a string list.
072         *
073         * @param jsonArray The JSON array. May be {@code null}.
074         *
075         * @return The corresponding string list, empty list if the JSON array
076         *         is {@code null} or empty.
077         */
078        public static List<String> toStringList(final JSONArray jsonArray) {
079
080                if (CollectionUtils.isEmpty(jsonArray)) {
081                        return Collections.emptyList();
082                }
083
084                List<String> stringList = new ArrayList<>(jsonArray.size());
085
086                for (Object o: jsonArray) {
087
088                        if (o == null) {
089                                continue; // skip
090                        }
091
092                        stringList.add(o.toString());
093                }
094
095                return stringList;
096        }
097
098
099        /**
100         * Converts the specified JSON array to a URI list.
101         *
102         * @param jsonArray The JSON array. May be {@code null}.
103         *
104         * @return The corresponding URI list, empty list if the JSON array is
105         *         {@code null} or empty.
106         *
107         * @throws ParseException If a JSON array item couldn't be parsed to a
108         *                        URI.
109         */
110        public static List<URI> toURIList(final JSONArray jsonArray)
111                throws ParseException {
112
113                if (CollectionUtils.isEmpty(jsonArray)) {
114                        return Collections.emptyList();
115                }
116
117                List<URI> uriList = new ArrayList<>(jsonArray.size());
118
119                for (Object o: jsonArray) {
120
121                        if (o == null) {
122                                continue; // skip
123                        }
124
125                        try {
126                                uriList.add(new URI(o.toString()));
127                        } catch (URISyntaxException e) {
128                                throw new ParseException("Illegal URI: " + e.getMessage(), e);
129                        }
130                }
131
132                return uriList;
133        }
134        
135        
136        /**
137         * Converts the specified JSON array to a JSON object list.
138         *
139         * @param jsonArray The JSON array. May be {@code null}.
140         *
141         * @return The corresponding JSON object list, empty list if the JSON
142         *         array is {@code null} or empty.
143         *
144         * @throws ParseException If a JSON array item couldn't be parsed to a
145         *                        JSON object.
146         */
147        public static List<JSONObject> toJSONObjectList(final JSONArray jsonArray)
148                throws ParseException {
149                
150                if (CollectionUtils.isEmpty(jsonArray)) {
151                        return Collections.emptyList();
152                }
153                
154                List<JSONObject> objectList = new ArrayList<>(jsonArray.size());
155                
156                int i=-1;
157                for (Object o: jsonArray) {
158                        
159                        i++;
160                        
161                        if (o == null) {
162                                continue; // skip
163                        }
164                        
165                        if (o instanceof JSONObject) {
166                                objectList.add((JSONObject)o);
167                        } else if (o instanceof Map) {
168                                @SuppressWarnings("unchecked")
169                                JSONObject jo = new JSONObject((Map<String, ?>)o);
170                                objectList.add(jo);
171                        } else {
172                                throw new ParseException("Invalid JSON object at position " + i);
173                        }
174                }
175                
176                return objectList;
177        }
178        
179        
180        /**
181         * Prevents public instantiation.
182         */
183        private JSONArrayUtils() {}
184}