When dealing with a JSon that some values are null Freemarker give an error in ?eval.
在处理JSon时,某些值为null Freemarker在?eval中给出错误。
with mapper.setSerializationInclusion(Inclusion.NON_NULL)
i can avoid this but i miss this information on the generated JSon.
使用mapper.setSerializationInclusion(Inclusion.NON_NULL)我可以避免这种情况,但我想念生成的JSon上的这些信息。
There's a way to achieve the evaluation with this null values?
有一种方法可以用这个空值来实现评估吗?
<#assign test = "{\"foo\":\"bar\"}">
<#assign m = test?eval>
${m.foo} <#-- prints: bar -->
Fail in eval
评估失败
<#assign test = "{\"foo\":null}">
<#assign m = test?eval> <#-- fail in eval -->
${m.foo}
1 个解决方案
#1
5
Unfortunately (or... infuriatingly), FTL doesn't know the concept of null
(although this might will change with 2.4). So even if you manage to create a Map
from that JSON where the foo
key exists but the associated value is null
(like you create a such Map
in Java), ${m.foo}
will still fail. Surely you can write ${m.foo!'null'}
, but that will print null
even if there's no foo
key at all. So maybe it's better if you provide a default value for the null
-s during the JSON evaluation:
不幸的是(或......令人愤怒),FTL不知道null的概念(尽管这可能会随着2.4而改变)。因此,即使您设法从存在foo密钥的JSON创建Map,但关联的值为null(就像您在Java中创建这样的Map),$ {m.foo}仍然会失败。当然你可以写$ {m.foo!'null'},但即使根本没有foo键也会打印null。因此,如果在JSON评估期间为null-s提供默认值,可能会更好:
<#function parseJSON json>
<#local null = 'null'> <#-- null is not a keyword in FTL -->
<#return json?eval>
</#function>
${parseJSON("{\"foo\":null}").foo} <#-- prints null -->
Now, however, you can't tell the difference between "null"
and null
. If that's a problem, you could chose some odd default, like '@@@null'
or even use a macro as indicator value and then use ?is_macro
to test if the value was null
(that hack is useful as JSON evaluation can't produce macros)...
但是,现在,您无法区分“null”和null。如果这是一个问题,你可以选择一些奇怪的默认值,比如'@@@ null',或者甚至使用一个宏作为指标值然后使用?is_macro来测试该值是否为null(该hack是有用的,因为JSON评估不能产生宏)...
#1
5
Unfortunately (or... infuriatingly), FTL doesn't know the concept of null
(although this might will change with 2.4). So even if you manage to create a Map
from that JSON where the foo
key exists but the associated value is null
(like you create a such Map
in Java), ${m.foo}
will still fail. Surely you can write ${m.foo!'null'}
, but that will print null
even if there's no foo
key at all. So maybe it's better if you provide a default value for the null
-s during the JSON evaluation:
不幸的是(或......令人愤怒),FTL不知道null的概念(尽管这可能会随着2.4而改变)。因此,即使您设法从存在foo密钥的JSON创建Map,但关联的值为null(就像您在Java中创建这样的Map),$ {m.foo}仍然会失败。当然你可以写$ {m.foo!'null'},但即使根本没有foo键也会打印null。因此,如果在JSON评估期间为null-s提供默认值,可能会更好:
<#function parseJSON json>
<#local null = 'null'> <#-- null is not a keyword in FTL -->
<#return json?eval>
</#function>
${parseJSON("{\"foo\":null}").foo} <#-- prints null -->
Now, however, you can't tell the difference between "null"
and null
. If that's a problem, you could chose some odd default, like '@@@null'
or even use a macro as indicator value and then use ?is_macro
to test if the value was null
(that hack is useful as JSON evaluation can't produce macros)...
但是,现在,您无法区分“null”和null。如果这是一个问题,你可以选择一些奇怪的默认值,比如'@@@ null',或者甚至使用一个宏作为指标值然后使用?is_macro来测试该值是否为null(该hack是有用的,因为JSON评估不能产生宏)...