<%
'=======================================================================================================================
' Small helpers for controllers that only ever return JSON (useLayout = False), so each new
' JSON API controller doesn't reimplement error-response formatting and string escaping.
'=======================================================================================================================

Sub WriteJsonError(statusLine, message)
    Response.Status = statusLine
    Response.Write "{""success"":false,""error"":" & JsonEncodeString(message) & "}"
End Sub

Function JsonEncodeString(s)
    Dim out : out = s
    out = Replace(out, "\", "\\")
    out = Replace(out, """", "\""")
    out = Replace(out, Chr(10), "\n")
    out = Replace(out, Chr(13), "\r")
    JsonEncodeString = """" & out & """"
End Function
%>
