Skip to content

Instantly share code, notes, and snippets.

@edgarv09
Forked from narenranjit/VelToJSON
Created April 2, 2020 23:31
Show Gist options
  • Save edgarv09/2561e3e73f8e9a393f7a6e80d9c2edb8 to your computer and use it in GitHub Desktop.
Save edgarv09/2561e3e73f8e9a393f7a6e80d9c2edb8 to your computer and use it in GitHub Desktop.
Convert objects in Velocity templates to JSON
#macro(VelListToJSON $list )
#set($myList = $list )## dereference
{
#foreach($key in $myList.keySet())
"$key":
#set($x = $myList.get($key))
#VelToJSON($x)
#if($foreachCount != $myList.keySet().size()) , #end
#end
}
#end
#macro(VelArrayToJSON $arr)
#set($myArr = $arr)
[
#foreach($x in $myArr)
#VelToJSON($x)
#if($foreachCount != $myArr.size()) , #end
#end
]
#end
##TODO: Make it not treat numbers as strings
#macro(VelToJSON $item)
#if($item.keySet())
#VelListToJSON($item)
#elseif($item.size())
#VelArrayToJSON($item)
#elseif($item == true || $item ==false)
$item
#else
"$item"
#end
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment