Skip to content

Commit

Permalink
change InputStream input reading to use UTF8.
Browse files Browse the repository at this point in the history
  • Loading branch information
UrielCh committed Aug 20, 2015
1 parent 0e3cd0d commit d39cfe6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

public class JSONParser {
/**
Expand Down Expand Up @@ -235,7 +236,7 @@ public Object parse(Reader in, ContainerFactory containerFactory, ContentHandler
* use to return Primitive Type, or String, Or JsonObject or JsonArray
* generated by a ContainerFactory
*/
public Object parse(InputStream in) throws ParseException {
public Object parse(InputStream in) throws ParseException, UnsupportedEncodingException {
if (pSBintream == null)
pSBintream = new JSONParserInputStream(mode);
return pSBintream.parse(in);
Expand All @@ -245,7 +246,7 @@ public Object parse(InputStream in) throws ParseException {
* use to return Primitive Type, or String, Or JsonObject or JsonArray
* generated by a ContainerFactory
*/
public Object parse(InputStream in, ContainerFactory containerFactory) throws ParseException {
public Object parse(InputStream in, ContainerFactory containerFactory) throws ParseException, UnsupportedEncodingException {
if (pSBintream == null)
pSBintream = new JSONParserInputStream(mode);
return pSBintream.parse(in, containerFactory);
Expand All @@ -256,7 +257,7 @@ public Object parse(InputStream in, ContainerFactory containerFactory) throws Pa
* generated by a ContainerFactory
*/
public Object parse(InputStream in, ContainerFactory containerFactory, ContentHandler handler)
throws ParseException {
throws ParseException, UnsupportedEncodingException {
if (pSBintream == null)
pSBintream = new JSONParserInputStream(mode);
return pSBintream.parse(in, containerFactory, handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -37,53 +34,28 @@ public JSONParserInputStream(int permissiveMode) {
* use to return Primitive Type, or String, Or JsonObject or JsonArray
* generated by a ContainerFactory
*/
public Object parse(InputStream in) throws ParseException {
public Object parse(InputStream in) throws ParseException, UnsupportedEncodingException {
return parse(in, ContainerFactory.FACTORY_SIMPLE, ContentHandlerDumy.HANDLER);
}

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray
* generated by a ContainerFactory
*/
public Object parse(InputStream in, ContainerFactory containerFactory) throws ParseException {
public Object parse(InputStream in, ContainerFactory containerFactory) throws ParseException, UnsupportedEncodingException {
return parse(in, containerFactory, ContentHandlerDumy.HANDLER);
}

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray
* generated by a ContainerFactory
* @throws UnsupportedEncodingException
*/
public Object parse(InputStream in, ContainerFactory containerFactory, ContentHandler handler)
throws ParseException {
//
this.in = in;
throws ParseException, UnsupportedEncodingException {
InputStreamReader i2 = new InputStreamReader(in, "utf8");
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++;
//
return super.parse(i2, containerFactory, handler);
}

protected void readS() throws IOException {
sb.append(c);
int i = in.read();
if (i == -1) {
c = EOI;
} else {
c = (char) i;
pos++;
}
}

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;
//
}
}
58 changes: 58 additions & 0 deletions json-smart/src/test/java/net/minidev/json/test/TestUtf8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.minidev.json.test;

import java.io.ByteArrayInputStream;
import java.io.StringReader;

import junit.framework.TestCase;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

public class TestUtf8 extends TestCase {
// Sinhalese language
static String[] nonLatinTexts = new String[] { "සිංහල ජාතිය", "日本語", "Русский", "فارسی", "한국어", "Հայերեն", "हिन्दी", "עברית", "中文", "አማርኛ", "മലയാളം",
"ܐܬܘܪܝܐ", "მარგალური" };

public void testString() throws Exception {
for (String nonLatinText : nonLatinTexts) {
String s = "{\"key\":\"" + nonLatinText + "\"}";
JSONObject obj = (JSONObject) JSONValue.parse(s);
String v = (String) obj.get("key"); // result is incorrect
System.out.println(v);
assertEquals(v, nonLatinText);
}
}

public void testReader() throws Exception {
for (String nonLatinText : nonLatinTexts) {
String s = "{\"key\":\"" + nonLatinText + "\"}";
StringReader reader = new StringReader(s);
JSONObject obj = (JSONObject) JSONValue.parse(reader);

String v = (String) obj.get("key"); // result is incorrect
System.out.println(v);
assertEquals(v, nonLatinText);
}
}

public void testInputStream() throws Exception {
for (String nonLatinText : nonLatinTexts) {
String s = "{\"key\":\"" + nonLatinText + "\"}";
ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes("utf8"));
JSONObject obj = (JSONObject) JSONValue.parse(bis);
String v = (String) obj.get("key"); // result is incorrect
System.out.println(v);
assertEquals(v, nonLatinText);
}
}

public void testBytes() throws Exception {
for (String nonLatinText : nonLatinTexts) {
String s = "{\"key\":\"" + nonLatinText + "\"}";
byte[] bs = s.getBytes("utf8");
JSONObject obj = (JSONObject) JSONValue.parse(bs);
String v = (String) obj.get("key"); // result is incorrect
System.out.println(v);
assertEquals(v, nonLatinText);
}
}
}

0 comments on commit d39cfe6

Please sign in to comment.