You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2018/10/04 18:53:59 UTC
[03/10] tinkerpop git commit: TINKERPOP-2055 Added special number
handling in javascript
TINKERPOP-2055 Added special number handling in javascript
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/2d3041f2
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/2d3041f2
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/2d3041f2
Branch: refs/heads/master
Commit: 2d3041f226310379c966214461c79cf47432f4c9
Parents: 854914e
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Oct 3 04:33:40 2018 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Oct 3 04:33:40 2018 -0400
----------------------------------------------------------------------
.../lib/structure/io/type-serializers.js | 30 ++++++++++++++-
.../test/unit/graphson-test.js | 39 ++++++++++++++++++++
2 files changed, 67 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2d3041f2/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
index fdf049f..5583c47 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/structure/io/type-serializers.js
@@ -49,11 +49,37 @@ class TypeSerializer {
class NumberSerializer extends TypeSerializer {
serialize(item) {
- return item;
+ if (isNaN(item)) {
+ return {
+ [typeKey]: 'g:Double',
+ [valueKey]: 'NaN'
+ };
+ } else if (item === Number.POSITIVE_INFINITY) {
+ return {
+ [typeKey]: 'g:Double',
+ [valueKey]: 'Infinity'
+ };
+ } else if (item === Number.NEGATIVE_INFINITY) {
+ return {
+ [typeKey]: 'g:Double',
+ [valueKey]: '-Infinity'
+ };
+ } else {
+ return item;
+ }
}
deserialize(obj) {
- return parseFloat(obj[valueKey]);
+ var val = obj[valueKey];
+ if (val === 'NaN') {
+ return NaN;
+ } else if (val === 'Infinity') {
+ return Number.POSITIVE_INFINITY;
+ } else if (val === '-Infinity') {
+ return Number.NEGATIVE_INFINITY;
+ } else {
+ return parseFloat(val);
+ }
}
canBeUsedFor(value) {
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2d3041f2/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
----------------------------------------------------------------------
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
index b459407..a1ac0d6 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/graphson-test.js
@@ -46,6 +46,30 @@ describe('GraphSONReader', function () {
assert.strictEqual(result, item[1]);
});
});
+ it('should parse GraphSON Nan from GraphSON', function () {
+ const reader = new GraphSONReader();
+ var result = reader.read({
+ "@type": "g:Double",
+ "@value": "NaN"
+ });
+ assert.ok(isNaN(result));
+ });
+ it('should parse GraphSON -Infinity and Nan from GraphSON', function () {
+ const reader = new GraphSONReader();
+ var result = reader.read({
+ "@type": "g:Double",
+ "@value": "-Infinity"
+ });
+ assert.strictEqual(result, Number.NEGATIVE_INFINITY);
+ });
+ it('should parse GraphSON Infinity and Nan from GraphSON', function () {
+ const reader = new GraphSONReader();
+ var result = reader.read({
+ "@type": "g:Double",
+ "@value": "Infinity"
+ });
+ assert.strictEqual(result, Number.POSITIVE_INFINITY);
+ });
it('should parse Date', function() {
const obj = { "@type" : "g:Date", "@value" : 1481750076295 };
const reader = new GraphSONReader();
@@ -102,6 +126,21 @@ describe('GraphSONWriter', function () {
const writer = new GraphSONWriter();
assert.strictEqual(writer.write(2), '2');
});
+ it('should write NaN', function () {
+ const writer = new GraphSONWriter();
+ const expected = JSON.stringify({ "@type" : "g:Double", "@value" : "NaN" });
+ assert.strictEqual(writer.write(NaN), expected);
+ });
+ it('should write Infinity', function () {
+ const writer = new GraphSONWriter();
+ const expected = JSON.stringify({ "@type" : "g:Double", "@value" : "Infinity" });
+ assert.strictEqual(writer.write(Number.POSITIVE_INFINITY), expected);
+ });
+ it('should write -Infinity', function () {
+ const writer = new GraphSONWriter();
+ const expected = JSON.stringify({ "@type" : "g:Double", "@value" : "-Infinity" });
+ assert.strictEqual(writer.write(Number.NEGATIVE_INFINITY), expected);
+ });
it('should write Date', function() {
const writer = new GraphSONWriter();
const expected = JSON.stringify({ "@type" : "g:Date", "@value" : 1481750076295 });