Skip to content
Closed
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
32 changes: 31 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

var jsonld = require("jsonld")
, uuid = require("uuid")
, IRI = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i
, IRI = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i
, RDFTYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
, XSDTYPE = "http://www.w3.org/2001/XMLSchema#"
, async = require("async")
, blanksRegexp = /^_:b\d+$/
, genActionStream;
Expand Down Expand Up @@ -43,6 +44,12 @@ function levelgraphJSONLD(db, jsonldOpts) {
}
value = blanks[value];
}
// preserve object data type if other then string
if(key === "object" && triple.object.datatype && triple.object.datatype.match(XSDTYPE)){
if(triple.object.datatype !== "http://www.w3.org/2001/XMLSchema#string"){
value = '"' + triple.object.value + '"^^<' + triple.object.datatype + '>';
}
}
acc[key] = value;
return acc;
}, {});
Expand Down Expand Up @@ -141,6 +148,29 @@ function levelgraphJSONLD(db, jsonldOpts) {
} else if (triple.object.indexOf("_:") !== 0) {
acc[triple.subject][triple.predicate] = {};
key = (triple.object.match(IRI)) ? "@id" : "@value";
// coerce
var coercionCheck = triple.object.match(/"(.*)"\^\^<http:\/\/www.w3.org\/2001\/XMLSchema#(.*)>/);
if (coercionCheck) {
switch (coercionCheck[2]) {
case "boolean":
if (coercionCheck[1] === "true") {
triple.object = true;
}
if (coercionCheck[1] === "false") {
triple.object = false;
}
break;
case "integer":
triple.object = parseInt(coercionCheck[1], 10);
break;
case "double":
triple.object = parseFloat(coercionCheck[1]);
break;
case "string":
triple.object = coercionCheck[1];
break;
}
}
acc[triple.subject][triple.predicate][key] = triple.object;
cb(null, acc);
} else {
Expand Down
244 changes: 244 additions & 0 deletions test/datatype_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
// http://json-ld.org/spec/latest/json-ld-api/#data-round-tripping

var level = require("level-test")()
, graph = require("levelgraph")
, jsonld = require("../");

describe("jsonld.put data type", function() {

var db, bbb;

beforeEach(function() {
db = jsonld(graph(level()));
bbb = fixture("bigbuckbunny.json");
});

describe("coerce", function() {

it("preserves boolean true", function(done) {
bbb.isFamilyFriendly = true;
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/isFamilyFriendly"
}, function(err, triples) {
expect(triples[0].object).to.equal('"true"^^<http://www.w3.org/2001/XMLSchema#boolean>');
done();
});
});
});

it("preserves boolean false", function(done) {
bbb.isFamilyFriendly = false;
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/isFamilyFriendly"
}, function(err, triples) {
expect(triples[0].object).to.equal('"false"^^<http://www.w3.org/2001/XMLSchema#boolean>');
done();
});
});
});

it("preserves integer positive", function(done) {
bbb.version = 2;
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/version"
}, function(err, triples) {
expect(triples[0].object).to.equal('"2"^^<http://www.w3.org/2001/XMLSchema#integer>');
done();
});
});
});

it("preserves integer negative", function(done) {
bbb.version = -2;
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/version"
}, function(err, triples) {
expect(triples[0].object).to.equal('"-2"^^<http://www.w3.org/2001/XMLSchema#integer>');
done();
});
});
});

it("preserves integer zero", function(done) {
bbb.version = 0;
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/version"
}, function(err, triples) {
expect(triples[0].object).to.equal('"0"^^<http://www.w3.org/2001/XMLSchema#integer>');
done();
});
});
});

it("preserves double positive", function(done) {
bbb.version = 12.345;
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/version"
}, function(err, triples) {
expect(triples[0].object).to.equal('"1.2345E1"^^<http://www.w3.org/2001/XMLSchema#double>');
done();
});
});
});

it("preserves double positive", function(done) {
bbb.version = -12.345;
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/version"
}, function(err, triples) {
expect(triples[0].object).to.equal('"-1.2345E1"^^<http://www.w3.org/2001/XMLSchema#double>');
done();
});
});
});

it("doesn't preserve string", function(done) {
bbb.contentRating = "MPAA PG-13";
db.jsonld.put(bbb, function() {
db.get({
predicate: "http://schema.org/contentRating"
}, function(err, triples) {
expect(triples[0].object).to.equal("MPAA PG-13");
done();
});
});
});
});
});
describe("jsonld.get data type", function() {

var db, bbb, triple;

beforeEach(function() {
db = jsonld(graph(level()));
bbb = fixture("bigbuckbunny.json");
triple = {
subject: bbb["@id"],
predicate: null,
object: null
};
});

describe("coerce", function() {

it("preserves boolean true", function(done) {
triple.predicate = "http://schema.org/isFamilyFriendly";
triple.object = '"true"^^<http://www.w3.org/2001/XMLSchema#boolean>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["isFamilyFriendly"]).to.be.true;
done();
});
});
});
});

it("preserves boolean false", function(done) {
triple.predicate = "http://schema.org/isFamilyFriendly";
triple.object = '"false"^^<http://www.w3.org/2001/XMLSchema#boolean>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["isFamilyFriendly"]).to.be.false;
done();
});
});
});
});

it("preserves integer positive", function(done) {
triple.predicate = "http://schema.org/version";
triple.object = '"2"^^<http://www.w3.org/2001/XMLSchema#integer>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["version"]).to.equal(2);
done();
});
});
});
});

it("preserves integer positive", function(done) {
triple.predicate = "http://schema.org/version";
triple.object = '"-2"^^<http://www.w3.org/2001/XMLSchema#integer>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["version"]).to.equal(-2);
done();
});
});
});
});

it("preserves integer zero", function(done) {
triple.predicate = "http://schema.org/version";
triple.object = '"0"^^<http://www.w3.org/2001/XMLSchema#integer>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["version"]).to.equal(0);
done();
});
});
});
});

it("preserves double positive", function(done) {
triple.predicate = "http://schema.org/version";
triple.object = '"1.2345E1"^^<http://www.w3.org/2001/XMLSchema#double>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["version"]).to.equal(12.345);
done();
});
});
});
});

it("preserves double negative", function(done) {
triple.predicate = "http://schema.org/version";
triple.object = '"-1.2345E1"^^<http://www.w3.org/2001/XMLSchema#double>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["version"]).to.equal(-12.345);
done();
});
});
});
});

it("does not preserve string", function(done) {
triple.predicate = "http://schema.org/contentRating";
triple.object = '"MPAA PG-13"^^<http://www.w3.org/2001/XMLSchema#string>';

db.jsonld.put(bbb, function() {
db.put(triple, function() {
db.jsonld.get(bbb["@id"], bbb["@context"], function(err, doc) {
expect(doc["contentRating"]).to.equal("MPAA PG-13");
done();
});
});
});
});

});
});
7 changes: 7 additions & 0 deletions test/fixture/bigbuckbunny.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@context": {
"@vocab": "http://schema.org/"
},
"@id": "http://www.bigbuckbunny.org/",
"name": "Big Buck Bunny"
}
2 changes: 1 addition & 1 deletion test/put_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe("jsonld.put", function() {
db.get({
subject: "http://manu.sporny.org#person"
, predicate: "http://xmlns.com/foaf/0.1/age"
, object: 42
, object: '"42"^^<http://www.w3.org/2001/XMLSchema#integer>'
}, function(err, triples) {
expect(triples).to.have.length(1);
done();
Expand Down