-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change InputStream input reading to use UTF8.
- Loading branch information
Showing
3 changed files
with
71 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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); | ||
|
@@ -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
58
json-smart/src/test/java/net/minidev/json/test/TestUtf8.java
This file contains 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,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); | ||
} | ||
} | ||
} |