Albert Kam
2013-11-28 12:45:53 UTC
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
(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