Delphi: Convert string into escaped json string

function EscapeString(const AValue: string): string; const ESCAPE = '\'; QUOTATION_MARK = '"'; REVERSE_SOLIDUS = '\'; SOLIDUS = '/'; BACKSPACE = #8; FORM_FEED = #12; NEW_LINE = #10; CARRIAGE_RETURN = #13; HORIZONTAL_TAB = #9; var AChar: Char; begin Result := ''; for AChar in AValue do begin case AChar of QUOTATION_MARK: Result := Result + ESCAPE + QUOTATION_MARK; REVERSE_SOLIDUS: Result := Result + ESCAPE + REVERSE_SOLIDUS; SOLIDUS: Result := Result + ESCAPE + SOLIDUS; BACKSPACE: Result := Result + ESCAPE + 'b'; FORM_FEED: Result := Result + ESCAPE + 'f'; NEW_LINE: Result := Result + ESCAPE + 'n'; CARRIAGE_RETURN: Result := Result + ESCAPE + 'r'; HORIZONTAL_TAB: Result := Result + ESCAPE + 't'; else begin if (Integer(AChar) < 32) or (Integer(AChar) > 126) then Result := Result + ESCAPE + 'u' + IntToHex(Integer(AChar), 4) else Result := Result + AChar; end; end; end; end;
Function for escape string to work with json.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.