JSON to Groovy parser

后端 未结 5 2129
暗喜
暗喜 2020-12-25 13:14

I found many things about converting Groovy to JSON, but oddly enough, not the other way.

What is the (best) JSON to Groovy parser around there ?

5条回答
  •  情话喂你
    2020-12-25 13:59

    JSON-lib claims to be able to transform POGO to JSON and back. If POGO means what I think it does (Plain Old Groovy Object), you're set :).

    They give this example:

    def strAsJsonObject = "{integer:1, bool: true}" as JSONObject
    

    Update:

    I've tried the lib myself, this is the complete code:

    import net.sf.*;
    import net.sf.json.*;
    import net.sf.json.groovy.*;
    
    println "hi"
    GJson.enhanceClasses()
    def strAsJsonObject = "{integer:1, bool: true}" as JSONObject
    println strAsJsonObject
    

    It'll chase you through a marathon of downloading dependencies (ezmorph, commons lang, commons logger) and once you've resolved them all, this is what you get:

    Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{integer:1, bool: true}' with class 'java.lang.String' to class 'net.sf.json.JSONObject'

    According to The mailing list, you get this for not calling GJsonlib.enhanceClasses(), but I did call that, as you can see above.

    I've concluded that it's a worthwhile endeavor to hate Groovy's JSON-lib.

提交回复
热议问题