Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmileParser getValueAsString() issue with JsonToken.FIELD_NAME #540

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,9 @@ public String getValueAsString() throws IOException
if (_currToken == JsonToken.VALUE_STRING) {
return _sharedString == null ? _textBuffer.contentsAsString() : _sharedString;
}
if (_currToken == JsonToken.FIELD_NAME) {
return currentName();
}
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
return null;
}
Expand All @@ -1712,6 +1715,9 @@ public String getValueAsString() throws IOException
public String getValueAsString(String defaultValue) throws IOException
{
if (_currToken != JsonToken.VALUE_STRING) {
if (_currToken == JsonToken.FIELD_NAME) {
return currentName();
}
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
return defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,14 @@ public void testStringField() throws IOException {
assertEquals(JsonToken.START_OBJECT, parser.nextToken());
assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals("a", parser.currentName());
assertEquals("a", parser.getText());
assertEquals("a", parser.getValueAsString());
assertEquals("a", parser.getValueAsString("x"));
assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
assertEquals("a", parser.currentName());
assertEquals("b", parser.getText());
assertEquals("b", parser.getValueAsString());
assertEquals("b", parser.getValueAsString("x"));
assertEquals(1, parser.getTextLength());
assertEquals(JsonToken.END_OBJECT, parser.nextToken());

Expand Down Expand Up @@ -281,14 +286,23 @@ public void testNestedObject() throws IOException

assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals("ob", parser.currentName());
assertEquals("ob", parser.getText());
assertEquals("ob", parser.getValueAsString());
assertEquals("ob", parser.getValueAsString("x"));
assertEquals(JsonToken.START_OBJECT, parser.nextToken());
assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals("num", parser.currentName());
assertEquals("num", parser.getText());
assertEquals("num", parser.getValueAsString());
assertEquals("num", parser.getValueAsString("y"));
assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
assertEquals(JsonToken.END_OBJECT, parser.nextToken());

assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals("arr", parser.currentName());
assertEquals("arr", parser.getText());
assertEquals("arr", parser.getValueAsString());
assertEquals("arr", parser.getValueAsString("z"));
assertEquals(JsonToken.START_ARRAY, parser.nextToken());
assertEquals(JsonToken.END_ARRAY, parser.nextToken());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,9 @@ public String getValueAsString() throws IOException
if (_currToken == JsonToken.VALUE_STRING) {
return _textBuffer.contentsAsString();
}
if (_currToken == JsonToken.FIELD_NAME) {
return currentName();
}
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
return null;
}
Expand All @@ -1183,6 +1186,9 @@ public String getValueAsString() throws IOException
public String getValueAsString(String defaultValue) throws IOException
{
if (_currToken != JsonToken.VALUE_STRING) {
if (_currToken == JsonToken.FIELD_NAME) {
return currentName();
}
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
return defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,30 +217,39 @@ public void testTrivialObject() throws IOException
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("abc", p.currentName());
assertEquals("abc", p.getText());
assertEquals("abc", p.getValueAsString());
assertEquals("abc", p.getValueAsString("xyz"));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(13, p.getIntValue());
assertToken(JsonToken.END_OBJECT, p.nextToken());
p.close();
assertToken(JsonToken.END_OBJECT, p.nextToken());
p.close();
}

public void testSimpleObject() throws IOException
{
byte[] data = _smileDoc("{\"a\":8, \"b\" : [ true ], \"c\" : { }, \"d\":{\"e\":null}}");
SmileParser p = _smileParser(data);
assertNull(p.getCurrentToken());
assertToken(JsonToken.START_OBJECT, p.nextToken());
byte[] data = _smileDoc("{\"a\":8, \"b\" : [ true ], \"c\" : { }, \"d\":{\"e\":null}}");
SmileParser p = _smileParser(data);
assertNull(p.currentToken());
assertToken(JsonToken.START_OBJECT, p.nextToken());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("a", p.currentName());
assertEquals("a", p.getText());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(8, p.getIntValue());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("a", p.currentName());
assertEquals("a", p.getText());
assertEquals("a", p.getValueAsString());
assertEquals("a", p.getValueAsString("x"));

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("b", p.currentName());
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(8, p.getIntValue());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("b", p.currentName());
assertEquals("b", p.getText());
assertEquals("b", p.getValueAsString());
assertEquals("b", p.getValueAsString("y"));

assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("c", p.currentName());
Expand Down Expand Up @@ -285,8 +294,8 @@ public void testNestedObject() throws IOException

public void testJsonSampleDoc() throws IOException
{
byte[] data = _smileDoc(SAMPLE_DOC_JSON_SPEC);
verifyJsonSpecSampleDoc(_smileParser(data), true);
byte[] data = _smileDoc(SAMPLE_DOC_JSON_SPEC);
verifyJsonSpecSampleDoc(_smileParser(data), true);
}

public void testUnicodeStringValues() throws IOException
Expand Down