Hi I would like to know if there is a way to generate JSON without lf? toJsonStringPretty: is generating lf and this is painful for tests. S.
CLAUDE.ai answer: The short answer: use STON toJsonString: obj (no Pretty). It produces compact JSON on a single line with zero line feeds: STON toJsonString: (Dictionary new at: 'foo' put: 1; at: 'bar' put: #(1 2 3); yourself) "=> '{"foo":1,"bar":[1,2,3]}'" Verified byte-level â the output contains no 0x0A (LF) bytes. Why toJsonStringPretty: is flaky for tests. It's a one-liner that delegates to STON jsonWriter with prettyPrint: true. The writer's newline character is set in STONWriter>>initialize: newLine := OSPlatform current lineEnding. So you get LF on macOS/Linux and CRLF on Windows â different expected strings per platform. That's the real source of the pain. If you actually want pretty output but deterministic line endings, build the writer yourself and call newLine: explicitly (it takes any string â CR, LF, CRLF, or even ''): String streamContents: [ :out | ((STON jsonWriter on: out) prettyPrint: true; newLine: String lf; yourself) nextPut: object ] STONWriter>>newLine: is a public accessor, and newlineIndent just writes whatever string you set, so passing String cr, String crlf, or '' all work. I confirmed it by reading STONWriter>>newLine:, newlineIndent, and dumping raw bytes from a pretty run with newLine: String cr â got byte 13, no byte 10. So, for test stability, the two clean answers for the pharo list are: 1. Use `STON toJsonString:` â compact, no newlines, no platform drift. 2. Or configure the writer: `(STON jsonWriter on: s) prettyPrint: true; newLine: String lf; ...` â pretty, but with a line ending you pick instead of the OS default. The base image only ships STON for JSON (no NeoJSON installed), so these are the relevant knobs. ----- Original message ----- From: stephane ducasse via Pharo-users <pharo-users@lists.pharo.org> To: Any question about pharo is welcome <pharo-users@lists.pharo.org> Cc: stephane ducasse <stephane.ducasse@inria.fr> Subject: [Pharo-users] JSON without lf? Date: Friday, April 10, 2026 6:54 AM Hi I would like to know if there is a way to generate JSON without lf? toJsonStringPretty: is generating lf and this is painful for tests. S.
I used the writer instead of the Facade. e.g. ^ String streamContents: [ :s | | writer | writer := STONWriter new. writer on: s. writer jsonMode: true. writer prettyPrint: true; newLine: newline; S
On 10 Apr 2026, at 12:54, stephane ducasse <stephane.ducasse@inria.fr> wrote:
Hi
I would like to know if there is a way to generate JSON without lf?
toJsonStringPretty: is generating lf and this is painful for tests.
S.
In Json, arbitrary anounts of white space can appear between tokens, including line feed. If this is âpainfulâ for your tests, then itâs unlikely to be the only thing that is. If you are comparing JSON irms *as* JSON, then linefeeds wonât matter . If you are comparing JSON items as strings, there are so many ways that different strrings can represent the same value -infinitely many, in fact - that linefeeds are the least of your worries. What are you actually doing in your tests? On Fri, 10 Apr 2026 at 10:55â¯PM, stephane ducasse via Pharo-users < pharo-users@lists.pharo.org> wrote:
Hi
I would like to know if there is a way to generate JSON without lf?
toJsonStringPretty: is generating lf and this is painful for tests.
S.
Let's define a JSON datum to be any of - nil - false or true - any kind of number - any kind of string - any kind of sequence (other than a string) whose elements are JSON data - any kind of dictionary whose keys are some kind of string and whose elements are JSON data. We start with the observation that it is dead simple to write Smalltalk code to print out a JSON datum in strict JSON format, as long as you don't get fancy. (Like NeoJSON does.) My Smalltalk code for this is 106 lines in methods, and by far the hardest thing was escaping the right characters the right way in strings. Everything else is simple and obvious. So if the existing JSON support in your language doesn't print what you want, it might well be easier to write your own than to find and understand the relevant documentation. We continue with the observation that if you want to test code that generates JSON data to see if you are getting the JSON data you're expecting, it's even easier to write #asJsonEquals:, where x asJsonEquals: y is true iff and only if x and y are both JSON data and would print the same way up to permutation of key:value pairs in JSON objects. My code is 34 lines of methods. Given that, which you want in your JSON toolkit anyway, [(code for testing) asJsonEquals: 'what you expect' readStream nextJson] assert is not hard to do. Test case that generate structured data should generally NOT compare against strings. This also applies to XML (where attribute order needs to be canonicalised or ignored). On Sat, 11 Apr 2026 at 10:53, Richard O'Keefe <raoknz@gmail.com> wrote:
In Json, arbitrary anounts of white space can appear between tokens, including line feed. If this is âpainfulâ for your tests, then itâs unlikely to be the only thing that is. If you are comparing JSON irms *as* JSON, then linefeeds wonât matter . If you are comparing JSON items as strings, there are so many ways that different strrings can represent the same value -infinitely many, in fact - that linefeeds are the least of your worries.
What are you actually doing in your tests?
On Fri, 10 Apr 2026 at 10:55â¯PM, stephane ducasse via Pharo-users < pharo-users@lists.pharo.org> wrote:
Hi
I would like to know if there is a way to generate JSON without lf?
toJsonStringPretty: is generating lf and this is painful for tests.
S.
Isnât toJSonString: what you want? Without the pretty which is adding the newlines? Norbert
Am 10.04.2026 um 12:55 schrieb stephane ducasse via Pharo-users <pharo-users@lists.pharo.org>:
Hi
I would like to know if there is a way to generate JSON without lf?
toJsonStringPretty: is generating lf and this is painful for tests.
S.
Hi My tests are about the documentation of a configuration written in JSON and wanted to have nicely formatted and not a bad one liner. So this is why I want to use the pretty printer but control the newLine: so I did it without the facade ^ String streamContents: [ :s | | writer | writer := STONWriter new. writer on: s. writer jsonMode: true. writer prettyPrint: true; newLine: newline; â¦. This way I controlled the nature of the newLine: S
Isnât toJSonString: what you want? Without the pretty which is adding the newlines?
Norbert
Am 10.04.2026 um 12:55 schrieb stephane ducasse via Pharo-users <pharo-users@lists.pharo.org>:
Hi
I would like to know if there is a way to generate JSON without lf?
toJsonStringPretty: is generating lf and this is painful for tests.
S.
I am confused. How is âcontrol the newLineâ the same as âwithout LFâ? On Windows, newline is CRLF, the Internet convention is CRLF, elsewhere, even on macOS, it is LF. Are you wanting the old MacOS CR convention? On Tue, 14 Apr 2026 at 6:29â¯AM, stephane ducasse via Pharo-users < pharo-users@lists.pharo.org> wrote:
Hi
My tests are about the documentation of a configuration written in JSON and wanted to have nicely formatted and not a bad one liner. So this is why I want to use the pretty printer but control the newLine: so I did it without the facade
^ String streamContents: [ :s | | writer | writer := STONWriter new. writer on: s. writer jsonMode: true. writer prettyPrint: true; newLine: newline; â¦.
This way I controlled the nature of the newLine:
S
Isnât toJSonString: what you want? Without the pretty which is adding the newlines?
Norbert
Am 10.04.2026 um 12:55 schrieb stephane ducasse via Pharo-users < pharo-users@lists.pharo.org>:
Hi
I would like to know if there is a way to generate JSON without lf?
toJsonStringPretty: is generating lf and this is painful for tests.
S.
participants (4)
-
Aaron Wohl -
Norbert Hartl -
Richard O'Keefe -
stephane ducasse