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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -