grails - create json string list from gorm query -


i have following domain object

class productquantity implements comparable {      static constraints = {     }      integer quantity      static mapping = {         version false     }      static belongsto = product     static hasmany = [products:product]      int compareto(obj) {         quantity.compareto(obj.quantity)     }  } 

i'd return json string looks [50,100,200]

with gorm, there away without having loop object , create set?

i started here

def availablequantities = productquantity.findall() 

any suggestions?

assuming doing controller :

productquantity.list() json 

you can control json in several ways :

1/ register json marshaller in bootstrap.groovy :

class bootstrap {     def init = { servletcontext ->         json.registerobjectmarshaller(productquantity) {             return it.quantity         }     } } 

2/ simple case, add tostring method productquantity class returns quantity property

3/ more complex use cases, can create custom named configurations (if want produce different types of json same domain class). see post.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -