Closed Bug 420397 Opened 18 years ago Closed 7 years ago

JSON roundtrip of UTF-8 chars fails

Categories

(Core :: DOM: Core & HTML, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED WORKSFORME

People

(Reporter: hello, Assigned: sayrer)

Details

Attachments

(1 file)

1.10 KB, application/x-javascript
Details
Attached file test case
I have a small test case which I believe should work: I'm trying to write out json to a file which contains some Japanese strings, then I'm reading it back in and checking it. See attached file. Just run it with xpcshell.
The file is in UTF-8, by the way.
The problem is only when using decodeFromStream(). When I do this instead, it works (maybe by chance, it's picking the right charset?): function read_stream(is) { var ret = ""; var chunk = is.read(4096); while (chunk.length > 0) { ret += chunk; chunk = is.read(4096); } return ret; } // ... var sis = Cc["@mozilla.org/scriptableinputstream;1"]. createInstance(Ci.nsIScriptableInputStream); sis.init(istream); var decoded = nativeJSON.decode(read_stream(sis));
It works if I change the line if (decoded.foo == "日本語の言葉です。") to if (decoded.foo == "\u65E5\u672C\u8A9E\u306E\u8A00\u8449\u3067\u3059\u3002") so the problem is that the source file isn't being read as UTF-8.
Summary: JSON roundtrip of CJKT chars fails → JSON roundtrip of UTF-8 chars fails
(In reply to comment #3) > the problem is that the source file isn't being read as UTF-8. Definitely a bug. The decoder should be able to tell from the first few bytes, per the JSON rfc.
Assignee: nobody → sayrer
Wait a minute. The decoder *is* recognizing the stream as UTF-8 and converting it, but the string "日本語の言葉です。" in the line I quoted is being taken as Latin-1. In the version in comment 2, using decode instead of decodeFromStream, both come out as Latin-1.
This bug might be related to bug 420411.
nsIJSON is gone.
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → WORKSFORME
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: