From 18777980f154a18545a5e169b6fcb6ff2df991e6 Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Fri, 24 Apr 2015 10:50:22 +0300 Subject: [PATCH] CB-8792 Fixes reading of json files using readAsText --- src/wp/File.cs | 8 +++++++- tests/tests.js | 20 +++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/wp/File.cs b/src/wp/File.cs index af1fb643f..941c9c867 100644 --- a/src/wp/File.cs +++ b/src/wp/File.cs @@ -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 { @@ -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) { diff --git a/tests/tests.js b/tests/tests.js index 369435723..b8a3c9518 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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)); @@ -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(); @@ -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); @@ -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,