json - 冷融合9 : Using serializeJson on Hibernate entity encodes numeric properties as strings

标签 json hibernate serialization coldfusion

我正在编写第一个 ORM 支持的 REST api,但在 JSON 序列化方面遇到了一些问题。这是我的实体定义之一:

component persistent="true" extends="Base" {

    property name="id"
             fieldtype="id"
             generator="native"
             ormtype="int"
             type="numeric";

    property name="relationship"
             type="string"
             ormtype="string";

    property name="comment"
             type="string"
             ormtype="text";

    //related entities
    property name="termA"
             cfc="term"
             fieldtype="many-to-one"
             fkcolumn="termA"
             lazy="false";

    property name="termB"
             cfc="term"
             fieldtype="many-to-one"
             fkcolumn="termB"
             lazy="false";

    //override getMemento to include appropriate relationships
    public struct function getMemento(){
        //standard stuff
        local.memento = super.getMemento();

        //custom add-ons
        local.memento["termA"] = this.getTermA().getId();
        local.memento["termB"] = this.getTermB().getId();

        return local.memento;
    }

}

这是它扩展的基础实体(用于 super.getMemento 等):

component extends="core.v1.models.Base" {

    public struct function getMemento(){
        local.memento = deserializeJson(serializeJson(this));
        //fix numerics showing up as strings
        if (structKeyExists(local.memento, "id")){
            local.memento.id = javacast("int", local.memento.id);
        }
        return local.memento;
    }

    public array function transformCollectionIntoMementos(array input){
        if (arrayLen(arguments.input) > 0){
            for (var i = 1; i <= arrayLen(arguments.input); i++){
                arraySet(arguments.input,i,i,arguments.input[i].getMemento());
            }
        }
        return arguments.input;
    }
}

这个扩展的类与这里无关。

最后,这是呈现数据的 json 表示形式的代码的简化版本:

<cfoutput>#serializeJson(transform(entityLoad("interaction")))#</cfoutput>

public array function transform(array input){
    if (arrayLen(arguments.input) > 0){
        for (var i = 1; i <= arrayLen(arguments.input); i++){
            arraySet(arguments.input,i,i,arguments.input[i].getMemento());
        }
    }
    return arguments.input;
}

您可以在第二个代码片段的 getMemento() 实现中看到,我尝试通过 javacasting 所有 ID 属性的值来解决此问题(每个实体都有一个名为“id”的属性) ) 为整数。这是行不通的。我也尝试过 javacast("int", local.memento.id * 1); ,但这也不起作用。

无论我尝试什么,生成的 json 看起来都是这样的:

[{"termB":"2","termA":"1","id":"1","relationship":"+"},{"termB":"4","termA":"1","id":"3","relationship":"-"}]

我期待的是:

[{"termB":"2","termA":"1","id":1,"relationship":"+"},{"termB":"4","termA":"1","id":3,"relationship":"-"}]

我迷茫了。我缺少什么?为什么这不起作用?

最佳答案

这是Adobe对serializeJSON所做的改变吗?

http://coldfusion.tcs.de/adobe-please-fix-coldfusion-serializejson/

关于json - 冷融合9 : Using serializeJson on Hibernate entity encodes numeric properties as strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6629281/

相关文章:

javascript - 从 ASP.NET MVC 端点返回 JavaScript 对象文字,而不是 JSON 字符串

javascript - JSON 数据未显示正确的信息

java - 如何为包含单个字段和数组的 JSON 创建 POJO?

hibernate - JPA 存储库与实体职责

c# - WCF:具有多个模块的数据协定序列化程序

c# - 使用不同的名称对 Json.Net 进行序列化和反序列化

Python Tweepy Bot,从 json 文件中随机发送图像和相应的文本标题

java - 创建名为 'sessionFactory' 的 bean 时出错 - 嵌套异常为 java.lang.NoClassDefFoundError :

java - jackson 自定义日期序列化程序

java - 错误: Column 'col_name' not found with createSQLQuery