【问题标题】:How to create javascript function within json by Gson?如何通过 Gson 在 json 中创建 javascript 函数?
【发布时间】:2020-09-23 04:35:34
【问题描述】:

是否可以通过 Gson 创建这样的 json?没有键的 javascript 函数在 json 中。

{
    autosave: {
        save( editor ) {
            return editor.saveData( editor.id, editor.getData() );
        },
        waitingTime: 2000
}

提前致谢。

【问题讨论】:

  • 不,函数不包含在 JSON 中。
  • 我知道这是一个无效的 json。但我认为这是可行的,虽然不推荐。
  • 为什么?这是无效的,你不能用任何字符串化器来做到这一点。您可以在对对象进行字符串化之后将函数添加到 JSON 字符串中,但是有什么价值,没有解析器可以解析该 JSON。在 JSON 中传递函数的典型方法是在应用程序中实现函数,并将函数的名称包含为字符串。然后在app中解析完JSON后,就可以将代表函数的字符串值替换为真实函数了。
  • 特殊要求。感谢您的回复。我现在知道如何实现它了。
  • Gson 对于这样的需求来说是完全错误的工具。为什么不使用 JavaScript 代码生成器或仅使用模板或其他更合适的方式生成有效的 JavaScript? JSON代表“JS object notation”,而不是“JS”,它不能处理除对象、数组和文字以外的任何东西。

标签: javascript json function gson


【解决方案1】:

最后,我找到了一种使用 TypeAdapterFactory 的方法。 这是代码。

class AutosaveAdapter implements TypeAdapterFactory {

    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> tokenType) {
        final TypeAdapter<T> adapter = gson.getDelegateAdapter(this, tokenType);

        return new TypeAdapter<T>() {
            @Override
            public T read(JsonReader reader) throws IOException {
                return adapter.read(reader);
            }

            @Override
            public void write(JsonWriter writer, T value) throws IOException {
                JsonElement tree = adapter.toJsonTree(value);

                if (value instanceof Autosave) {
                    String dom = value.toString();
                    JsonObject jo = (JsonObject) tree;
                    jo.addProperty("autosave", dom );
                }
                gson.getAdapter(JsonElement.class).write(writer, tree);
            }
        };
    }
}

class Autosave {

    int waitingTime;

    @Expose(serialize = false, deserialize = false)
    String name;

    @Expose(serialize = false, deserialize = false)
    String arguments;

    @Expose(serialize = false, deserialize = false)
    String body;

    Autosave(int waitingTime, String name, String arguments, String body) {
        this.waitingTime = waitingTime;
        this.name = name;
        this.arguments = arguments;
        this.body = body;
    }

    public String toString() {
        return "{ waitingTime:" + waitingTime + "," +
                  name +" ( "+arguments+" ) { " + body + " } "+
                "}";
    }
    
}

测试:

public static void main(String... args) {
        Autosave autosave = new Autosave(2000, "save", "editor", "return editor.saveData( editor.id, editor.getData() );");
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setLenient();
        Gson gson = gsonBuilder
                .excludeFieldsWithoutExposeAnnotation()
                .registerTypeAdapterFactory(new AutosaveAdapter())
                .create();
        System.out.println(gson.toJson(autosave));
    }

【讨论】:

  • 您是否注意到输出与您在问题中提出的不同并且不匹配?
猜你喜欢
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
相关资源
最近更新 更多