diff --git a/Readme.md b/Readme.md index cc3dd5525..a943e14a4 100644 --- a/Readme.md +++ b/Readme.md @@ -1175,6 +1175,19 @@ than a string. * TIME (could be mapped to Date, but what date would be set?) * GEOMETRY (never used those, get in touch if you do) +### JSON Object + +Any string, that can be converted to JSON ("{json_object:true}") is return as JSON object. +To insert or update JSON into TEXT column you can simply do: +```js +var query = connection.query('insert into table ?',[text_column:{json_object:true}], function(err, results) { +``` + +You cannot do this: +```js +var query = connection.query('insert into table text_column=?',{json_object:true}, function(err, results) { +``` + It is not recommended (and may go away / change in the future) to disable type casting, but you can currently do so on either the connection: diff --git a/lib/protocol/Parser.js b/lib/protocol/Parser.js index 82815bd4e..35fd995d7 100644 --- a/lib/protocol/Parser.js +++ b/lib/protocol/Parser.js @@ -298,6 +298,15 @@ Parser.prototype.parseString = function(length) { var end = offset + length; var value = this._buffer.toString(this._encoding, offset, end); + var json; + if (value[0]=='{' && value[value.length-1]=='}' ) + try{ + json=JSON.parse(value); + if(typeof json!='number') value=json; + } + catch(abc){ + json=null; + } this._offset = end; return value; }; diff --git a/lib/protocol/SqlString.js b/lib/protocol/SqlString.js index 06c17cb24..3148f77a2 100644 --- a/lib/protocol/SqlString.js +++ b/lib/protocol/SqlString.js @@ -37,7 +37,17 @@ SqlString.escape = function(val, stringifyObjects, timeZone) { if (typeof val === 'object') { if (stringifyObjects) { - val = val.toString(); + // val=val.toString() + var found; + for(var i in val) { + if (i=='toString') found=true; + } + if(found){ + val=val.toString(); + } + else{ + val = JSON.stringify(val); + } } else { return SqlString.objectToValues(val, timeZone); } diff --git a/test/unit/pool/test-escape.js b/test/unit/pool/test-escape.js index 553ded57a..11309c6b0 100644 --- a/test/unit/pool/test-escape.js +++ b/test/unit/pool/test-escape.js @@ -12,7 +12,7 @@ server.listen(common.fakeServerPort, function (err) { assert.equal(pool.escape({ a: 123 }), "`a` = 123"); assert.equal(pool2.escape('Super'), "'Super'"); - assert.equal(pool2.escape({ a: 123 }), "'[object Object]'"); + assert.equal(pool2.escape({ a: 123 }), "'{\\\"a\\\":123}'"); pool.end(function (err) { assert.ifError(err); diff --git a/test/unit/protocol/test-SqlString.js b/test/unit/protocol/test-SqlString.js index 7a79713d2..19be897c1 100644 --- a/test/unit/protocol/test-SqlString.js +++ b/test/unit/protocol/test-SqlString.js @@ -55,7 +55,7 @@ test('SqlString.escape', { }, 'nested objects are cast to strings': function() { - assert.equal(SqlString.escape({a: {nested: true}}), "`a` = '[object Object]'"); + assert.equal(SqlString.escape({a: {nested: true}}), "`a` = '{\\\"nested\\\":true}'"); }, 'arrays are turned into lists': function() { @@ -63,11 +63,11 @@ test('SqlString.escape', { }, 'nested arrays are turned into grouped lists': function() { - assert.equal(SqlString.escape([[1,2,3], [4,5,6], ['a', 'b', {nested: true}]]), "(1, 2, 3), (4, 5, 6), ('a', 'b', '[object Object]')"); + assert.equal(SqlString.escape([[1,2,3], [4,5,6], ['a', 'b', {nested: true}]]), "(1, 2, 3), (4, 5, 6), ('a', 'b', '{\\\"nested\\\":true}')"); }, 'nested objects inside arrays are cast to strings': function() { - assert.equal(SqlString.escape([1, {nested: true}, 2]), "1, '[object Object]', 2"); + assert.equal(SqlString.escape([1, {nested: true}, 2]), "1, '{\\\"nested\\\":true}', 2"); }, 'strings are quoted': function() { @@ -167,7 +167,7 @@ test('SqlString.format', { 'objects is not converted to values': function () { var sql = SqlString.format('?', { 'hello': 'world' }, true); - assert.equal(sql, "'[object Object]'"); + assert.equal(sql, "'{\\\"hello\\\":\\\"world\\\"}'"); var sql = SqlString.format('?', { toString: function () { return 'hello'; } }, true); assert.equal(sql, "'hello'");