需要帮助创建自定义对象marshaller以返回地图对象数组

时间:2014-07-02 17:43:09

标签: json grails groovy

我有一个grails REST Web服务。我已经为我的两个域对象注册了自定义对象marshallers,如下所示。我的响应是一个Place对象数组,但我希望响应有一个根节点位置。请参阅下面的代码和当前输出,然后是我想要的输出。

class Place extends BaseDomain
{
    double latitude
    double longitude
    String streetAddress
    String userTitle
    boolean permitted
    boolean confirmed
    UUID userAddedID

    static hasMany =
    [
        comments:Comment
    ]

    static constraints =
    {
        userAddedID(nullable: true)
        latitude(range: -90..90)
        longitude(range: -180..180)
    }
}

class Comment extends BaseDomain
{
    String comment

    static belongsTo = [place: Place]

    static constraints =
    {

    }
}

自举

JSON.registerObjectMarshaller(Place)
                {
                    def returnArray = [:]

                    returnArray['id'] = it.id
                    returnArray['latitude'] = it.latitude
                    returnArray['longitude'] = it.longitude
                    returnArray['streetAddress'] = it.streetAddress
                    returnArray['userTitle'] = it.userTitle
                    returnArray['permitted'] = it.permitted
                    returnArray['confirmed'] = it.confirmed
                    returnArray['userAddedID'] = it.userAddedID
                    returnArray['comments'] = it.comments

                    return returnArray
                }

        JSON.registerObjectMarshaller(Comment)
                {
                    def returnArray = [:]
                    returnArray['id'] = it.id
                    returnArray['comment'] = it.comment

                    return returnArray
                }

我从服务中返回的用于调用列表位置的输出是

[
    {
        "id": 1,
        "latitude": 12225274,
        "longitude": -121.520563,
        "streetAddress": "0000 E. test St.
        "userTitle": "My House",
        "permitted": true,
        "confirmed": false,
        "userAddedID": null,
        "comments": [
            {
                "id": 5,
                "comment": "This is a nifty place"
            },
            {
                "id": 4,
                "comment": "This is a cool place"
            }
        ]
    },
    {
        "id": 2,
        "latitude": 12227499,
        "longitude": -121.515328,
        "streetAddress": "0000 E. test St.",
        "userTitle": "Krishna",
        "permitted": false,
        "confirmed": false,
        "userAddedID": null,
        "comments": [
            {
                "id": 6,
                "comment": "This is okay"
            }
        ]
    },
    {
        "id": 3,
        "latitude": 56.175015,
        "longitude": 78.042155,
        "streetAddress": "0000 E. test St.",
        "userTitle": "Kat",
        "permitted": true,
        "confirmed": false,
        "userAddedID": null,
        "comments": []
    }
]

但我想

{
 places : [
        {
            "id": 1,
            "latitude": 12225274,
            "longitude": -121.520563,
            "streetAddress": "0000 E. test St.
            "userTitle": "My House",
            "permitted": true,
            "confirmed": false,
            "userAddedID": null,
            "comments": [
                {
                    "id": 5,
                    "comment": "This is a nifty place"
                },
                {
                    "id": 4,
                    "comment": "This is a cool place"
                }
            ]
        },
        {
            "id": 2,
            "latitude": 12227499,
            "longitude": -121.515328,
            "streetAddress": "0000 E. test St.",
            "userTitle": "Krishna",
            "permitted": false,
            "confirmed": false,
            "userAddedID": null,
            "comments": [
                {
                    "id": 6,
                    "comment": "This is okay"
                }
            ]
        },
        {
            "id": 3,
            "latitude": 56.175015,
            "longitude": 78.042155,
            "streetAddress": "0000 E. test St.",
            "userTitle": "Kat",
            "permitted": true,
            "confirmed": false,
            "userAddedID": null,
            "comments": []
        }
    ]
}

0 个答案:

没有答案