From 51a5873b8b69ab0416d683644c48dcc8260b1e60 Mon Sep 17 00:00:00 2001 From: Ondrej Date: Thu, 21 Mar 2013 22:44:00 +0100 Subject: [PATCH 1/3] fixed wrong serialization of objects with default toString() Objects that use the default toString() method will not create usefull serialized output so we make JSON out of them. Also we now allow Arrays to be serialized to JSON this way too as the array datatype will not cover Arrays of objects with a default toString() method. --- lib/datatypes.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/datatypes.js b/lib/datatypes.js index 47b0d18c..5e86f319 100644 --- a/lib/datatypes.js +++ b/lib/datatypes.js @@ -189,7 +189,7 @@ datatypes = { // Sure, Arrays are technically Objects, but we're treating Array as a // separate datatype. Remember, instanceof Array fails across window // boundaries, so let's also make sure the Object isn't Array-ish - if (typeof val != 'object' || _isArray(val)) { + if (typeof val != 'object'/* || _isArray(val)*/) { // by der_On: allow saving of arrays as the datatype array only saves arrays of numbers or strings correctly, but not arrays of objects return { err: i18n.getText('model.validatesObject', {name: name}, locale) , val: null @@ -205,6 +205,11 @@ datatypes = { , opts = options || {}; if (typeof input.toString == 'function') { val = input.toString(); + + // if this happens the object had no usefull toString() method and we should make JSON out of it + if (val == "[object Object]") { + val = JSON.stringify(input); + } } else { val = JSON.stringify(input); From 8ff337a7a2c433a5318ce356be1734768cc648b9 Mon Sep 17 00:00:00 2001 From: Ondrej Date: Thu, 21 Mar 2013 23:40:33 +0100 Subject: [PATCH 2/3] made serializing of arrays for 'object' datatype work --- lib/datatypes.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/datatypes.js b/lib/datatypes.js index 5e86f319..5bf8ad30 100644 --- a/lib/datatypes.js +++ b/lib/datatypes.js @@ -203,9 +203,13 @@ datatypes = { , serialize: function (input, options) { var val , opts = options || {}; - if (typeof input.toString == 'function') { + + // Arrays will be converted to JSON + if (_isArray(input)) { + val = JSON.stringify(input); + } else if (typeof input.toString == 'function') { val = input.toString(); - + // if this happens the object had no usefull toString() method and we should make JSON out of it if (val == "[object Object]") { val = JSON.stringify(input); From 41c8886271b20220c21c25b478682d32b60d457f Mon Sep 17 00:00:00 2001 From: Ondrej Date: Thu, 21 Mar 2013 23:46:55 +0100 Subject: [PATCH 3/3] fields for object and array datatypes are now created --- lib/generators/sql.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/generators/sql.js b/lib/generators/sql.js index 8f2bedb9..d5d7cd95 100644 --- a/lib/generators/sql.js +++ b/lib/generators/sql.js @@ -1,4 +1,3 @@ - var model = require('../index') , utils = require('utilities') , generator @@ -13,6 +12,8 @@ datatypeMap = { , 'date': 'date' , 'datetime': 'timestamp' , 'time': 'time' +, 'object': 'text' +, 'array': 'text' }; generator = new (function () {