cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an-s...@apache.org
Subject cordova-plugin-file git commit: CB-8792 Fixes reading of json files using readAsText
Date Mon, 27 Apr 2015 08:50:21 GMT
Repository: cordova-plugin-file
Updated Branches:
  refs/heads/master 4e85ac8ff -> 18777980f


CB-8792 Fixes reading of json files using readAsText


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/18777980
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/18777980
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/18777980

Branch: refs/heads/master
Commit: 18777980f154a18545a5e169b6fcb6ff2df991e6
Parents: 4e85ac8
Author: Vladimir Kotikov <v-vlkoti@microsoft.com>
Authored: Fri Apr 24 10:50:22 2015 +0300
Committer: Vladimir Kotikov <v-vlkoti@microsoft.com>
Committed: Mon Apr 27 11:34:05 2015 +0300

----------------------------------------------------------------------
 src/wp/File.cs |  8 +++++++-
 tests/tests.js | 20 +++++++++++++++-----
 2 files changed, 22 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/18777980/src/wp/File.cs
----------------------------------------------------------------------
diff --git a/src/wp/File.cs b/src/wp/File.cs
index af1fb64..941c9c8 100644
--- a/src/wp/File.cs
+++ b/src/wp/File.cs
@@ -22,6 +22,7 @@ using System.Security;
 using System.Text;
 using System.Windows;
 using System.Windows.Resources;
+using WPCordovaClassLib.Cordova.JSON;
 
 namespace WPCordovaClassLib.Cordova.Commands
 {
@@ -766,7 +767,12 @@ namespace WPCordovaClassLib.Cordova.Commands
                     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)
             {

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/18777980/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
index 3694357..b8a3c95 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,


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message