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

How to deal with Infinity in serialization? #1834

Closed
skgadi opened this issue Apr 30, 2020 · 5 comments
Closed

How to deal with Infinity in serialization? #1834

skgadi opened this issue Apr 30, 2020 · 5 comments

Comments

@skgadi
Copy link

skgadi commented Apr 30, 2020

Hi,
The reviver option in JSON.parse is very helpful. But, I am not sure how to deal with Infinity. At present, I am loosing its information. Here is an example:

var a="[1,2i,1/0]";
var b=math.evaluate(a);
var c = JSON.stringify(b);
var d = JSON.parse(c,  math.json.reviver);

By the time I reach last step, the variable d is losing its Infinity value.

How can I fix this problem?

Thank you for your time.

Originally posted by @skgadi in #27 (comment)

@josdejong
Copy link
Owner

Thanks for reporting Suresh, this is a very good point.

So basically, the serialization should somehow serialize these special numeric values, so they can be revived correctly:

const aOriginal = Infinity
const stringified = JSON.stringify(aOriginal) // is '"null"'
const aRevived = JSON.parse(stringified, math.json.reviver)// is null, should be Infinity
assert.strictEqual(aOriginal, aRevived) // is false, should be true

The difficulty is that this is behavior of the JavaScript number type and the built-in stringifier JSON.stringify for which we want different behavior.

A possible solution would be to implement a JSON replace function, which turns numbers with a special value like Infinity into a special stringified object. Something like:

const aOriginal = Infinity
const stringified = JSON.stringify(aOriginal, math.json.replacer) 
    // '{"mathjs":"number","value":"Infinity"}'
const aRevived = JSON.parse(stringified, math.json.reviver)
assert.strictEqual(aOriginal, aRevived) // true

@josdejong
Copy link
Owner

I created a fix in 28b7a02 (not yet published)

@skgadi
Copy link
Author

skgadi commented May 3, 2020

Thank you for resolving it real quick.
I will wait for the next release to include it in my project.

@josdejong
Copy link
Owner

Fixed now in v6.6.5

@skgadi
Copy link
Author

skgadi commented May 4, 2020

Thank you. 👍

josdejong added a commit that referenced this issue May 6, 2020
* Update devDependencies

* Fixed `eigs` not using `config.epsilon` (see #1789)

* Should be able to take NAMED_DELIMITER as object keys (#1798)

Co-authored-by: Jos de Jong <[email protected]>

* Update history

* Clarify the documentation on `scope` when using `rawArgs`, see #1055

* Bump @babel/preset-env from 7.9.0 to 7.9.5 (#1810)

Bumps [@babel/preset-env](https://github.com/babel/babel) from 7.9.0 to 7.9.5.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/master/CHANGELOG.md)
- [Commits](babel/babel@v7.9.0...v7.9.5)

Signed-off-by: dependabot-preview[bot] <[email protected]>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Prefix the cli test with 'node' so it works on windows (#1807)

* Prefix the cli test with 'node' so it works on windows

Shouldnt provide any issues for other systems I hope

* Revert "Prefix the cli test with 'node' so it works on windows"

This reverts commit 4cd2704.

* Revert "Revert "Prefix the cli test with 'node' so it works on windows""

This reverts commit 268b594.

Co-authored-by: Jos de Jong <[email protected]>

* Fix #1808: `toNumber()` not working on a unitless unit (#1811)

* Update history

* Fix #1813: bug in engineering notation for numbers of function `format`, sometimes resulting in needless trailing zeros

* Improve explanation of engineering notation in function format

* Publish v6.6.3

* Bump uglify-js from 3.8.1 to 3.9.0 (#1816)

Bumps [uglify-js](https://github.com/mishoo/UglifyJS2) from 3.8.1 to 3.9.0.
- [Release notes](https://github.com/mishoo/UglifyJS2/releases)
- [Commits](mishoo/UglifyJS@v3.8.1...v3.9.0)

Signed-off-by: dependabot-preview[bot] <[email protected]>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Publish v6.6.4

* Bump uglify-js from 3.9.0 to 3.9.1 (#1818)

Bumps [uglify-js](https://github.com/mishoo/UglifyJS2) from 3.9.0 to 3.9.1.
- [Release notes](https://github.com/mishoo/UglifyJS2/releases)
- [Commits](mishoo/UglifyJS@v3.9.0...v3.9.1)

Signed-off-by: dependabot-preview[bot] <[email protected]>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Bump karma from 4.4.1 to 5.0.3 (#1830)

Bumps [karma](https://github.com/karma-runner/karma) from 4.4.1 to 5.0.3.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](karma-runner/karma@v4.4.1...v5.0.3)

Signed-off-by: dependabot-preview[bot] <[email protected]>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Bump karma-mocha from 1.3.0 to 2.0.1 (#1832)

* Bump karma from 5.0.3 to 5.0.4 (#1839)

Bumps [karma](https://github.com/karma-runner/karma) from 5.0.3 to 5.0.4.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](karma-runner/karma@v5.0.3...v5.0.4)

Signed-off-by: dependabot-preview[bot] <[email protected]>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Bump mocha from 7.1.1 to 7.1.2 (#1827)

* Update devDependencies

* Fix #1834: value `Infinity` cannot be serialized and deserialized

* Fix broken bundle tests and linting issues

* Fix unit test on node 8 (yeah, I know)

* Fix #1842: value `Infinity` not turned into the latex symbol `\\infty`

* Publish v6.6.5

* Fix fixer.io example, the free plan doesn't support SSL

* Change node versions

Co-authored-by: jos <[email protected]>
Co-authored-by: Veeloxfire <[email protected]>
Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants