Skip to content

Add JSON support in query and response #1299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
9 changes: 9 additions & 0 deletions lib/protocol/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
12 changes: 11 additions & 1 deletion lib/protocol/SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/pool/test-escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/protocol/test-SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ 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() {
assert.equal(SqlString.escape([1, 2, 'c']), "1, 2, 'c'");
},

'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() {
Expand Down Expand Up @@ -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'");
Expand Down