Skip to content

Commit 801753b

Browse files
committed
Update ML stringify to match JS, fix #6
1 parent 3732788 commit 801753b

9 files changed

Lines changed: 49 additions & 9 deletions

File tree

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+
}

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+
}

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+
}

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

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",

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:

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

src/main/org/hjson/HjsonWriter.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,16 @@ void writeString(String value, Writer tw, int level, String separator) throws IO
144144
// format or we must replace the offending characters with safe escape
145145
// sequences.
146146

147-
boolean test=false;
148-
for(char ch : valuec) { if (needsEscape(ch)) { test=true; break; } }
149-
if (!test) { tw.write(separator+"\""+value+"\""); return; }
150-
151-
test=false;
152-
for(char ch : valuec) { if (needsEscapeML(ch)) { test=true; break; } }
153-
if (!test && !value.contains("'''")) writeMLString(value, tw, level, separator);
147+
boolean noEscape=true;
148+
for(char ch : valuec) { if (needsEscape(ch)) { noEscape=false; break; } }
149+
if (noEscape) { tw.write(separator+"\""+value+"\""); return; }
150+
151+
boolean noEscapeML=true, allWhite=true;
152+
for(char ch : valuec) {
153+
if (needsEscapeML(ch)) { noEscapeML=false; break; }
154+
else if (!HjsonParser.isWhiteSpace(ch)) allWhite=false;
155+
}
156+
if (noEscapeML && !allWhite && !value.contains("'''")) writeMLString(value, tw, level, separator);
154157
else tw.write(separator+"\""+JsonWriter.escapeString(value)+"\"");
155158
}
156159
else tw.write(separator+value);
@@ -216,6 +219,7 @@ static boolean needsEscapeML(char c) {
216219
switch (c) {
217220
case '\n':
218221
case '\r':
222+
case '\t':
219223
return false;
220224
default:
221225
return needsQuotes(c);

src/test/org/hjson/test/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public static void main(String[] args) throws Exception {
8585
boolean allOK=true;
8686

8787
for (String file : testNames) {
88+
if (file.contains("/")) continue; // skip for now
8889
int extIdx=file.lastIndexOf('.');
8990
String name=file.substring(0, extIdx);
9091
name=name.substring(0, name.length()-5);

0 commit comments

Comments
 (0)