-
Notifications
You must be signed in to change notification settings - Fork 16
Fix for issue 48: JSONValue.parse does not correctly decode UTF-8 bytes from an inputstream #1
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
Open
johnrevill
wants to merge
4
commits into
netplex:master
Choose a base branch
from
johnrevill:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
074aecc
Captured issue 48 in a unit test ("JSONValue.parse does not correctly
johnrevill faaa95f
Fix for issue 48 ("JSONValue.parse does not correctly decode UTF-8 bytes
johnrevill 033817d
Correcting indentation
johnrevill 14b5fef
Correcting indentation
johnrevill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,19 +15,16 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import static net.minidev.json.parser.ParseException.ERROR_UNEXPECTED_EOF; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
/** | ||
* Parser for JSON text. Please note that JSONParser is NOT thread-safe. | ||
* | ||
* @author Uriel Chemouni <[email protected]> | ||
*/ | ||
class JSONParserInputStream extends JSONParserStream { | ||
private InputStream in; | ||
|
||
class JSONParserInputStream extends JSONParserReader { | ||
// len | ||
public JSONParserInputStream(int permissiveMode) { | ||
super(permissiveMode); | ||
|
@@ -40,50 +37,55 @@ public JSONParserInputStream(int permissiveMode) { | |
public Object parse(InputStream in) throws ParseException { | ||
return parse(in, ContainerFactory.FACTORY_SIMPLE, ContentHandlerDumy.HANDLER); | ||
} | ||
|
||
/** | ||
* use to return Primitive Type, or String, Or JsonObject or JsonArray | ||
* generated by a ContainerFactory | ||
* | ||
* @throws UnsupportedEncodingException If the named charset is not supported by an InputStreamReader | ||
*/ | ||
public Object parse(InputStream in, String charset) throws ParseException, UnsupportedEncodingException { | ||
return parse(in, ContainerFactory.FACTORY_SIMPLE, ContentHandlerDumy.HANDLER, charset); | ||
} | ||
|
||
/** | ||
* use to return Primitive Type, or String, Or JsonObject or JsonArray | ||
* generated by a ContainerFactory | ||
* | ||
* NB: This uses the default charset | ||
*/ | ||
public Object parse(InputStream in, ContainerFactory containerFactory) throws ParseException { | ||
return parse(in, containerFactory, ContentHandlerDumy.HANDLER); | ||
} | ||
|
||
/** | ||
* use to return Primitive Type, or String, Or JsonObject or JsonArray | ||
* generated by a ContainerFactory | ||
* | ||
* @throws UnsupportedEncodingException If the named charset is not supported by an InputStreamReader | ||
*/ | ||
public Object parse(InputStream in, ContainerFactory containerFactory, String charset) throws ParseException, UnsupportedEncodingException { | ||
return parse(in, containerFactory, ContentHandlerDumy.HANDLER, charset); | ||
} | ||
|
||
/** | ||
* use to return Primitive Type, or String, Or JsonObject or JsonArray | ||
* generated by a ContainerFactory | ||
* | ||
* NB: This uses the default charset | ||
*/ | ||
public Object parse(InputStream in, ContainerFactory containerFactory, ContentHandler handler) | ||
throws ParseException { | ||
// | ||
this.in = in; | ||
this.pos = -1; | ||
return super.parse(containerFactory, handler); | ||
} | ||
|
||
protected void read() throws IOException { | ||
int i = in.read(); | ||
c = (i == -1) ? (char) EOI : (char) i; | ||
pos++; | ||
// | ||
} | ||
|
||
protected void readS() throws IOException { | ||
sb.append(c); | ||
int i = in.read(); | ||
if (i == -1) { | ||
c = EOI; | ||
} else { | ||
c = (char) i; | ||
pos++; | ||
} | ||
return super.parse(new InputStreamReader(in)); | ||
} | ||
|
||
protected void readNoEnd() throws ParseException, IOException { | ||
int i = in.read(); | ||
if (i == -1) | ||
throw new ParseException(pos - 1, ERROR_UNEXPECTED_EOF, "EOF"); | ||
c = (char) i; | ||
// | ||
|
||
/** | ||
* use to return Primitive Type, or String, Or JsonObject or JsonArray | ||
* generated by a ContainerFactory | ||
* | ||
* @throws UnsupportedEncodingException If the named charset is not supported by an InputStreamReader | ||
*/ | ||
public Object parse(InputStream in, ContainerFactory containerFactory, ContentHandler handler, String charset) throws ParseException, UnsupportedEncodingException { | ||
return super.parse(new InputStreamReader(in, charset)); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
json-smart/src/test/java/net/minidev/json/test/TestIssue48.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package net.minidev.json.test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
import junit.framework.TestCase; | ||
import net.minidev.json.JSONObject; | ||
import net.minidev.json.JSONValue; | ||
import net.minidev.json.parser.ContentHandler; | ||
import net.minidev.json.parser.ParseException; | ||
|
||
/** | ||
* Catches issue 48: "JSONValue.parse does not correctly decode UTF-8 bytes from an inputstream" | ||
* mentioned in https://code.google.com/p/json-smart/issues/detail?id=48 | ||
*/ | ||
public class TestIssue48 extends TestCase { | ||
/** | ||
* Covers the case described in https://code.google.com/p/json-smart/issues/detail?id=48 | ||
*/ | ||
public void testIssue48() throws UnsupportedEncodingException { | ||
final String testJsonString = "{\"balance\":1000.21,\"num\":100,\"nickname\":null,\"is_vip\":true,\"Sinhalese\":\"සිංහල ජාතිය\",\"name\":\"foo\"}"; | ||
|
||
final ByteArrayInputStream bis = new ByteArrayInputStream(testJsonString.getBytes("UTF-8")); | ||
|
||
final JSONObject obj = (JSONObject) JSONValue.parse(bis); | ||
|
||
final String actual = (String) obj.get("Sinhalese"); | ||
|
||
assertEquals("සිංහල ජාතිය", actual); | ||
} | ||
|
||
/** | ||
* Covers the same issue seen when using SAXParse with an InputStream source drawing on bytes of UTF-8 | ||
*/ | ||
public void testIssue48_SAXParse_InputStream_UTF8() throws Exception { | ||
final String originalText = "僧伽罗人"; | ||
final String testJsonString = "[ \"" + originalText + "\" ]"; | ||
|
||
final ByteArrayInputStream bis = new ByteArrayInputStream(testJsonString.getBytes("UTF-8")); | ||
|
||
JSONValue.SAXParse(bis, new AssertingHandler(originalText)); | ||
} | ||
|
||
/** | ||
* Covers the same issue seen when using SAXParse with an InputStream source drawing on bytes of UTF-16 | ||
*/ | ||
public void testIssue48_SAXParse_InputStream_UTF16() throws Exception { | ||
final String originalText = "僧伽罗人"; | ||
final String testJsonString = "[ \"" + originalText + "\" ]"; | ||
|
||
final ByteArrayInputStream bis = new ByteArrayInputStream(testJsonString.getBytes("UTF-16")); | ||
|
||
JSONValue.SAXParse(bis, new AssertingHandler(originalText)); | ||
} | ||
|
||
/** | ||
* Covers the same issue seen when using SAXParse with a Reader source. | ||
* | ||
* A Reader doesn't seem to be affected by the issue, probably because it reads | ||
* a character at a time, rather than a byte at a time. | ||
*/ | ||
public void testIssue48_SAXParse_Reader() throws Exception { | ||
final String originalText = "僧伽罗人"; | ||
final String testJsonString = "[ \"" + originalText + "\" ]"; | ||
|
||
final Reader source = new StringReader(testJsonString); | ||
|
||
JSONValue.SAXParse(source, new AssertingHandler(originalText)); | ||
} | ||
|
||
/** | ||
* A ContentHandler that checks any parsed primitive against a single expected | ||
* value and fails the test if it's not the same | ||
*/ | ||
private static class AssertingHandler implements ContentHandler { | ||
final String expectedPrimitive; | ||
|
||
public AssertingHandler(final String expectedPrimitive) { | ||
this.expectedPrimitive = expectedPrimitive; | ||
} | ||
|
||
public void startJSON() throws ParseException, IOException { } | ||
public void endJSON() throws ParseException, IOException { } | ||
public boolean startObject() throws ParseException, IOException { return true; } | ||
public boolean endObject() throws ParseException, IOException { return true; } | ||
public boolean startObjectEntry(String key) throws ParseException, IOException { return true; } | ||
public boolean endObjectEntry() throws ParseException, IOException { return true; } | ||
public boolean startArray() throws ParseException, IOException { return true; } | ||
public boolean endArray() throws ParseException, IOException { return true; } | ||
|
||
public boolean primitive(final Object value) throws ParseException, IOException { | ||
assertEquals(this.expectedPrimitive, value); | ||
return true; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use the params instead of ignoring what is passed in?