mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-10 11:55:49 +00:00
Bug 727967 - Support conversion from/to JSON in Dict.jsm. r=gavin
This commit is contained in:
parent
61bccd06eb
commit
a2d539c456
@ -29,10 +29,13 @@ function unconvert(aProp) {
|
||||
* @param aInitial An object containing the initial keys and values of this
|
||||
* dictionary. Only the "own" enumerable properties of the
|
||||
* object are considered.
|
||||
* If |aInitial| is a string, it is assumed to be JSON and parsed into an object.
|
||||
*/
|
||||
function Dict(aInitial) {
|
||||
if (aInitial === undefined)
|
||||
aInitial = {};
|
||||
if (typeof aInitial == "string")
|
||||
aInitial = JSON.parse(aInitial);
|
||||
var items = {}, count = 0;
|
||||
// That we don't look up the prototype chain is guaranteed by Iterator.
|
||||
for (var [key, val] in Iterator(aInitial)) {
|
||||
@ -225,11 +228,22 @@ Dict.prototype = Object.freeze({
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a string representation of this dictionary.
|
||||
* Returns a String representation of this dictionary.
|
||||
*/
|
||||
toString: function Dict_toString() {
|
||||
return "{" +
|
||||
[(key + ": " + val) for ([key, val] in this.items)].join(", ") +
|
||||
"}";
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a JSON representation of this dictionary.
|
||||
*/
|
||||
toJSON: function Dict_toJSON() {
|
||||
let obj = {};
|
||||
for (let [key, item] of Iterator(this._state.items)) {
|
||||
obj[unconvert(key)] = item;
|
||||
}
|
||||
return JSON.stringify(obj);
|
||||
},
|
||||
});
|
||||
|
@ -252,6 +252,37 @@ function test_set_property_lazy_getter() {
|
||||
}
|
||||
}
|
||||
|
||||
// This is used by both test_construct_dict_from_json_string and test_serialize_dict_to_json_string
|
||||
function _sort_comp_arr(arr1,arr2){
|
||||
arr1.sort();
|
||||
arr2.sort();
|
||||
do_check_eq(arr1.toString(),arr2.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests constructing a dictionary from a JSON string.
|
||||
*/
|
||||
function test_construct_dict_from_json_string() {
|
||||
let d1 = new Dict({a:1, b:2, c:"foobar"});
|
||||
let d2 = new Dict(JSON.stringify(({a:1, b:2, c:"foobar"})));
|
||||
_sort_comp_arr(d1.listkeys(),d2.listkeys());
|
||||
do_check_eq(d1.get("a"), d2.get("a"));
|
||||
do_check_eq(d1.get("b"), d2.get("b"));
|
||||
do_check_eq(d1.get("c"), d2.get("c"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests serializing a dictionary to a JSON string.
|
||||
*/
|
||||
function test_serialize_dict_to_json_string() {
|
||||
let d1 = new Dict({a:1, b:2, c:"foobar"});
|
||||
let d2 = new Dict(d1.toJSON());
|
||||
_sort_comp_arr(d1.listkeys(),d2.listkeys());
|
||||
do_check_eq(d1.get("a"), d2.get("a"));
|
||||
do_check_eq(d1.get("b"), d2.get("b"));
|
||||
do_check_eq(d1.get("c"), d2.get("c"));
|
||||
}
|
||||
|
||||
var tests = [
|
||||
test_get_set_has_del,
|
||||
test_get_default,
|
||||
@ -262,7 +293,9 @@ var tests = [
|
||||
test_iterators,
|
||||
test_set_property_strict,
|
||||
test_set_property_non_strict,
|
||||
test_set_property_lazy_getter
|
||||
test_set_property_lazy_getter,
|
||||
test_construct_dict_from_json_string,
|
||||
test_serialize_dict_to_json_string
|
||||
];
|
||||
|
||||
function run_test() {
|
||||
|
Loading…
Reference in New Issue
Block a user