Skip to content

Commit

Permalink
Fix #188
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 31, 2015
1 parent 4b041e9 commit 698f3a7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ JSON library.
#177: Add a check so `JsonGenerator.writeString()` won't work if `writeFieldName()` expected.
#182: Inconsistent TextBuffer#getTextBuffer behavior
(contributed by Masaru H)
#188: `JsonParser.getValueAsString()` should return field name for `JsonToken.FIELD_NAME`, not `null`
- Minor improvement to construction of "default PrettyPrinter": now overridable by data format
modules
- Implement a new yet more optimized symbol table for byte-backed parsers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,22 @@ public String getValueAsString() throws IOException {
if (_currToken == JsonToken.VALUE_STRING) {
return getText();
}
if (_currToken == JsonToken.FIELD_NAME) {
return getCurrentName();
}
return getValueAsString(null);
}

@Override
public String getValueAsString(String defaultValue) throws IOException {
if (_currToken != JsonToken.VALUE_STRING) {
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
return defaultValue;
}
if (_currToken == JsonToken.VALUE_STRING) {
return getText();
}
if (_currToken == JsonToken.FIELD_NAME) {
return getCurrentName();
}
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
return defaultValue;
}
return getText();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ public final String getValueAsString() throws IOException
}
return _textBuffer.contentsAsString();
}
if (_currToken == JsonToken.FIELD_NAME) {
return getCurrentName();
}
return super.getValueAsString(null);
}

Expand All @@ -263,6 +266,9 @@ public final String getValueAsString(String defValue) throws IOException {
}
return _textBuffer.contentsAsString();
}
if (_currToken == JsonToken.FIELD_NAME) {
return getCurrentName();
}
return super.getValueAsString(defValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ public String getValueAsString() throws IOException
}
return _textBuffer.contentsAsString();
}
if (_currToken == JsonToken.FIELD_NAME) {
return getCurrentName();
}
return super.getValueAsString(null);
}

Expand All @@ -316,6 +319,9 @@ public String getValueAsString(String defValue) throws IOException
}
return _textBuffer.contentsAsString();
}
if (_currToken == JsonToken.FIELD_NAME) {
return getCurrentName();
}
return super.getValueAsString(defValue);
}

Expand Down
59 changes: 59 additions & 0 deletions src/test/java/com/fasterxml/jackson/core/json/TestJsonParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.core.json;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.util.JsonParserDelegate;

import java.io.*;
import java.net.URL;
Expand Down Expand Up @@ -510,7 +511,65 @@ private void _testHandlingOfInvalidSpaceFromResource(boolean useStream) throws E
}
jp.close();
}

public void testGetValueAsTextBytes() throws Exception
{
JsonFactory f = new JsonFactory();
_testGetValueAsText(f, true, false);
_testGetValueAsText(f, true, true);
}

public void testGetValueAsTextChars() throws Exception
{
JsonFactory f = new JsonFactory();
_testGetValueAsText(f, false, false);
_testGetValueAsText(f, false, true);
}

@SuppressWarnings("resource")
private void _testGetValueAsText(JsonFactory f,
boolean useBytes, boolean delegate) throws Exception
{
String JSON = "{\"a\":1,\"b\":true,\"c\":null,\"d\":\"foo\"}";
JsonParser p = useBytes ? f.createParser(JSON.getBytes("UTF-8"))
: f.createParser(JSON);

if (delegate) {
p = new JsonParserDelegate(p);
}

assertToken(JsonToken.START_OBJECT, p.nextToken());
assertNull(p.getValueAsString());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("a", p.getText());
assertEquals("a", p.getValueAsString());
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
assertEquals("1", p.getValueAsString());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("b", p.getValueAsString());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
assertEquals("true", p.getValueAsString());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("c", p.getValueAsString());
assertToken(JsonToken.VALUE_NULL, p.nextToken());
// null token returned as Java null, as per javadoc
assertNull(p.getValueAsString());

assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("d", p.getValueAsString());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertEquals("foo", p.getValueAsString());

assertToken(JsonToken.END_OBJECT, p.nextToken());
assertNull(p.getValueAsString());

assertNull(p.nextToken());
p.close();
}

/*
/**********************************************************
/* Helper methods
Expand Down

0 comments on commit 698f3a7

Please sign in to comment.