Skip to content

Commit

Permalink
SmileParser getValueAsString() issue with JsonToken.FIELD_NAME (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
johhud1 authored Jan 4, 2025
1 parent e501694 commit e5f1a17
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ private void _testSimplePojoViaParser(Employee empl, byte[] avro,
assertFalse(p.hasTextCharacters());
assertEquals("name", p.currentName());
assertEquals("name", p.getText());
assertEquals("name", p.getValueAsString());
assertEquals("name", p.getValueAsString("x"));
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals(empl.name, p.getText());

Expand All @@ -70,6 +72,9 @@ private void _testSimplePojoViaParser(Employee empl, byte[] avro,
assertTrue(p.hasTextCharacters());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("age", p.currentName());
assertEquals("age", p.getText());
assertEquals("age", p.getValueAsString());
assertEquals("age", p.getValueAsString("x"));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.INT, p.getNumberType());
assertEquals(Integer.valueOf(empl.age), p.getNumberValue());
Expand All @@ -82,6 +87,9 @@ private void _testSimplePojoViaParser(Employee empl, byte[] avro,
sw = new StringWriter();
assertEquals(6, p.getText(sw));
assertEquals("emails", sw.toString());
assertEquals("emails", p.getText());
assertEquals("emails", p.getValueAsString());
assertEquals("emails", p.getValueAsString("x"));

assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
Expand Down
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 @@ -169,6 +169,9 @@ public void testUnknownSymbolExceptionForFieldNameIsWrapped() throws IOException
Assert.assertEquals(JsonToken.START_OBJECT, parser.nextToken());
Assert.assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
Assert.assertEquals("a", parser.currentName());
Assert.assertEquals("a", parser.getText());
Assert.assertEquals("a", parser.getValueAsString());
Assert.assertEquals("a", parser.getValueAsString("b"));
Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, parser.nextValue());
Assert.assertEquals(1, parser.getIntValue());
parser.nextValue(); // Should encounter unknown symbol and fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,37 @@
import com.fasterxml.jackson.core.JsonToken;

public class SimpleIonReadTest {
private final IonFactory ION_F = new IonFactory();
// // // Actual tests; low level

@Test
public void testSimpleStructRead() throws IOException
{
IonFactory f = new IonFactory();
JsonParser jp = f.createParser("{a:\"value\",b:42, c:null}");
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("a", jp.currentName());
assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
assertEquals("value", jp.getText());
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextValue());
assertEquals("b", jp.currentName());
assertEquals(42, jp.getIntValue());
assertEquals(JsonToken.VALUE_NULL, jp.nextValue());
assertEquals("c", jp.currentName());
assertEquals(JsonToken.END_OBJECT, jp.nextToken());
jp.close();
try (JsonParser p = ION_F.createParser("{a:\"value\",b:42, c:null}")) {
assertEquals(JsonToken.START_OBJECT, p.nextToken());
assertEquals(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("a", p.currentName());
assertEquals("a", p.getText());
assertEquals("a", p.getValueAsString());
assertEquals("a", p.getValueAsString("x"));
assertEquals(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("value", p.getText());
assertEquals("value", p.getText());
assertEquals("value", p.getValueAsString());
assertEquals("value", p.getValueAsString("x"));
assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextValue());
assertEquals("b", p.currentName());
assertEquals(42, p.getIntValue());
assertEquals(JsonToken.VALUE_NULL, p.nextValue());
assertEquals("c", p.currentName());
assertEquals(JsonToken.END_OBJECT, p.nextToken());
}
}

@Test
public void testSimpleListRead() throws IOException
{
IonFactory f = new IonFactory();
JsonParser jp = f.createParser("[ 12, true, null, \"abc\" ]");
JsonParser jp = ION_F.createParser("[ 12, true, null, \"abc\" ]");
assertEquals(JsonToken.START_ARRAY, jp.nextToken());
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextValue());
assertEquals(12, jp.getIntValue());
Expand All @@ -63,8 +68,7 @@ public void testSimpleListRead() throws IOException
@Test
public void testSimpleStructAndArray() throws IOException
{
IonFactory f = new IonFactory();
JsonParser jp = f.createParser("{a:[\"b\",\"c\"], b:null}");
JsonParser jp = ION_F.createParser("{a:[\"b\",\"c\"], b:null}");
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("a", jp.currentName());
Expand All @@ -85,8 +89,7 @@ public void testSimpleStructAndArray() throws IOException
@Test
public void testMixed() throws IOException
{
IonFactory f = new IonFactory();
JsonParser jp = f.createParser("{a:[ 1, { b: 13}, \"xyz\" ], c:null, d:true}");
JsonParser jp = ION_F.createParser("{a:[ 1, { b: 13}, \"xyz\" ], c:null, d:true}");
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.START_ARRAY, jp.nextValue());
//assertEquals("a", jp.currentName());
Expand All @@ -113,8 +116,7 @@ public void testMixed() throws IOException

@Test
public void testNullIonType() throws IOException {
IonFactory f = new IonFactory();
JsonParser jp = f.createParser("{a:\"value\",b:42, c:null.int}");
JsonParser jp = ION_F.createParser("{a:\"value\",b:42, c:null.int}");
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("a", jp.currentName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,9 @@ public String getValueAsString() throws IOException
}
return _textBuffer.contentsAsString();
}
if (_currToken == JsonToken.FIELD_NAME) {
return currentName();
}
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
return null;
}
Expand All @@ -1405,6 +1408,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 @@ -137,12 +137,18 @@ public void testReadPointLong() throws Exception
assertNull(p.currentName());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("x", p.currentName());
assertEquals("x", p.getText());
assertEquals("x", p.getValueAsString());
assertEquals("x", p.getValueAsString("y"));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(NumberType.LONG, p.getNumberType());
assertEquals(NumberTypeFP.UNKNOWN, p.getNumberTypeFP());
assertEquals(input.x, p.getIntValue());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("y", p.currentName());
assertEquals("y", p.getText());
assertEquals("y", p.getValueAsString());
assertEquals("y", p.getValueAsString("abc"));
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals(input.y, p.getIntValue());
assertToken(JsonToken.END_OBJECT, p.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

0 comments on commit e5f1a17

Please sign in to comment.