@lowerdeck/serialize 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +11 -0
- package/.turbo/turbo-test.log +22 -0
- package/README.md +48 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.modern.js +2 -0
- package/dist/index.modern.js.map +1 -0
- package/dist/index.module.js +2 -0
- package/dist/index.module.js.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/json.d.ts +5 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.test.d.ts +2 -0
- package/dist/json.test.d.ts.map +1 -0
- package/package.json +33 -0
- package/src/index.ts +1 -0
- package/src/json.test.ts +30 -0
- package/src/json.ts +38 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
[0m[2m[35m$[0m [2m[1mmicrobundle[0m
|
|
3
|
+
[34mBuild "@lowerdeck/serialize" to dist:[39m
|
|
4
|
+
[32m556 B[39m: [37mindex.cjs[39m.gz
|
|
5
|
+
[32m475 B[39m: [37mindex.cjs[39m.br
|
|
6
|
+
[32m531 B[39m: [37mindex.modern.js[39m.gz
|
|
7
|
+
[32m453 B[39m: [37mindex.modern.js[39m.br
|
|
8
|
+
[32m520 B[39m: [37mindex.module.js[39m.gz
|
|
9
|
+
[32m440 B[39m: [37mindex.module.js[39m.br
|
|
10
|
+
[32m635 B[39m: [37mindex.umd.js[39m.gz
|
|
11
|
+
[32m545 B[39m: [37mindex.umd.js[39m.br
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
[0m[2m[35m$[0m [2m[1mvitest run --passWithNoTests[0m
|
|
3
|
+
[?25l
|
|
4
|
+
[1m[46m RUN [49m[22m [36mv3.2.4 [39m[90m/Users/tobias/code/metorial/metorial-enterprise/oss/src/packages/backend/serialize[39m
|
|
5
|
+
|
|
6
|
+
[?2026h
|
|
7
|
+
[1m[33m ❯ [39m[22msrc/json.test.ts[2m [queued][22m
|
|
8
|
+
|
|
9
|
+
[2m Test Files [22m[1m[32m0 passed[39m[22m[90m (1)[39m
|
|
10
|
+
[2m Tests [22m[1m[32m0 passed[39m[22m[90m (0)[39m
|
|
11
|
+
[2m Start at [22m10:23:32
|
|
12
|
+
[2m Duration [22m101ms
|
|
13
|
+
[?2026l[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K [32m✓[39m src/json.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 2[2mms[22m[39m
|
|
14
|
+
[32m✓[39m serialize[2m > [22mencode should return encoded data[32m 1[2mms[22m[39m
|
|
15
|
+
[32m✓[39m serialize[2m > [22mdecode should return decoded data[32m 0[2mms[22m[39m
|
|
16
|
+
|
|
17
|
+
[2m Test Files [22m [1m[32m1 passed[39m[22m[90m (1)[39m
|
|
18
|
+
[2m Tests [22m [1m[32m2 passed[39m[22m[90m (2)[39m
|
|
19
|
+
[2m Start at [22m 10:23:32
|
|
20
|
+
[2m Duration [22m 222ms[2m (transform 30ms, setup 0ms, collect 32ms, tests 2ms, environment 0ms, prepare 39ms)[22m
|
|
21
|
+
|
|
22
|
+
[?25h
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# `@lowerdeck/serialize`
|
|
2
|
+
|
|
3
|
+
Enhanced serialization using SuperJSON to handle complex types that standard JSON cannot encode. Preserves Maps, Sets, Dates, and other non-standard JSON types.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lowerdeck/serialize
|
|
9
|
+
yarn add @lowerdeck/serialize
|
|
10
|
+
bun add @lowerdeck/serialize
|
|
11
|
+
pnpm add @lowerdeck/serialize
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { serialize } from '@lowerdeck/serialize';
|
|
18
|
+
|
|
19
|
+
// Encode complex data types
|
|
20
|
+
const data = {
|
|
21
|
+
date: new Date('2024-01-01'),
|
|
22
|
+
map: new Map([['key', 'value']]),
|
|
23
|
+
set: new Set([1, 2, 3]),
|
|
24
|
+
undefined: undefined,
|
|
25
|
+
bigint: 123n
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const encoded = serialize.encode(data);
|
|
29
|
+
console.log(encoded); // JSON string with type metadata
|
|
30
|
+
|
|
31
|
+
// Decode back to original types
|
|
32
|
+
const decoded = serialize.decode(encoded);
|
|
33
|
+
console.log(decoded.date instanceof Date); // true
|
|
34
|
+
console.log(decoded.map instanceof Map); // true
|
|
35
|
+
console.log(decoded.set instanceof Set); // true
|
|
36
|
+
|
|
37
|
+
// Perfect for storing in localStorage or databases
|
|
38
|
+
localStorage.setItem('data', serialize.encode(data));
|
|
39
|
+
const restored = serialize.decode(localStorage.getItem('data'));
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
This project is licensed under the Apache License 2.0.
|
|
45
|
+
|
|
46
|
+
<div align="center">
|
|
47
|
+
<sub>Built with ❤️ by <a href="https://metorial.com">Metorial</a></sub>
|
|
48
|
+
</div>
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/e(require("superjson"));function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},r.apply(null,arguments)}function n(e,t){if(null==e)return{};var r={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(-1!==t.indexOf(n))continue;r[n]=e[n]}return r}var o=["$$TYPES$$"],a=["__mode"];exports.serialize={encode:function(e){var n,o=t.default.serialize(e);return null!=(n=o.meta)&&n.referentialEqualities&&delete o.meta.referentialEqualities,JSON.stringify(r({$$TYPES$$:r({__mode:"object"==typeof o.json?"object":"value"},o.meta),data:"object"!=typeof o.json?o.json:void 0},"object"==typeof o.json?o.json:{}))},decode:function(e){if("string"==typeof e&&(e=JSON.parse(e)),"object"!=typeof e)return e;var r=e.$$TYPES$$,i=n(e,o);if("object"!=typeof r||!r.__mode)return e;var f=r.__mode,u=n(r,a);return t.default.deserialize({json:"value"==f?i.data:i,meta:u})}};
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/json.ts"],"sourcesContent":["// @ts-ignore\nimport SuperJSON from 'superjson';\n\nexport let serialize = {\n encode: (data: any) => {\n let sup = SuperJSON.serialize(data);\n\n if (sup.meta?.referentialEqualities) {\n delete sup.meta.referentialEqualities;\n }\n\n return JSON.stringify({\n $$TYPES$$: {\n __mode: typeof sup.json == 'object' ? 'object' : 'value',\n ...sup.meta\n },\n\n data: typeof sup.json != 'object' ? sup.json : undefined,\n\n ...(typeof sup.json == 'object' ? sup.json : {})\n });\n },\n\n decode: (data: any) => {\n if (typeof data == 'string') data = JSON.parse(data);\n\n if (typeof data != 'object') return data;\n\n let { $$TYPES$$, ...rest } = data;\n\n if (typeof $$TYPES$$ != 'object' || !$$TYPES$$.__mode) return data;\n\n let { __mode, ...meta } = $$TYPES$$;\n let json = __mode == 'value' ? rest.data : rest;\n\n return SuperJSON.deserialize({ json, meta });\n }\n};\n"],"names":["encode","data","_sup$meta","sup","SuperJSON","serialize","meta","referentialEqualities","JSON","stringify","_extends","$$TYPES$$","__mode","json","undefined","decode","parse","rest","_objectWithoutPropertiesLoose","_excluded","_excluded2","deserialize"],"mappings":"2gBAGuB,CACrBA,OAAQ,SAACC,GAAa,IAAAC,EAChBC,EAAMC,EAAS,QAACC,UAAUJ,GAM9B,OAJIC,OAAJA,EAAIC,EAAIG,OAAJJ,EAAUK,8BACLJ,EAAIG,KAAKC,sBAGXC,KAAKC,UAASC,EAAA,CACnBC,UAASD,EAAA,CACPE,OAA2B,iBAAZT,EAAIU,KAAmB,SAAW,SAC9CV,EAAIG,MAGTL,KAAyB,iBAAZE,EAAIU,KAAmBV,EAAIU,UAAOC,GAExB,iBAAZX,EAAIU,KAAmBV,EAAIU,KAAO,CAAA,GAEjD,EAEAE,OAAQ,SAACd,GAGP,GAFmB,iBAARA,IAAkBA,EAAOO,KAAKQ,MAAMf,IAE5B,iBAARA,EAAkB,OAAOA,EAEpC,IAAMU,EAAuBV,EAAvBU,UAAcM,EAAIC,EAAKjB,EAALkB,GAExB,GAAwB,iBAAbR,IAA0BA,EAAUC,OAAQ,OAAOX,EAE9D,IAAMW,EAAoBD,EAApBC,OAAWN,EAAIY,EAAKP,EAASS,GAGnC,OAAOhB,EAAAA,QAAUiB,YAAY,CAAER,KAFV,SAAVD,EAAoBK,EAAKhB,KAAOgB,EAENX,KAAAA,GACvC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import e from"superjson";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},t.apply(null,arguments)}function r(e,t){if(null==e)return{};var r={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(-1!==t.indexOf(n))continue;r[n]=e[n]}return r}const n=["$$TYPES$$"],o=["__mode"];let a={encode:r=>{var n;let o=e.serialize(r);return null!=(n=o.meta)&&n.referentialEqualities&&delete o.meta.referentialEqualities,JSON.stringify(t({$$TYPES$$:t({__mode:"object"==typeof o.json?"object":"value"},o.meta),data:"object"!=typeof o.json?o.json:void 0},"object"==typeof o.json?o.json:{}))},decode:t=>{if("string"==typeof t&&(t=JSON.parse(t)),"object"!=typeof t)return t;let{$$TYPES$$:a}=t,i=r(t,n);if("object"!=typeof a||!a.__mode)return t;let{__mode:l}=a,f=r(a,o);return e.deserialize({json:"value"==l?i.data:i,meta:f})}};export{a as serialize};
|
|
2
|
+
//# sourceMappingURL=index.modern.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.modern.js","sources":["../src/json.ts"],"sourcesContent":["// @ts-ignore\nimport SuperJSON from 'superjson';\n\nexport let serialize = {\n encode: (data: any) => {\n let sup = SuperJSON.serialize(data);\n\n if (sup.meta?.referentialEqualities) {\n delete sup.meta.referentialEqualities;\n }\n\n return JSON.stringify({\n $$TYPES$$: {\n __mode: typeof sup.json == 'object' ? 'object' : 'value',\n ...sup.meta\n },\n\n data: typeof sup.json != 'object' ? sup.json : undefined,\n\n ...(typeof sup.json == 'object' ? sup.json : {})\n });\n },\n\n decode: (data: any) => {\n if (typeof data == 'string') data = JSON.parse(data);\n\n if (typeof data != 'object') return data;\n\n let { $$TYPES$$, ...rest } = data;\n\n if (typeof $$TYPES$$ != 'object' || !$$TYPES$$.__mode) return data;\n\n let { __mode, ...meta } = $$TYPES$$;\n let json = __mode == 'value' ? rest.data : rest;\n\n return SuperJSON.deserialize({ json, meta });\n }\n};\n"],"names":["serialize","encode","data","_sup$meta","sup","SuperJSON","meta","referentialEqualities","JSON","stringify","_extends","$$TYPES$$","__mode","json","undefined","decode","parse","rest","_objectWithoutPropertiesLoose","_excluded","_excluded2","deserialize"],"mappings":"iaAGW,IAAAA,EAAY,CACrBC,OAASC,IAAa,IAAAC,EACpB,IAAIC,EAAMC,EAAUL,UAAUE,GAM9B,OAJIC,OAAJA,EAAIC,EAAIE,OAAJH,EAAUI,8BACLH,EAAIE,KAAKC,sBAGXC,KAAKC,UAASC,EACnBC,CAAAA,UAASD,GACPE,OAA2B,iBAAZR,EAAIS,KAAmB,SAAW,SAC9CT,EAAIE,MAGTJ,KAAyB,iBAAZE,EAAIS,KAAmBT,EAAIS,UAAOC,GAExB,iBAAZV,EAAIS,KAAmBT,EAAIS,KAAO,CAAA,KAIjDE,OAASb,IAGP,GAFmB,iBAARA,IAAkBA,EAAOM,KAAKQ,MAAMd,IAE5B,iBAARA,EAAkB,OAAOA,EAEpC,IAAIS,UAAEA,GAAuBT,EAATe,EAAIC,EAAKhB,EAAIiB,GAEjC,GAAwB,iBAAbR,IAA0BA,EAAUC,OAAQ,OAAOV,EAE9D,IAAIU,OAAEA,GAAoBD,EAATL,EAAIY,EAAKP,EAASS,GAGnC,OAAOf,EAAUgB,YAAY,CAAER,KAFV,SAAVD,EAAoBK,EAAKf,KAAOe,EAENX"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import e from"superjson";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},t.apply(null,arguments)}function r(e,t){if(null==e)return{};var r={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(-1!==t.indexOf(n))continue;r[n]=e[n]}return r}var n=["$$TYPES$$"],o=["__mode"],a={encode:function(r){var n,o=e.serialize(r);return null!=(n=o.meta)&&n.referentialEqualities&&delete o.meta.referentialEqualities,JSON.stringify(t({$$TYPES$$:t({__mode:"object"==typeof o.json?"object":"value"},o.meta),data:"object"!=typeof o.json?o.json:void 0},"object"==typeof o.json?o.json:{}))},decode:function(t){if("string"==typeof t&&(t=JSON.parse(t)),"object"!=typeof t)return t;var a=t.$$TYPES$$,i=r(t,n);if("object"!=typeof a||!a.__mode)return t;var f=a.__mode,u=r(a,o);return e.deserialize({json:"value"==f?i.data:i,meta:u})}};export{a as serialize};
|
|
2
|
+
//# sourceMappingURL=index.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.module.js","sources":["../src/json.ts"],"sourcesContent":["// @ts-ignore\nimport SuperJSON from 'superjson';\n\nexport let serialize = {\n encode: (data: any) => {\n let sup = SuperJSON.serialize(data);\n\n if (sup.meta?.referentialEqualities) {\n delete sup.meta.referentialEqualities;\n }\n\n return JSON.stringify({\n $$TYPES$$: {\n __mode: typeof sup.json == 'object' ? 'object' : 'value',\n ...sup.meta\n },\n\n data: typeof sup.json != 'object' ? sup.json : undefined,\n\n ...(typeof sup.json == 'object' ? sup.json : {})\n });\n },\n\n decode: (data: any) => {\n if (typeof data == 'string') data = JSON.parse(data);\n\n if (typeof data != 'object') return data;\n\n let { $$TYPES$$, ...rest } = data;\n\n if (typeof $$TYPES$$ != 'object' || !$$TYPES$$.__mode) return data;\n\n let { __mode, ...meta } = $$TYPES$$;\n let json = __mode == 'value' ? rest.data : rest;\n\n return SuperJSON.deserialize({ json, meta });\n }\n};\n"],"names":["serialize","encode","data","_sup$meta","sup","SuperJSON","meta","referentialEqualities","JSON","stringify","_extends","$$TYPES$$","__mode","json","undefined","decode","parse","rest","_objectWithoutPropertiesLoose","_excluded","_excluded2","deserialize"],"mappings":"+ZAGWA,EAAY,CACrBC,OAAQ,SAACC,GAAa,IAAAC,EAChBC,EAAMC,EAAUL,UAAUE,GAM9B,OAJIC,OAAJA,EAAIC,EAAIE,OAAJH,EAAUI,8BACLH,EAAIE,KAAKC,sBAGXC,KAAKC,UAASC,EAAA,CACnBC,UAASD,EAAA,CACPE,OAA2B,iBAAZR,EAAIS,KAAmB,SAAW,SAC9CT,EAAIE,MAGTJ,KAAyB,iBAAZE,EAAIS,KAAmBT,EAAIS,UAAOC,GAExB,iBAAZV,EAAIS,KAAmBT,EAAIS,KAAO,CAAA,GAEjD,EAEAE,OAAQ,SAACb,GAGP,GAFmB,iBAARA,IAAkBA,EAAOM,KAAKQ,MAAMd,IAE5B,iBAARA,EAAkB,OAAOA,EAEpC,IAAMS,EAAuBT,EAAvBS,UAAcM,EAAIC,EAAKhB,EAALiB,GAExB,GAAwB,iBAAbR,IAA0BA,EAAUC,OAAQ,OAAOV,EAE9D,IAAMU,EAAoBD,EAApBC,OAAWN,EAAIY,EAAKP,EAASS,GAGnC,OAAOf,EAAUgB,YAAY,CAAER,KAFV,SAAVD,EAAoBK,EAAKf,KAAOe,EAENX,KAAAA,GACvC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("superjson")):"function"==typeof define&&define.amd?define(["exports","superjson"],t):t((e||self).serialize={},e.superjson)}(this,function(e,t){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=/*#__PURE__*/n(t);function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)({}).hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},r.apply(null,arguments)}function i(e,t){if(null==e)return{};var n={};for(var o in e)if({}.hasOwnProperty.call(e,o)){if(-1!==t.indexOf(o))continue;n[o]=e[o]}return n}var a=["$$TYPES$$"],f=["__mode"];e.serialize={encode:function(e){var t,n=o.default.serialize(e);return null!=(t=n.meta)&&t.referentialEqualities&&delete n.meta.referentialEqualities,JSON.stringify(r({$$TYPES$$:r({__mode:"object"==typeof n.json?"object":"value"},n.meta),data:"object"!=typeof n.json?n.json:void 0},"object"==typeof n.json?n.json:{}))},decode:function(e){if("string"==typeof e&&(e=JSON.parse(e)),"object"!=typeof e)return e;var t=e.$$TYPES$$,n=i(e,a);if("object"!=typeof t||!t.__mode)return e;var r=t.__mode,u=i(t,f);return o.default.deserialize({json:"value"==r?n.data:n,meta:u})}}});
|
|
2
|
+
//# sourceMappingURL=index.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/json.ts"],"sourcesContent":["// @ts-ignore\nimport SuperJSON from 'superjson';\n\nexport let serialize = {\n encode: (data: any) => {\n let sup = SuperJSON.serialize(data);\n\n if (sup.meta?.referentialEqualities) {\n delete sup.meta.referentialEqualities;\n }\n\n return JSON.stringify({\n $$TYPES$$: {\n __mode: typeof sup.json == 'object' ? 'object' : 'value',\n ...sup.meta\n },\n\n data: typeof sup.json != 'object' ? sup.json : undefined,\n\n ...(typeof sup.json == 'object' ? sup.json : {})\n });\n },\n\n decode: (data: any) => {\n if (typeof data == 'string') data = JSON.parse(data);\n\n if (typeof data != 'object') return data;\n\n let { $$TYPES$$, ...rest } = data;\n\n if (typeof $$TYPES$$ != 'object' || !$$TYPES$$.__mode) return data;\n\n let { __mode, ...meta } = $$TYPES$$;\n let json = __mode == 'value' ? rest.data : rest;\n\n return SuperJSON.deserialize({ json, meta });\n }\n};\n"],"names":["encode","data","_sup$meta","sup","SuperJSON","serialize","meta","referentialEqualities","JSON","stringify","_extends","$$TYPES$$","__mode","json","undefined","decode","parse","rest","_objectWithoutPropertiesLoose","_excluded","_excluded2","deserialize"],"mappings":"owBAGuB,CACrBA,OAAQ,SAACC,GAAa,IAAAC,EAChBC,EAAMC,EAAS,QAACC,UAAUJ,GAM9B,OAJIC,OAAJA,EAAIC,EAAIG,OAAJJ,EAAUK,8BACLJ,EAAIG,KAAKC,sBAGXC,KAAKC,UAASC,EAAA,CACnBC,UAASD,EAAA,CACPE,OAA2B,iBAAZT,EAAIU,KAAmB,SAAW,SAC9CV,EAAIG,MAGTL,KAAyB,iBAAZE,EAAIU,KAAmBV,EAAIU,UAAOC,GAExB,iBAAZX,EAAIU,KAAmBV,EAAIU,KAAO,CAAA,GAEjD,EAEAE,OAAQ,SAACd,GAGP,GAFmB,iBAARA,IAAkBA,EAAOO,KAAKQ,MAAMf,IAE5B,iBAARA,EAAkB,OAAOA,EAEpC,IAAMU,EAAuBV,EAAvBU,UAAcM,EAAIC,EAAKjB,EAALkB,GAExB,GAAwB,iBAAbR,IAA0BA,EAAUC,OAAQ,OAAOX,EAE9D,IAAMW,EAAoBD,EAApBC,OAAWN,EAAIY,EAAKP,EAASS,GAGnC,OAAOhB,EAAAA,QAAUiB,YAAY,CAAER,KAFV,SAAVD,EAAoBK,EAAKhB,KAAOgB,EAENX,KAAAA,GACvC"}
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAGA,eAAO,IAAI,SAAS;mBACH,GAAG;mBAmBH,GAAG;CAcnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.test.d.ts","sourceRoot":"","sources":["../src/json.test.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lowerdeck/serialize",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"author": "Tobias Herber",
|
|
8
|
+
"license": "Apache 2",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"source": "src/index.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"default": "./dist/index.modern.js"
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.cjs",
|
|
16
|
+
"module": "./dist/index.module.js",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"unpkg": "./dist/index.umd.js",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "vitest run --passWithNoTests",
|
|
21
|
+
"lint": "prettier src/**/*.ts --check",
|
|
22
|
+
"build": "microbundle"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"superjson": "^2.2.5"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"microbundle": "^0.15.1",
|
|
29
|
+
"@lowerdeck/tsconfig": "^1.0.0",
|
|
30
|
+
"typescript": "5.8.2",
|
|
31
|
+
"vitest": "^3.1.2"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './json';
|
package/src/json.test.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { serialize } from './json';
|
|
3
|
+
|
|
4
|
+
describe('serialize', () => {
|
|
5
|
+
test('encode should return encoded data', () => {
|
|
6
|
+
let data = { foo: 'bar' };
|
|
7
|
+
let result = serialize.encode(data);
|
|
8
|
+
|
|
9
|
+
expect(JSON.parse(result)).toEqual({
|
|
10
|
+
$$TYPES$$: {
|
|
11
|
+
__mode: 'object'
|
|
12
|
+
},
|
|
13
|
+
value: undefined,
|
|
14
|
+
foo: 'bar'
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('decode should return decoded data', () => {
|
|
19
|
+
let data = {
|
|
20
|
+
$$TYPES$$: {
|
|
21
|
+
__mode: 'object'
|
|
22
|
+
},
|
|
23
|
+
value: undefined,
|
|
24
|
+
foo: 'bar'
|
|
25
|
+
};
|
|
26
|
+
let result = serialize.decode(data);
|
|
27
|
+
|
|
28
|
+
expect(result).toEqual({ foo: 'bar' });
|
|
29
|
+
});
|
|
30
|
+
});
|
package/src/json.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import SuperJSON from 'superjson';
|
|
3
|
+
|
|
4
|
+
export let serialize = {
|
|
5
|
+
encode: (data: any) => {
|
|
6
|
+
let sup = SuperJSON.serialize(data);
|
|
7
|
+
|
|
8
|
+
if (sup.meta?.referentialEqualities) {
|
|
9
|
+
delete sup.meta.referentialEqualities;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return JSON.stringify({
|
|
13
|
+
$$TYPES$$: {
|
|
14
|
+
__mode: typeof sup.json == 'object' ? 'object' : 'value',
|
|
15
|
+
...sup.meta
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
data: typeof sup.json != 'object' ? sup.json : undefined,
|
|
19
|
+
|
|
20
|
+
...(typeof sup.json == 'object' ? sup.json : {})
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
decode: (data: any) => {
|
|
25
|
+
if (typeof data == 'string') data = JSON.parse(data);
|
|
26
|
+
|
|
27
|
+
if (typeof data != 'object') return data;
|
|
28
|
+
|
|
29
|
+
let { $$TYPES$$, ...rest } = data;
|
|
30
|
+
|
|
31
|
+
if (typeof $$TYPES$$ != 'object' || !$$TYPES$$.__mode) return data;
|
|
32
|
+
|
|
33
|
+
let { __mode, ...meta } = $$TYPES$$;
|
|
34
|
+
let json = __mode == 'value' ? rest.data : rest;
|
|
35
|
+
|
|
36
|
+
return SuperJSON.deserialize({ json, meta });
|
|
37
|
+
}
|
|
38
|
+
};
|
package/tsconfig.json
ADDED