[Conkeror] [PATCH] Session management: get rid of modules/json.js, since io.js makes things so
David House
dmhouse at gmail.com
Thu Sep 4 08:56:15 PDT 2008
---
modules/json.js | 66 ----------------------------------------------------
modules/session.js | 9 ++++---
2 files changed, 5 insertions(+), 70 deletions(-)
delete mode 100644 modules/json.js
diff --git a/modules/json.js b/modules/json.js
deleted file mode 100644
index 6ddd5f2..0000000
--- a/modules/json.js
+++ /dev/null
@@ -1,66 +0,0 @@
-/** JSON SUPPORT
- * Utility class for encoding and decoding JSON.
- */
-
-function json() {
- this.json = Cc["@mozilla.org/dom/json;1"] .createInstance(Ci.nsIJSON);
-}
-
-/**
- * Encode obj into JSON, return it.
- */
-json.prototype.encode = function(obj) {
- return this.json.encode(obj);
-}
-
-/**
- * Decode str as a JSON string, and return the resulting object.
- */
-json.prototype.decode = function(str) {
- return this.json.decode(str);
-}
-
-/**
- * Encode obj into JSON, and save it into path.
- */
-json.prototype.encodeToFile = function(path, obj) {
- // We should theoretically be able to use nsIJSON.encodeToStream to avoid
- // doing the UTF encoding ourselves, but I couldn't get it to output
- // anything other than an empty string.
- var cos = Cc["@mozilla.org/intl/converter-output-stream;1"]
- .createInstance(Ci.nsIConverterOutputStream);
- var os = Cc["@mozilla.org/network/file-output-stream;1"]
- .createInstance(Ci.nsIFileOutputStream);
- var file = Cc["@mozilla.org/file/local;1"]
- .createInstance(Ci.nsILocalFile);
- file.initWithPath(path);
- os.init(file, -1, -1, 0);
- cos.init(os, "UTF-8", 0, 0);
- cos.writeString(this.json.encode(obj));
- cos.close();
-}
-
-/**
- * Read the file at path, decode it, and return the resulting object.
- */
-json.prototype.decodeFromFile = function(path) {
- var cis = Cc["@mozilla.org/intl/converter-input-stream;1"]
- .createInstance(Ci.nsIConverterInputStream);
- var is = Cc["@mozilla.org/network/file-input-stream;1"]
- .createInstance(Ci.nsIFileInputStream);
- const replace = Ci .nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER;
- var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
- file.initWithPath(path);
- is.init(file, -1, 0, 0);
- cis.init(is, "UTF-8", 1024, replace);
-
- var str = {};
- var input = "";
- while (cis.readString(4096, str) != 0) {
- input += str.value;
- }
- is.close();
- return this.json.decode(input);
-}
-
-conkeror.json = new json();
\ No newline at end of file
diff --git a/modules/session.js b/modules/session.js
index af4bbe1..4efc690 100644
--- a/modules/session.js
+++ b/modules/session.js
@@ -6,13 +6,12 @@
* restore_session();
*/
-require('json.js');
-
function session_manager(sess_file) {
if (typeof sess_file == "undefined")
sess_file = profile_dir + "/session.json";
this.path = sess_file;
- this.session = json.decodeFromFile(sess_file);
+ this.session = Cc["@mozilla.org/dom/json;1"] .createInstance(Ci.nsIJSON)
+ .decode(read_text_file(get_file(sess_file)));
}
/**
@@ -35,7 +34,9 @@ session_manager.prototype.get_session = function () {
* Encode a session to disk
*/
session_manager.prototype.save = function () {
- json.encodeToFile(this.path, this.get_session());
+ var data = Cc["@mozilla.org/dom/json;1"] .createInstance(Ci.nsIJSON)
+ .encode(this.get_session())
+ write_text_file(get_file(this.path), data);
}
add_hook("quit_hook",
--
1.5.4.3
More information about the Conkeror
mailing list