1
0
Fork 0
mirror of https://github.com/NaN-tic/sao-old.git synced 2023-12-14 02:12:52 +01:00

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
This commit is contained in:
Diego Abad 2016-11-26 19:43:00 +01:00
parent c97d011b8e
commit 92b095a8e7
2 changed files with 45 additions and 16 deletions

View file

@ -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;
});

View file

@ -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() {