Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/wp/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
using System.Text;
using System.Windows;
using System.Windows.Resources;
using WPCordovaClassLib.Cordova.JSON;

namespace WPCordovaClassLib.Cordova.Commands
{
Expand Down Expand Up @@ -766,7 +767,12 @@ public void readAsText(string options)
byte[] buffer = this.readFileBytes(filePath, startPos, endPos, isoFile);
text = encoding.GetString(buffer, 0, buffer.Length);
}
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, text), callbackId);

// JIRA: https://issues.apache.org/jira/browse/CB-8792
// Need to perform additional serialization here because NativeExecution is always trying
// to do JSON.parse() on command result. This leads to issue when trying to read JSON files
var resultText = JsonHelper.Serialize(text);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, resultText), callbackId);
}
catch (Exception ex)
{
Expand Down
20 changes: 15 additions & 5 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2191,11 +2191,14 @@ exports.defineAutoTests = function () {
reader.onloadend = verifier;
reader.readAsText(blob);
});
function writeDummyFile(writeBinary, callback, done) {
function writeDummyFile(writeBinary, callback, done, fileContents) {
var fileName = "dummy.txt",
fileEntry = null,
fileData = '\u20AC\xEB - There is an exception to every rule. Except this one.',
fileDataAsBinaryString = '\xe2\x82\xac\xc3\xab - There is an exception to every rule. Except this one.',
// use default string if file data is not provided
fileData = fileContents !== undefined ? fileContents :
'\u20AC\xEB - There is an exception to every rule. Except this one.',
fileDataAsBinaryString = fileContents !== undefined ? fileContents :
'\xe2\x82\xac\xc3\xab - There is an exception to every rule. Except this one.',
createWriter = function (fe) {
fileEntry = fe;
fileEntry.createWriter(writeFile, failed.bind(null, done, 'fileEntry.createWriter - Error reading file: ' + fileName));
Expand All @@ -2213,7 +2216,7 @@ exports.defineAutoTests = function () {
// create a file, write to it, and read it in again
createFile(fileName, createWriter, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
}
function runReaderTest(funcName, writeBinary, done, verifierFunc, sliceStart, sliceEnd) {
function runReaderTest(funcName, writeBinary, done, verifierFunc, sliceStart, sliceEnd, fileContents) {
writeDummyFile(writeBinary, function (fileEntry, file, fileData, fileDataAsBinaryString) {
var verifier = function (evt) {
expect(evt).toBeDefined();
Expand All @@ -2230,7 +2233,7 @@ exports.defineAutoTests = function () {
file = file.slice(sliceStart, file.size, file.type);
}
reader[funcName](file);
}, done);
}, done, fileContents);
}
function arrayBufferEqualsString(ab, str) {
var buf = new Uint8Array(ab);
Expand All @@ -2246,6 +2249,13 @@ exports.defineAutoTests = function () {
done();
});
});
it("file.spec.84.1 should read JSON file properly, readAsText", function (done) {
var testObject = {key1: "value1", key2: 2};
runReaderTest('readAsText', false, done, function (evt, fileData, fileDataAsBinaryString) {
expect(evt.target.result).toEqual(JSON.stringify(testObject));
done();
}, undefined, undefined, JSON.stringify(testObject));
});
it("file.spec.85 should read file properly, Data URI", function (done) {
runReaderTest('readAsDataURL', true, done, function (evt, fileData, fileDataAsBinaryString) {
/* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,
Expand Down