Skip to content

Commit 765b7d7

Browse files
committed
HjsonWriter will always emit root braces
1 parent af07424 commit 765b7d7

3 files changed

Lines changed: 15 additions & 21 deletions

File tree

src/main/org/hjson/HjsonOptions.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,29 @@
2626
*/
2727
public class HjsonOptions {
2828

29-
private boolean emitRootBraces;
3029
private IHjsonDsfProvider[] dsf;
3130

3231
public HjsonOptions() {
33-
emitRootBraces = true; // default
3432
dsf=new IHjsonDsfProvider[0];
3533
}
3634

3735
/**
3836
* Detects whether root braces should be emitted.
3937
*
38+
* @deprecated will always return true.
4039
* @return <code>true</code> if this feature is enabled.
4140
*/
42-
public boolean getEmitRootBraces() { return emitRootBraces; }
41+
@Deprecated
42+
public boolean getEmitRootBraces() { return true; }
4343

4444
/**
4545
* Sets whether root braces should be emitted.
4646
*
47+
* @deprecated root braces are always emitted.
4748
* @param value value
4849
*/
49-
public void setEmitRootBraces(boolean value) { emitRootBraces=value; }
50+
@Deprecated
51+
public void setEmitRootBraces(boolean value) { }
5052

5153
/**
5254
* Returns the DSF providers.

src/main/org/hjson/HjsonWriter.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@
2828

2929
class HjsonWriter {
3030

31-
private boolean emitRootBraces;
3231
private IHjsonDsfProvider[] dsfProviders;
3332

3433
static Pattern needsEscapeName=Pattern.compile("[,\\{\\[\\}\\]\\s:#\"]|//|/\\*|'''");
3534

3635
public HjsonWriter(HjsonOptions options) {
3736
if (options!=null) {
38-
emitRootBraces=options.getEmitRootBraces();
3937
dsfProviders=options.getDsfProviders();
4038
} else {
41-
emitRootBraces=true;
4239
dsfProviders=new IHjsonDsfProvider[0];
4340
}
4441
}
@@ -48,7 +45,7 @@ void nl(Writer tw, int level) throws IOException {
4845
for (int i=0; i<level; i++) tw.write(" ");
4946
}
5047

51-
public void save(JsonValue value, Writer tw, int level, String separator, boolean noIndent, boolean isRootObject) throws IOException {
48+
public void save(JsonValue value, Writer tw, int level, String separator, boolean noIndent) throws IOException {
5249
if (value==null) {
5350
tw.write(separator);
5451
tw.write("null");
@@ -67,23 +64,18 @@ public void save(JsonValue value, Writer tw, int level, String separator, boolea
6764
switch (value.getType()) {
6865
case OBJECT:
6966
JsonObject obj=value.asObject();
70-
boolean showBraces=!isRootObject || emitRootBraces;
7167
if (!noIndent) { if (obj.size()>0) nl(tw, level); else tw.write(separator); }
72-
if (showBraces) tw.write('{');
73-
else level--; // reduce level for root
68+
tw.write('{');
7469

75-
boolean skipFirst=!showBraces;
7670
for (JsonObject.Member pair : obj) {
77-
if (!skipFirst) nl(tw, level+1); else skipFirst=false;
71+
nl(tw, level+1);
7872
tw.write(escapeName(pair.getName()));
7973
tw.write(":");
80-
save(pair.getValue(), tw, level+1, " ", false, false);
74+
save(pair.getValue(), tw, level+1, " ", false);
8175
}
8276

83-
if (showBraces) {
84-
if (obj.size()>0) nl(tw, level);
85-
tw.write('}');
86-
}
77+
if (obj.size()>0) nl(tw, level);
78+
tw.write('}');
8779
break;
8880
case ARRAY:
8981
JsonArray arr=value.asArray();
@@ -92,7 +84,7 @@ public void save(JsonValue value, Writer tw, int level, String separator, boolea
9284
tw.write('[');
9385
for (int i=0; i<n; i++) {
9486
nl(tw, level+1);
95-
save(arr.get(i), tw, level+1, "", true, false);
87+
save(arr.get(i), tw, level+1, "", true);
9688
}
9789
if (n>0) nl(tw, level);
9890
tw.write(']');

src/main/org/hjson/JsonValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public void writeTo(Writer writer, Stringify format) throws IOException {
501501
switch (format) {
502502
case PLAIN: new JsonWriter(false).save(this, buffer, 0); break;
503503
case FORMATTED: new JsonWriter(true).save(this, buffer, 0); break;
504-
case HJSON: new HjsonWriter(null).save(this, buffer, 0, "", true, true); break;
504+
case HJSON: new HjsonWriter(null).save(this, buffer, 0, "", true); break;
505505
}
506506
buffer.flush();
507507
}
@@ -519,7 +519,7 @@ public void writeTo(Writer writer, Stringify format) throws IOException {
519519
public void writeTo(Writer writer, HjsonOptions options) throws IOException {
520520
if (options==null) throw new NullPointerException("options is null");
521521
WritingBuffer buffer=new WritingBuffer(writer, 128);
522-
new HjsonWriter(options).save(this, buffer, 0, "", true, true);
522+
new HjsonWriter(options).save(this, buffer, 0, "", true);
523523
buffer.flush();
524524
}
525525

0 commit comments

Comments
 (0)