FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/examples/serialization.js at develop · lineCode/mathjs · GitHub
lineCode
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
examples
/
serialization.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
16 lines (14 loc) · 626 Bytes
Breadcrumbs
mathjs
/
examples
/
serialization.js
Copy path
File metadata and controls
16 lines (14 loc) · 626 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// load math.js (using node.js)
const
{
complex
,
replacer
,
reviver
,
typeOf
}
=
require
(
'..'
)
// serialize a math.js data type into a JSON string
// the replacer function is needed to correctly stringify a value like Infinity
const
x
=
complex
(
'2+3i'
)
const
str1
=
JSON
.
stringify
(
x
,
replacer
)
console
.
log
(
str1
)
// outputs {"mathjs":"Complex","re":2,"im":3}
// deserialize a JSON string into a math.js data type
// note that the reviver of math.js is needed for this:
const
str2
=
'{"mathjs":"Unit","value":5,"unit":"cm"}'
const
y
=
JSON
.
parse
(
str2
,
reviver
)
console
.
log
(
typeOf
(
y
)
)
// 'Unit'
console
.
log
(
y
.
toString
(
)
)
// 5 cm
Back
|
FazBrowse Home
|
New Git URL