(no subject)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

(no subject)

Albert Kam
Hello, i'm currently trying to dump my object as a json with freemarker (latest version).

And so i started looking around for existing solution and found one at :
I've pasted the macro on the end of the mail.

Now the problem is when i tried it out, i seem to find an enumerable type, which cannot be enumerated ?
Here's the exception i got :

Getting the number of items or enumerating the items is not supported on this sequence+method (wrapper: f.e.b.SimpleMethodModel) value. (Hint 1: Maybe you wanted to call this method first and then do something with its return value. Hint 2: Getting items by intex possibly works, hence it's a "+sequence".)
And this happened on the line of :
<#elseif object?is_enumerable> 
...
<#list object as item> 

Now i wonder whether i can do a check in the ftl whether this can be enumerated or not before i actually enumerate it ?
Currently i dont know which field causes that, and why it's not enumerable.

The complete macro :

<#macro objectToJsonMacro object> 
<@compress single_line=true> 
    <#if object?is_hash || object?is_hash_ex> 
        <#assign first="true"> 
        { 
        <#list object?keys as key> 
            <#if first="false">,</#if> 
            <#assign value><@objectToJsonMacro object=object<key> /></#assign> 
            "${key}" : ${value?trim} 
            <#assign first="false"> 
        </#list> 
        } 
    <#elseif object?is_enumerable> 
        <#assign first="true"> 
        [ 
        <#list object as item> 
            <#if first="false">,</#if> 
            <#assign value><@objectToJsonMacro object=item /></#assign> 
            ${value?trim} 
            <#assign first="false"> 
        </#list> 
        ] 
    <#else> 
        "${object?trim}" 
    </#if> 
</@compress> 
</#macro> 

Regards,
Bertie

------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349351&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: (no subject)

Daniel Dekany
Thursday, November 28, 2013, 1:45:53 PM, Albert Kam wrote:

> Hello, i'm currently trying to dump my object as a json with freemarker (latest version).
>
> And so i started looking around for existing solution and found one at :
> http://ericbrandel.com/2013/03/28/freemarker-container-to-json/
> I've pasted the macro on the end of the mail.
>
> Now the problem is when i tried it out, i seem to find an
> enumerable type, which cannot be enumerated ?
> Here's the exception i got :
>
> Getting the number of items or enumerating the items is not
> supported on this sequence+method (wrapper: f.e.b.SimpleMethodModel)

This used to happen if you have a method like `Whatever foo(int
index)`, and so then you could write something like `${foo[i]}`, which
actually does the same as `${foo(i)}`. It doesn't make much sense
IMHO, but it's like that for ages and backward compatibility must be
kept. So `foo` is a sequence as you can apply `[index]` on it, but you
can't enumerate it as its impossible to query the sequence size.

> value. (Hint 1: Maybe you wanted to call this method first and then
> do something with its return value. Hint 2: Getting items by intex
> possibly works, hence it's a "+sequence".)
> And this happened on the line of :
> <#elseif object?is_enumerable>
> ...
> <#list object as item>
>
> Now i wonder whether i can do a check in the ftl whether this can
> be enumerated or not before i actually enumerate it ?

With ?enumerable... except that the author of ?enumerable didn't
thought about these odd sequence-like-methods. I will fix that.
Meanwhile, you can fix it be using your own TemplateMethodModelEx for
detecting this. (Or by risking that if something ?is_sequence &&
?is_method then it's never really a sequence... I'm not sure if that's
true though.)

> Currently i dont know which field causes that, and why it's not enumerable.
>
> The complete macro :
>
> <#macro objectToJsonMacro object>
> <@compress single_line=true>
>     <#if object?is_hash || object?is_hash_ex>

A hash_ex is always a hash, so you only need the is_hash

>         <#assign first="true">
>         {
>         <#list object?keys as key>
>             <#if first="false">,</#if>
>             <#assign value><@objectToJsonMacro object=object<key> /></#assign>
>             "${key}" : ${value?trim}
>             <#assign first="false">
>         </#list>
>         }
>     <#elseif object?is_enumerable>

&& !isSimpleMethodModel(object)

where isSimpleMethodModel will have to be implement by you. Or you
pre-patch FreeMarker...

>         <#assign first="true">
>         [
>         <#list object as item>
>             <#if first="false">,</#if>
>             <#assign value><@objectToJsonMacro object=item /></#assign>
>             ${value?trim}
>             <#assign first="false">

You don't need this `first` thing. <#if item_has_next>,</#if>.

>         </#list>
>         ]
>     <#else>
>         "${object?trim}"
>     </#if>
> </@compress>
> </#macro>
>
> Regards,
> Bertie

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349351&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user