简体   繁体   中英

Groovy JsonBuilder: using field names that are variable names too

I'm using groovy.json.JsonBuilder and having trouble specifying a fieldname that is also the name of a variable in the current scope.

This works:

System.out.println(new GroovyShell().evaluate(
          "def builder = new groovy.json.JsonBuilder();"
        + "def age = 23;"
        + "builder.example {"
        + "  name 'Fred';"
        + "  'age1' 27;"
        + "  blah {"
        + "    foo 'bar';"
        + "  };"
        + "};"
        + "return builder.toPrettyString()"));

And produces output:

{
    "example": {
        "name": "Fred",
        "age1": 27,
        "blah": {
            "foo": "bar"
        }
    }
}

But this fails (the field 'age' for some reason conflicts with the variable):

System.out.println(new GroovyShell().evaluate(
          "def builder = new groovy.json.JsonBuilder();"
        + "def age = 23;"
        + "builder.example {"
        + "  name 'Fred';"
        + "  'age' 27;"
        + "  blah {"
        + "    foo 'bar';"
        + "  };"
        + "};"
        + "return builder.toPrettyString()"));

And produces an exception:

groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.call() is applicable for argument types: (Integer) values: [27]
Possible solutions: wait(), abs(), any(), wait(long), wait(long, int), max(int, int)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:72)
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:48)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
    at Script1$_run_closure1.doCall(Script1.groovy:5)
    at Script1$_run_closure1.doCall(Script1.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:104)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:326)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:264)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041)
    at groovy.lang.Closure.call(Closure.java:421)
    at groovy.lang.Closure.call(Closure.java:415)
    at groovy.json.JsonDelegate.cloneDelegateAndGetContent(JsonDelegate.java:91)
    at groovy.json.JsonBuilder.invokeMethod(JsonBuilder.java:314)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
    at Script1.run(Script1.groovy:3)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:444)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:482)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:453)

I do not want to change the name of my variable. Is there some way to force JsonBuilder to accept the field name? As you can see, I tried to put it in quotes, but that didn't help.

Use delegate.age in order to refer to the surrounding closure instead of referring to the variable.

System.out.println(new GroovyShell().evaluate(
          "def builder = new groovy.json.JsonBuilder();"
        + "def age = 23;"
        + "builder.example {"
        + "  name 'Fred';"
        + "  delegate.age 27;"
        + "  blah {"
        + "    foo 'bar';"
        + "  };"
        + "};"
        + "return builder.toPrettyString()"));

should give you

{
    "example": {
        "name": "Fred",
        "age": 27,
        "blah": {
            "foo": "bar"
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM