Skip to content

Commit 38743e1

Browse files
committed
add option to turn of legacy support for omitting root braces
1 parent 765b7d7 commit 38743e1

11 files changed

Lines changed: 83 additions & 64 deletions

File tree

assets/charset_test.hjson

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
2-
js-ascii: "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
3-
ml-ascii:
4-
'''
5-
! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
6-
'''
1+
{
2+
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
3+
js-ascii: "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
4+
ml-ascii:
5+
'''
6+
! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
7+
'''
8+
}

assets/failJSON24_test.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

assets/failStr7a_test.hjson

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
# invalid string containing a newline
3+
foo : "
4+
"
5+
}

assets/root_result.hjson

Lines changed: 0 additions & 7 deletions
This file was deleted.

assets/root_result.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

assets/root_test.hjson

Lines changed: 0 additions & 6 deletions
This file was deleted.

assets/testlist.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ failJSON20_test.json
2020
failJSON21_test.json
2121
failJSON22_test.json
2222
failJSON23_test.json
23-
failJSON24_test.json
2423
failJSON26_test.json
2524
failJSON28_test.json
2625
failJSON29_test.json
@@ -61,6 +60,7 @@ failStr6a_test.hjson
6160
failStr6b_test.hjson
6261
failStr6c_test.hjson
6362
failStr6d_test.hjson
63+
failStr7a_test.hjson
6464
kan_test.hjson
6565
keys_test.hjson
6666
mltabs_test.json
@@ -70,7 +70,6 @@ pass2_test.json
7070
pass3_test.json
7171
pass4_test.json
7272
passSingle_test.hjson
73-
root_test.hjson
7473
stringify1_test.hjson
7574
strings_test.hjson
7675
trail_test.hjson
@@ -80,4 +79,5 @@ stringify/quotes_keys_test.hjson
8079
stringify/quotes_strings_ml_test.json
8180
stringify/quotes_strings_test.hjson
8281
extra/notabs_test.json
83-
extra/separator_test.json
82+
extra/root_test.hjson
83+
extra/separator_test.json

assets/trail_test.hjson

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
// the following line contains trailing whitespace:
2-
foo: 0 -- this string starts at 0 and ends at 1, preceding and trailing whitespace is ignored -- 1
1+
{
2+
// the following line contains trailing whitespace:
3+
foo: 0 -- this string starts at 0 and ends at 1, preceding and trailing whitespace is ignored -- 1
4+
}

src/main/org/hjson/HjsonOptions.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,57 @@
2727
public class HjsonOptions {
2828

2929
private IHjsonDsfProvider[] dsf;
30+
private boolean legacyRoot;
3031

3132
public HjsonOptions() {
3233
dsf=new IHjsonDsfProvider[0];
34+
legacyRoot=true;
3335
}
3436

3537
/**
36-
* Detects whether root braces should be emitted.
38+
* Returns the DSF providers.
39+
*
40+
* @return providers.
41+
*/
42+
public IHjsonDsfProvider[] getDsfProviders() { return dsf.clone(); }
43+
44+
/**
45+
* Sets the DSF providers.
46+
*
47+
* @param value value
48+
*/
49+
public void setDsfProviders(IHjsonDsfProvider[] value) { dsf=value.clone(); }
50+
51+
/**
52+
* Detects whether objects without root braces are supported.
3753
*
38-
* @deprecated will always return true.
3954
* @return <code>true</code> if this feature is enabled.
4055
*/
41-
@Deprecated
42-
public boolean getEmitRootBraces() { return true; }
56+
public boolean getParseLegacyRoot() { return legacyRoot; }
4357

4458
/**
4559
* Sets whether root braces should be emitted.
4660
*
47-
* @deprecated root braces are always emitted.
4861
* @param value value
4962
*/
50-
@Deprecated
51-
public void setEmitRootBraces(boolean value) { }
63+
public void setParseLegacyRoot(boolean value) { legacyRoot=value; }
5264

5365
/**
54-
* Returns the DSF providers.
66+
* Detects whether root braces should be emitted.
5567
*
56-
* @return providers.
68+
* @deprecated will always return true.
69+
* @return <code>true</code> if this feature is enabled.
5770
*/
58-
public IHjsonDsfProvider[] getDsfProviders() { return dsf.clone(); }
71+
@Deprecated
72+
public boolean getEmitRootBraces() { return true; }
5973

6074
/**
61-
* Sets the DSF providers.
75+
* Sets whether root braces should be emitted.
6276
*
77+
* @deprecated root braces are always emitted.
6378
* @param value value
6479
*/
65-
public void setDsfProviders(IHjsonDsfProvider[] value) { dsf=value.clone(); }
80+
@Deprecated
81+
public void setEmitRootBraces(boolean value) { }
82+
6683
}

src/main/org/hjson/HjsonParser.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,20 @@ class HjsonParser {
3737
private int current;
3838
private StringBuilder captureBuffer, peek;
3939
private boolean capture;
40+
private boolean legacyRoot;
4041

4142
private IHjsonDsfProvider[] dsfProviders;
4243

4344
HjsonParser(String string, HjsonOptions options) {
4445
buffer=string;
4546
reset();
46-
if (options!=null) dsfProviders=options.getDsfProviders();
47-
else dsfProviders=new IHjsonDsfProvider[0];
47+
if (options!=null) {
48+
dsfProviders=options.getDsfProviders();
49+
legacyRoot=options.getParseLegacyRoot();
50+
} else {
51+
dsfProviders=new IHjsonDsfProvider[0];
52+
legacyRoot=true;
53+
}
4854
}
4955

5056
HjsonParser(Reader reader, HjsonOptions options) throws IOException {
@@ -75,23 +81,27 @@ JsonValue parse() throws IOException {
7581
read();
7682
skipWhiteSpace();
7783

78-
switch (current) {
79-
case '[':
80-
case '{':
81-
return checkTrailing(readValue());
82-
default:
83-
try {
84-
// assume we have a root object without braces
85-
return checkTrailing(readObject(true));
86-
} catch (Exception exception) {
87-
// test if we are dealing with a single JSON value instead (true/false/null/num/"")
88-
reset();
89-
read();
90-
skipWhiteSpace();
91-
try { return checkTrailing(readValue()); }
92-
catch (Exception exception2) { }
93-
throw exception; // throw original error
94-
}
84+
if (legacyRoot) {
85+
switch (current) {
86+
case '[':
87+
case '{':
88+
return checkTrailing(readValue());
89+
default:
90+
try {
91+
// assume we have a root object without braces
92+
return checkTrailing(readObject(true));
93+
} catch (Exception exception) {
94+
// test if we are dealing with a single JSON value instead (true/false/null/num/"")
95+
reset();
96+
read();
97+
skipWhiteSpace();
98+
try { return checkTrailing(readValue()); }
99+
catch (Exception exception2) { }
100+
throw exception; // throw original error
101+
}
102+
}
103+
} else {
104+
return checkTrailing(readValue());
95105
}
96106
}
97107

0 commit comments

Comments
 (0)