You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by "Dániel Dékány (Jira)" <ji...@apache.org> on 2021/08/04 13:43:00 UTC

[jira] [Commented] (FREEMARKER-185) eval_json fails

    [ https://issues.apache.org/jira/browse/FREEMARKER-185?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17393165#comment-17393165 ] 

Dániel Dékány commented on FREEMARKER-185:
------------------------------------------

The error is not thrown by {{?eval_json}}. But when you try to print {{vals}}, that fails since there's no implicit conversion from what's basically a `Map` to text. Of course if you just leave {{?eval_json}} out, and so leave that JSON as a string, then it will print it as is. So I'm not sure what the actual use-case here is. {{?eval_json}} is generally used exactly because you don't want just string, but want pick values from inside the JSON.

> eval_json fails
> ---------------
>
>                 Key: FREEMARKER-185
>                 URL: https://issues.apache.org/jira/browse/FREEMARKER-185
>             Project: Apache Freemarker
>          Issue Type: Bug
>          Components: engine
>    Affects Versions: 2.3.31
>            Reporter: Aniruddha
>            Priority: Major
>              Labels: features
>
>  
> {code:java}
> public static void main(String[] args) throws Throwable{
>         Configuration cfg = new Configuration(new Version("2.3.31"));
>         StringTemplateLoader stringLoader = new StringTemplateLoader();
>         cfg.setTemplateLoader(stringLoader);
>         StringBuilder sb = new StringBuilder();
>         sb.append("<#assign vals = '{  \"success\":true,  \"timestamp\":1627886463,  \"base\":\"Data1\",  \"date\":\"2021-08-02\",  \"values\":{    \"data2\":7.5,    \"data3\":44,    \"data4\":33  }}'?eval_json>");
>         sb.append("${vals}");
>         stringLoader.putTemplate("sample", sb.toString());
>         Map<String, Object> input = new HashMap<String, Object>();
>         Template template = cfg.getTemplate("sample");
>         Writer consoleWriter = new OutputStreamWriter(System.out);
>         template.process(input, consoleWriter);
>     }
> {code}
> The above code fails with below error
>  
>   FreeMarker template error (DEBUG mode; use RETHROW in production!):
>  For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), or "template output" , but this has evaluated to an extended_hash (wrapper: f.t.SimpleHash):
>  ==> vals [in template "sample" at line 1, column 175]
> ----
> FTL stack trace ("~" means nesting-related):
>  - Failed at: ${vals} [in template "sample" at line 1, column 173]
> ----
> {{}}
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)