You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Net Wolf <ne...@gmail.com> on 2020/02/03 07:30:50 UTC

Return Map from Directive?

Hi.

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.

Something like:
# set mappings = JsonToMap ($jsonString)
# set username = $mappings($userid)

I thought I could just write a directive for JsonToMap but directives seem to return a string rather than a map.

Alternatively could I add a method in the context somehow? Perhaps I could then call something like...

# set username = $jsonString.toMap().get($userid)

Sorry if I've misunderstood something fundamental in velocity. I'm interested in receiving some guidance on the best way to do this sort of conversion.

Thanks in advance.
Net Wolf

Re: Return Map from Directive?

Posted by Net Wolf <ne...@gmail.com>.
Thanks Mike for your reply.

I had not previously seen the velocity tools project. Thanks for pointing that out.
 I've had a quick look over the JSONtool javadoc, but can't see anything there out of the box to convert to a map.

However I have full control over the context and can easily add a tool to it as per your first example.
Thanks for that suggestion. It really will be as simple as...

context.put("jsonTool", new JsonTool());

Thanks again. Net Wolf.

On February 3, 2020 7:00:26 PM UTC, Mike Kienenberger <mk...@gmail.com> 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.
>
>How you insert the tool depends how you use Velocity (do you set it up
>programmatically in java? using velocity tools?  using ant? etc).
>
>After the tool is in the context, you would do something like this:
>
>#set ($mapping = $jsonTool.convertJSONStringToJavaMap($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.
>
>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.apache.org/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.
>
>But you can also insert tools as simply as
>
>context.put("jsonTool", new JsonTool());
>
>
>
>On Mon, Feb 3, 2020 at 2:31 AM Net Wolf <ne...@gmail.com> wrote:
>>
>> Hi.
>>
>> 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.
>>
>> Something like:
>> # set mappings = JsonToMap ($jsonString)
>> # set username = $mappings($userid)
>>
>> I thought I could just write a directive for JsonToMap but directives
>seem to return a string rather than a map.
>>
>> Alternatively could I add a method in the context somehow? Perhaps I
>could then call something like...
>>
>> # set username = $jsonString.toMap().get($userid)
>>
>> Sorry if I've misunderstood something fundamental in velocity. I'm
>interested in receiving some guidance on the best way to do this sort
>of conversion.
>>
>> Thanks in advance.
>> Net Wolf
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>For additional commands, e-mail: user-help@velocity.apache.org

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

Re: Return Map from Directive?

Posted by Mike Kienenberger <mk...@gmail.com>.
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.

How you insert the tool depends how you use Velocity (do you set it up
programmatically in java? using velocity tools?  using ant? etc).

After the tool is in the context, you would do something like this:

#set ($mapping = $jsonTool.convertJSONStringToJavaMap($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.

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.apache.org/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.

But you can also insert tools as simply as

context.put("jsonTool", new JsonTool());



On Mon, Feb 3, 2020 at 2:31 AM Net Wolf <ne...@gmail.com> wrote:
>
> Hi.
>
> 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.
>
> Something like:
> # set mappings = JsonToMap ($jsonString)
> # set username = $mappings($userid)
>
> I thought I could just write a directive for JsonToMap but directives seem to return a string rather than a map.
>
> Alternatively could I add a method in the context somehow? Perhaps I could then call something like...
>
> # set username = $jsonString.toMap().get($userid)
>
> Sorry if I've misunderstood something fundamental in velocity. I'm interested in receiving some guidance on the best way to do this sort of conversion.
>
> Thanks in advance.
> Net Wolf

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org