UnitTest( bang.asp )

Output

{ "foo": "bar" }
bar
{ "fo'o": "ba'r" }
ba'r
{ "f\"oo": "b\"ar" }
b"ar
{ "foo": "b\tar\r\nb'az" }
b	ar
b'az
{ "foo": "b'ar" }
b'ar

Sources

/bang_function.asp

<!--#include virtual="/lib/unit-tests.asp"-->
<!--#include virtual="/lib/axe/classes/Parsers/json2.asp"-->
<!--#include virtual="/lib/axe/classes/Utilities/stringbuilder.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en">
    <head>
        <title>AXE - Unit Test - Bang Function</title>
        <!--// meta tags //-->
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <!--// link tags //-->
        <link rel="stylesheet" type="text/css" media="screen" href="/lib/unit-tests.css" />
    </head>
    <body>
        <div id="container">
            <div id="container-hd">
                <h1>UnitTest( bang.asp )</h1>
            </div>
            <div id="container-bd">
 
<h2>Output</h2>
<div class="code"><pre><%
 
function [!](byVal v)
    dim mode, i, c _
      , Sb
 
    if( isArray(v) ) then
        v = join( v, vbNewline )
    end if
 
    mode = "NORMAL"' [ NORMAL, IN_SQ_STR, IN_DQ_STR ]
 
    set Sb = new StringBuilder
 
    i = 1
    while( i <= len(v) )
        c = mid(v, i, 1)
        select case c
            case """"
                select case mode
                    case "NORMAL"
                        mode = "IN_DQ_STR"
                        Sb.append(c)
                    case "IN_SQ_STR"
                        Sb.append("\""")
                    case "IN_DQ_STR"
                        mode = "NORMAL"
                        Sb.append(c)
                end select
 
            case "'"
                select case mode
                    case "NORMAL"
                        mode = "IN_SQ_STR"
                        Sb.append("""")
                    case "IN_SQ_STR"
                        mode = "NORMAL"
                        Sb.append("""")
                    case "IN_DQ_STR"
                        Sb.append(c)
                end select
 
            case "\"
                select case mode
                    case "NORMAL"
                        Sb.append(c)
                    case "IN_SQ_STR"
                        if( mid(v, i, 2) = "\'" ) then
                            i = i + 1
                            Sb.append("'")
                        else
                            Sb.append(c)
                        end if
                    case "IN_DQ_STR"
                        if( mid(v, i, 2) = "\""" ) then
                            i = i + 1
                            Sb.append("\""")
                        else
                            Sb.append(c)
                        end if
                end select
 
            case else
                Sb.append(c)
 
        end select
 
        i = i + 1
    wend
 
    set [!] = JSON.parse( Sb.toString() )
end function
 
function [!ε](byVal v)
    dim mode, i, c _
      , Sb
 
    mode = "NORMAL"' [ NORMAL, IN_SQ_STR, IN_DQ_STR ]
 
    set Sb = new StringBuilder
 
    i = 1
    while( i <= len(v) )
        c = mid(v, i, 1)
        select case c
            case """"
                select case mode
                    case "NORMAL"
                        mode = "IN_DQ_STR"
                        Sb.append(c)
                    case "IN_SQ_STR"
                        Sb.append("\""")
                    case "IN_DQ_STR"
                        mode = "NORMAL"
                        Sb.append(c)
                end select
 
            case "'"
                select case mode
                    case "NORMAL"
                        mode = "IN_SQ_STR"
                        Sb.append("""")
                    case "IN_SQ_STR"
                        mode = "NORMAL"
                        Sb.append("""")
                    case "IN_DQ_STR"
                        Sb.append(c)
                end select
 
            case "\"
                select case mode
                    case "NORMAL"
                        Sb.append(c)
                    case "IN_SQ_STR"
                        if( mid(v, i, 2) = "\'" ) then
                            i = i + 1
                            Sb.append("'")
                        else
                            Sb.append(c)
                        end if
                    case "IN_DQ_STR"
                        if( mid(v, i, 2) = "\""" ) then
                            i = i + 1
                            Sb.append("\""")
                        else
                            Sb.append(c)
                        end if
                end select
 
            case else
                Sb.append(c)
 
        end select
 
        i = i + 1
    wend
 
    [!ε] = Sb.toString()
    set Sb = nothing
end function
 
' simple case
Response.write( [!ε]("{ 'foo': 'bar' }") )
Response.write( vbNewline )
 
Response.write( [!]("{ 'foo': 'bar' }").foo )
Response.write( vbNewline )
 
' with a single quote mark
Response.write( [!ε]("{ 'fo\'o': 'ba\'r' }") )
Response.write( vbNewline )
 
Response.write( [!]("{ 'fo\'o': 'ba\'r' }").get("fo'o") )
Response.write( vbNewline )
 
' with a quote mark
Response.write( [!ε]("{ 'f""oo': 'b""ar' }") )
Response.write( vbNewline )
 
Response.write( [!]("{ 'f""oo': 'b""ar' }").get("f""oo") )
Response.write( vbNewline )
 
' with json escape chars
Response.write( [!ε]("{ 'foo': 'b\tar\r\nb\'az' }") )
Response.write( vbNewline )
 
Response.write( [!]("{ 'foo': 'b\tar\r\nb\'az' }").foo )
Response.write( vbNewline )
 
' common
Response.write( [!ε]("{ ""foo"": ""b'ar"" }") )
Response.write( vbNewline )
 
Response.write( [!]("{ ""foo"": ""b'ar"" }").foo )
Response.write( vbNewline )
 
%></pre></div>
 
            </div>
            <div id="container-ft">
                <h2>Sources</h2>
                <h3><%= Request.ServerVariables("SCRIPT_NAME") %></h3>
                <div class="code"><%= geshify( loadTextFile( Server.mapPath( Request.ServerVariables("SCRIPT_NAME") ) ), "asp" ) %></div>
            </div>
        </div>
        <!--// javascript tags //-->
    </body>
</html>