Skip to content

Commit

Permalink
GPII-2110: Added support for JSON 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasper Markus committed Nov 2, 2016
1 parent 83c5bff commit 21b711f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/DataSources.md
Expand Up @@ -258,6 +258,7 @@ Kettle features three built-in content encoding grades which can be configured a
|Grade name| Encoding type | Content-Type header |
|----------|---------------|----------------|
|`kettle.dataSource.encoding.JSON`|[JSON](http://json.org)|`application/json`|
|`kettle.dataSource.encoding.JSON5`|[JSON5](http://json5.org)|`application/json`|
|`kettle.dataSource.encoding.formenc`|[form encoding](http://www.w3.org/TR/html401/interact/forms.html#didx-applicationx-www-form-urlencoded)|`application/x-www-form-urlencoded`|
|`kettle.dataSource.encoding.none`|No encoding|`text/plain`|

Expand Down
32 changes: 31 additions & 1 deletion lib/dataSource-core.js
Expand Up @@ -14,7 +14,8 @@ https://github.com/fluid-project/kettle/blob/master/LICENSE.txt

var fluid = fluid || require("infusion"),
jsonlint = jsonlint || (require && require("jsonlint")),
kettle = fluid.registerNamespace("kettle");
kettle = fluid.registerNamespace("kettle"),
JSON5 = JSON5 || require("json5");


/** Some common content encodings - suitable to appear as the "encoding" subcomponent of a dataSource **/
Expand All @@ -28,6 +29,15 @@ fluid.defaults("kettle.dataSource.encoding.JSON", {
contentType: "application/json"
});

fluid.defaults("kettle.dataSource.encoding.JSON5", {
gradeNames: "fluid.component",
invokers: {
parse: "kettle.dataSource.parseJSON5",
render: "kettle.dataSource.stringifyJSON5"
},
contentType: "application/json5"
});

fluid.defaults("kettle.dataSource.encoding.formenc", {
gradeNames: "fluid.component",
invokers: {
Expand Down Expand Up @@ -101,6 +111,26 @@ kettle.dataSource.stringifyJSON = function (obj) {
return obj === undefined ? "" : JSON.stringify(obj, null, 4);
};

kettle.dataSource.parseJSON5 = function (string) {
var togo = fluid.promise();
if (!string) {
togo.resolve(undefined);
} else {
try {
togo.resolve(JSON5.parse(string));
} catch (err) {
togo.reject({
message: err.message || err
});
}
}
return togo;
};

kettle.dataSource.stringifyJSON5 = function (obj) {
return obj === undefined ? "" : JSON5.stringify(obj, null, 4);
};

/**
* The head of the hierarchy of dataSource components. These abstract
* over the process of read and write access to data, following a simple CRUD-type semantic, indexed by
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -23,7 +23,8 @@
"jsonlint": "1.6.0",
"resolve": "1.1.6",
"node-uuid": "1.4.7",
"path-to-regexp": "1.5.3"
"path-to-regexp": "1.5.3",
"json5": "0.5.0"
},
"devDependencies": {
"fluid-grunt-eslint": "18.1.2",
Expand Down
52 changes: 51 additions & 1 deletion tests/DataSourceFileTests.js
Expand Up @@ -469,6 +469,54 @@ fluid.defaults("kettle.tests.dataSource.17.CouchDB.file.set.existing.expand", {
}
});

fluid.defaults("kettle.tests.dataSource.18.JSON5file.standard", {
gradeNames: ["kettle.tests.simpleDataSourceTest"],
name: "18. Testing JSON5 file datasource with standard response",
components: {
dataSource: {
type: "kettle.dataSource.file.moduleTerms",
options: {
path: "%kettle/tests/data/dataSourceJSON5TestFile.json5",
components: {
encoding: {
type: "kettle.dataSource.encoding.JSON5"
}
}
}
}
},
invokers: {
responseFunc: {
funcName: "kettle.tests.dataSource.testResponse",
args: [{
dataSource: "works"
}, "{arguments}.0"]
}
}
});

fluid.defaults("kettle.tests.dataSource.19.JSON5file.empty", {
gradeNames: ["kettle.tests.simpleDataSourceTest"],
name: "19. Testing JSON5 file datasource with empty response",
components: {
dataSource: {
type: "kettle.dataSource.file.moduleTerms",
options: {
path: "%kettle/tests/data/emptyDataSourceTestFile.txt",
components: {
encoding: {
type: "kettle.dataSource.encoding.JSON5"
}
}
}
}
},
invokers: {
responseFunc: "kettle.tests.dataSource.testEmptyResponse"
}
});



kettle.tests.dataSource.standardTests = [
"kettle.tests.dataSource.1.file.empty",
Expand All @@ -487,7 +535,9 @@ kettle.tests.dataSource.standardTests = [
"kettle.tests.dataSource.14.CouchDB.file.set",
"kettle.tests.dataSource.15.CouchDB.file.set.existing",
"kettle.tests.dataSource.16.file.set.expand",
"kettle.tests.dataSource.17.CouchDB.file.set.existing.expand"
"kettle.tests.dataSource.17.CouchDB.file.set.existing.expand",
"kettle.tests.dataSource.18.JSON5file.standard",
"kettle.tests.dataSource.19.JSON5file.empty"
];

// Convert each of the standard test fixture grades into tests of the equivalent promise-based API
Expand Down
6 changes: 6 additions & 0 deletions tests/shared/DataSourceTestUtils.js
Expand Up @@ -189,3 +189,9 @@ kettle.tests.expectJSONDiagnostic = function (error) {
jqUnit.assertTrue("Got message mentioning filename ", error.message.indexOf("invalidJSONFile") !== -1);
jqUnit.assertTrue("Got message mentioning line number of error ", error.message.indexOf("59") !== -1);
};

kettle.tests.expectJSON5Diagnostic = function (error) {
fluid.log("Received JSON5 diagnostic error " + JSON.stringify(error, null, 2));
jqUnit.assertTrue("Got message mentioning filename ", error.message.indexOf("invalidJSON5File") !== -1);
jqUnit.assertTrue("Got message mentioning line number of error ", error.message.indexOf("49") !== -1);
};

0 comments on commit 21b711f

Please sign in to comment.