Results 1 to 4 of 4

Thread: JSon_StrEscape, JSon_StrUnEscape

  1. #1
    Junior Member
    Join Date
    Dec 2023
    Posts
    25
    Rep Power
    4

    JSon_StrEscape, JSon_StrUnEscape

    First, in the help file, these commands are listed like this: JSon_StrEscape$ JSon_UnStrEscape$

    But they don't actually work with the dollar signs attached.

    I think they got confused with: Url_Escape$ Url_UnEscape$
    which do have dollar signs.


    Anywho, heres some examples for the man page, and a larger one for the SampleScripts folder.

    ' Example: JSon_StrEscape Function
    Uses "Console"
    
    Dim inputString, result As String
    
    ' Example 1: Quotes and backslashes
    inputString = "Hello ""World"" with \backslashes\"
    result = JSon_StrEscape(inputString)
    PrintL "Original: " & inputString
    PrintL "Escaped : " & result
    PrintL
    
    ' Example 2: Control characters (newline, tab, carriage return)
    inputString = "Line 1" & $CRLF & "Line 2" & $TAB & "Tabbed text"
    result = JSon_StrEscape(inputString)
    PrintL "Original:"
    PrintL inputString
    PrintL "Escaped : " & result
    PrintL
    
    ' Example 3: Preparing string for JSON output
    inputString = "User said: ""Hello!"" Path: C:\Users\John"
    result = JSon_StrEscape(inputString)
    PrintL "Original: " & inputString
    PrintL "Escaped : " & result
    PrintL "JSON    : {\""message\"":\"" & result & ""\""}"
    PrintL
    
    WaitKey "Finished. Press any key to exit..."
    

    ' Example: JSon_StrUnEscape Function
    Uses "Console"
    
    Dim jsonString,result As String
    
    ' Example 1: Escaped quotes and backslashes
    jsonString = "\""Hello \""World\"" with \\backslashes\\\"""
    result = JSon_StrUnEscape(jsonString)
    Printl "Original: " & jsonString
    Printl "Result  : " & result
    Printl
    
    ' Example 2: Control characters (newline, tab, carriage return)
    jsonString = "Line 1\\nLine 2\\tTabbed text\\rCarriage return"
    result = JSon_StrUnEscape(jsonString)
    Printl "Original: " & jsonString
    Printl "Result  :"
    Printl result
    Printl
    
    ' Example 3: Typical JSON data processing
    jsonString = "{\""name\"":\""John\"",\""message\"":\""Hello\\nWorld!\"",\""path\"":\""C:\\\\Users\\\\John\""}"
    result = JSon_StrUnEscape(jsonString)
    Printl "JSON String: " & jsonString
    Printl "Unescaped  : " & result
    PrintL
    
    WaitKey "Finished. Press any key to exit..."
    

    ' ============================================================================
    ' THINBASIC Example Program - JSon_StrUnEscape Function Demo
    '
    ' This program demonstrates how to use the JSon_StrUnEscape function
    ' to transform JSON strings by unescaping special characters
    ' ============================================================================
    
    Uses "Console"
    
    ' Display program header
    PrintL "THINBASIC JSon_StrUnEscape Function Example"
    PrintL String$(50, "=")
    PrintL
    
    ' Declare variables
    Dim originalJsonString, unescapedString As String
    Dim counter As Long
    
    ' Example 1: Basic escaped quotes and backslashes
    PrintL "Example 1: Escaped quotes and backslashes"
    PrintL String$(45, "-")
    originalJsonString = "\""Hello \""World\"" with \\backslashes\\\"""
    unescapedString = JSon_StrUnEscape(originalJsonString)
    
    PrintL "Original JSON String: " & originalJsonString
    PrintL "Unescaped String    : " & unescapedString
    PrintL
    
    ' Example 2: Control characters (newline, tab, carriage return)
    PrintL "Example 2: Control characters"
    PrintL String$(30, "-")
    originalJsonString = "Line 1\\nLine 2\\tTabbed\\rCarriage Return"
    unescapedString = JSon_StrUnEscape(originalJsonString)
    
    PrintL "Original JSON String: " & originalJsonString
    PrintL "Unescaped String    :"
    PrintL unescapedString
    PrintL
    
    ' Example 3: Form feed and backspace characters
    PrintL "Example 3: Form feed and backspace"
    PrintL String$(35, "-")
    originalJsonString = "Text with\\fform feed and\\bbackspace"
    unescapedString = JSon_StrUnEscape(originalJsonString)
    
    PrintL "Original JSON String: " & originalJsonString
    PrintL "Unescaped String    : " & unescapedString
    PrintL
    
    ' Example 4: Forward slash (optional escaping)
    PrintL "Example 4: Forward slash escaping"
    PrintL String$(34, "-")
    originalJsonString = "URL: https:\\/\\/www.example.com\\/path"
    unescapedString = JSon_StrUnEscape(originalJsonString)
    
    PrintL "Original JSON String: " & originalJsonString
    PrintL "Unescaped String    : " & unescapedString
    PrintL
    
    ' Example 5: Complex JSON-like string with multiple escape sequences
    PrintL "Example 5: Complex example with multiple escapes"
    PrintL String$(48, "-")
    originalJsonString = "{\""message\"":\""Hello\\nWorld!\"",\""path\"":\""C:\\\\Users\\\\John\"",\""tab\"":\""Item\\tValue\""}"""
    unescapedString = JSon_StrUnEscape(originalJsonString)
    
    PrintL "Original JSON String: " & originalJsonString
    PrintL "Unescaped String    : " & unescapedString
    PrintL
    
    ' Example 6: Processing an array of JSON strings
    PrintL "Example 6: Processing multiple JSON strings"
    PrintL String$(42, "-")
    
    Dim jsonStrings(5) As String
    jsonStrings(1) = "\""First item with \""quotes\""\"""
    jsonStrings(2) = "\""Second\\nitem\\nwith\\nnewlines\"""
    jsonStrings(3) = "\""Third\\titem\\twith\\ttabs\"""
    jsonStrings(4) = "\""Path: C:\\\\Program Files\\\\MyApp\"""
    jsonStrings(5) = "\""Mixed: \""Hello\""\\n\\tWorld!\"""
    
    For counter = 1 To 5
      PrintL "JSON String " & Str$(counter) & ":"
      PrintL "  Original : " & jsonStrings(counter)
      PrintL "  Unescaped: " & JSon_StrUnEscape(jsonStrings(counter))
      PrintL
    Next counter
    
    ' Example 7: Error handling - empty string
    PrintL "Example 7: Edge cases"
    PrintL String$(21, "-")
    
    ' Empty string
    originalJsonString = ""
    unescapedString = JSon_StrUnEscape(originalJsonString)
    PrintL "Empty string result: '" & unescapedString & "'"
    
    ' String with no escape sequences
    originalJsonString = "Simple string with no escapes"
    unescapedString = JSon_StrUnEscape(originalJsonString)
    PrintL "No escapes  result : '" & unescapedString & "'"
    PrintL
    
    ' Practical example: Processing JSON data from a file or API
    PrintL "Example 8: Practical JSON processing simulation"
    PrintL String$(49, "-")
    
    ' Simulate JSON response data
    Dim jsonResponse As String
    jsonResponse = "{\""name\"":\""John Doe\"",\""message\"":\""Welcome!\\nPlease check your C:\\\\Documents folder.\"",\""timestamp\"":\""2024-01-15\\t10:30:00\""}"
    
    PrintL "Simulated JSON Response:"
    PrintL jsonResponse
    PrintL
    PrintL "After JSon_StrUnEscape processing:"
    PrintL JSon_StrUnEscape(jsonResponse)
    PrintL
    
    WaitKey "Finished. Press any key to exit..."
    

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    58
    Posts
    8,876
    Rep Power
    10
    Thanks a lot for this finding and for the examples.
    Will add $ sign like described in help and examples for the next update.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Junior Member
    Join Date
    Dec 2023
    Posts
    25
    Rep Power
    4

    More help file examples

    Certainly.
    I would like to help expand the help file because I refer to it so much & see so many entries without examples. In the future, Whats the best way to submit short example programs for it? Post them here? Private message to you or someone else? IN a .ZIP archive?

  4. #4
    Member
    Join Date
    Feb 2023
    Location
    London, Ontario, Canada
    Posts
    58
    Rep Power
    15
    Quote Originally Posted by TheInsider View Post
    In the future, Whats the best way to submit short example programs for it? Post them here? Private message to you or someone else? IN a .ZIP archive?
    How about posting on your GitHub site?

    Here's some samples from my GitHub site.

    Ref: https://github.com/joec4281/SimpleCOMDll

    Ref: https://github.com/joec4281/PSCom

    Ref: https://github.com/joec4281/cConnection

    Another option, use PasteBin Pastes - Pastebin.com

    Either way, share links to your thinBasic code on Github or PasteBin here in the forums.

    When this forum gets brought down by bots again, your code examples will be safe elsewhere.

    Joe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •