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.util.LinkedList;
022import java.util.List;
023
024import com.nimbusds.oauth2.sdk.ParseException;
025
026import net.minidev.json.parser.JSONParser;
027import net.minidev.json.writer.JsonReader;
028
029
030/**
031 * JSON helper methods.
032 */
033public final class JSONUtils {
034
035
036        /**
037         * Parses a JSON value.
038         *
039         * @param s The JSON string to parse. Must not be {@code null}.
040         *
041         * @return The JSON value.
042         *
043         * @throws ParseException If the string cannot be parsed to a JSON
044         *                        value.
045         */
046        public static Object parseJSON(final String s)
047                throws ParseException {
048
049                try {
050                        return new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT | JSONParser.ACCEPT_TAILLING_SPACE).parse(s);
051                } catch (net.minidev.json.parser.ParseException e) {
052                        throw new ParseException("Invalid JSON: " + e.getMessage(), e);
053                } catch (NullPointerException e) {
054                        throw new ParseException("The JSON string must not be null", e);
055                } catch (Exception e) {
056                        throw new ParseException("Unexpected exception: " + e.getMessage(), e);
057                }
058        }
059
060
061        /**
062         * Parses a JSON value while keeping the order of JSON object members.
063         *
064         * @param s The JSON string to parse. Must not be {@code null}.
065         *
066         * @return The JSON value.
067         *
068         * @throws ParseException If the string cannot be parsed to a JSON
069         *                        value.
070         */
071        public static Object parseJSONKeepingOrder(final String s)
072                throws ParseException {
073
074                try {
075                        return new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT | JSONParser.ACCEPT_TAILLING_SPACE).parse(s, new JsonReader().DEFAULT_ORDERED);
076
077                } catch (net.minidev.json.parser.ParseException e) {
078
079                        throw new ParseException("Invalid JSON: " + e.getMessage(), e);
080                }
081        }
082        
083        
084        /**
085         * Casts an object.
086         *
087         * @param o     The object. Must not be {@code null}.
088         * @param clazz The expected class of the object. Must not be
089         *              {@code null}.
090         *
091         * @return The cast object.
092         *
093         * @throws ParseException If the object is not of the expected type.
094         */
095        @SuppressWarnings("unchecked")
096        public static <T> T to(final Object o, final Class<T> clazz)
097                throws ParseException {
098                
099                if (! clazz.isAssignableFrom(o.getClass()))
100                        throw new ParseException("Unexpected type: " + o.getClass());
101                
102                return (T)o;
103        }
104        
105        
106        /**
107         * Casts an object to a boolean.
108         *
109         * @param o The object. Must not be {@code null}.
110         *
111         * @return The boolean value.
112         *
113         * @throws ParseException If the object is not of the expected type.
114         */
115        public static boolean toBoolean(final Object o)
116                throws ParseException {
117                
118                return to(o, Boolean.class);
119        }
120        
121        
122        /**
123         * Casts an object to a number.
124         *
125         * @param o The object. Must not be {@code null}.
126         *
127         * @return The number.
128         *
129         * @throws ParseException If the object is not of the expected type.
130         */
131        public static Number toNumber(final Object o)
132                throws ParseException {
133                
134                return to(o, Number.class);
135        }
136        
137        
138        /**
139         * Casts an object to a string.
140         *
141         * @param o The object. Must not be {@code null}.
142         *
143         * @return The string.
144         *
145         * @throws ParseException If the object is not of the expected type.
146         */
147        public static String toString(final Object o)
148                throws ParseException {
149                
150                return to(o, String.class);
151        }
152        
153        
154        /**
155         * Casts an object to a list.
156         *
157         * @param o The object. Must not be {@code null}.
158         *
159         * @return The list.
160         *
161         * @throws ParseException If the object is not of the expected type.
162         */
163        public static List<?> toList(final Object o)
164                throws ParseException {
165                
166                return to(o, List.class);
167        }
168        
169        
170        /**
171         * Casts an object to a list then returns a string list copy of it
172         * casting each item to a string.
173         *
174         * @param o The object. Must not be {@code null}.
175         *
176         * @return The string list.
177         *
178         * @throws ParseException If the object is not of the expected type.
179         */
180        public static List<String> toStringList(final Object o)
181                throws ParseException {
182                
183                List<String> stringList = new LinkedList<>();
184                try {
185                        for (Object item: toList(o)) {
186                                stringList.add((String)item);
187                        }
188                } catch (ClassCastException e) {
189                        throw new ParseException("Item not a string");
190                }
191                return stringList;
192        }
193        
194        
195        /**
196         * Prevents instantiation.
197         */
198        private JSONUtils() {}
199}