You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中通过GSON为ApexCharts添加无引号JS函数配置

在GSON中生成不带引号的JavaScript函数(适配ApexCharts配置)

要解决GSON将JavaScript函数序列化为带引号字符串的问题,核心是让GSON在输出时将函数代码当作原生JavaScript代码而非JSON字符串处理,以下两种方案可以实现:

方案一:自定义类型适配器(推荐,适合复杂配置)

这种方式通过自定义类型和适配器,让GSON识别并正确序列化JS函数,适合需要构建复杂JSON配置的场景。

  1. 定义JS函数标记类
    创建一个简单类来封装JS函数代码,用于和普通字符串区分:
public class JsFunction {
    private final String code;

    public JsFunction(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
}
  1. 编写类型适配器
    实现GSON的TypeAdapter接口,指定JS函数的序列化规则:
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;

public class JsFunctionAdapter extends TypeAdapter<JsFunction> {
    @Override
    public void write(JsonWriter out, JsFunction value) throws IOException {
        // 使用jsonValue直接输出原生代码,不加引号
        out.jsonValue(value.getCode());
    }

    @Override
    public JsFunction read(JsonReader in) throws IOException {
        // 若不需要反序列化JS函数,直接抛出不支持异常
        throw new UnsupportedOperationException("Deserialization of JS functions is not supported");
    }
}
  1. 注册适配器并构建JSON
    在创建Gson实例时注册适配器,然后将JsFunction对象添加到配置中:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

public class ApexChartConfigGenerator {
    public static void main(String[] args) {
        // 注册适配器构建Gson实例
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(JsFunction.class, new JsFunctionAdapter())
                .setPrettyPrinting() // 可选,格式化输出
                .create();

        // 构建ApexCharts配置
        JsonObject config = new JsonObject();
        JsonObject dataLabels = new JsonObject();

        // 添加formatter函数
        String formatterCode = "function (val, opt) { return opt.w.globals.labels[opt.dataPointIndex] + ':  ' + val }";
        dataLabels.add("formatter", gson.toJsonTree(new JsFunction(formatterCode)));

        config.add("dataLabels", dataLabels);

        // 生成最终JSON
        String jsonConfig = gson.toJson(config);
        System.out.println(jsonConfig);
    }
}

方案二:直接使用JsonWriter(适合简单配置)

如果你的JSON结构比较简单,可以直接用JsonWriter手动构建输出,跳过JsonObject的自动序列化:

import com.google.gson.stream.JsonWriter;
import java.io.StringWriter;
import java.io.IOException;

public class SimpleConfigGenerator {
    public static void main(String[] args) throws IOException {
        StringWriter writer = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(writer);
        jsonWriter.setIndent("  "); // 格式化输出

        // 手动构建JSON结构
        jsonWriter.beginObject();
        jsonWriter.name("dataLabels");
        jsonWriter.beginObject();
        jsonWriter.name("formatter");
        // 直接写入JS函数代码,不加引号
        jsonWriter.jsonValue("function (val, opt) { return opt.w.globals.labels[opt.dataPointIndex] + ':  ' + val }");
        jsonWriter.endObject();
        jsonWriter.endObject();

        jsonWriter.close();
        String jsonConfig = writer.toString();
        System.out.println(jsonConfig);
    }
}

两种方案输出的JSON中,formatter字段的值都会是不带引号的原生JavaScript函数,ApexCharts可以正常解析。

内容的提问来源于stack exchange,提问作者sdo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 18:52:35