josdejong / josdejong/mathjs

toObject and fromObject

Open
#1,679 7 comments 1 reaction 0 assignees View on GitHub
feature help wanted
Dominant language
JavaScript
Stars
15.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

I can serialize and de-serialize an expression to JSON as shown here: https://mathjs.org/docs/core/serialization.html

In my application the expression is part of a bigger JSON which is de-serialized at a higher level. I would need to de-serialize an expression from an object like this:
```js
const node = math.parse('age >= 21 ? 999 + 5 : 111 + 2')
const objectExpression = node.toObject();
const node2 = math.fromObject(objectExpression)
```

Of course I could do something like this but it's not optimal because I would have to serialize the object to JSON first:
```js
const node = math.parse('age >= 21 ? 999 + 5 : 111 + 2')
const json = JSON.stringify(node);
const expression = JSON.parse(json, math.revive);
```

Is there a way to do this without calling the revive function recursively? I tried to use [map-keys-deep](https://github.com/Kikobeats/map-keys-deep) but the reviver gets called for a parent leaf and then for the child and it seems that the order need to be reversed to use the revive function.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.