Skip to content

Commit 7e89cfe

Browse files
committed
Fix #484
1 parent 6eb76dc commit 7e89cfe

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ JSON library.
1919
#467: Create `JsonReadFeature` to move JSON-specific `JsonParser.Feature`s to
2020
#480: `SerializableString` value can not directly render to Writer
2121
(requested by Philippe M)
22+
#484: Implement `UTF8JsonGenerator.writeRawValue(SerializableString)` (and
23+
`writeRaw(..)`) more efficiently
2224

2325
2.9.7 (not yet released)
2426

src/main/java/com/fasterxml/jackson/core/JsonGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public abstract class JsonGenerator
3434
*/
3535
public enum Feature {
3636
// // Low-level I/O / content features
37-
37+
3838
/**
3939
* Feature that determines whether generator will automatically
4040
* close underlying output target that is NOT owned by the
@@ -1095,6 +1095,7 @@ public abstract void writeUTF8String(byte[] text, int offset, int length)
10951095
*
10961096
* @since 2.1
10971097
*/
1098+
// public abstract void writeRaw(SerializableString raw) throws IOException;
10981099
public void writeRaw(SerializableString raw) throws IOException {
10991100
writeRaw(raw.getValue());
11001101
}

src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,12 @@ protected final void _writePPFieldName(SerializableString name) throws IOExcepti
424424
}
425425
_outputBuffer[_outputTail++] = _quoteChar;
426426
}
427-
_writeBytes(name.asQuotedUTF8());
427+
int len = name.appendQuotedUTF8(_outputBuffer, _outputTail);
428+
if (len < 0) {
429+
_writeBytes(name.asQuotedUTF8());
430+
} else {
431+
_outputTail += len;
432+
}
428433
if (addQuotes) {
429434
if (_outputTail >= _outputEnd) {
430435
_flushBuffer();
@@ -651,19 +656,23 @@ public void writeRaw(String text, int offset, int len) throws IOException
651656
@Override
652657
public void writeRaw(SerializableString text) throws IOException
653658
{
654-
byte[] raw = text.asUnquotedUTF8();
655-
if (raw.length > 0) {
656-
_writeBytes(raw);
659+
int len = text.appendUnquotedUTF8(_outputBuffer, _outputTail);
660+
if (len < 0) {
661+
_writeBytes(text.asUnquotedUTF8());
662+
} else {
663+
_outputTail += len;
657664
}
658665
}
659666

660667
// since 2.5
661668
@Override
662669
public void writeRawValue(SerializableString text) throws IOException {
663670
_verifyValueWrite(WRITE_RAW);
664-
byte[] raw = text.asUnquotedUTF8();
665-
if (raw.length > 0) {
666-
_writeBytes(raw);
671+
int len = text.appendUnquotedUTF8(_outputBuffer, _outputTail);
672+
if (len < 0) {
673+
_writeBytes(text.asUnquotedUTF8());
674+
} else {
675+
_outputTail += len;
667676
}
668677
}
669678

src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java

+29-17
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,37 @@ protected final void _writeFieldName(SerializableString name, boolean commaBefor
193193
_outputBuffer[_outputTail++] = ',';
194194
}
195195
// Alternate mode, in which quoting of field names disabled?
196-
final char[] quoted = name.asQuotedChars();
197196
if (_cfgUnqNames) {
198-
writeRaw(quoted, 0, quoted.length);
197+
final char[] ch = name.asQuotedChars();
198+
writeRaw(ch, 0, ch.length);
199199
return;
200200
}
201201
// we know there's room for at least one more char
202202
_outputBuffer[_outputTail++] = _quoteChar;
203203
// The beef:
204-
final int qlen = quoted.length;
205-
if ((_outputTail + qlen + 1) >= _outputEnd) {
206-
writeRaw(quoted, 0, qlen);
207-
// and closing quotes; need room for one more char:
208-
if (_outputTail >= _outputEnd) {
209-
_flushBuffer();
210-
}
211-
_outputBuffer[_outputTail++] = _quoteChar;
212-
} else {
213-
System.arraycopy(quoted, 0, _outputBuffer, _outputTail, qlen);
214-
_outputTail += qlen;
215-
_outputBuffer[_outputTail++] = _quoteChar;
204+
205+
int len = name.appendQuoted(_outputBuffer, _outputTail);
206+
if (len < 0) {
207+
_writeFieldNameTail(name);
208+
return;
209+
}
210+
_outputTail += len;
211+
if (_outputTail >= _outputEnd) {
212+
_flushBuffer();
216213
}
214+
_outputBuffer[_outputTail++] = _quoteChar;
217215
}
218-
216+
217+
private final void _writeFieldNameTail(SerializableString name) throws IOException
218+
{
219+
final char[] quoted = name.asQuotedChars();
220+
writeRaw(quoted, 0, quoted.length);
221+
if (_outputTail >= _outputEnd) {
222+
_flushBuffer();
223+
}
224+
_outputBuffer[_outputTail++] = _quoteChar;
225+
}
226+
219227
/*
220228
/**********************************************************
221229
/* Output method implementations, structural
@@ -339,7 +347,6 @@ protected final void _writePPFieldName(SerializableString name, boolean commaBef
339347
} else {
340348
_cfgPrettyPrinter.beforeObjectEntries(this);
341349
}
342-
343350
final char[] quoted = name.asQuotedChars();
344351
if (_cfgUnqNames) {// non-standard, omit quotes
345352
writeRaw(quoted, 0, quoted.length);
@@ -540,7 +547,12 @@ public void writeRaw(String text, int start, int len) throws IOException
540547
// @since 2.1
541548
@Override
542549
public void writeRaw(SerializableString text) throws IOException {
543-
writeRaw(text.getValue());
550+
int len = text.appendUnquoted(_outputBuffer, _outputTail);
551+
if (len < 0) {
552+
writeRaw(text.getValue());
553+
return;
554+
}
555+
_outputTail += len;
544556
}
545557

546558
@Override

0 commit comments

Comments
 (0)