使用Groovy JsonBuilder设置委托值 [英] Setting delegate value with Groovy JsonBuilder

查看:217
本文介绍了使用Groovy JsonBuilder设置委托值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是此处的问题的后续问题)

(This is a follow up question to a question asked here)

我使用Groovy的JsonBuilder动态生成以下JSON:

I'm using Groovy's JsonBuilder to dynamically generate the following JSON:

{
    "type": {
        "__type": "urn",
        "value": "myCustomValue1"
    },
    "urn": {
        "__type": "urn",
        "value": "myCustomValue2"
    },
    "date": {
        "epoch": 1265662800000,
        "str": "2010-02-08T21:00:00Z"
    },
    "metadata": [{
        "ratings": [{
            "rating": "NR",
            "scheme": "eirin",
            "_type": {
                "__type": "urn",
                "value": "myCustomValue3"
            }
        }],
        "creators": [Jim, Bob, Joe]
    }]
}

使用此代码:

def addUrn(parent, type, urnVal) {
    parent."$type" {
        __type "urn"
        "value" urnVal
    }
}

String getEpisode(String myCustomVal1, String myCustomVal2, String myCustomVal3) {    
    def builder = new groovy.json.JsonBuilder()
    builder {
        addUrn(delegate, "type", myCustomVal1)
        addUrn(delegate, "urn", "some:urn:$myCustomVal2")
        "date" {
            epoch 1265662800000
            str "2010-02-08T21:00:00Z"
        }
       "metadata" ({
                ratings ({
                        rating "G"
                        scheme "eirin"
                        addUrn(delegate, "_type", "$myCustomVal3")
                })
                creators "Jim", "Bob", "Joe"                    
        })
    }

    return root.toString();
}

代码抛出一个 StackOverflowError 因为第三个​​调用 addUrn (在嵌套的评级元素下我评论该行,它的工作完美(除了我错过了一个必要的信息块的事实)。

The code throws a StackOverflowError because of the third call to addUrn (under the nested ratings element. If I comment that line out, it works perfectly (other than the fact that I'm missing a necessary chunk of info).


  1. 为什么这样会发生吗?

  2. 如何将委托人设置为直接父母,例如评级

  1. Why is this happening?
  2. How to I set the delegate to the immediate parent, e.g. ratings?

我尝试使用metaClass无效。

I've tried using the metaClass to no avail.

推荐答案

这是很丑的(LOL),但会给你预期的结果:

This is pretty ugly (LOL) , but will give you the expected result:

def addUrn(parent, type, urnVal) {
    parent."$type" {
        __type "urn"
        "value" urnVal
    }
}

String getEpisode(String myCustomVal1, String myCustomVal2, String myCustomVal3) {
    def builder = new groovy.json.JsonBuilder()
    def root = builder {
        addUrn(delegate, "type", myCustomVal1)
        addUrn(delegate, "urn", "some:urn:$myCustomVal2")
        "date" {
            epoch 1265662800000
            str "2010-02-08T21:00:00Z"
        }
        "metadata" ([{([
                "ratings" ([{
                        rating "G"
                        scheme "eirin"
                        this.addUrn(delegate, "_type", "$myCustomVal3")
                }]),
                creators ("Jim", "Bob", "Joe")
        ])}])
    }

    println builder.toPrettyString()
}

注意: -


  • 在上一个问题中,不正确地说,代理人有
    来引用直接的父母。其实它确实是指立即
    父母。相反,我们必须在调用方法时引用脚本(它有
    addUrn 方法),因此使用这个当调用 addUrn 里面的评级。或者,您可以将类似于 addUrn 的方法发送评级。

  • 括号,链括号和方括号的使用和顺序在元数据之后看到的内容很重要。明白这将是繁琐的。但只有值得关注的是坚持使用方法调用,声明列表和使用闭包的基础知识。尝试每行缩进每个大括号,您将能够抓住潜在的魔法。 :)

  • StackOverFlow错误的原因是方法 getEpisode 无法达到方法 addUrn 由脚本拥有。

  • In the previous question, I was incorrect in saying that delegate has to refer to immediate parent. Actually it does refer to immediate parent. Instead, we have to refer to the script (which has the addUrn method) while calling the method, hence use of this when calling addUrn inside ratings. Alternatively you can ship "ratings" to a method similar to addUrn.
  • The use and sequence of parenthesis, chain brace and square brace is important what you see after "metadata". Making understand that will be cumbersome here. But only thing to keep an eye on is to stick to the basics of using method calls, declaring lists and use of closures. Try to indent each brace per line, you would be able to grab the underlying magic. :)
  • The reason for StackOverFlow error was that the method getEpisode was unable to reach the method addUrn which is owned by the script.

直接在 Groovy Web Console

这篇关于使用Groovy JsonBuilder设置委托值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆