说明
入门
入门
例子
文档
核心
配置
链式调用
扩展
序列化
表达式
解析和评估
句法
表达式树
代数
定制
安全
数据类型
数字
大数字
分数
复数
矩阵
单位
参考
类
方法
常量
定制捆绑
命令行界面
历史
序列化 - Math.js文档 - 笔下光年
网站首页
序列化
Math.js has a number of data types like `Matrix`, `Complex`, and `Unit`. These types are instantiated JavaScript objects. To be able to store these data types or send them between processes, they must be serialized. The data types of math.js can be serialized to JSON. Use cases: - Store data in a database or on disk. - Interchange of data between a server and a client. - Interchange of data between a web worker and the browser. Math.js types can be serialized using JavaScript’s built-in `JSON.stringify` function: ```javascript const x = math.complex('2 + 3i') const str = JSON.stringify(x, math.replacer) console.log(str) // outputs a string '{"mathjs":"Complex","re":2,"im":3}' ``` > IMPORTANT: in most cases works, serialization correctly without passing the `math.replacer` function as second argument. This is because in most cases we can rely on the default behavior of JSON.stringify, which uses the `.toJSON` method on classes like `Unit` and `Complex` to correctly serialize them. However, there are a few special cases like the number `Infinity` which does require the replacer function in order to be serialized without losing information: without it, `Infinity` will be serialized as `"null"` and cannot be deserialized correctly. > So, it’s best to always pass the `math.replacer` function to prevent weird edge cases. In order to deserialize a string, containing math.js data types, `JSON.parse` can be used. In order to recognize the data types of math.js, `JSON.parse` must be called with the reviver function of math.js: ```javascript const json = '{"mathjs":"Unit","value":5,"unit":"cm","fixPrefix":false}' const x = JSON.parse(json, math.reviver) // Unit 5 cm ``` Note that if math.js is used in conjunction with other data types, it is possible to use multiple reviver functions at the same time by cascading them: ```javascript const reviver = function (key, value) { return reviver1(key, reviver2(key, value)) } ```
上一篇:
扩展
下一篇:
表达式