Skip to content

Commit b13bde6

Browse files
committed
minor reworking of #285, add release notes
1 parent 7a0991a commit b13bde6

File tree

12 files changed

+162
-219
lines changed

12 files changed

+162
-219
lines changed

release-notes/CREDITS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ Lokesh Kumar N (LokeshN@github)
8282
(2.8.0)
8383
* Contributed implementation for #86: Allow inclusion of request body for JsonParseException
8484
(2.8.0)
85+
* Contributed implementation for #285: Allow inclusion of request body for JsonParseException
86+
(2.8.0)
8587

8688
Tanguy Leroux (tlrx@github)
8789
* Reported, contributed fix for #280: FilteringGeneratorDelegate.writeUTF8String()

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ JSON library.
2929
#276: Add support for serializing using `java.io.DataOutput`
3030
#280: Add `JsonParser.finishToken()` to force full, non-lazy reading of current token
3131
#282: Fail to report error for trying to write field name outside Object (root level)
32+
#285: Add `JsonParser.getText(Writer)`
33+
(contributed by LokesN)
3234
- Add `JsonParser.currentToken()` and `JsonParser.currentTokenId()` as replacements
3335
for `getCurrentToken()` and `getCurrentTokenId()`, respectively. Existing methods
3436
will likely be deprecated in 2.9.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,14 +1032,21 @@ public int currentTokenId() {
10321032

10331033
/**
10341034
* Method to read the textual representation of the current token in chunks and
1035-
* pass it to the given Writer
1035+
* pass it to the given Writer.
1036+
* Conceptually same as calling:
1037+
*<pre>
1038+
* writer.write(parser.getText());
1039+
*</pre>
1040+
* but should typically be more efficient as longer content does need to
1041+
* be combined into a single <code>String</code> to return, and write
1042+
* can occur directly from intermediate buffers Jackson uses.
10361043
*
10371044
* @return The number of characters written to the Writer
10381045
*
10391046
* @since 2.8
10401047
*/
1041-
public abstract int readText(Writer writer) throws IOException, UnsupportedOperationException;
1042-
1048+
public abstract int getText(Writer writer) throws IOException, UnsupportedOperationException;
1049+
10431050
/**
10441051
* Method similar to {@link #getText}, but that will return
10451052
* underlying (unmodifiable) character array that contains

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

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.core.json;
22

33
import java.io.*;
4-
import java.util.List;
54

65
import com.fasterxml.jackson.core.*;
76
import com.fasterxml.jackson.core.base.ParserBase;
@@ -274,49 +273,31 @@ public final String getText() throws IOException
274273
return _getText2(t);
275274
}
276275

277-
@Override
278-
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
276+
@Override // since 2.8
277+
public int getText(Writer writer) throws IOException
278+
{
279279
JsonToken t = _currToken;
280-
//Stores the length of the bytes read
281-
int len = 0;
282280
if (t == JsonToken.VALUE_STRING) {
283281
if (_tokenIncomplete) {
284282
_tokenIncomplete = false;
285283
_finishString(); // only strings can be incomplete
286284
}
287-
List<char[]> segments = _textBuffer.getCharacterSegments();
288-
289-
//Indicates the currently read text buffer index which refers to the
290-
//TextBuffer character segment
291-
int readTextBufferIndex = 0;
292-
//if there are character segments, then use them and write them to the writer
293-
while(segments != null && readTextBufferIndex < segments.size()) {
294-
writer.write(segments.get(readTextBufferIndex));
295-
len += segments.get(readTextBufferIndex).length;
296-
readTextBufferIndex++;
297-
}
298-
//if there are no character segments left, then read the string from the current segment, and
299-
//write them directly to the buffer
300-
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
301-
len += _textBuffer.getCurrentSegmentSize();
302-
303-
}
304-
else if(t != null) {
305-
switch (t.id()) {
306-
case ID_FIELD_NAME:
307-
writer.write(_parsingContext.getCurrentName());
308-
break;
309-
case ID_STRING:
310-
case ID_NUMBER_INT:
311-
case ID_NUMBER_FLOAT:
312-
writer.write(_textBuffer.contentsAsString());
313-
break;
314-
default:
315-
writer.write(t.asString());
285+
return _textBuffer.contentsToWriter(writer);
286+
}
287+
if (t == JsonToken.FIELD_NAME) {
288+
String n = _parsingContext.getCurrentName();
289+
writer.write(n);
290+
return n.length();
291+
}
292+
if (t != null) {
293+
if (t.isNumeric()) {
294+
return _textBuffer.contentsToWriter(writer);
316295
}
296+
char[] ch = t.asCharArray();
297+
writer.write(ch);
298+
return ch.length;
317299
}
318-
319-
return len;
300+
return 0;
320301
}
321302

322303
// // // Let's override default impls for improved performance

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

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.*;
44
import java.util.Arrays;
5-
import java.util.List;
65

76
import com.fasterxml.jackson.core.*;
87
import com.fasterxml.jackson.core.base.ParserBase;
@@ -186,50 +185,32 @@ public String getText() throws IOException
186185
}
187186

188187
@Override
189-
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
188+
public int getText(Writer writer) throws IOException
189+
{
190190
JsonToken t = _currToken;
191-
//Stores the length of the bytes read
192-
int len = 0;
193191
if (t == JsonToken.VALUE_STRING) {
194192
if (_tokenIncomplete) {
195193
_tokenIncomplete = false;
196194
_finishString(); // only strings can be incomplete
197195
}
198-
List<char[]> segments = _textBuffer.getCharacterSegments();
199-
200-
//Indicates the currently read text buffer index which refers to the
201-
//TextBuffer character segment
202-
int readTextBufferIndex = 0;
203-
//if there are character segments, then use them and write them to the writer
204-
while(segments != null && readTextBufferIndex < segments.size()) {
205-
writer.write(segments.get(readTextBufferIndex));
206-
len += segments.get(readTextBufferIndex).length;
207-
readTextBufferIndex++;
208-
}
209-
//if there are no character segments left, then read the string from the current segment, and
210-
//write them directly to the buffer
211-
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
212-
len += _textBuffer.getCurrentSegmentSize();
213-
214-
}
215-
else if(t != null) {
216-
switch (t.id()) {
217-
case ID_FIELD_NAME:
218-
writer.write(_parsingContext.getCurrentName());
219-
break;
220-
case ID_STRING:
221-
case ID_NUMBER_INT:
222-
case ID_NUMBER_FLOAT:
223-
writer.write(_textBuffer.contentsAsString());
224-
break;
225-
default:
226-
writer.write(t.asString());
196+
return _textBuffer.contentsToWriter(writer);
197+
}
198+
if (t == JsonToken.FIELD_NAME) {
199+
String n = _parsingContext.getCurrentName();
200+
writer.write(n);
201+
return n.length();
202+
}
203+
if (t != null) {
204+
if (t.isNumeric()) {
205+
return _textBuffer.contentsToWriter(writer);
227206
}
207+
char[] ch = t.asCharArray();
208+
writer.write(ch);
209+
return ch.length;
228210
}
229-
230-
return len;
211+
return 0;
231212
}
232-
213+
233214
// // // Let's override default impls for improved performance
234215
@Override
235216
public String getValueAsString() throws IOException

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

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.*;
44
import java.util.Arrays;
5-
import java.util.List;
65

76
import com.fasterxml.jackson.core.*;
87
import com.fasterxml.jackson.core.base.ParserBase;
@@ -319,50 +318,32 @@ public String getText() throws IOException
319318
}
320319
return _getText2(_currToken);
321320
}
322-
323-
@Override
324-
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
321+
322+
@Override // since 2.8
323+
public int getText(Writer writer) throws IOException
324+
{
325325
JsonToken t = _currToken;
326-
//Stores the length of the bytes read
327-
int len = 0;
328326
if (t == JsonToken.VALUE_STRING) {
329327
if (_tokenIncomplete) {
330328
_tokenIncomplete = false;
331329
_finishString(); // only strings can be incomplete
332330
}
333-
List<char[]> segments = _textBuffer.getCharacterSegments();
334-
335-
//Indicates the currently read text buffer index which refers to the
336-
//TextBuffer character segment
337-
int readTextBufferIndex = 0;
338-
//if there are character segments, then use them and write them to the writer
339-
while(segments != null && readTextBufferIndex < segments.size()) {
340-
writer.write(segments.get(readTextBufferIndex));
341-
len += segments.get(readTextBufferIndex).length;
342-
readTextBufferIndex++;
343-
}
344-
//if there are no character segments left, then read the string from the current segment, and
345-
//write them directly to the buffer
346-
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
347-
len += _textBuffer.getCurrentSegmentSize();
348-
349-
}
350-
else if(t != null) {
351-
switch (t.id()) {
352-
case ID_FIELD_NAME:
353-
writer.write(_parsingContext.getCurrentName());
354-
break;
355-
case ID_STRING:
356-
case ID_NUMBER_INT:
357-
case ID_NUMBER_FLOAT:
358-
writer.write(_textBuffer.contentsAsString());
359-
break;
360-
default:
361-
writer.write(t.asString());
331+
return _textBuffer.contentsToWriter(writer);
332+
}
333+
if (t == JsonToken.FIELD_NAME) {
334+
String n = _parsingContext.getCurrentName();
335+
writer.write(n);
336+
return n.length();
337+
}
338+
if (t != null) {
339+
if (t.isNumeric()) {
340+
return _textBuffer.contentsToWriter(writer);
362341
}
342+
char[] ch = t.asCharArray();
343+
writer.write(ch);
344+
return ch.length;
363345
}
364-
365-
return len;
346+
return 0;
366347
}
367348

368349
// // // Let's override default impls for improved performance

src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
144144
@Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); }
145145
@Override public int getTextLength() throws IOException { return delegate.getTextLength(); }
146146
@Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); }
147-
@Override public int readText(Writer writer) throws IOException, UnsupportedOperationException { return delegate.readText(writer); }
147+
@Override public int getText(Writer writer) throws IOException, UnsupportedOperationException { return delegate.getText(writer); }
148148

149149
/*
150150
/**********************************************************

src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.fasterxml.jackson.core.util;
22

3+
import java.io.*;
34
import java.math.BigDecimal;
4-
import java.util.ArrayList;
5-
import java.util.Arrays;
6-
import java.util.List;
5+
import java.util.*;
76

87
import com.fasterxml.jackson.core.io.NumberInput;
98

@@ -400,6 +399,45 @@ public double contentsAsDouble() throws NumberFormatException {
400399
return NumberInput.parseDouble(contentsAsString());
401400
}
402401

402+
/**
403+
* @since 2.8
404+
*/
405+
public int contentsToWriter(Writer w) throws IOException
406+
{
407+
if (_resultArray != null) {
408+
w.write(_resultArray);
409+
return _resultArray.length;
410+
}
411+
if (_resultString != null) { // Can take a shortcut...
412+
w.write(_resultString);
413+
return _resultString.length();
414+
}
415+
// Do we use shared array?
416+
if (_inputStart >= 0) {
417+
final int len = _inputLen;
418+
if (len > 0) {
419+
w.write(_inputBuffer, _inputStart, len);
420+
}
421+
return len;
422+
}
423+
// nope, not shared
424+
int total = 0;
425+
if (_segments != null) {
426+
for (int i = 0, end = _segments.size(); i < end; ++i) {
427+
char[] curr = _segments.get(i);
428+
int currLen = curr.length;
429+
w.write(curr, 0, currLen);
430+
total += currLen;
431+
}
432+
}
433+
int len = _currentSize;
434+
if (len > 0) {
435+
w.write(_currentSegment, 0, len);
436+
total += len;
437+
}
438+
return total;
439+
}
440+
403441
/*
404442
/**********************************************************
405443
/* Public mutators:
@@ -507,15 +545,6 @@ public void append(String str, int offset, int len)
507545
} while (len > 0);
508546
}
509547

510-
/**
511-
* Returns the raw list of character segments
512-
*
513-
* @return The character segments
514-
*/
515-
public List<char[]> getCharacterSegments() {
516-
return _segments;
517-
}
518-
519548
/*
520549
/**********************************************************
521550
/* Raw access, for high-performance use:

src/test/java/com/fasterxml/jackson/core/json/ParserSequenceTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
public class ParserSequenceTest
77
extends com.fasterxml.jackson.core.BaseTest
88
{
9-
private final JsonFactory JSON_FACTORY = new JsonFactory();
10-
119
public void testSimple() throws Exception
1210
{
1311
JsonParser p1 = JSON_FACTORY.createParser("[ 1 ]");

0 commit comments

Comments
 (0)