From 367d304f5766fa5a68a4e7a467b79be348adaf68 Mon Sep 17 00:00:00 2001 From: Alexey Gordeyev Date: Wed, 6 Jan 2016 22:41:03 +0200 Subject: [PATCH] fix parse json --- lib/adapters/mysql.js | 6 +++++- lib/adapters/neo4j.js | 5 +++-- lib/adapters/postgres.js | 7 +++++++ lib/adapters/sqlite3.js | 8 +++++++- package.json | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/adapters/mysql.js b/lib/adapters/mysql.js index e3b7ac1..14a5462 100644 --- a/lib/adapters/mysql.js +++ b/lib/adapters/mysql.js @@ -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; diff --git a/lib/adapters/neo4j.js b/lib/adapters/neo4j.js index f152f14..164d817 100644 --- a/lib/adapters/neo4j.js +++ b/lib/adapters/neo4j.js @@ -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; } diff --git a/lib/adapters/postgres.js b/lib/adapters/postgres.js index 77437ee..15d0bed 100644 --- a/lib/adapters/postgres.js +++ b/lib/adapters/postgres.js @@ -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; } }); diff --git a/lib/adapters/sqlite3.js b/lib/adapters/sqlite3.js index 4f67662..4dc019b 100644 --- a/lib/adapters/sqlite3.js +++ b/lib/adapters/sqlite3.js @@ -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; diff --git a/package.json b/package.json index b8f4265..3ea00c8 100644 --- a/package.json +++ b/package.json @@ -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",