[stringtemplate-interest] Generating JSON using StringTemplate

Udo Borkowski ub at abego-software.de
Wed Nov 16 07:11:05 PST 2011


Hi Piotr,

you don't need to add the 'format="…"'  in many many places. Just create one template containing the formatted string rendering and use that template instead of the '…' stuff.

E.g. create a template "q" (for "quote")


> q(s) ::= <<
> <s;format="singleQuote">
> >>
> 
> main(user) ::= <<
> {
>    firstName: <q(user.firstName)>,
>    lastName: <q(user.lastName)>
> }
> >>

This will output:

> {
>    firstName: 'Joe',
>    lastName: 'O\'Donovan'
> }



Depending on the concrete application I would also consider adding a template to render both the key and value:

> kv(key, value) ::= <<
> <key>: <value;format="singleQuote">
> >>
> 
> main(user) ::= <<
> {
>    <kv("firstName",user.firstName)>,
>    <kv("lastName", user.lastName)>
> }
> >>


Udo


On 15.11.2011, at 15:20, p_machner at poczta.fm wrote:

> Thanks a lot for your tip Johan :) I was considering this solution, it should allow me to do what I need but it has one disadvantage - it will clutter up my templates with something like 'format="string"' in many many places and this is something I've wanted to avoid.
> Any other ideas?
> 
> Thanks and best regards,
> Piotr
> 
> "Johan Stuyts" <j.stuyts at javathinker.com> pisze:
>>> Any ideas on how this escaping can be easily achieved?
>> 
>> You have to specify the format option in the template, and detect it in  
>> the attribute renderer:
>> 
>> 
>> Regards,
>> 
>> Johan
>> _______________________________________________
>> stringtemplate-interest mailing list
>> stringtemplate-interest at antlr.org
>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>> 
> 
> 
> 
> ----------------------------------------------------------------
> Masz strone www? Dodaj ja do katalogu!
> Sprawdz >>  http://linkint.pl/f2a8a
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20111116/b5db5145/attachment.html 


More information about the stringtemplate-interest mailing list