Skip to content

Instantly share code, notes, and snippets.

@ruudud
Created January 18, 2016 13:59
Show Gist options
  • Save ruudud/698035f96e860ef85d3f to your computer and use it in GitHub Desktop.
Save ruudud/698035f96e860ef85d3f to your computer and use it in GitHub Desktop.
Freemarker JSON dump
<#assign foo><@objectToJson object=data.myobj /></#assign>
${foo?html}
<#macro objectToJson object>
<@compress single_line=true>
<#if object?is_hash || object?is_hash_ex>
<#assign first="true">
{
<#list object?keys as key>
<#if first="false">,</#if>
<#assign foo = key />
<#assign value><@objectToJson object=object[key]!"" /></#assign>
"${key}": ${value?trim}
<#assign first="false">
</#list>
}
<#elseif object?is_enumerable>
<#assign first="true">
[
<#list object as item>
<#if first="false">,</#if>
<#assign value><@objectToJson object=item /></#assign>
${value?trim}
<#assign first="false">
</#list>
]
<#else>
"${object?trim}"
</#if>
</@compress>
</#macro>
@garronej
Copy link

Great, thanks for sharing, will use in https://github.com/InseeFrLab/keycloakify

@garronej
Copy link

garronej commented Jun 22, 2021

<#macro objectToJson object depth>
    <@compress>

        <#local isHash = false>
        <#attempt>
            <#local isHash = object?is_hash || object?is_hash_ex>
        <#recover>
            /* can't evaluate if object is hash */
            undefined
            <#return>
        </#attempt>
        <#if isHash>

            <#local keys = "">

            <#attempt>
                <#local keys = object?keys>
            <#recover>
                /* can't list keys of object */
                undefined
                <#return>
            </#attempt>

            {${'\n'}

            <#list keys as key>

                <#if key == "class">
                    /* skipping "class" property of object */
                    <#continue>
                </#if>

                <#local value = "">

                <#attempt>
                    <#local value = object[key]>
                <#recover>
                    /* couldn't dereference ${key} of object */
                    <#continue>
                </#attempt>

                <#if depth gt 4>
                    /* Avoid calling recustively too many times depth: ${depth}, key: ${key} */
                    <#continue>
                </#if>

                "${key}": <@objectToJson object=value depth=depth+1/>,

            </#list>

            }${'\n'}

            <#return>

        </#if>


        <#local isMethod = "">
        <#attempt>
            <#local isMethod = object?is_method>
        <#recover>
            /* can't test if object is a method */
            undefined
            <#return>
        </#attempt>

        <#if isMethod>
            undefined
            <#return>
        </#if>



        <#local isBoolean = "">
        <#attempt>
            <#local isBoolean = object?is_boolean>
        <#recover>
            /* can't test if object is a boolean */
            undefined
            <#return>
        </#attempt>

        <#if isBoolean>
            ${object?c}
            <#return>
        </#if>


        <#local isEnumerable = "">
        <#attempt>
            <#local isEnumerable = object?is_enumerable>
        <#recover>
            /* can't test if object is enumerable */
            undefined
            <#return>
        </#attempt>

        <#if isEnumerable>

            [${'\n'}

            <#list object as item>

                <@objectToJson object=item depth=depth+1/>,

            </#list>

            ]${'\n'}

            <#return>
        </#if>


        <#attempt>
            "${object?no_esc}"
        <#recover>
            /* couldn't convert into string non hash, non method, non boolean, non enumerable object */
            undefined;
            <#return>
        </#attempt>


    </@compress>
</#macro>
<script>

<#assign obj = .data_model>

<#assign foo><@objectToJson object=obj depth=0 /></#assign>

console.log(${foo});
console.log(JSON.parse(JSON.stringify(${foo})));

</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment