All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nimbusds.oauth2.sdk.util.JSONArrayUtils Maven / Gradle / Ivy

Go to download

OAuth 2.0 SDK with OpenID Connection extensions for developing client and server applications.

There is a newer version: 11.19.1
Show newest version
/*
 * oauth2-oidc-sdk
 *
 * Copyright 2012-2016, Connect2id Ltd and contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.nimbusds.oauth2.sdk.util;


import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.nimbusds.oauth2.sdk.ParseException;
import net.minidev.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;


/**
 * JSON array helper methods for parsing and typed retrieval of values.
 */
public class JSONArrayUtils {


	/**
	 * Parses a JSON array.
	 *
	 * 

Specific JSON to Java entity mapping (as per JSON Simple): * *

    *
  • JSON numbers mapped to {@code java.lang.Number}. *
  • JSON integer numbers mapped to {@code long}. *
  • JSON fraction numbers mapped to {@code double}. *
* * @param s The JSON array string to parse. Must not be {@code null}. * * @return The JSON array. * * @throws ParseException If the string cannot be parsed to a JSON * array. */ public static JSONArray parse(final String s) throws ParseException { Object o = JSONUtils.parseJSON(s); if (o instanceof JSONArray) return (JSONArray)o; else throw new ParseException("The JSON entity is not an array"); } /** * Converts the specified JSON array to a string list. * * @param jsonArray The JSON array. May be {@code null}. * * @return The corresponding string list, empty list if the JSON array * is {@code null} or empty. */ public static List toStringList(final JSONArray jsonArray) { if (CollectionUtils.isEmpty(jsonArray)) { return Collections.emptyList(); } List stringList = new ArrayList<>(jsonArray.size()); for (Object o: jsonArray) { if (o == null) { continue; // skip } stringList.add(o.toString()); } return stringList; } /** * Converts the specified JSON array to a URI list. * * @param jsonArray The JSON array. May be {@code null}. * * @return The corresponding URI list, empty list if the JSON array is * {@code null} or empty. * * @throws ParseException If a JSON array item couldn't be parsed to a * URI. */ public static List toURIList(final JSONArray jsonArray) throws ParseException { if (CollectionUtils.isEmpty(jsonArray)) { return Collections.emptyList(); } List uriList = new ArrayList<>(jsonArray.size()); for (Object o: jsonArray) { if (o == null) { continue; // skip } try { uriList.add(new URI(o.toString())); } catch (URISyntaxException e) { throw new ParseException("Illegal URI: " + e.getMessage(), e); } } return uriList; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy