[ https://issues.apache.org/jira/browse/GROOVY-11314?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Denis Jakupovic updated GROOVY-11314: ------------------------------------- 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. {code:java} JsonBuilder Class: public String toPrettyString() { return JsonOutput.prettyPrint(toString()); } {code} However, we can construct a JsonBuilder with JsonGenerator and disableUnicodeEscaping, this is not possible with JsonOutput though because there is a final constructor. It would be great if the JsonOutput hat the same feature. The JsonOutput object uses the DefaultJsonGenerator with enabled unicode escaping. We need an option here and another logic in the prettyPrint function see here: {code:java} JsonOutput Class: 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; {code} And here: [https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharBuf.java#L379] {code:java} 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); } } {code} If the JsonBuilder is constructed with a JsonGenerator it should be constructed with JsonOuput as well and the prettyPrint function shall not add escaped strings. Currently there is no way to prettyPrint a json with the groovy.json classes without having escaped characters in the generated json. *I think here is the problem:* [https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharBuf.java#L349] {code:java} private static boolean shouldEscape(int c, boolean disableUnicodeEscaping) { if (c < 32) { /* less than space is a control char */ return true; } else if (c == 34) { /* double quote */ return true; } else if (c == 92) { /* backslash */ return true; } else if (!disableUnicodeEscaping && c > 126) { /* non-ascii char range */ return true; } return false; } {code} Best Denis was: 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. {code:java} JsonBuilder Class: public String toPrettyString() { return JsonOutput.prettyPrint(toString()); } {code} However, we can construct a JsonBuilder with JsonGenerator and disableUnicodeEscaping, this is not possible with JsonOutput though because there is a final constructor. It would be great if the JsonOutput hat the same feature. The JsonOutput object uses the DefaultJsonGenerator with enabled unicode escaping. We need an option here and another logic in the prettyPrint function see here: {code:java} JsonOutput Class: 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; {code} And here: https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharBuf.java#L379 {code:java} 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); } } {code} If the JsonBuilder is constructed with a JsonGenerator it should be constructed with JsonOuput as well and the prettyPrint function shall not add escaped strings. Currently there is no way to prettyPrint a json with the groovy.json classes without having escaped characters in the generated json. Best Denis > JsonOutput Pretty Print always escapes characters > ------------------------------------------------- > > Key: GROOVY-11314 > URL: https://issues.apache.org/jira/browse/GROOVY-11314 > Project: Groovy > Issue Type: Bug > Components: JSON > Affects Versions: 3.0.19 > Reporter: Denis Jakupovic > Priority: Major > > 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. > {code:java} > JsonBuilder Class: > public String toPrettyString() { > return JsonOutput.prettyPrint(toString()); > } {code} > However, we can construct a JsonBuilder with JsonGenerator and > disableUnicodeEscaping, this is not possible with JsonOutput though because > there is a final constructor. It would be great if the JsonOutput hat the > same feature. The JsonOutput object uses the DefaultJsonGenerator with > enabled unicode escaping. We need an option here and another logic in the > prettyPrint function see here: > {code:java} > JsonOutput Class: > 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; {code} > And here: > [https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharBuf.java#L379] > {code:java} > 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); > } > } {code} > If the JsonBuilder is constructed with a JsonGenerator it should be > constructed with JsonOuput as well and the prettyPrint function shall not add > escaped strings. > Currently there is no way to prettyPrint a json with the groovy.json classes > without having escaped characters in the generated json. > *I think here is the problem:* > [https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/CharBuf.java#L349] > {code:java} > private static boolean shouldEscape(int c, boolean disableUnicodeEscaping) { > if (c < 32) { /* less than space is a control char */ > return true; > } else if (c == 34) { /* double quote */ > return true; > } else if (c == 92) { /* backslash */ > return true; > } else if (!disableUnicodeEscaping && c > 126) { /* non-ascii char > range */ > return true; > } return false; > } {code} > Best > Denis -- This message was sent by Atlassian Jira (v8.20.10#820010)