data 与 json 转换

dataTransform.js

 1 const toString = Object.prototype.toString;
 2 const type = (val) => toString.call(val).slice(8, -1);
 3 const hash = () => Object.create(null);
 4 
 5 const requestTransformMain = (key, val) => {
 6     if ([undefined, null, NaN, Infinity, -Infinity].includes(val)) {
 7         const obj = hash();
 8         obj.type = 'Basic';
 9         obj.value = String(val);
10         return obj;
11     }
12     if (type(val) === 'Function') {
13         const obj = hash();
14         obj.type = 'Function';
15         obj.value = val.toString();
16         return obj;
17     }
18     if (type(val) === 'Set') {
19         const obj = hash();
20         obj.type = 'Set';
21         const value = (obj.value = {});
22         Array.from(val).forEach((_val, index) => {
23             value[`Set[${index}]=>`] = _val;
24         });
25         return obj;
26     }
27     if (type(val) === 'Map') {
28         const obj = hash();
29         obj.type = 'Map';
30         const value = (obj.value = {});
31         Array.from(val).forEach(([_key, _val], index) => {
32             value[`Map[${index}][key]=>`] = _key;
33             value[`Map[${index}][value]=>`] = _val;
34         });
35         return obj;
36     }
37     return val;
38 };
39 
40 const responseTransformMain = (val) => {
41     if (val.type === 'Basic') return eval(val.value);
42     if (val.type === 'Function') return eval(val.value);
43     if (val.type === 'Set') return new Set(Object.values(responseTransformMain(val.value)));
44     if (val.type === 'Map') {
45         const map = new Map(),
46             data = val.value,
47             arr = [];
48         for (const key in data) {
49             arr.push(responseTransformMain(data[key]));
50             if (arr.length % 2 === 0) {
51                 map.set(arr.shift(), arr.shift());
52             }
53         }
54         return map;
55     }
56     if (type(val) === 'Object') {
57         const obj = {},
58             data = val;
59         for (const key in data) {
60             obj[key] = responseTransformMain(data[key]);
61         }
62         return obj;
63     }
64     if (type(val) === 'Array') return val.map((item) => responseTransformMain(item));
65     return val;
66 };
67 
68 const requestTransform = (data) => JSON.stringify(data, requestTransformMain, '\t');
69 const responseTransform = (data) => responseTransformMain(JSON.parse(data));
70 
71 export { requestTransform, responseTransform };

index.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3     <head>
 4         <meta charset="UTF-8" />
 5         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 6         <title>Document</title>
 7         <style></style>
 8     </head>
 9     <body>
10         <div id="app"></div>
11         <script type="module">
12             import { requestTransform, responseTransform } from './dataTransform.js';
13             const data = {
14                 map: new Map([
15                     ['book', 'js'],
16                     [() => 1, () => 2],
17                 ]),
18                 set: new Set([1, undefined, null, NaN, () => 1]),
19             };
20             console.log(requestTransform(data));
21             console.log(responseTransform(requestTransform(data)));
22         </script>
23         <script></script>
24     </body>
25 </html>

 

posted @ 2024-01-26 02:48  万物有序  阅读(59)  评论(0编辑  收藏  举报