Skip to content

Commit bce18da

Browse files
authored
[JS] toJSON method is very wrong - it should return an object to be stringified (#380)
* toJSON method is very wrong - it should return an object to be stringified * don't call toJSON directly, JSON.stringify will do that
1 parent e1918fd commit bce18da

File tree

6 files changed

+68
-14
lines changed

6 files changed

+68
-14
lines changed

javascript/sbp/sbp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ SBP.prototype.toJSON = function toJSON () {
141141
dict[k] = this.fields[k];
142142
}.bind(this));
143143

144-
return JSON.stringify(dict);
144+
return dict;
145145
};
146146

147147
/**

javascript/test_http_port.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Test decoding bytes coming from an HTTP stream.
3+
*/
4+
5+
var path = require('path');
6+
var dispatch = require(path.resolve(__dirname, './sbp/')).dispatch;
7+
var request = require('request');
8+
9+
// Message construction
10+
var constructMsg = require(path.resolve(__dirname, './sbp/construct'));
11+
var sbpPiksi = require(path.resolve(__dirname, './sbp/piksi'));
12+
var MsgReset = sbpPiksi.MsgReset;
13+
14+
// Change this to a valid piksi serial port path.
15+
var serial = '/dev/cu.usbserial-00001014';
16+
17+
var piksi = request('http://localhost:7770');
18+
19+
console.log('Opened piksi.');
20+
21+
dispatch(piksi, function (err, framedMsg) {
22+
console.log('Message from piksi:', err, framedMsg.messageType, framedMsg);
23+
});
24+

javascript/test_serial_port.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var sbpPiksi = require(path.resolve(__dirname, './sbp/piksi'));
1212
var MsgReset = sbpPiksi.MsgReset;
1313

1414
// Change this to a valid piksi serial port path.
15-
var serial = '/dev/cu.usbserial-00002014';
15+
var serial = '/dev/cu.usbserial-00001014';
1616

1717
var piksi = new SerialPort(serial, {
1818
//baudrate: 115200 // serial or uart
@@ -25,14 +25,4 @@ piksi.on('open', function () {
2525
dispatch(piksi, function (err, framedMsg) {
2626
console.log('Message from piksi:', err, framedMsg.messageType);
2727
});
28-
29-
setTimeout(function () {
30-
console.log('Sending a MSG_RESET');
31-
32-
var msgBytes = constructMsg(MsgReset, {}).toBuffer();
33-
34-
piksi.write(msgBytes, function () {
35-
console.log('wrote bytes', msgBytes, arguments);
36-
});
37-
}, 1000);
3828
});

javascript/test_serial_port_reset.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* To run this, install node-serialport globally: `npm install -g serialport`
3+
*/
4+
5+
var SerialPort = require('serialport');
6+
var path = require('path');
7+
var dispatch = require(path.resolve(__dirname, './sbp/')).dispatch;
8+
9+
// Message construction
10+
var constructMsg = require(path.resolve(__dirname, './sbp/construct'));
11+
var sbpPiksi = require(path.resolve(__dirname, './sbp/piksi'));
12+
var MsgReset = sbpPiksi.MsgReset;
13+
14+
// Change this to a valid piksi serial port path.
15+
var serial = '/dev/cu.usbserial-00002014';
16+
17+
var piksi = new SerialPort(serial, {
18+
//baudrate: 115200 // serial or uart
19+
baudrate: 1000000 // USB
20+
});
21+
22+
piksi.on('open', function () {
23+
console.log('Opened piksi.');
24+
25+
dispatch(piksi, function (err, framedMsg) {
26+
console.log('Message from piksi:', err, framedMsg.messageType);
27+
});
28+
29+
setTimeout(function () {
30+
console.log('Sending a MSG_RESET');
31+
32+
var msgBytes = constructMsg(MsgReset, {}).toBuffer();
33+
34+
piksi.write(msgBytes, function () {
35+
console.log('wrote bytes', msgBytes, arguments);
36+
});
37+
}, 1000);
38+
});

javascript/tests/test_decode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('test packages based on YAML descriptors', function () {
4747
});
4848
it('should serialize back to JSON properly', function () {
4949
var msg = decodeMsg();
50-
assert.deepEqual(JSON.parse(msg.toJSON()), JSON.parse(testSpec['raw_json']));
50+
assert.deepEqual(JSON.parse(JSON.stringify(msg)), JSON.parse(testSpec['raw_json']));
5151
});
5252
it('should be identical to constructed message with identical fields', function () {
5353
var msg = decodeMsg();

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"js-yaml": "^3.4.3"
1818
},
1919
"devDependencies": {
20-
"mocha": "^2.4.5"
20+
"mocha": "^2.4.5",
21+
"request": "^2.74.0",
22+
"serialport": "^4.0.1"
2123
}
2224
}

0 commit comments

Comments
 (0)