Skip to content

Commit 9ca4898

Browse files
committed
feat(json): Improve JsonStructure API and documentation
1 parent 6f75341 commit 9ca4898

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
11
package datadog.json;
22

3+
/** The {@link JsonStructure} keeps track of JSON value being built. */
34
interface JsonStructure {
5+
/**
6+
* Begins an object.
7+
*
8+
* @throws IllegalStateException if the object can not be started at this position.
9+
*/
410
void beginObject();
511

12+
/**
13+
* Checks whether the current position is within an object.
14+
*
15+
* @return {@code true} if the current position is within an object, {@code false} otherwise.
16+
*/
617
boolean objectStarted();
718

19+
/**
20+
* Ends the current object.
21+
*
22+
* @throws IllegalStateException if the current position is not within an object.
23+
*/
824
void endObject();
925

26+
/** Begins an array. */
1027
void beginArray();
1128

29+
/**
30+
* Checks whether the current position is within an array.
31+
*
32+
* @return {@code true} if the current position is within an array, {@code false} otherwise.
33+
*/
1234
boolean arrayStarted();
1335

36+
/**
37+
* Ends the current array.
38+
*
39+
* @throws IllegalStateException if the current position is not within an array.
40+
*/
1441
void endArray();
1542

16-
void checkNameAllowed();
17-
18-
void checkValueAllowed();
43+
/**
44+
* Adds a name to the current object.
45+
*
46+
* @throws IllegalStateException if the current position is not within an object.
47+
*/
48+
void addName();
49+
50+
/**
51+
* Adds a value.
52+
*
53+
* @throws IllegalStateException if the current position can not have a value.
54+
*/
55+
void addValue();
1956
}

components/json/src/main/java/datadog/json/JsonWriter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.io.OutputStreamWriter;
1010

1111
/**
12-
* A lightweight JSON writer without dependencies. It performs minimal JSON structure check unless
12+
* A lightweight JSON writer without dependencies. It performs minimal JSON structure checks unless
1313
* using the lenient mode.
1414
*/
1515
public final class JsonWriter implements Flushable, AutoCloseable {
@@ -71,7 +71,7 @@ public JsonWriter name(String name) {
7171
if (name == null) {
7272
throw new IllegalArgumentException("name cannot be null");
7373
}
74-
this.structure.checkNameAllowed();
74+
this.structure.addName();
7575
injectCommaIfNeeded();
7676
writeStringLiteral(name);
7777
write(':');
@@ -84,7 +84,7 @@ public JsonWriter name(String name) {
8484
* @return This writer.
8585
*/
8686
public JsonWriter nullValue() {
87-
this.structure.checkValueAllowed();
87+
this.structure.addValue();
8888
injectCommaIfNeeded();
8989
writeStringRaw("null");
9090
endsValue();
@@ -112,7 +112,7 @@ public JsonWriter jsonValue(String value) {
112112
* @return This writer.
113113
*/
114114
public JsonWriter value(boolean value) {
115-
this.structure.checkValueAllowed();
115+
this.structure.addValue();
116116
injectCommaIfNeeded();
117117
writeStringRaw(value ? "true" : "false");
118118
endsValue();
@@ -129,7 +129,7 @@ public JsonWriter value(String value) {
129129
if (value == null) {
130130
return nullValue();
131131
}
132-
this.structure.checkValueAllowed();
132+
this.structure.addValue();
133133
injectCommaIfNeeded();
134134
writeStringLiteral(value);
135135
endsValue();
@@ -143,7 +143,7 @@ public JsonWriter value(String value) {
143143
* @return This writer.
144144
*/
145145
public JsonWriter value(int value) {
146-
this.structure.checkValueAllowed();
146+
this.structure.addValue();
147147
injectCommaIfNeeded();
148148
writeStringRaw(Integer.toString(value));
149149
endsValue();
@@ -157,7 +157,7 @@ public JsonWriter value(int value) {
157157
* @return This writer.
158158
*/
159159
public JsonWriter value(long value) {
160-
this.structure.checkValueAllowed();
160+
this.structure.addValue();
161161
injectCommaIfNeeded();
162162
writeStringRaw(Long.toString(value));
163163
endsValue();
@@ -174,7 +174,7 @@ public JsonWriter value(float value) {
174174
if (Float.isNaN(value)) {
175175
return nullValue();
176176
}
177-
this.structure.checkValueAllowed();
177+
this.structure.addValue();
178178
injectCommaIfNeeded();
179179
writeStringRaw(Float.toString(value));
180180
endsValue();
@@ -191,7 +191,7 @@ public JsonWriter value(double value) {
191191
if (Double.isNaN(value)) {
192192
return nullValue();
193193
}
194-
this.structure.checkValueAllowed();
194+
this.structure.addValue();
195195
injectCommaIfNeeded();
196196
writeStringRaw(Double.toString(value));
197197
endsValue();

components/json/src/main/java/datadog/json/LenientJsonStructure.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.json;
22

3+
/** A permissive {@link JsonStructure} that performs no structural checks on the built JSON. */
34
class LenientJsonStructure implements JsonStructure {
45
LenientJsonStructure() {}
56

@@ -26,8 +27,8 @@ public boolean arrayStarted() {
2627
public void endArray() {}
2728

2829
@Override
29-
public void checkNameAllowed() {}
30+
public void addName() {}
3031

3132
@Override
32-
public void checkValueAllowed() {}
33+
public void addValue() {}
3334
}

components/json/src/main/java/datadog/json/SafeJsonStructure.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import java.util.ArrayDeque;
66
import java.util.Deque;
77

8+
/**
9+
* This {@link JsonStructure} performs minimal structure checks to ensure the built JSON is
10+
* coherent.
11+
*/
812
class SafeJsonStructure implements JsonStructure {
913
private final Deque<Boolean> structure;
1014
private boolean complete;
@@ -63,14 +67,14 @@ public void endArray() {
6367
}
6468

6569
@Override
66-
public void checkNameAllowed() {
70+
public void addName() {
6771
if (!objectStarted()) {
6872
throw new IllegalStateException("Object not started");
6973
}
7074
}
7175

7276
@Override
73-
public void checkValueAllowed() {
77+
public void addValue() {
7478
if (this.complete) {
7579
throw new IllegalStateException("Object is complete");
7680
}

components/json/src/test/java/datadog/json/JsonStructureTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void lenientStructure() {
2727
lenientJsonStructure.beginArray();
2828
assertTrue(lenientJsonStructure.arrayStarted());
2929
lenientJsonStructure.endArray();
30-
assertDoesNotThrow(lenientJsonStructure::checkNameAllowed);
31-
assertDoesNotThrow(lenientJsonStructure::checkValueAllowed);
30+
assertDoesNotThrow(lenientJsonStructure::addName);
31+
assertDoesNotThrow(lenientJsonStructure::addValue);
3232
}
3333
}

0 commit comments

Comments
 (0)