From 92b095a8e755a3db634795b7ea3366a045042da7 Mon Sep 17 00:00:00 2001 From: Diego Abad Date: Sat, 26 Nov 2016 19:43:00 +0100 Subject: [PATCH] Prepare pyson expression before encode when use date and datetime fields Moment library implements its own toJSON function returning string format: http://momentjs.com/docs/#/displaying/as-json/ As moment object has toJSON function the replacer used to stringify pyson expressions never receive a moment object because it is dumped before. It is needed to prepare pyson object as same design as Sao.rpc.prepareObject issue6027 review31701002 --- src/pyson.js | 48 ++++++++++++++++++++++++++++++++---------------- tests/sao.js | 13 +++++++++++++ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/pyson.js b/src/pyson.js index 041ba2b..266ec88 100644 --- a/src/pyson.js +++ b/src/pyson.js @@ -39,28 +39,44 @@ }; Sao.PYSON.Encoder = Sao.class_(Object, { + prepare: function(value, index, parent) { + if (value !== null && value !== undefined) { + if (value instanceof Array) { + value = jQuery.extend([], value); + for (var i = 0, length = value.length; i < length; i++) { + this.prepare(value[i], i, value); + } + } else if (value._isAMomentObject) { + if (value.isDate) { + value = new Sao.PYSON.Date( + value.year(), + value.month() + 1, + value.date()).pyson(); + } else { + value = new Sao.PYSON.DateTime( + value.year(), + value.month() + 1, + value.date(), + value.hours(), + value.minutes(), + value.seconds(), + value.milliseconds() * 1000).pyson(); + } + } + } + if (parent) { + parent[index] = value; + } + return parent || value; + }, + encode: function(pyson) { + pyson = this.prepare(pyson); return JSON.stringify(pyson, function(k, v) { if (v instanceof Sao.PYSON.PYSON) { return v.pyson(); } else if (v === null || v === undefined) { return null; - } else if (v._isAMomentObject) { - if (v.isDate) { - return Sao.PYSON.Date( - v.getFullYear(), - v.getMonth(), - v.getDate()).pyson(); - } else { - return Sao.PYSON.DateTime( - v.getFullYear(), - v.getMonth(), - v.getDate(), - v.getHours(), - v.getMinutes(), - v.getSeconds(), - v.getMilliseconds()).pyson(); - } } return v; }); diff --git a/tests/sao.js b/tests/sao.js index 3736d91..6b00031 100644 --- a/tests/sao.js +++ b/tests/sao.js @@ -45,9 +45,22 @@ QUnit.test('PYSON Encoder', function() { var encoder = new Sao.PYSON.Encoder(); var none; + var date = Sao.Date(2002, 0, 1); + var datetime = Sao.DateTime(2002, 0, 1, 12, 30, 0, 0); + var pyson_date = new Sao.PYSON.Date(2002, 1, 1).pyson(); + var pyson_datetime = new Sao.PYSON.DateTime( + 2002, 1, 1, 12, 30, 0, 0).pyson(); + var array = ["create_date", '>=', date]; + var pyson_array = ["create_date", '>=', pyson_date]; QUnit.strictEqual(encoder.encode(), 'null', "encode()"); QUnit.strictEqual(encoder.encode(none), 'null', "encode(none)"); QUnit.strictEqual(encoder.encode(null), 'null', "encode()"); + QUnit.strictEqual(encoder.encode(date), + JSON.stringify(pyson_date), "encode(date)"); + QUnit.strictEqual(encoder.encode(datetime), + JSON.stringify(pyson_datetime), "encode(datetime)"); + QUnit.strictEqual(encoder.encode(array), + JSON.stringify(pyson_array), "encode(array)"); }); QUnit.test('PYSON.Eval', function() {