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:
STON toJsonString: — compact, no newlines, no platform drift.(STON jsonWriter on: s) prettyPrint: true; newLine: String lf; ... — pretty, but with a
----- 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.
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.
Let's define a JSON datum to be any of
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.
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 <
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.