Discussion:
[FreeMarker-user] (no subject)
Albert Kam
2013-11-28 12:45:53 UTC
Permalink
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
Daniel Dekany
2013-11-28 20:13:00 UTC
Permalink
Hello, i'm currently trying to dump my object as a json with freemarker (latest version).
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 ?
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".)
<#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.
<#macro objectToJsonMacro object>
<#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>
"${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>
${value?trim}
<#assign first="false">
You don't need this `first` thing. <#if item_has_next>,</#if>.
</#list>
]
<#else>
"${object?trim}"
</#if>
</#macro>
Regards,
Bertie
--
Thanks,
Daniel Dekany
Loading...