简体   繁体   English

如何通过Gson在json中创建javascript function?

[英]How to create javascript function within json by Gson?

Is it possible to create json like this via Gson?是否可以通过 Gson 像这样创建 json? A javascript function without a key is within the json.没有钥匙的javascript function在json里面。

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

Thanks in advance.提前致谢。

Finally, I find a way to do it by using TypeAdapterFactory.最后,我找到了一种使用 TypeAdapterFactory 来完成它的方法。 Here is the code.这是代码。

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);
            }
        };
    }
}

And

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 + " } "+
                "}";
    }
    
}

To test:去测试:

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));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在函数中的javascript中创建2` this`引用 - How to create 2 `this` reference in javascript from within a function 如何在 JavaScript 函数中使用单引号(撇号)转义 JSON 值 - How to escape JSON values with Single Quotes(apostrophe) within JavaScript function 如何在 javascript 的 map 函数中创建字典/哈希图? - How can I create a dictionary/hashmap within a map function in javascript? 如何在OrientDB中的javascript函数内创建新文档 - How to create a new doc within a javascript function in OrientDB 如何在 javascript 映射 function 中创建累积变量 - How to create an accumulation variable within javascript mapping function 如何使用javascript从HTML函数中的json数组创建下拉列表? - How to create dropdown list from a json array in a function in HTML with javascript? 如何使用另一个JavaScript文件中的参数创建一个JavaScript函数以在另一个JavaScript中使用? - how to create a javascript function with parameters in another javascript file to use within another javascript? 如何创建“另存为”和“加载”对话框以在javascript中创建/加载json文件/数据? - How do I create “save as” and “load” dialogs to create/load json files/data within javascript? JavaScript:在特定范围内创建函数 - JavaScript: create a function within a specific scope 在 JavaScript 中创建闭包的返回函数 - return function within to create closure in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM