0

I receive from another program Map with string representation of elements:

Map<String,String> properties = new HashMap<String,String>() {{
    put("news[0].title", "Title 1");
    put("news[0].body",  "Body 1");
    put("news[1].title", "Title 2");
    put("news[1].body",  "Body 2");
}};

I need to render it into freemarker-template. In question

freemarker-flat-structure-of-passing-parameters-transfer-to-array-of-objects

we decided that it is impossible to parse this kind of values in freemarker. But freemarker can eval json.

So I need to know how to transform this map into objects or json. I need something like that:

{
    "news": [
        {"title": "Title1", "body": "Body1"},
        {"title": "Title2", "body": "Body2"}            
    ]
}

Names of parameters in map are unknown, not exactly "news", not exactly "title" and "body", I don't know. May be there are some libraries for such purposes?

Community
  • 1
  • 1
Ivan
  • 465
  • 5
  • 19

2 Answers2

1

Since you are using your own language there, just write a parser for it. It's not a complex language after all. Also, transforming it to JSON doesn't make sense, based on what I know about the problem. Yes, the FTL expression syntax and JSON syntax overlaps quite much. But you shouldn't parse anything inside the FTL-s, just parse your language to List-s and JavaBeans and/or Map-s, and pass that plain Java object to FreeMarker.

ddekany
  • 29,656
  • 4
  • 57
  • 64
0

GSon and Jackson libraries are there for conversion between Java objects and JSON strings.

Seyf
  • 889
  • 8
  • 16