Skip to content

Commit

Permalink
fix parse json
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Jan 6, 2016
1 parent f5faab6 commit 367d304
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/adapters/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,11 @@ MySQL.prototype.fromDatabase = function (model, data) {
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
}
if ((props[key].type.name || '').toString().toLowerCase() === 'json' && typeof val == "string") {
val = JSON.parse(val);
try{
val = JSON.parse(val);
} catch(err){

}
}
}
data[key] = val;
Expand Down
5 changes: 3 additions & 2 deletions lib/adapters/neo4j.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,9 @@ Neo4j.prototype.fromDatabase = function (model, data) {
if (typeof val === 'string') {
try {
clean[key] = JSON.parse(val);
} catch(err){}

} catch(err){
clean[key] = val;
}
} else {
clean[key] = val;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/adapters/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ PG.prototype.fromDatabase = function (model, data) {
Object.keys(data).forEach(function (key) {
var val = data[key];
if (props[key]) {
if ((props[key].type.name || '').toString().toLowerCase() === 'json' && typeof val == "string") {
try{
val = JSON.parse(val);
} catch(err){

}
}
data[key] = val;
}
});
Expand Down
8 changes: 7 additions & 1 deletion lib/adapters/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ SQLite3.prototype.fromDatabase = function (model, data) {
var val = data[key], type = (props[key].type.name || '').toString().toLowerCase();
if (props[key]) {
if (type === 'json' && typeof val == "string") {
data[key] = JSON.parse(val);
if ((props[key].type.name || '').toString().toLowerCase() === 'json' && typeof val == "string") {
try{
data[key] = JSON.parse(val);
} catch(err){
data[key] = val;
}
}
} else if (type === 'date') {
if (!val) {
val = null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "caminte",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, rethinkdb, postgres, sqlite, tingodb",
"version": "0.2.5",
"version": "0.2.6",
"author": {
"name": "Aleksej Gordejev",
"email": "aleksej@gordejev.lv",
Expand Down

0 comments on commit 367d304

Please sign in to comment.