Skip to content

Commit 43ae24f

Browse files
committed
Update ML stringify to match JS
1 parent cc2eea7 commit 43ae24f

8 files changed

Lines changed: 39 additions & 3 deletions

File tree

Hjson/HjsonWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void writeString(string value, TextWriter tw, int level, bool hasComment, string
186186
// sequences.
187187

188188
if (!value.Any(c => needsEscape(c))) tw.Write(separator+"\""+value+"\"");
189-
else if (!value.Any(c => needsEscapeML(c)) && !value.Contains("'''")) writeMLString(value, tw, level, separator);
189+
else if (!value.Any(c => needsEscapeML(c)) && !value.Contains("'''") && !value.All(c => BaseReader.IsWhite(c))) writeMLString(value, tw, level, separator);
190190
else tw.Write(separator+"\""+JsonWriter.EscapeString(value)+"\"");
191191
}
192192
else tw.Write(separator+value);
@@ -263,6 +263,7 @@ static bool needsEscapeML(char c)
263263
{
264264
case '\n':
265265
case '\r':
266+
case '\t':
266267
return false;
267268
default:
268269
return needsQuotes(c);

test/assets/mltabs_result.hjson

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
foo:
3+
'''
4+
bar joe
5+
oki doki
6+
two tabs
7+
'''
8+
}

test/assets/mltabs_result.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar\tjoe\noki\tdoki\n\t\ttwo tabs"
3+
}

test/assets/mltabs_test.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar\tjoe\noki\tdoki\n\t\ttwo tabs"
3+
}

test/assets/strings_result.hjson

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
22
text1: This is a valid string value.
33
text2: a \ is just a \
4-
text3: "You need quotes\tfor escapes"
4+
text3: '''You need quotes for escapes'''
55
text4a: " untrimmed "
66
text4b: " untrimmed"
77
text4c: "untrimmed "
8+
notml1: "\n"
9+
notml2: " \n"
10+
notml3: "\n \n \n \n"
11+
notml4: "\t\n"
812
multiline1:
913
'''
1014
first line

test/assets/strings_result.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"text4a": " untrimmed ",
66
"text4b": " untrimmed",
77
"text4c": "untrimmed ",
8+
"notml1": "\n",
9+
"notml2": " \n",
10+
"notml3": "\n \n \n \n",
11+
"notml4": "\t\n",
812
"multiline1": "first line\n indented line\nlast line",
913
"multiline2": "first line\n indented line\nlast line",
1014
"multiline3": "first line\n indented line\nlast line\n",

test/assets/strings_test.hjson

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
text4b: " untrimmed"
1111
text4c: "untrimmed "
1212

13+
notml1: "\n"
14+
notml2: " \n"
15+
notml3: "\n \n \n \n"
16+
notml4: "\t\n"
17+
1318
# multiline string
1419

1520
multiline1:

test/assets/testlist.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ failStr6c_test.hjson
6363
failStr6d_test.hjson
6464
kan_test.hjson
6565
keys_test.hjson
66+
mltabs_test.json
6667
oa_test.hjson
6768
pass1_test.json
6869
pass2_test.json
@@ -72,4 +73,11 @@ passSingle_test.hjson
7273
root_test.hjson
7374
stringify1_test.hjson
7475
strings_test.hjson
75-
trail_test.hjson
76+
trail_test.hjson
77+
stringify/quotes_all_test.hjson
78+
stringify/quotes_always_test.hjson
79+
stringify/quotes_keys_test.hjson
80+
stringify/quotes_strings_ml_test.json
81+
stringify/quotes_strings_test.hjson
82+
extra/notabs_test.json
83+
extra/separator_test.json

0 commit comments

Comments
 (0)