001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.util;
017
018import com.alibaba.fastjson2.JSONObject;
019import com.alibaba.fastjson2.JSONPath;
020
021import java.util.List;
022import java.util.Map;
023import java.util.concurrent.ConcurrentHashMap;
024
025public class JSONUtil {
026
027    private static final Map<String, JSONPath> jsonPaths = new ConcurrentHashMap<>();
028
029    public static JSONPath getJsonPath(String path) {
030        return MapUtil.computeIfAbsent(jsonPaths, path, JSONPath::of);
031    }
032
033    public static double[] readDoubleArray(JSONObject jsonObject, String path) {
034        if (jsonObject == null || path == null) {
035            return null;
036        }
037
038        JSONPath jsonPath = getJsonPath(path);
039        Object result = jsonPath.eval(jsonObject);
040
041        if (result == null) {
042            return null;
043        }
044
045        if (result instanceof List) {
046            List<?> list = (List<?>) result;
047            double[] array = new double[list.size()];
048            for (int i = 0; i < list.size(); i++) {
049                Object item = list.get(i);
050                if (item instanceof Number) {
051                    array[i] = ((Number) item).doubleValue();
052                } else if (item == null) {
053                    array[i] = 0.0; // 或抛异常,根据需求
054                } else {
055                    // 尝试转换字符串?或报错
056                    try {
057                        array[i] = Double.parseDouble(item.toString());
058                    } catch (NumberFormatException e) {
059                        return null; // 或抛异常
060                    }
061                }
062            }
063            return array;
064        }
065
066        return null;
067    }
068
069    public static String readString(JSONObject jsonObject, String path) {
070        if (jsonObject == null || path == null) {
071            return null;
072        }
073
074        JSONPath jsonPath = getJsonPath(path);
075        Object result = jsonPath.eval(jsonObject);
076
077        if (result == null) {
078            return null;
079        }
080
081        if (result instanceof String) {
082            return (String) result;
083        }
084
085        return null;
086    }
087
088    public static Integer readInteger(JSONObject jsonObject, String path) {
089        if (jsonObject == null || path == null) {
090            return null;
091        }
092        JSONPath jsonPath = getJsonPath(path);
093        Object result = jsonPath.eval(jsonObject);
094        if (result == null) {
095            return null;
096        }
097        if (result instanceof Number) {
098            return ((Number) result).intValue();
099        }
100        if (result instanceof String) {
101            return Integer.parseInt((String) result);
102        }
103        throw new IllegalArgumentException("Invalid JSON path result type: " + result.getClass().getName());
104    }
105
106    public static Long readLong(JSONObject jsonObject, String path) {
107        if (jsonObject == null || path == null) {
108            return null;
109        }
110        JSONPath jsonPath = getJsonPath(path);
111        Object result = jsonPath.eval(jsonObject);
112        if (result == null) {
113            return null;
114        }
115        if (result instanceof Number) {
116            return ((Number) result).longValue();
117        }
118        if (result instanceof String) {
119            return Long.getLong((String) result);
120        }
121        throw new IllegalArgumentException("Invalid JSON path result type: " + result.getClass().getName());
122    }
123
124    public static String detectErrorMessage(JSONObject jsonObject) {
125        JSONObject errorObject = jsonObject.getJSONObject("error");
126        if (errorObject == null) {
127            return null;
128        }
129        String errorMessage = errorObject.getString("message");
130        String errorCode = errorObject.getString("code");
131        return errorCode == null ? errorMessage : (errorCode + ": " + errorMessage);
132    }
133}