
com.nimbusds.oauth2.sdk.util.JSONArrayUtils Maven / Gradle / Ivy
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 - 2025 Weber Informatics LLC | Privacy Policy