Skip to content

Commit 1877798

Browse files
author
Vladimir Kotikov
committed
CB-8792 Fixes reading of json files using readAsText
1 parent 4e85ac8 commit 1877798

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/wp/File.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
using System.Text;
2323
using System.Windows;
2424
using System.Windows.Resources;
25+
using WPCordovaClassLib.Cordova.JSON;
2526

2627
namespace WPCordovaClassLib.Cordova.Commands
2728
{
@@ -766,7 +767,12 @@ public void readAsText(string options)
766767
byte[] buffer = this.readFileBytes(filePath, startPos, endPos, isoFile);
767768
text = encoding.GetString(buffer, 0, buffer.Length);
768769
}
769-
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, text), callbackId);
770+
771+
// JIRA: https://issues.apache.org/jira/browse/CB-8792
772+
// Need to perform additional serialization here because NativeExecution is always trying
773+
// to do JSON.parse() on command result. This leads to issue when trying to read JSON files
774+
var resultText = JsonHelper.Serialize(text);
775+
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, resultText), callbackId);
770776
}
771777
catch (Exception ex)
772778
{

tests/tests.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,11 +2191,14 @@ exports.defineAutoTests = function () {
21912191
reader.onloadend = verifier;
21922192
reader.readAsText(blob);
21932193
});
2194-
function writeDummyFile(writeBinary, callback, done) {
2194+
function writeDummyFile(writeBinary, callback, done, fileContents) {
21952195
var fileName = "dummy.txt",
21962196
fileEntry = null,
2197-
fileData = '\u20AC\xEB - There is an exception to every rule. Except this one.',
2198-
fileDataAsBinaryString = '\xe2\x82\xac\xc3\xab - There is an exception to every rule. Except this one.',
2197+
// use default string if file data is not provided
2198+
fileData = fileContents !== undefined ? fileContents :
2199+
'\u20AC\xEB - There is an exception to every rule. Except this one.',
2200+
fileDataAsBinaryString = fileContents !== undefined ? fileContents :
2201+
'\xe2\x82\xac\xc3\xab - There is an exception to every rule. Except this one.',
21992202
createWriter = function (fe) {
22002203
fileEntry = fe;
22012204
fileEntry.createWriter(writeFile, failed.bind(null, done, 'fileEntry.createWriter - Error reading file: ' + fileName));
@@ -2213,7 +2216,7 @@ exports.defineAutoTests = function () {
22132216
// create a file, write to it, and read it in again
22142217
createFile(fileName, createWriter, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
22152218
}
2216-
function runReaderTest(funcName, writeBinary, done, verifierFunc, sliceStart, sliceEnd) {
2219+
function runReaderTest(funcName, writeBinary, done, verifierFunc, sliceStart, sliceEnd, fileContents) {
22172220
writeDummyFile(writeBinary, function (fileEntry, file, fileData, fileDataAsBinaryString) {
22182221
var verifier = function (evt) {
22192222
expect(evt).toBeDefined();
@@ -2230,7 +2233,7 @@ exports.defineAutoTests = function () {
22302233
file = file.slice(sliceStart, file.size, file.type);
22312234
}
22322235
reader[funcName](file);
2233-
}, done);
2236+
}, done, fileContents);
22342237
}
22352238
function arrayBufferEqualsString(ab, str) {
22362239
var buf = new Uint8Array(ab);
@@ -2246,6 +2249,13 @@ exports.defineAutoTests = function () {
22462249
done();
22472250
});
22482251
});
2252+
it("file.spec.84.1 should read JSON file properly, readAsText", function (done) {
2253+
var testObject = {key1: "value1", key2: 2};
2254+
runReaderTest('readAsText', false, done, function (evt, fileData, fileDataAsBinaryString) {
2255+
expect(evt.target.result).toEqual(JSON.stringify(testObject));
2256+
done();
2257+
}, undefined, undefined, JSON.stringify(testObject));
2258+
});
22492259
it("file.spec.85 should read file properly, Data URI", function (done) {
22502260
runReaderTest('readAsDataURL', true, done, function (evt, fileData, fileDataAsBinaryString) {
22512261
/* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,

0 commit comments

Comments
 (0)