Skip to content

Commit

Permalink
Merge pull request #285 from LokeshN/readtext-jsonparser1
Browse files Browse the repository at this point in the history
issue #15 - readtext in jsonparser
  • Loading branch information
cowtowncoder committed May 18, 2016
2 parents 780394c + ca17d2f commit 7a0991a
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,16 @@ public int currentTokenId() {
*/
public abstract String getText() throws IOException;

/**
* Method to read the textual representation of the current token in chunks and
* pass it to the given Writer
*
* @return The number of characters written to the Writer
*
* @since 2.8
*/
public abstract int readText(Writer writer) throws IOException, UnsupportedOperationException;

/**
* Method similar to {@link #getText}, but that will return
* underlying (unmodifiable) character array that contains
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.core.json;

import java.io.*;
import java.util.List;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.ParserBase;
Expand Down Expand Up @@ -273,6 +274,51 @@ public final String getText() throws IOException
return _getText2(t);
}

@Override
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
JsonToken t = _currToken;
//Stores the length of the bytes read
int len = 0;
if (t == JsonToken.VALUE_STRING) {
if (_tokenIncomplete) {
_tokenIncomplete = false;
_finishString(); // only strings can be incomplete
}
List<char[]> segments = _textBuffer.getCharacterSegments();

//Indicates the currently read text buffer index which refers to the
//TextBuffer character segment
int readTextBufferIndex = 0;
//if there are character segments, then use them and write them to the writer
while(segments != null && readTextBufferIndex < segments.size()) {
writer.write(segments.get(readTextBufferIndex));
len += segments.get(readTextBufferIndex).length;
readTextBufferIndex++;
}
//if there are no character segments left, then read the string from the current segment, and
//write them directly to the buffer
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
len += _textBuffer.getCurrentSegmentSize();

}
else if(t != null) {
switch (t.id()) {
case ID_FIELD_NAME:
writer.write(_parsingContext.getCurrentName());
break;
case ID_STRING:
case ID_NUMBER_INT:
case ID_NUMBER_FLOAT:
writer.write(_textBuffer.contentsAsString());
break;
default:
writer.write(t.asString());
}
}

return len;
}

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

// @since 2.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.*;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.ParserBase;
Expand Down Expand Up @@ -184,6 +185,51 @@ public String getText() throws IOException
return _getText2(_currToken);
}

@Override
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
JsonToken t = _currToken;
//Stores the length of the bytes read
int len = 0;
if (t == JsonToken.VALUE_STRING) {
if (_tokenIncomplete) {
_tokenIncomplete = false;
_finishString(); // only strings can be incomplete
}
List<char[]> segments = _textBuffer.getCharacterSegments();

//Indicates the currently read text buffer index which refers to the
//TextBuffer character segment
int readTextBufferIndex = 0;
//if there are character segments, then use them and write them to the writer
while(segments != null && readTextBufferIndex < segments.size()) {
writer.write(segments.get(readTextBufferIndex));
len += segments.get(readTextBufferIndex).length;
readTextBufferIndex++;
}
//if there are no character segments left, then read the string from the current segment, and
//write them directly to the buffer
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
len += _textBuffer.getCurrentSegmentSize();

}
else if(t != null) {
switch (t.id()) {
case ID_FIELD_NAME:
writer.write(_parsingContext.getCurrentName());
break;
case ID_STRING:
case ID_NUMBER_INT:
case ID_NUMBER_FLOAT:
writer.write(_textBuffer.contentsAsString());
break;
default:
writer.write(t.asString());
}
}

return len;
}

// // // Let's override default impls for improved performance
@Override
public String getValueAsString() throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.*;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.ParserBase;
Expand Down Expand Up @@ -318,6 +319,51 @@ public String getText() throws IOException
}
return _getText2(_currToken);
}

@Override
public final int readText(Writer writer) throws IOException, UnsupportedOperationException {
JsonToken t = _currToken;
//Stores the length of the bytes read
int len = 0;
if (t == JsonToken.VALUE_STRING) {
if (_tokenIncomplete) {
_tokenIncomplete = false;
_finishString(); // only strings can be incomplete
}
List<char[]> segments = _textBuffer.getCharacterSegments();

//Indicates the currently read text buffer index which refers to the
//TextBuffer character segment
int readTextBufferIndex = 0;
//if there are character segments, then use them and write them to the writer
while(segments != null && readTextBufferIndex < segments.size()) {
writer.write(segments.get(readTextBufferIndex));
len += segments.get(readTextBufferIndex).length;
readTextBufferIndex++;
}
//if there are no character segments left, then read the string from the current segment, and
//write them directly to the buffer
writer.write(_textBuffer.getCurrentSegment(), 0, _textBuffer.getCurrentSegmentSize());
len += _textBuffer.getCurrentSegmentSize();

}
else if(t != null) {
switch (t.id()) {
case ID_FIELD_NAME:
writer.write(_parsingContext.getCurrentName());
break;
case ID_STRING:
case ID_NUMBER_INT:
case ID_NUMBER_FLOAT:
writer.write(_textBuffer.contentsAsString());
break;
default:
writer.write(t.asString());
}
}

return len;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;

Expand Down Expand Up @@ -143,6 +144,7 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
@Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); }
@Override public int getTextLength() throws IOException { return delegate.getTextLength(); }
@Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); }
@Override public int readText(Writer writer) throws IOException, UnsupportedOperationException { return delegate.readText(writer); }

/*
/**********************************************************
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

Expand Down Expand Up @@ -506,6 +507,15 @@ public void append(String str, int offset, int len)
} while (len > 0);
}

/**
* Returns the raw list of character segments
*
* @return The character segments
*/
public List<char[]> getCharacterSegments() {
return _segments;
}

/*
/**********************************************************
/* Raw access, for high-performance use:
Expand Down
84 changes: 84 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/read/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,90 @@ private void _testGetValueAsText(int mode, boolean delegate) throws Exception
}
p.close();
}

public void testReadText() throws Exception {
final String JSON = "{\"a\":\"this is a sample text for json parsing using readText() method\",\"b\":true,\"c\":null,\"d\":\"foo\"}";
//create parser in reader mode..
JsonParser parser = createParser(MODE_READER, JSON);
//move the token until the string field
parser.nextToken();
parser.nextToken();
parser.nextToken();

Writer writer = new StringWriter();
int len = parser.readText(writer);

assertTrue("String length should be same", writer.toString().length() == "this is a sample text for json parsing using readText() method".length());
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in stream mode..
parser = createParser(MODE_INPUT_STREAM, JSON);
//move the token until the string field
parser.nextToken();
parser.nextToken();
parser.nextToken();

writer = new StringWriter();
len = parser.readText(writer);

assertTrue("String length should be same", writer.toString().length() == "this is a sample text for json parsing using readText() method".length());
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in data input mode..
parser = createParser(MODE_DATA_INPUT, JSON);
//move the token until the string field
parser.nextToken();
parser.nextToken();
parser.nextToken();

writer = new StringWriter();
len = parser.readText(writer);

assertTrue("String length should be same", writer.toString().length() == "this is a sample text for json parsing using readText() method".length());
assertEquals("Returned length should be same", len, writer.toString().length());
}

public void testLongerReadText() throws Exception {
StringBuilder builder = new StringBuilder();
for(int i= 0; i < 1000; i++) {
builder.append("Sample Text"+i);
}
String longText = builder.toString();
final String JSON = "{\"a\":\""+ longText +"\",\"b\":true,\"c\":null,\"d\":\"foo\"}";
//create parser in reader mode..
JsonParser parser = createParser(MODE_READER, JSON);
//move the token until the string field
parser.nextToken();
parser.nextToken();
parser.nextToken();

Writer writer = new StringWriter();
int len = parser.readText(writer);
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in stream mode..
parser = createParser(MODE_INPUT_STREAM, JSON);
//move the token until the string field
parser.nextToken();
parser.nextToken();
parser.nextToken();

writer = new StringWriter();
len = parser.readText(writer);
assertEquals("Returned length should be same", len, writer.toString().length());

//create parser in data input mode..
parser = createParser(MODE_DATA_INPUT, JSON);
//move the token until the string field
parser.nextToken();
parser.nextToken();
parser.nextToken();

writer = new StringWriter();
len = parser.readText(writer);
assertEquals("Returned length should be same", len, writer.toString().length());

}

/*
/**********************************************************
Expand Down

0 comments on commit 7a0991a

Please sign in to comment.