Skip to content

Commit 8513148

Browse files
committed
Add full unicode support to optimizedText
1 parent 86f9bd2 commit 8513148

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/ESUTF8StreamJsonParser.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ protected Text _finishAndReturnText() throws IOException {
104104
return null;
105105
}
106106
}
107+
case 2, 3, 4 -> {
108+
int bytesToSkip = codes[c];
109+
if (ptr + bytesToSkip > max) {
110+
return null;
111+
}
112+
ptr += bytesToSkip;
113+
++stringLength;
114+
}
107115
default -> {
108116
return null;
109117
}

libs/x-content/impl/src/test/java/org/elasticsearch/xcontent/provider/json/ESUTF8StreamJsonParserTests.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ public void testGetValueAsText() throws IOException {
4343
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
4444
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
4545

46-
var textRef = parser.getValueAsText().bytes();
47-
assertThat(textRef, Matchers.notNullValue());
48-
assertThat(textRef.offset(), Matchers.equalTo(9));
49-
assertThat(textRef.offset() + textRef.length(), Matchers.equalTo(12));
50-
assertTextRef(textRef, "bar");
46+
var text = parser.getValueAsText();
47+
assertThat(text, Matchers.notNullValue());
48+
49+
var bytes = text.bytes();
50+
assertThat(bytes.offset(), Matchers.equalTo(9));
51+
assertThat(bytes.offset() + bytes.length(), Matchers.equalTo(12));
52+
assertTextRef(bytes, "bar");
5153

5254
assertThat(parser.getValueAsString(), Matchers.equalTo("bar"));
5355
assertThat(parser.getValueAsText(), Matchers.nullValue());
@@ -60,9 +62,9 @@ public void testGetValueAsText() throws IOException {
6062
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
6163
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
6264

63-
var textRef = parser.getValueAsText();
64-
assertThat(textRef, Matchers.notNullValue());
65-
assertTextRef(textRef.bytes(), "bar\"baz\"");
65+
var text = parser.getValueAsText();
66+
assertThat(text, Matchers.notNullValue());
67+
assertTextRef(text.bytes(), "bar\"baz\"");
6668
});
6769

6870
testParseJson("{\"foo\": \"b\\u00e5r\"}", parser -> {
@@ -79,8 +81,17 @@ public void testGetValueAsText() throws IOException {
7981
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
8082
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
8183

82-
assertThat(parser.getValueAsText(), Matchers.nullValue());
84+
var text = parser.getValueAsText();
85+
assertThat(text, Matchers.notNullValue());
86+
87+
var bytes = text.bytes();
88+
assertThat(bytes.offset(), Matchers.equalTo(9));
89+
assertThat(bytes.offset() + bytes.length(), Matchers.equalTo(13));
90+
assertTextRef(bytes, "bår");
91+
8392
assertThat(parser.getValueAsString(), Matchers.equalTo("bår"));
93+
94+
assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.END_OBJECT));
8495
});
8596

8697
testParseJson("{\"foo\": [\"lorem\", \"ipsum\", \"dolor\"]}", parser -> {
@@ -169,7 +180,6 @@ private TestInput buildRandomInput(int length) {
169180
var value = Character.toChars(randomCodepoint(false));
170181
input.append(value);
171182
result.append(value);
172-
doesSupportOptimized = false;
173183
}
174184
}
175185
} else {

0 commit comments

Comments
 (0)