001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import org.opencms.json.JSONArray;
031import org.opencms.json.JSONException;
032import org.opencms.json.JSONObject;
033
034import java.util.function.Function;
035
036/**
037 * Utility class for JSON-related functions.
038 */
039public class CmsJsonUtil {
040
041    /**
042     * Recursively walks through a JSON structure and returns a copy of it, but transforms primitive
043     * values using the given function for the copy.
044     *
045     * @param obj a JSONObject or JSONArray
046     * @param func the function to apply to primitive values
047     * @return the copy with the replaced values
048     * @throws JSONException if JSON operations fail
049     */
050    public static Object mapJson(Object obj, Function<Object, Object> func) throws JSONException {
051
052        if (obj instanceof JSONObject) {
053            return mapJsonObject((JSONObject)obj, func);
054        } else if (obj instanceof JSONArray) {
055            return mapJsonArray((JSONArray)obj, func);
056        } else {
057            return func.apply(obj);
058        }
059    }
060
061    /**
062     * Recursively walks through a JSON object and returns a copy of it, but transforms primitive
063     * values using the given function for the copy.
064     *
065     * @param obj a JSONObject
066     * @param func the function to apply to primitive values
067     * @return the copy with the replaced values
068     * @throws JSONException if JSON operations fail
069     */
070    public static JSONObject mapJsonObject(JSONObject obj, Function<Object, Object> func) throws JSONException {
071
072        JSONObject result = new JSONObject();
073        for (String key : obj.keySet()) {
074            Object val = obj.opt(key);
075            Object val2 = mapJson(val, func);
076            result.put(key, val2);
077        }
078        return result;
079    }
080
081    /**
082     * Recursively walks through a JSON array and returns a copy of it, but transforms primitive
083     * values using the given function for the copy.
084     *
085     * @param array a JSON array
086     * @param func the function to apply to primitive values
087     * @return the copy with the replaced values
088     * @throws JSONException if JSON operations fail
089     */
090
091    private static JSONArray mapJsonArray(JSONArray array, Function<Object, Object> func) throws JSONException {
092
093        JSONArray result = new JSONArray();
094        for (int i = 0; i < array.length(); i++) {
095            result.put(mapJson(array.get(i), func));
096        }
097        return result;
098    }
099
100}