Use implicit object conversion with #toJSON

JSON.stringify, if told to convert an object with a #toJSON method, will instead
stringify the result of the #toJSON method.
This commit is contained in:
Andrew Stewart 2014-06-06 14:11:35 -07:00
parent af4bbd963b
commit ebdeeccd12
7 changed files with 42 additions and 42 deletions

View File

@ -45,18 +45,18 @@ router.get("/robots", function(req, res) {
var data = [];
for (var bot in Cylon.robots) {
data.push(Cylon.robots[bot].data());
data.push(Cylon.robots[bot]);
}
res.json(data);
});
router.get("/robots/:robot", load, function(req, res) {
res.json(req.robot.data());
res.json(req.robot);
});
router.get("/robots/:robot/commands", load, function(req, res) {
res.json(req.robot.data().commands);
res.json(req.robot.toJSON().commands);
});
router.all("/robots/:robot/commands/:command", load, function(req, res) {
@ -67,11 +67,11 @@ router.all("/robots/:robot/commands/:command", load, function(req, res) {
});
router.get("/robots/:robot/devices", load, function(req, res) {
res.json(req.robot.data().devices);
res.json(req.robot.toJSON().devices);
});
router.get("/robots/:robot/devices/:device", load, function(req, res) {
res.json(req.device.data());
res.json(req.device);
});
router.get("/robots/:robot/devices/:device/events/:event", load, function(req, res) {
@ -95,7 +95,7 @@ router.get("/robots/:robot/devices/:device/events/:event", load, function(req, r
});
router.get("/robots/:robot/devices/:device/commands", load, function(req, res) {
res.json(req.device.data().commands);
res.json(req.device.toJSON().commands);
});
router.all("/robots/:robot/devices/:device/commands/:command", load, function(req, res) {
@ -106,9 +106,9 @@ router.all("/robots/:robot/devices/:device/commands/:command", load, function(re
});
router.get("/robots/:robot/connections", load, function(req, res) {
res.json(req.robot.data().connections);
res.json(req.robot.toJSON().connections);
});
router.get("/robots/:robot/connections/:connection", load, function(req, res) {
res.json(req.connection.data());
res.json(req.connection);
});

View File

@ -52,10 +52,10 @@ module.exports = Connection = function Connection(opts) {
subclass(Connection, EventEmitter);
// Public: Exports basic data for the Connection
// Public: Expresses the Connection in JSON format
//
// Returns an Object containing Connection data
Connection.prototype.data = function() {
Connection.prototype.toJSON = function() {
return {
name: this.name,
port: this.port,

View File

@ -73,15 +73,15 @@ Device.prototype.halt = function() {
return this.driver.halt();
};
// Public: Exports basic data for the Connection
// Public: Expresses the Device in JSON format
//
// Returns an Object containing Connection data
Device.prototype.data = function() {
Device.prototype.toJSON = function() {
return {
name: this.name,
driver: this.driver.constructor.name || this.driver.name,
pin: this.pin,
connection: this.connection.data(),
connection: this.connection.toJSON(),
commands: this.driver.commands()
};
};

View File

@ -108,15 +108,15 @@ Robot.randomName = function() {
return "Robot " + (Math.floor(Math.random() * 100000));
};
// Public: Exports basic data for the Robot
// Public: Expresses the Robot in a JSON-serializable format
//
// Returns an Object containing Robot data
Robot.prototype.data = function() {
Robot.prototype.toJSON = function() {
var connections = (function() {
var results = [];
for (var n in this.connections) {
var conn = this.connections[n];
results.push(conn.data());
results.push(conn.toJSON());
}
return results;
}).call(this);
@ -125,7 +125,7 @@ Robot.prototype.data = function() {
var results = [];
for (var n in this.devices) {
var device = this.devices[n];
results.push(device.data());
results.push(device.toJSON());
}
return results;
}).call(this);

View File

@ -29,28 +29,28 @@ describe("Cylon.Connection", function() {
});
});
describe("#data", function() {
var data = connection.data();
describe("#toJSON", function() {
var json = connection.toJSON();
it("returns an object", function() {
expect(data).to.be.an('object');
expect(json).to.be.an('object');
});
it("contains the connection's name", function() {
expect(data.name).to.be.eql("loopback");
expect(json.name).to.be.eql("loopback");
});
it("contains the connection's port", function() {
expect(data.port).to.be.eql("/dev/null");
expect(json.port).to.be.eql("/dev/null");
});
it("contains the connection's adaptor name", function() {
expect(data.adaptor).to.be.eql("Loopback");
expect(json.adaptor).to.be.eql("Loopback");
});
it("contains the connection's ID", function() {
var id = connection.connection_id;
expect(data.connection_id).to.be.eql(id);
expect(json.connection_id).to.be.eql(id);
});
});

View File

@ -109,31 +109,31 @@ describe("Cylon.Device", function() {
});
});
describe("#data", function() {
var data = device.data();
describe("#toJSON", function() {
var json = device.toJSON();
it("returns an object", function() {
expect(data).to.be.a('object');
expect(json).to.be.a('object');
});
it("contains the device's name", function() {
expect(data.name).to.be.eql(device.name);
expect(json.name).to.be.eql(device.name);
});
it("contains the device's pin", function() {
expect(data.pin).to.be.eql(device.pin);
expect(json.pin).to.be.eql(device.pin);
});
it("contains the device's driver name", function() {
expect(data.driver).to.be.eql('Ping');
expect(json.driver).to.be.eql('Ping');
});
it("contains the device's connection data", function() {
expect(data.connection).to.be.eql(device.connection.data());
it("contains the device's connection json", function() {
expect(json.connection).to.be.eql(device.connection.toJSON());
});
it("contains the device's driver commands", function() {
expect(data.commands).to.be.eql(driver.commands());
expect(json.commands).to.be.eql(driver.commands());
});
});

View File

@ -88,34 +88,34 @@ describe("Robot", function() {
})
})
describe("#data", function() {
describe("#toJSON", function() {
var bot = new Robot({
connection: { name: 'loopback', adaptor: 'loopback' },
device: { name: 'ping', driver: 'ping' }
});
var data = bot.data();
var json = bot.toJSON();
it("returns an object", function() {
expect(data).to.be.a('object');
expect(json).to.be.a('object');
});
it("contains the robot's name", function() {
expect(data.name).to.eql(bot.name);
expect(json.name).to.eql(bot.name);
});
it("contains the robot's commands", function() {
expect(data.commands).to.eql(bot.commands);
expect(json.commands).to.eql(bot.commands);
});
it("contains the robot's devices", function() {
var deviceData = bot.devices.ping.data();
expect(data.devices).to.eql([deviceData]);
var deviceJSON = bot.devices.ping.toJSON();
expect(json.devices).to.eql([deviceJSON]);
});
it("contains the robot's connections", function() {
var connectionData = bot.connections.loopback.data();
expect(data.connections).to.eql([connectionData]);
var connectionJSON = bot.connections.loopback.toJSON();
expect(json.connections).to.eql([connectionJSON]);
});
});