Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix float keys being encoded as ints #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/com/cognitect/transit/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ goog.scope(function () {
handlers.NumberHandler = function Transit$NumberHandler() {
};
handlers.NumberHandler.prototype.tag = function (v) {
return "i";
if (isNaN(v) || v === Infinity || v === -Infinity || v % 1 === 0) {
return "i";
} else {
return "d";
}
};
handlers.NumberHandler.prototype.rep = function (v) {
return v;
Expand Down
6 changes: 3 additions & 3 deletions src/com/cognitect/transit/impl/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ goog.scope(function () {
}
};

writer.JSONMarshaller.prototype.emitDouble = function (d, asMapKey, cache) {
writer.JSONMarshaller.prototype.emitDouble = function (n, asMapKey, cache) {
if (asMapKey) {
return this.emitString(d.ESC, "d", d, asMapKey, cache);
return this.emitString(d.ESC, "d", n, asMapKey, cache);
} else {
return d;
return n;
}
};

Expand Down
22 changes: 21 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,26 @@ exports.testWriteSpecialDoubleValues = function(test) {
test.done();
};

// =============================================================================
// Double values as map keys
// =============================================================================

exports.testReadDoubleValuesAsMapKeys = function(test) {
var r = transit.reader();

test.ok(transit.equals(r.read("[\"^ \",\"~d1.1\",1.1]"), transit.map([1.1, 1.1])));

test.done();
};

exports.testWriteDoubleValuesAsMapKeys = function(test) {
var w = transit.writer();

test.equal(w.write(transit.map([1.1, 1.1])), "[\"^ \",\"~d1.1\",1.1]");

test.done();
};

// =============================================================================
// Map Not Found
// =============================================================================
Expand Down Expand Up @@ -1291,7 +1311,7 @@ exports.testBadCache = function(test) {
w = t.writer("json"),
r = t.reader("json");

transit.equals(pathological, r.read(w.write(pathological)));
test.ok(transit.equals(pathological, r.read(w.write(pathological))));

test.done();
};