Serialization and linebreaks

mlevy's Avatar


07 Feb, 2013 03:57 PM

I am trying to output the contents of a WYSIWYG region into a JSON string and maintain the HTML markup. The SerializerTool gets very close to what I need on this, but I still need to eliminate the linbreaks and escape quotes in the output to keep the Javascript intact. I'm new to Velocity, so I've been trying to work with the SerializerTool in combination with the EscapeTool, but it looks like I can't store the results of the SerializerTool as a variable I can pass to the EscapeTool.

Being able to do this sequence would really simplify the creation of JSON objects in Cascade. Is there an approach to serialize XML/HTML and then escape it for Javascript?

Velocity Code Logic Example

<script type="text/javascript">
#set($items = $_XPathTool.selectNodes($contentRoot, '//item'))
    var data = [
    #foreach($item in $items)
       #set($article = $_XPathTool.selectSingleNode($item, 'article/node()') )
#set($article_serialize = $_SerializerTool.serialize($article, false)) { "article" : "${_EscapeTool.javascript($article_serialize)}" },
#end ]; }

  1. 1 Posted by Ryan Griffith on 07 Feb, 2013 08:31 PM

    Ryan Griffith's Avatar


    It looks like $_EscapeTool.javascript() is a little finicky with the serialized content, so what I did was create a macro that escapes some of the special characters and line breaks present within the content.

    I was using an RSS feed in my example, but I hope the following will get you started in the right direction.

    #macro (JSONEscape $str)$str.replace("${_EscapeTool.backslash}", "\\${_EscapeTool.backslash}").replace("${_EscapeTool.quote}", "\\${_EscapeTool.quote}").replace("'", "\'").replaceAll("\r\n", "")#end
    <script type="text/javascript">
    #set($items = $_XPathTool.selectNodes($contentRoot, '//item'))
        var data = [
        #foreach($item in $items)
           #set($article_serialize = $_SerializerTool.serialize($item, true))
           #set($article_serialize = "#JSONEscape($article_serialize)")
            "article" : "${article_serialize}"

    Note: Velocity has an outstanding issue in which it will add extra whitespace for every new line and tab, this is why I removed all of the spacing from my macro. You may have to do the same within your loop if you have issues with additional spacing.

    Please let me know if you have any questions.


  2. 2 Posted by Ryan Griffith on 18 Feb, 2013 02:14 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    Just wanted to follow up to see if you had a chance to view my recent comment.

    Please let us know if you have any questions.


  3. 3 Posted by mlevy on 18 Feb, 2013 02:51 PM

    mlevy's Avatar

    Yes Ryan. This is perfect. Thank you.

  4. 4 Posted by Ryan Griffith on 18 Feb, 2013 03:12 PM

    Ryan Griffith's Avatar

    Thank you for following up, Matthew. Glad to hear that did the trick.

    Have a great day!

  5. Ryan Griffith closed this discussion on 18 Feb, 2013 03:12 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac