(no subject)
Albert Kam <[email protected]> Thu, 28 Nov 2013 19:45:53 +0700
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <CAMT20g-xAsbLX0nB99_NYyrznorbsGy1CDSC1cf0P_t=Ffa7nQ@mail.gmail.com> |
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) 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
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user