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

Fix for issue 48: JSONValue.parse does not correctly decode UTF-8 bytes from an inputstream #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

johnrevill
Copy link

Here's a possible fix for issue 48 (https://code.google.com/p/json-smart/issues/detail?id=48).

The problem seemed to be that the parsing for InputStreams was taking a byte at a time from the InputStream and considering that byte to be a character. This causes some strangeness when parsing UTF characters beyond the usual ascii range.

The changes are confined to just the JSONParserInputStream class. Instead of using the InputStream directly, it now wraps it in a InputStreamReader (http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html) which deals with multi-byte characters. As a side effect of wrapping it up in a Reader, the JSONParserReader can be used instead, which is why the class now extends JSONParserReader.

Hope this helps.

I'm less familiar with json-smart-v2, but could have a go at fixing the issue in that version too if you want.

from an inputstream",
https://code.google.com/p/json-smart/issues/detail?id=48).
    
Instead of taking a byte at a time from the InputStream, this now wraps
the InputStream in an InputStreamReader and uses the existing
JSONParserReader to read one character at a time.
    
A few extra parse() methods were added to take the source bytes'
character set as a parameter. This allows for cases when the platform's
default character set is not wanted.
c = (char) i;
pos++;
}
return super.parse(new InputStreamReader(in));

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?

Suggested change
return super.parse(new InputStreamReader(in));
return super.parse(new InputStreamReader(in), containerFactory, handler);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants