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

JsonOutput Pretty Print always escapes characters

    XMLWordPrintableJSON

Details

    • Wish
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.23, 3.0.20, 4.0.18
    • 4.0.19, 5.0.0-alpha-6
    • JSON
    • None

    Description

      Hi,

      the groovy.json package is widely used.
      https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java 

      If we use the toPrettyString function the json is being escaped. 

      JsonBuilder Class:
      public String toPrettyString() {
        return JsonOutput.prettyPrint(toString());
      } 

      However, we can construct a JsonBuilder with JsonGenerator and disableUnicodeEscaping and use new JsonBuilder(content, generator).toString() to create a proper json string representation. This is not possible with JsonOutput though because there is a final constructor and uses a default generator. However disableUnicodeEscaping is true by default but it is not handled properly by the JsonOutput class. It would be great if the JsonOutput had the same feature to construct JsonOutput with a custom JsonGenerator. The JsonOutput object uses the DefaultJsonGenerator with enabled unicode escaping through Options but the toJson() and prettyPrint methods do not handle the escaping properly. 

      https://github.com/apache/groovy/blob/GROOVY_3_0_X/subprojects/groovy-json/src/main/java/groovy/json/JsonOutput.java#L209

      case STRING:
        String textStr = token.getText();
        String textWithoutQuotes = textStr.substring(1, textStr.length() - 1);
        if (textWithoutQuotes.length() > 0) {
          output.addJsonEscapedString(textWithoutQuotes);
        } else {
          output.addQuoted(Chr.array());
        }
        break; 

      And here: https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharBuf.java#L379

      public final CharBuf addJsonEscapedString(final char[] charArray, boolean disableUnicodeEscaping) {
        if (charArray.length == 0) return this;
          if (hasAnyJSONControlChars(charArray, disableUnicodeEscaping)) {
            return doAddJsonEscapedString(charArray, disableUnicodeEscaping);
          } else {
            return this.addQuoted(charArray);
          }
         } 

      If the JsonBuilder is constructed with a JsonGenerator it should be constructed with JsonOuput as well and the prettyPrint and toJson function shall not add escaped strings. The Bug is in JsonOutput in the CharBuf call.

      This has to be fixed for toPretty method: 

      case STRING:
         String textStr = token.getText();
         String textWithoutQuotes = textStr.substring(1, textStr.length() - 1);
         if (textWithoutQuotes.length() > 0) {
           output.addJsonEscapedString(textWithoutQuotes, disableUnicodeEscaping);
         } else {
           output.addQuoted(Chr.array());
         } 
         break; 

      output.addJsonEscapedString(textWithoutQuotes, disableUnicodeEscaping) should be called with disableUnicodeEscaping. 

      Currently there is no way to prettyPrint a json with the groovy.json classes without having escaped characters in the generated json. 

      Best

      Denis

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              djakupovic Denis Jakupovic
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: