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

Improved json serialization / deserialization #2774

Draft
wants to merge 17 commits into
base: develop
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions src/expression/node/AccessorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import { accessFactory } from './utils/access.js'
const name = 'AccessorNode'
const dependencies = [
'subset',
'fromJSON',
'Node'
]

export const createAccessorNode = /* #__PURE__ */ factory(name, dependencies, ({ subset, Node }) => {
export const createAccessorNode = /* #__PURE__ */ factory(name, dependencies, ({ subset, Node, fromJSON }) => {
const access = accessFactory({ subset })

/**
Expand Down Expand Up @@ -191,7 +192,7 @@ export const createAccessorNode = /* #__PURE__ */ factory(name, dependencies, ({
toJSON () {
return {
mathjs: name,
object: this.object,
object: this.object?.toJSON(),
index: this.index
}
}
Expand All @@ -205,7 +206,7 @@ export const createAccessorNode = /* #__PURE__ */ factory(name, dependencies, ({
* @returns {AccessorNode}
*/
static fromJSON (json) {
return new AccessorNode(json.object, json.index)
return new AccessorNode(fromJSON(json.object), json.index)
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/expression/node/ArrayNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { factory } from '../../utils/factory.js'

const name = 'ArrayNode'
const dependencies = [
'Node'
'Node',
'fromJSON'
]

export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ Node }) => {
export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ Node, fromJSON }) => {
class ArrayNode extends Node {
/**
* @constructor ArrayNode
Expand Down Expand Up @@ -117,7 +118,7 @@ export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ No
toJSON () {
return {
mathjs: name,
items: this.items
items: this.items?.map(i => i.toJSON())
}
}

Expand All @@ -129,7 +130,7 @@ export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* @returns {ArrayNode}
*/
static fromJSON (json) {
return new ArrayNode(json.items)
return new ArrayNode(json.items.map(fromJSON))
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/expression/node/AssignmentNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const name = 'AssignmentNode'
const dependencies = [
'subset',
'?matrix', // FIXME: should not be needed at all, should be handled by subset
'Node'
'Node',
'fromJSON'
]

export const createAssignmentNode = /* #__PURE__ */ factory(name, dependencies, ({ subset, matrix, Node }) => {
export const createAssignmentNode = /* #__PURE__ */ factory(name, dependencies, ({ subset, matrix, Node, fromJSON }) => {
const access = accessFactory({ subset })
const assign = assignFactory({ subset, matrix })

Expand Down Expand Up @@ -258,9 +259,9 @@ export const createAssignmentNode = /* #__PURE__ */ factory(name, dependencies,
toJSON () {
return {
mathjs: name,
object: this.object,
index: this.index,
value: this.value
object: this.object?.toJSON(),
index: this.index?.toJSON(),
value: this.value?.toJSON()
}
}

Expand All @@ -273,7 +274,7 @@ export const createAssignmentNode = /* #__PURE__ */ factory(name, dependencies,
* @returns {AssignmentNode}
*/
static fromJSON (json) {
return new AssignmentNode(json.object, json.index, json.value)
return new AssignmentNode(fromJSON(json.object), fromJSON(json.index), fromJSON(json.value))
}

/**
Expand Down
Loading