0

I am creating a test application that simply creates a JSON representation of an object and sends it to the template, I then want to use the JSON in a a JS script on the front end. I am using Ratpack and Java Handlebars to do this.

Here is my Ratpack handler

class HighChartHandler extends InjectionHandler {

    void handle(Context ctx, TestDataJson testDataJson) {
        testDataJson.goals = 1000
        testDataJson.name = "Nick"
        def jsonData = json(testDataJson)
        ctx.render(handlebarsTemplate('highchartTest.html', model: jsonData))
    }
}

And then I try to simply render the data on the page using

<h1>Graph Test</h1>
<p>This is a WIP highchart test</p>
<p>{{model}}</p>

However I get this message :

ratpack.jackson.internal.DefaultJsonRender@467db85c

I want to simply render something like

{"name":"Forlan","goals":1000}
pocockn
  • 1,965
  • 5
  • 21
  • 36

2 Answers2

1

The method you're using, Jackson.json(Object) https://ratpack.io/manual/current/api/ratpack/jackson/Jackson.html#json-java.lang.Object- whose return type is of JsonRender https://ratpack.io/manual/current/api/ratpack/jackson/JsonRender.html

So when you're running it through the template, it's simply calling "JsonRender#toString()which results in what you're seeing:ratpack.jackson.internal.DefaultJsonRender@467db85c`

The Jackson.json method returns what is known in Ratpack as a Renderer. It tells Ratpack how to represent the Object that you've provided to the Renderer.

In order to produce json mixed with html, I would do something like this:

def jsonData = new groovy.json.JsonOutput.toJson(testDataJson)
ctx.render(handlebarsTemplate('highchartTest.html', model: [model: jsonData]))

I haven't tested this but it should work.

Dan Hyun
  • 536
  • 3
  • 7
-1

Try to use

<p>Name: {{name}}</p>
<p>Goals: {{goals}}</p>

instead of

<p>{{model}}</p>
siordache
  • 112
  • 1
  • 3