Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7476

JsonSlurper sometimes does not parse escaped double quotes on parsing with JsonParsingUsingCharacterSource

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.3.11, 2.4.3
    • 2.4.4
    • JSON

    Description

      This came up when parsing a JSON string that had escaped JSON in it.
      E.g.

      {"string": "{\"key\": \"value\"}"}

      If the escape character used for ReaderCharacterSource.findNextChar is the last character in the buffer, then "foundEscape" is ignored for the first character in the recursion call (ReaderCharacterSource:229) and the match character is not added to the result.

      import groovy.json.internal.ReaderCharacterSource
      
      def testString = '"double\\"quote"'
      def expectedString = testString.substring(1, testString.length() - 1)
      def escapedQuotePos = testString.indexOf('"', 1)
      
      int quoteChar = (int)'"'.charAt(0)
      int backslashChar = (int)'\\'.charAt(0)
      
      ReaderCharacterSource rcs
      char[] result
      
      rcs = new ReaderCharacterSource(new StringReader(testString), escapedQuotePos - 1)
      result = rcs.findNextChar(quoteChar, backslashChar)
      assert(expectedString == new String(result))
      
      rcs = new ReaderCharacterSource(new StringReader(testString), escapedQuotePos + 1)
      result = rcs.findNextChar(quoteChar, backslashChar)
      assert(expectedString == new String(result))
      
      // This one fails due to the backslash being the last character in the buffer
      rcs = new ReaderCharacterSource(new StringReader(testString), escapedQuotePos)
      result = rcs.findNextChar(quoteChar, backslashChar)
      assert(expectedString == new String(result))
      

      Attachments

        Activity

          People

            pascalschumacher Pascal Schumacher
            gvaneyck Gabriel Van Eyck
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: