diff --git a/src/com/cognitect/transit/handlers.js b/src/com/cognitect/transit/handlers.js index a67beb9..9d55690 100644 --- a/src/com/cognitect/transit/handlers.js +++ b/src/com/cognitect/transit/handlers.js @@ -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; diff --git a/src/com/cognitect/transit/impl/writer.js b/src/com/cognitect/transit/impl/writer.js index f2ad33a..4daa3ae 100644 --- a/src/com/cognitect/transit/impl/writer.js +++ b/src/com/cognitect/transit/impl/writer.js @@ -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; } }; diff --git a/test/tests.js b/test/tests.js index 5842f52..1f3e54f 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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 // ============================================================================= @@ -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(); }; \ No newline at end of file