• Error on last charachter check

    By Andrew M Krier 1 decade ago

    We are using your library to parse a response from the Google Maps API. Unfortunately the last character of the JSON response happens to be and ASCII 10 (line feed). This causes the Parse function to throw an Error because it is obviously looking for a } or a ].

    Maybe this is Google's bad in not complying with the JSON spec, but I thought I would bring it to your attention either way. Thanks your for your work on this. It has been very helpful.

    • By Matthias Wille 9 years ago

      The reason for this is that trim() is used to remove whitespace (ONLY). Following code will fix the issue:

      %REM

      jsonReader class modification

      %END REM


      Public Function Parse(p_sJSON As String) As Variant

      '*********************************************************************************************

      '* Purpose: This is the only public method for this class. It returns an object

      '* created from parsing the input JSON string.

      '*

      '* Input: p_sJSON: The JSON string to parse

      '*

      '* Output: Either a JSONArray or JSONObject or combination

      '*

      '* Calls: ParseMe

      '*********************************************************************************************

      Dim sFirstChar As String

      Dim sLastChar As String

      On Error Goto ErrorHandler

      %REM

      instead of trim() I added a custom myTrim() function

      to remove tabs, carriage returns and line feeds

      %END REM

      Me.m_sJSON = MyTrim(p_sJSON)

      Me.m_iIndex = 0

      Me.m_iPrevIndex = -1

      Me.m_iLen = Len(Me.m_sJSON)

      Me.m_iOrigLen = Len(Me.m_sJSON)

      Me.m_sWorking = Me.m_sJSON

      Me.m_sChar = Left(Me.m_sWorking, 1)

      sFirstChar = Left(Me.m_sJSON, 1)

      sLastChar = Right(Me.m_sJSON, 1)

      If (sFirstChar = "[" And sLastChar = "]") Or (sFirstChar = "{" And sLastChar = "}") Then

      Set Parse = Me.ParseMe

      Else

      Set Parse = Nothing

      %REM

      Added more informative error message

      %END REM



      Error 1000, ERR_INVALID_JSON & " (Block character mismatch ASCII(" & Asc(sFirstChar) & "," & Asc(sLastChar) & ") "

      End If

      Done:

      Exit Function

      ErrorHandler:

      Call Me.RaiseError(Error$ & " Context: " & ERR_CURRENT_CHAR & "'" & Me.m_sChar & "'; " & _

      ERR_PREVIOUS_CHAR & "'" & Me.m_sChar & "'; " & _

      ERR_REMAINING_STRING & "'" & Me.m_sWorking & "'")

      End Function

      %REM

      Function myTrim

      Description: Comments for Function

      %END REM

      Private Function myTrim(srcstr As string) As String

      Dim lindex As Integer

      Dim rindex As Integer

      lindex = 1

      rindex = Len(srcstr)

      Do While True

      If lindex >= rindex Then

      Exit Do

      ElseIf Mid(srcstr,lindex,1) = Chr(9) Then

      lindex = lindex + 1

      ElseIf Mid(srcstr,lindex,1) = Chr(10) Then

      lindex = lindex + 1

      ElseIf Mid(srcstr,lindex,1) = Chr(13) Then

      lindex = lindex + 1

      ElseIf Mid(srcstr,lindex,1) = " " Then

      lindex = lindex + 1

      Else

      Exit do

      End If

      Loop

      Do While True

      If rindex <= lindex Then

      Exit Do

      ElseIf Mid(srcstr,rindex,1) = Chr(9) Then

      rindex = rindex - 1

      ElseIf Mid(srcstr,rindex,1) = Chr(10) Then

      rindex = rindex - 1

      ElseIf Mid(srcstr,rindex,1) = Chr(13) Then

      rindex = rindex - 1

      ElseIf Mid(srcstr,rindex,1) = " " Then

      rindex = rindex - 1

      Else

      Exit Do

      End If

      Loop

      If lindex<=rindex then

      myTrim = Mid(srcstr,lindex,rindex-lindex+1)

      Else

      myTrim = ""

      End If

      End Function


      • By Lars Berntrop 9 years ago

        Thanks! I'm integratring this into a version which will hopefully perform better.