Re: Return Map from Directive?
Net Wolf <[email protected]> Mon, 03 Feb 2020 22:36:06 +0000
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <[email protected]> |
------C6RPCAJT0Q2B3F0C690JH2P789W08P
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
Thanks Mike for your reply=2E
I had not previously seen the velocity tools project=2E Thanks for pointin=
g that out=2E
I've had a quick look over the JSONtool javadoc, but can't see anything t=
here out of the box to convert to a map=2E
However I have full control over the context and can easily add a tool to =
it as per your first example=2E
Thanks for that suggestion=2E It really will be as simple as=2E=2E=2E
context=2Eput("jsonTool", new JsonTool());
Thanks again=2E Net Wolf=2E
On February 3, 2020 7:00:26 PM UTC, Mike Kienenberger <mkienenb@gmail=2Eco=
m> wrote:
>What you typically do is insert a "tool" (which is a POJO -- any java
>class instance) into your context, then call a method on that tool to
>do something=2E
>
>How you insert the tool depends how you use Velocity (do you set it up
>programmatically in java? using velocity tools? using ant? etc)=2E
>
>After the tool is in the context, you would do something like this:
>
>#set ($mapping =3D $jsonTool=2EconvertJSONStringToJavaMap($userid) )
>
>This assumes that the object you inserted as "jasonTool" has a method
>signature like this:
>
>public Map convertJSONStringToJavaMap(UserIdType userid)
>
>Note that there is nothing special about an object inserted as a tool
>or about the methods on the tool=2E
>
>The documentation here is probably a good place to start learning
>about tools, although you do not have to use this approach to insert a
>tool:
>
>https://velocity=2Eapache=2Eorg/tools/devel/
>
>Also, it does look like VelocityTools already has a JSON tool object
>-- Maybe it already does what you want without any effort on your
>part=2E
>
>But you can also insert tools as simply as
>
>context=2Eput("jsonTool", new JsonTool());
>
>
>
>On Mon, Feb 3, 2020 at 2:31 AM Net Wolf <netwolfuk@gmail=2Ecom> wrote:
>>
>> Hi=2E
>>
>> I'm hoping to extend Velocity to be able to convert a JSON string to
>a Java Map so that template authors can map values in Velocity=2E
>>
>> Something like:
>> # set mappings =3D JsonToMap ($jsonString)
>> # set username =3D $mappings($userid)
>>
>> I thought I could just write a directive for JsonToMap but directives
>seem to return a string rather than a map=2E
>>
>> Alternatively could I add a method in the context somehow? Perhaps I
>could then call something like=2E=2E=2E
>>
>> # set username =3D $jsonString=2EtoMap()=2Eget($userid)
>>
>> Sorry if I've misunderstood something fundamental in velocity=2E I'm
>interested in receiving some guidance on the best way to do this sort
>of conversion=2E
>>
>> Thanks in advance=2E
>> Net Wolf
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@velocity=2Eapache=2Eorg
>For additional commands, e-mail: user-help@velocity=2Eapache=2Eorg
--=20
Sent from my Android device with K-9 Mail=2E Please excuse my brevity=2E
------C6RPCAJT0Q2B3F0C690JH2P789W08P--