1

I want to serialize my java data classes to JSON. To be honest, I posted a relevant question few days ago, however I haven't got my answer so far. At the moment, all answers suggest the Jackson's annotations. But I prefer not to get the classes involved directly and I think it's better that delegate this work to views.

Nevertheless, I found out that SpringMVC supports GroovyTemplate and I've a pleasant experience to deal with Groovy templating and builders and so I've a good sense about them. But I could find Groovy templates to build only html files. All of the examples begin with similar lines like this:

html {
      head {
      title( ... )
...
      }
}

Is there any way to write Groovy template in context of SpringMVC that provide JSON? For example, something like Grails JSON views known as gson files(see this).

I appreciate all for paying attention.

vahidreza
  • 843
  • 1
  • 8
  • 19

1 Answers1

0

Groovy itself has good functionality to produce json.

Your classes can be easily converted into json:

import groovy.json.*

class SomeClass{
   String name
   int value
   List someList = ['value1', 'value2', 'value3']
}

assert JsonOutput.toJson(new SomeClass(name:'myClass', value: 3)) == 
'{"value":3,"someList":["value1","value2","value3"],"name":"myClass"}'

it is also easy to create your own template engine. For example:

def SOME_CLASS_TEMPLATE = {SomeClass someClass->
   [
      the_json:[
         message: 'Message',
         name: someClass.name,
         first_list_value: someClass.someList?.getAt(0)
      ]
   ]
}

assert JsonOutput.toJson(SOME_CLASS_TEMPLATE(new SomeClass(name:'myClass', value: 3))) == 
'{"the_json":{"message":"Message","name":"myClass","first_list_value":"value1"}}'

formatted json output:

assert JsonOutput.prettyPrint(JsonOutput.toJson(SOME_CLASS_TEMPLATE(new SomeClass(name:'myClass', value: 3)))) == 
'''{
    "the_json": {
        "message": "Message",
        "name": "myClass",
        "first_list_value": "value1"
    }
}'''

Also check other classes from groovy.json package (JsonGenerator, JsonBuilder) they may be more preferable for your tasks.

Evgeny Smirnov
  • 2,886
  • 1
  • 12
  • 22
  • Thank you. But I know that groovy provide this capability. I want to use it as view template in SpringMCV. – vahidreza May 30 '18 at 10:48
  • JsonOutput.toJson() returns String. You only need to return this string from your controller – Evgeny Smirnov May 30 '18 at 11:27
  • It seems that I couldn't explain my wish clearly. I need something like grails json view but in java and spring mvc. – vahidreza May 30 '18 at 20:46
  • You can add groovy plugin to your build file. Then you can write templates in groovy and call them from your java classes. Or your would like to use only java? – Evgeny Smirnov May 31 '18 at 08:06
  • I'm ok with groovy. But the problem is that spring doesn't support groovy template as a view. I want to use groovy template for generating json in views not in java classes. – vahidreza May 31 '18 at 09:15