From 6bacf786a35ebf8ef60bc5906b3032ac1d73de15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 18 Apr 2013 18:14:00 +0200 Subject: [PATCH] Fix PYSON and JSON for null value --- src/pyson.js | 2 +- src/rpc.js | 5 +++-- tests/sao.js | 16 ++++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/pyson.js b/src/pyson.js index 9e8c49142e..dc6963908a 100644 --- a/src/pyson.js +++ b/src/pyson.js @@ -54,7 +54,7 @@ }, decode: function(str) { var reviver = function(k, v) { - if (typeof v == 'object') { + if (typeof v == 'object' && v !== null) { var cls = Sao.PYSON[v.__class__]; if (cls) { return cls.eval_(v, this.__context); diff --git a/src/rpc.js b/src/rpc.js index 741dd33074..d9c2ab586a 100644 --- a/src/rpc.js +++ b/src/rpc.js @@ -64,7 +64,7 @@ Sao.rpc.convertJSONObject(value[i], i, value); } } else if ((typeof(value) != 'string') && - (typeof(value) != 'number')) { + (typeof(value) != 'number') && (value !== null)) { if (value && value.__class__) { switch (value.__class__) { case 'datetime': @@ -101,7 +101,8 @@ for (var i = 0, length = value.length; i < length; i++) { Sao.rpc.prepareObject(value[i], i, value); } - } else if ((typeof(value) != 'string') && (typeof(value) != 'number')) { + } else if ((typeof(value) != 'string') && + (typeof(value) != 'number') && (value !== null)) { if (value instanceof Date) { if (value.getHours() || value.getMinutes() || value.getSeconds()) diff --git a/tests/sao.js b/tests/sao.js index 9ae085a245..705dce7e85 100644 --- a/tests/sao.js +++ b/tests/sao.js @@ -20,16 +20,28 @@ }, 'datetime': new Date(2012, 11, 29, 19, 59, 10), 'date': new Date(2012, 11, 29), - 'decimal': new Sao.Decimal(1.1) + 'decimal': new Sao.Decimal(1.1), + 'null': null }; for (var name in tests) { var test = tests[name]; var result = Sao.rpc.convertJSONObject(jQuery.parseJSON( JSON.stringify(Sao.rpc.prepareObject(test)))); - QUnit.ok(test.toString() == result.toString(), 'JSON ' + name); + var ok_; + if (name == 'null') { + ok_ = test == result; + } else { + ok_ = test.toString() == result.toString(); + } + QUnit.ok(ok_, 'JSON ' + name); } }); + QUnit.test('PYSON Decoder', function() { + var decoder = new Sao.PYSON.Decoder(); + QUnit.strictEqual(decoder.decode('null'), null, "decode('null')"); + }); + QUnit.test('PYSON.Eval', function() { var value; value = new Sao.PYSON.Eval('test').pyson();