From 5a4f0cb79114b3f0f2627a599a115c5b02aef043 Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:58:08 +0800 Subject: [PATCH] Add File --- .../chain/node/GraalToFastjsonUtils.java | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 agents-flex-chain/agents-flex-chain-js/src/main/java/com/agentsflex/chain/node/GraalToFastjsonUtils.java diff --git a/agents-flex-chain/agents-flex-chain-js/src/main/java/com/agentsflex/chain/node/GraalToFastjsonUtils.java b/agents-flex-chain/agents-flex-chain-js/src/main/java/com/agentsflex/chain/node/GraalToFastjsonUtils.java new file mode 100644 index 0000000..b5600e1 --- /dev/null +++ b/agents-flex-chain/agents-flex-chain-js/src/main/java/com/agentsflex/chain/node/GraalToFastjsonUtils.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.agentsflex.chain.node;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import org.graalvm.polyglot.Value;
+
+import java.util.*;
+
+public class GraalToFastjsonUtils {
+ public static Object toFastJsonValue(Object obj) {
+ if (obj == null) {
+ return null;
+ }
+
+ if (obj instanceof Value) {
+ Value value = (Value) obj;
+
+ if (value.isNull()) {
+ return null;
+ }
+
+ if (value.isBoolean()) {
+ return value.as(Boolean.class);
+ }
+
+ if (value.isNumber()) {
+ if (value.fitsInLong()) {
+ return value.as(Long.class);
+ } else if (value.fitsInDouble()) {
+ return value.as(Double.class);
+ } else {
+ return value.toString(); // e.g., BigInt
+ }
+ }
+
+ if (value.isString()) {
+ return value.as(String.class);
+ }
+
+ if (value.hasArrayElements()) {
+ long size = value.getArraySize();
+ JSONArray array = new JSONArray();
+ for (long i = 0; i < size; i++) {
+ array.add(toFastJsonValue(value.getArrayElement(i)));
+ }
+ return array;
+ }
+
+ if (value.hasMembers()) {
+ JSONObject object = new JSONObject();
+ Set