0% found this document useful (0 votes)
95 views2 pages

Visualfox-Json - Parte7

This procedure unescapes a JSON string by handling escape characters like \b, \t, \n, etc. either using a JSON library for best performance or by manually walking the string and replacing escapes. It returns the unescaped equivalent of the input JSON string.

Uploaded by

luiz santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views2 pages

Visualfox-Json - Parte7

This procedure unescapes a JSON string by handling escape characters like \b, \t, \n, etc. either using a JSON library for best performance or by manually walking the string and replacing escapes. It returns the unescaped equivalent of the input JSON string.

Uploaded by

luiz santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

PROTECTED PROCEDURE unescapechars

LPARAMETERS tcJSONStringToUnescape

*********************************************************************
*!* PURPOSE
*********************************************************************
*!* Unescape a JSON string according to RFC4627 section 2.5
*********************************************************************
*!* PARAMETERS
*********************************************************************
*!* tcJSONStringToUnescape
*!* A JSON string that is to be unescaped
*********************************************************************
*!* RETURN
*********************************************************************
*!* String - escaped equivalent of the JSON string sent in
*********************************************************************

LOCAL lcReturnUnescapedString, lnCharCounter, lnStringLength


LOCAL lnEscapePosition, lnEscapeOccurrence,
lnPositionAfterPreviousEscape
LOCAL lnPositionDifference

[Link] = ""

IF [Link] && using FLL to unescape chars (best performance)


IF ATC("[Link]", SET("Library")) = 0
SET LIBRARY TO [Link]
ENDIF
[Link] =
JSONUnescapeStr([Link])
ELSE && Use VFP to unescape chars (no dependency on FLL)
[Link] = AT("\", [Link])
IF [Link] > 0 && do any escaped chars exist?
[Link] = LEFT([Link],
[Link] - 1) && add everything before the escape to the return string
[Link] = 1
DO WHILE [Link] > 0 && walk all the escape
characters in the string
[Link] = SUBSTR([Link],
[Link] + 1, 1)
[Link] = [Link]
+ 2
DO CASE
CASE [Link] = "b" && ASCII 8
[Link] = CHR(8)
CASE [Link] = "t" && ASCII 9
[Link] = CHR(9)
CASE [Link] = "n" && ASCII 10
[Link] = CHR(10)
CASE [Link] = "f" && ASCII 12
[Link] = CHR(12)
CASE [Link] = "r" && ASCII 13
[Link] = CHR(13)
CASE [Link] = ["] && ASCII 34
[Link] = CHR(34)
CASE [Link] = "\" && ASCII 92
[Link] =
[Link] + 1
CASE [Link] = "u" && u00XX
[Link] = CHR(EVALUATE("0x" +
SUBSTR([Link], [Link] + 2, 4)))
[Link] =
[Link] + 4
ENDCASE
[Link] = [Link]
+ [Link]
[Link] = [Link] + 1
[Link] = AT("\",
[Link], [Link])
IF [Link] > 0
[Link] = (([Link] -
[Link]))
[Link] =
[Link] + SUBSTR([Link],
[Link], [Link])
ELSE
[Link] =
[Link] + SUBSTR([Link],
[Link])
ENDIF
ENDDO
ELSE
[Link] = [Link]
ENDIF
ENDIF
RETURN ([Link])

*********************************************************************
*!* ADDITIONAL NOTES AND COMMENTS
*********************************************************************
*!* See [Link]() for additional information regarding JSON char
escaping
*********************************************************************
ENDPROC

You might also like