<

Table of contents
  1. Server Side
    1. GRAILS TYPE Converters
    2. JSON
      1. JSON Parser Example
  2. Frontend manipulation
    1. Using messageSource
    2. Render grails tags to return in controller
    3. save grails tag in variable and render on page
    4. JSON
      1. Javascript manipulation

</details>




Server Side

GRAILS TYPE Converters

Convert and check type in controller

TypeCheck

param.short(var)
param.byte(var)
param.long(var)
param.double(var)
param.boolean(var)

JSON

JSON Parser Example

This is your JSON object that should be passed in to the method


def json = '''{
                  "markings": {
                      "headMarkings": "Brindle",
                      "leftForeMarkings": "",
                      "rightForeMarkings": "sock",
                      "leftHindMarkings": "sock",
                      "rightHindMarkings": "",
                      "otherMarkings": ""
                   }
                }'''

def jsonObj = grails.converters.JSON.parse(json)
print jsonObj
//optput [markings:[rightForeMarkings:sock, otherMarkings:, leftForeMarkings:, leftHindMarkings:sock, rightHindMarkings:, headMarkings:Brindle]]

def jsonStr = jsonObj.toString()

//This is the string which should be persisted in db
assert jsonStr == '{"markings":{"rightForeMarkings":"sock","otherMarkings":"","leftForeMarkings":"","leftHindMarkings":"sock","rightHindMarkings":"","headMarkings":"Brindle"}}'

//Get back json obj from json str
def getBackJsobObj = grails.converters.JSON.parse(jsonStr)

assert getBackJsobObj.markings.leftHindMarkings == 'sock'

Frontend manipulation

Using messageSource

i18n Docs

 messageSource.getMessage('batch.user.registration.confirmation.message', [jobId as String].toArray(), LocaleContextHolder.locale)

Render grails tags to return in controller

render g.select(from: languages, optionKey: "key", optionValue: "value", name: "languageChoice", class: "form-control", value: assessmentLanguage)

save grails tag in variable and render on page

"${yourTag.encodeAsRaw()}"

or

"${raw(user.description)}"

JSON

Javascript manipulation


<script>
    const catalogsByType = null;
</script>
<g:applyCodec encodeAs="none">
    catalogsByType = ${resultCatalogs.catalogsByType as grails.converters.JSON};
</g:applyCodec>
<script>
    let data = $
    {raw(data)}

</script>