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

org.labkey.remoteapi.internal.ResponseUtils Maven / Gradle / Ivy

Go to download

The client-side library for Java developers is a separate JAR from the LabKey Server code base. It can be used by any Java program, including another Java web application.

The newest version!
package org.labkey.remoteapi.internal;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ResponseUtils
{
    private ResponseUtils()
    {
        // Static utility class. Do not construct.
    }

    /**
     * Convert JSON Object to map and make a thoroughly unmodifiable copy of that map.
* Any nested {@link JSONObject}s or {@link JSONArray}s will be similarly processed. * @param jsonObject Map to copy * @return Unmodifiable copy of the provided JSONObject. * @see JSONObject#toMap() */ public static Map deepUnmodifiableMap(JSONObject jsonObject) { if (jsonObject == null) return null; return deepUnmodifiableMap(jsonObject.toMap()); } /** * Make a thoroughly unmodifiable copy of a Map that was generated by a JSONObject.
* Any nested {@link Map}s or {@link List}s will be made unmodifiable. * @param map Map to copy * @return Unmodifiable copy of the provided Map * @see JSONObject#toMap() */ public static Map deepUnmodifiableMap(Map map) { if (map == null) return null; Map mapCopy = new LinkedHashMap<>(map.size()); for (String key : map.keySet()) { mapCopy.put(key, processValueForDeepCopy(map.get(key))); } return Collections.unmodifiableMap(mapCopy); } /** * Make a thoroughly unmodifiable copy of a List that was generated by a JSONArray.
* Any nested {@link Map}s or {@link List}s will also be made unmodifiable. * @param list List to copy * @return Unmodifiable copy of the provided Map * @see JSONArray#toList() */ public static List deepUnmodifiableList(List list) { if (list == null) return null; List listCopy = new ArrayList<>(list.size()); for (Object value : list) { listCopy.add(processValueForDeepCopy(value)); } return Collections.unmodifiableList(listCopy); } private static Object processValueForDeepCopy(Object value) { if (value == null) { return null; } else if (value instanceof Map map) { return deepUnmodifiableMap(map); } else if (value instanceof List list) { return deepUnmodifiableList(list); } else if (value == JSONObject.wrap(value)) { return value; } else { throw new IllegalArgumentException("Invalid type for deep copy: " + value.getClass()); } } }