groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Closed] (GROOVY-6416) JsonBuilder returns blank {} format inside a mixin when using 'this' to pass to JsonBuilder
Date Wed, 01 Feb 2017 23:17:53 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-6416?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Paul King closed GROOVY-6416.
-----------------------------

> JsonBuilder returns blank {} format inside a mixin when using 'this' to pass to JsonBuilder
> -------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-6416
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6416
>             Project: Groovy
>          Issue Type: Bug
>          Components: JSON
>    Affects Versions: 2.1.6, 2.1.8
>         Environment: GGTS grails IDE using groovy 2.1.6
>            Reporter: William Woodman
>            Assignee: Jochen Theodorou
>            Priority: Minor
>              Labels: jsonBuilder, mixin
>
> bit weird this.
> i was trying to do a simple persistence excercise using GMongo and what i did was try
a simple mixin - to take the Json output of a class i add the mixin to and post into mongoDB.
 However i hit a snag and i suspect its a bug but not sure and have not received a post back
from any of the fora - so here goes 
>  
> i define a simple test class like this (its in a script file) 
> {code}
> class MyClass { 
>         String name 
>         int size 
> } 
> //set the mixin 
> MyClass.mixin Persistence 
> def myClass = new MyClass(name:"william", size:10) 
> JsonBuilder bldr = new JsonBuilder(myClass) 
> println "myClass json : " + bldr.toString() 
> {code}
> this all works as expected and the bldr generates the right string when i pass the instance
of my class to the JsonBuilder - outside the mixin.
> my persistence mixin looks like this at the moment 
> {code}
> //this is a mixin class - and trace debug stuff 
> class Persistence { 
>         def save () { 
>                 println "this: "+ this.class + " with name:  $name and size " + this.size
+ " dump: " + this.dump()
>                  def will = new Person (first:"will", last:'woodman', langs: ['script':
'groovy', 'static': 'java'])
>                  JsonBuilder bldr = new JsonBuilder(this) 
>                 println "content : " +  bldr.content + " pretty str :" + bldr.toPrettyString()
>                  JsonBuilder bldr2 = new JsonBuilder() 
>                 JsonBuilder bldr3 = new JsonBuilder() 
>                 JsonBuilder bldr4 = new JsonBuilder(will) 
>                 /*def root = bldr2.export { 
>                         thingy { 
>                                 firstName: "william" 
>                                 psize: 10 
>                                 address (town: 'ipswich', postcode: 'IP4 2TH') 
>                         } 
>                 }*/ 
>                 //map form 
>                 bldr2 name : this.name, size : this.size 
>                 //closure form 
>                 bldr3 { 
>                         name this.name 
>                         size this.size 
>                 } 
>                 println "bldr: " + bldr.toString() 
>                 println "bldr2: " + bldr2.toString() 
>                 println "bldr3: " + bldr3.toString() 
>                 println "bldr4: " + bldr4.toString() 
> {code}
> when i run the code and use the first iplementation  and pass 'this' which is an instance
of MyClass as this is a mixin - then i get am empty Json string - not the right answer  
> line 3 in save : JsonBuilder bldr = new JsonBuilder(this) - produces '{}'
> I tried creating a local class just above and pass that to JsonBuilder and it works fine
for that instance 
>  
> then out of perversity - if i change the original class definition to add Cloneable like
this
>  
> {code}
> class MyClass implements Cloneable{ 
>         String name 
>         int size 
> } 
> {code}
> and change the Mixin to pass a clone of 'this' and pass the clone to the bldr it all
works correctly
>  
> {code}
> //this is a mixin class 
> class Persistence { 
>         def save () { 
>                 println "this: "+ this.class + " with name:  $name and size " + this.size
+ " dump: " + this.dump()
>                  def will = new Person (first:"will", last:'woodman', langs: ['script':
'groovy', 'static': 'java'])
>                  JsonBuilder bldr = new JsonBuilder(this.clone()) 
>                 println "content : " +  bldr.content + " pretty str :" + bldr.toPrettyString()
> {code}
>          
> line 3 in save now reads : JsonBuilder bldr = new JsonBuilder(this.clone())  and this
works correctly with a fully formatted json string  
> - i dont really want to have to have the object to persist to have to to implement Cloneable.
 so why does the first form - new JsonBuilder (this) return me an {} json string - but the
second form -  new JsonBuilder(this.clone()) works just fine - very weird - has any one got
any ideas as to why its doing this
>  
> within the mixin - this is an instance of MyClass as expected - here is the trace of
the working cloneable version - and as you can see the last line bldr: is the correct form
>  
> {code}
> myClass json : {"size":10,"name":"william"} 
> this: class com.softwood.MyClass with name:  william and size 10 dump: <com.softwood.MyClass@1c0bddc
name=william size=10>
> content : com.softwood.MyClass@643473 pretty str :{ 
>     "size": 10, 
>     "name": "william" 
> } 
> bldr: {"size":10,"name":"william"} 
> {code}
> here is the same trace using removing the Cloneable and just passing this to the builder
in the mixin
>  
> {code}
> myClass json : {"size":10,"name":"william"} 
> this: class com.softwood.MyClass with name:  william and size 10 dump: <com.softwood.MyClass@f0cae7
name=william size=10>
> content : com.softwood.Persistence@af778e pretty str :{ 
>     
> } 
> bldr: {} 
> {code} 
> I cant for the life of me figure out why this is so - and have elected to try and contruct
a map from the properties variable instead and persist the class via this route - so i sort
of have another angle of attack - 
> If this is not a bug but a feature - perhaps someone could explain why it works this
way as it doesnt make sense to me  
>  



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Mime
View raw message