Ticket #361 (closed task: fixed)
allow template function for json serialization of variables in the template language
| Reported by: | mpo | Owned by: | jgou |
|---|---|---|---|
| Priority: | major | Milestone: | 0.5 |
| Component: | universe/kauri-template | Version: | |
| Keywords: | Cc: | kauri-discuss@… |
Description
${toJson(x)} should produce a string output that is a json compliant representation of the passed object x.
The chars should be obtained by applying the json lib we use. (jackson)
It should do the reverse operation of loading the json variable from source. (where the input source is parsed and mapped onto a java object)
Additional discussion: does it make sense not to parse the json source, and load the variable as a string directly? This would require some @mode=text attribute on the t:variable to indicate this.
Some more Context:
This feature request came as a reported side-effect of the changes made for #175. As discussed in #358 there are some benefits for doing this explicitly and not only for objects loaded from json sources.
See the comments in #358 for a dirty groovy-based variant of what could be done. (it migth be used as a hack/work-around for those that cannot wait)
In the documenation of this function it seems wise to refer to proper usage of this feature, and possibly more logical alternatives (ajax) as found in the comments of #358
Change History
comment:3 follow-up: ↓ 5 Changed 3 years ago by jgou
All our standard template functions are prefixed, only some kauri-specific ones such as publicUri are not. I suggest adding the toJson function as a standard template function, so I guess we 'd better also use a prefix.
How about ${json:toJson(foo)} ?
comment:4 Changed 3 years ago by jgou
- Status changed from accepted to closed
- Resolution set to fixed
comment:5 in reply to: ↑ 3 Changed 3 years ago by jgou
Replying to jgou:
All our standard template functions are prefixed, only some kauri-specific ones such as publicUri are not. I suggest adding the toJson function as a standard template function, so I guess we 'd better also use a prefix.
How about ${json:toJson(foo)} ?
toJson seems not entirely accurate, better choices are json:toJsonstring or json:serialize() or json:stringify() ...
We decided to go for ${json:serialize(obj)} .
Recent discussion in #358 adds more weight to the idea of having the suggested @mode="text" supported on t:variable too.
Reading json variables from @src and _not_ parsing them ever on the server-side, but simply injecting-transferring them out into the stream seems a common and wanted use case.
Note that we maybe need to think about additional escaping and interpretation tricks to properly support this use case:
Indeed: including these strings into embedded js generated by the template will cause other unwanted side effects through the known js-endline-interpretation (newlines are equivalent to newlines in js) so a json object holding a string with a newline will probably yield syntax-errors.
when t:variable x holds something that in json looks like
{"txt", "some text with newline!"};then the template construct
var fail = ${toJson(x)};will produce the failing syntax:
var fail = {"txt", "some text with newline!"};a better technique would be to have some
<pre class="kauri-json-data" id="success">${toJson}</pre>in combination with some standard jQuery stuff (we provide in kauri-util?) to read the json-contents from '#success' and parse it into usable js objects in the client. (along with standard css to hide this data)
This would better align with