Skip to content

Commit ab5423f

Browse files
committed
Support reading/writing values in JSON strings without java.lang.String
Signed-off-by: Lukas Jungmann <[email protected]>
1 parent 73ebee8 commit ab5423f

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

impl/src/main/java/org/eclipse/jsonp/JsonGeneratorImpl.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ private JsonGenerator writeName(String name) {
152152

153153
@Override
154154
public JsonGenerator write(String name, String fieldValue) {
155-
if (currentContext.scope != Scope.IN_OBJECT) {
156-
throw new JsonGenerationException(
157-
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
158-
}
159-
writeName(name);
160-
writeEscapedString(fieldValue);
155+
write(name, (CharSequence) fieldValue);
161156
return this;
162157
}
163158

@@ -338,7 +333,7 @@ public JsonGenerator write(String name, JsonValue value) {
338333
break;
339334
case STRING:
340335
JsonString str = (JsonString)value;
341-
write(name, str.getString());
336+
write(name, str.getChars());
342337
break;
343338
case NUMBER:
344339
JsonNumber number = (JsonNumber)value;
@@ -480,6 +475,15 @@ public JsonGenerator writeEnd() {
480475
return this;
481476
}
482477

478+
void write(String name, CharSequence fieldValue) {
479+
if (currentContext.scope != Scope.IN_OBJECT) {
480+
throw new JsonGenerationException(
481+
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
482+
}
483+
writeName(name);
484+
writeEscapedString(fieldValue);
485+
}
486+
483487
protected void writeComma() {
484488
if (isCommaAllowed()) {
485489
writeChar(',');
@@ -530,7 +534,7 @@ public void close() {
530534
// ^ ^ ^ ^
531535
// | | | |
532536
// begin end begin end
533-
void writeEscapedString(String string) {
537+
void writeEscapedString(CharSequence string) {
534538
writeChar('"');
535539
int len = string.length();
536540
for(int i = 0; i < len; i++) {
@@ -582,10 +586,15 @@ void writeEscapedString(String string) {
582586
writeChar('"');
583587
}
584588

585-
void writeString(String str, int begin, int end) {
589+
void writeString(CharSequence str, int begin, int end) {
586590
while (begin < end) { // source begin and end indexes
587591
int no = Math.min(buf.length - len, end - begin);
588-
str.getChars(begin, begin + no, buf, len);
592+
if (str instanceof String) {
593+
((String)str).getChars(begin, begin + no, buf, len);
594+
} else {
595+
// if passed a non-string, assume this is deliberate
596+
getChars(str, begin, begin + no, buf, len);
597+
}
589598
begin += no; // Increment source index
590599
len += no; // Increment dest index
591600
if (len >= buf.length) {
@@ -594,7 +603,7 @@ void writeString(String str, int begin, int end) {
594603
}
595604
}
596605

597-
void writeString(String str) {
606+
void writeString(CharSequence str) {
598607
writeString(str, 0, str.length());
599608
}
600609

@@ -669,6 +678,15 @@ private static int stringSize(int x) {
669678
return i+1;
670679
}
671680

681+
void getChars(CharSequence str, int srcBegin, int srcEnd, char[] dst, int dstBegin) {
682+
int length = srcEnd - srcBegin;
683+
for (int i = 0 ; i < length ; i++) {
684+
int srcIdx = srcBegin + i;
685+
int dstIdx = dstBegin + i;
686+
dst[dstIdx] = str.charAt(srcIdx);
687+
}
688+
}
689+
672690
/**
673691
* Places characters representing the integer i into the
674692
* character array buf. The characters are placed into

impl/src/main/java/org/eclipse/jsonp/JsonParserImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class JsonParserImpl implements JsonParser {
6060

6161
private final Stack stack = new Stack();
6262
private final JsonTokenizer tokenizer;
63-
63+
6464
public JsonParserImpl(Reader reader, BufferPool bufferPool) {
6565
this(reader, bufferPool, false);
6666
}
@@ -173,7 +173,7 @@ public JsonValue getValue() {
173173
return getObject(new JsonObjectBuilderImpl(bufferPool));
174174
case KEY_NAME:
175175
case VALUE_STRING:
176-
return new JsonStringImpl(getString());
176+
return new JsonStringImpl(getCharSequence());
177177
case VALUE_NUMBER:
178178
if (isDefinitelyInt()) {
179179
return JsonNumberImpl.getJsonNumber(getInt());
@@ -321,6 +321,14 @@ private JsonArray getArray(JsonArrayBuilder builder) {
321321
throw parsingException(JsonToken.EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]");
322322
}
323323

324+
private CharSequence getCharSequence() {
325+
if (currentEvent == Event.KEY_NAME || currentEvent == Event.VALUE_STRING
326+
|| currentEvent == Event.VALUE_NUMBER) {
327+
return tokenizer.getCharSequence();
328+
}
329+
throw new IllegalStateException(JsonMessages.PARSER_GETSTRING_ERR(currentEvent));
330+
}
331+
324332
private JsonObject getObject(JsonObjectBuilder builder) {
325333
while(hasNext()) {
326334
JsonParser.Event e = next();

impl/src/main/java/org/eclipse/jsonp/JsonStringImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
*/
2626
final class JsonStringImpl implements JsonString {
2727

28-
private final String value;
28+
private final CharSequence value;
2929

30-
JsonStringImpl(String value) {
30+
JsonStringImpl(CharSequence value) {
3131
this.value = value;
3232
}
3333

3434
@Override
3535
public String getString() {
36-
return value;
36+
return value.toString();
3737
}
3838

3939
@Override

impl/src/main/java/org/eclipse/jsonp/JsonTokenizer.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import jakarta.json.stream.JsonLocation;
2323
import jakarta.json.stream.JsonParser;
2424
import jakarta.json.stream.JsonParsingException;
25-
import java.io.*;
25+
import java.io.Closeable;
26+
import java.io.IOException;
27+
import java.io.Reader;
2628
import java.math.BigDecimal;
2729
import java.util.Arrays;
2830

@@ -510,6 +512,11 @@ String getValue() {
510512
return new String(buf, storeBegin, storeEnd-storeBegin);
511513
}
512514

515+
CharSequence getCharSequence() {
516+
int len = storeEnd - storeBegin;
517+
return new StringBuilder(len).append(buf, storeBegin, len);
518+
}
519+
513520
BigDecimal getBigDecimal() {
514521
if (bd == null) {
515522
bd = new BigDecimal(buf, storeBegin, storeEnd-storeBegin);
@@ -531,7 +538,7 @@ int getInt() {
531538
return getBigDecimal().intValue();
532539
}
533540
}
534-
541+
535542
long getLong() {
536543
// no need to create BigDecimal for common integer values (1-18 digits)
537544
int storeLen = storeEnd-storeBegin;
@@ -553,7 +560,7 @@ boolean isDefinitelyInt() {
553560
int storeLen = storeEnd-storeBegin;
554561
return !fracOrExp && (storeLen <= 9 || (minus && storeLen <= 10));
555562
}
556-
563+
557564
// returns true for common long values (1-18 digits).
558565
// So there are cases it will return false even though the number is long
559566
boolean isDefinitelyLong() {
@@ -582,5 +589,5 @@ private JsonParsingException expectedChar(int unexpected, char expected) {
582589
return new JsonParsingException(
583590
JsonMessages.TOKENIZER_EXPECTED_CHAR(unexpected, location, expected), location);
584591
}
585-
592+
586593
}

0 commit comments

Comments
 (0)