001/** 002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.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.gnu.org/licenses/lgpl-3.0.txt 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 dev.tinyflow.core.code.impl; 017 018import com.alibaba.fastjson.JSONArray; 019import com.alibaba.fastjson.JSONObject; 020import org.graalvm.polyglot.Value; 021 022import java.util.Collection; 023import java.util.Map; 024import java.util.Objects; 025import java.util.Set; 026 027public class GraalvmToFastJSONUtils { 028 public static Object toFastJsonValue(Object obj) { 029 if (obj == null) { 030 return null; 031 } 032 033 if (obj instanceof Value) { 034 Value value = (Value) obj; 035 036 if (value.isNull()) { 037 return null; 038 } 039 040 if (value.isBoolean()) { 041 return value.as(Boolean.class); 042 } 043 044 if (value.isNumber()) { 045 if (value.fitsInLong()) { 046 return value.as(Long.class); 047 } else if (value.fitsInDouble()) { 048 return value.as(Double.class); 049 } else { 050 return value.toString(); // e.g., BigInt 051 } 052 } 053 054 if (value.isString()) { 055 return value.as(String.class); 056 } 057 058 if (value.hasArrayElements()) { 059 long size = value.getArraySize(); 060 JSONArray array = new JSONArray(); 061 for (long i = 0; i < size; i++) { 062 array.add(toFastJsonValue(value.getArrayElement(i))); 063 } 064 return array; 065 } 066 067 if (value.hasMembers()) { 068 JSONObject object = new JSONObject(); 069 Set<String> keys = value.getMemberKeys(); 070 for (String key : keys) { 071 Value member = value.getMember(key); 072 // 排除函数 073 if (!member.canExecute()) { 074 object.put(key, toFastJsonValue(member)); 075 } 076 } 077 return object; 078 } 079 080 return value.toString(); 081 } 082 083 // 处理标准 Java 类型 --------------------------------------- 084 085 if (obj instanceof Map) { 086 Map<?, ?> map = (Map<?, ?>) obj; 087 JSONObject jsonObject = new JSONObject(); 088 for (Map.Entry<?, ?> entry : map.entrySet()) { 089 String key = Objects.toString(entry.getKey(), "null"); 090 Object convertedValue = toFastJsonValue(entry.getValue()); 091 jsonObject.put(key, convertedValue); 092 } 093 return jsonObject; 094 } 095 096 if (obj instanceof Collection) { 097 Collection<?> coll = (Collection<?>) obj; 098 JSONArray array = new JSONArray(); 099 for (Object item : coll) { 100 array.add(toFastJsonValue(item)); 101 } 102 return array; 103 } 104 105 if (obj.getClass().isArray()) { 106 int length = java.lang.reflect.Array.getLength(obj); 107 JSONArray array = new JSONArray(); 108 for (int i = 0; i < length; i++) { 109 Object item = java.lang.reflect.Array.get(obj, i); 110 array.add(toFastJsonValue(item)); 111 } 112 return array; 113 } 114 115 // 基本类型直接返回 116 if (obj instanceof String || 117 obj instanceof Boolean || 118 obj instanceof Byte || 119 obj instanceof Short || 120 obj instanceof Integer || 121 obj instanceof Long || 122 obj instanceof Float || 123 obj instanceof Double) { 124 return obj; 125 } 126 127 // 兜底:调用 toString 128 return obj.toString(); 129 } 130 131 /** 132 * 转为 JSONObject(适合根是对象) 133 */ 134 public static JSONObject toJSONObject(Object obj) { 135 Object result = toFastJsonValue(obj); 136 if (result instanceof JSONObject) { 137 return (JSONObject) result; 138 } else if (result instanceof Map) { 139 return new JSONObject((Map) result); 140 } else { 141 return new JSONObject().fluentPut("value", result); 142 } 143 } 144}