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 |
This comment has been minimized.
This comment has been minimized.
TheClassic
commented
Jan 5, 2018
@mexitek @narenranjit |
This comment has been minimized.
This comment has been minimized.
wmertens
commented
Feb 13, 2019
@TheClassic gists don't support notifications :( you won't get this mention but maybe you'll revisit this topic someday :) |
This comment has been minimized.
This comment has been minimized.
li-a
commented
Apr 12, 2019
•
Please note that JSON (like JavaScript) has encoding and escaping rules that will not be followed by this code. So for example, if your strings are allowed to contain characters like |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
mexitek commentedMay 16, 2017
Modified a bit. Now checks for Numbers: