Skip to content

Commit 9ee992a

Browse files
committed
feat: implied-string-literals
Added support for implied-string-literals to the various JsonUrlWriter classes.
1 parent 6c9293c commit 9ee992a

File tree

17 files changed

+1344
-306
lines changed

17 files changed

+1344
-306
lines changed

module/jsonurl-core/src/main/java/org/jsonurl/JsonUrl.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,9 @@ public static <V> V parseLiteral(
846846
* @param text source
847847
* @param start offset in source
848848
* @param end length of source
849-
* @return dest
849+
* @return true if dest was modified
850850
*/
851-
public static <T extends Appendable> T appendLiteral(// NOPMD - CyclomaticComplexity
851+
public static <T extends Appendable> boolean appendLiteral(// NOPMD - CyclomaticComplexity
852852
T dest,
853853
CharSequence text,
854854
int start,
@@ -861,27 +861,20 @@ public static <T extends Appendable> T appendLiteral(// NOPMD - CyclomaticComple
861861
//
862862
// empty string
863863
//
864-
if (isEmptyUnquotedStringOK) {
865-
return dest;
866-
}
867-
868-
if (isImpliedStringLiteral) {
869-
//
870-
// no way to represent it
871-
//
872-
throw new IOException(MSG_EXPECT_LITERAL.getMessageText());
864+
if (isEmptyUnquotedStringOK || isImpliedStringLiteral) {
865+
return false;
873866
}
874867

875868
//
876869
// the empty string must be quoted
877870
//
878871
dest.append("''");
879-
return dest;
872+
return true;
880873
}
881874

882875
if (isImpliedStringLiteral) {
883876
Encode.encode(dest, text, start, end, false, true);
884-
return dest;
877+
return true;
885878
}
886879

887880
//
@@ -907,7 +900,7 @@ public static <T extends Appendable> T appendLiteral(// NOPMD - CyclomaticComple
907900
//
908901
dest.append('\'').append(text, start, end).append('\'');
909902
}
910-
return dest;
903+
return true;
911904
}
912905

913906
//
@@ -933,6 +926,6 @@ public static <T extends Appendable> T appendLiteral(// NOPMD - CyclomaticComple
933926
break;
934927
}
935928

936-
return dest;
929+
return true;
937930
}
938931
}

module/jsonurl-core/src/main/java/org/jsonurl/JsonUrlStringBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public class JsonUrlStringBuilder extends
3535
*/
3636
private static final int DEFAULT_SIZE = 1 << 10;
3737

38+
/**
39+
* My StringBuilder.
40+
*/
41+
private final StringBuilder builder; // NOPMD - AvoidStringBufferField
42+
3843
/**
3944
* Create a new JsonUrlStringBuilder.
4045
*
@@ -60,6 +65,7 @@ public JsonUrlStringBuilder(int size) {
6065
*/
6166
public JsonUrlStringBuilder(StringBuilder dest) {
6267
super(dest);
68+
this.builder = dest;
6369
}
6470

6571
@Override
@@ -71,4 +77,13 @@ public String build() {
7177
public String toString() {
7278
return out.toString();
7379
}
80+
81+
/**
82+
* Clear the internal buffer.
83+
* @return this
84+
*/
85+
public JsonUrlStringBuilder clear() {
86+
builder.setLength(0);
87+
return this;
88+
}
7489
}

module/jsonurl-core/src/main/java/org/jsonurl/JsonUrlTextAppender.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,51 +116,67 @@ public JsonUrlTextAppender<A,R> nameSeparator() throws IOException {
116116

117117
@Override
118118
public JsonUrlTextAppender<A,R> addNull() throws IOException {
119+
if (impliedStringLiterals) {
120+
throw new IOException(
121+
"can not represent null when implied-string-literals is true");
122+
}
123+
119124
out.append("null");
120125
return this;
121126
}
122127

123128
@Override
124129
public JsonUrlTextAppender<A,R> add(BigDecimal value) throws IOException {
130+
if (value == null) {
131+
return addNull();
132+
}
133+
125134
if (impliedStringLiterals) {
126135
add(String.valueOf(value), false);
127136

128137
} else {
129138
out.append(String.valueOf(value));
130139
}
140+
131141
return this;
132142
}
133143

134144
@Override
135145
public JsonUrlTextAppender<A,R> add(BigInteger value) throws IOException {
146+
if (value == null) {
147+
return addNull();
148+
}
149+
136150
if (impliedStringLiterals) {
137151
add(String.valueOf(value), false);
138152

139153
} else {
140154
out.append(String.valueOf(value));
141155
}
156+
157+
142158
return this;
143159
}
144160

145161
@Override
146162
public JsonUrlTextAppender<A,R> add(long value) throws IOException {
147163
if (impliedStringLiterals) {
148164
add(String.valueOf(value), false);
149-
150165
} else {
151166
out.append(String.valueOf(value));
152167
}
168+
153169
return this;
154170
}
155171

156172
@Override
157173
public JsonUrlTextAppender<A,R> add(double value) throws IOException {
158174
if (impliedStringLiterals) {
159175
add(String.valueOf(value), false);
160-
161176
} else {
162177
out.append(String.valueOf(value));
163178
}
179+
164180
return this;
165181
}
166182

@@ -172,7 +188,7 @@ public JsonUrlTextAppender<A,R> add(boolean value) throws IOException {
172188

173189
@Override
174190
public JsonUrlTextAppender<A,R> add(char value) throws IOException {
175-
out.append(value); // FIXME - need to encode
191+
out.append(value);
176192
return this;
177193
}
178194

@@ -182,6 +198,14 @@ public JsonUrlTextAppender<A,R> add(
182198
int start,
183199
int end,
184200
boolean isKey) throws IOException {
201+
202+
if (text == null) {
203+
if (isKey) {
204+
throw new IOException("object key can not be null");
205+
}
206+
return addNull();
207+
}
208+
185209
JsonUrl.appendLiteral(
186210
out,
187211
text,
@@ -190,6 +214,7 @@ public JsonUrlTextAppender<A,R> add(
190214
isKey,
191215
isKey ? allowEmptyUnquotedKey : allowEmptyUnquotedValue,
192216
impliedStringLiterals);
217+
193218
return this;
194219
}
195220

0 commit comments

Comments
 (0)