1 | // load math.js (using node.js)
|
2 | var math = require('../index');
|
3 |
|
4 | // serialize a math.js data type into a JSON string
|
5 | var x = math.complex('2+3i');
|
6 | var str1 = JSON.stringify(x);
|
7 | console.log(str1);
|
8 | // outputs {"mathjs":"Complex","re":2,"im":3}
|
9 |
|
10 | // deserialize a JSON string into a math.js data type
|
11 | // note that the reviver of math.js is needed for this:
|
12 | var str2 = '{"mathjs":"Unit","value":5,"unit":"cm"}';
|
13 | var y = JSON.parse(str2, math.json.reviver);
|
14 | console.log(math.typeof(y)); // 'Unit'
|
15 | console.log(y.toString()); // 5 cm
|