jasone 0.0.1
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/LICENSE.md +9 -0
- package/README.md +124 -0
- package/index.cjs +1 -0
- package/index.d.cts +40 -0
- package/index.d.ts +40 -0
- package/index.js +1 -0
- package/package.json +31 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Lukas Heizmann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Jasone
|
|
2
|
+
|
|
3
|
+
A lightweight, extensible JSON encoder and decoder which supports custom types.
|
|
4
|
+
|
|
5
|
+
**NOTICE:** Jasone is still in early development and might not be stable yet.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- 🚀 Fast & Lightweight: Minimal footprint.
|
|
10
|
+
- 🔌 Extensible: Easily add custom types.
|
|
11
|
+
- 💻 TypeScript: Written in TypeScript for type safety and better DX.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# npm
|
|
17
|
+
npm install jasone
|
|
18
|
+
|
|
19
|
+
# pnpm
|
|
20
|
+
pnpm add jasone
|
|
21
|
+
|
|
22
|
+
# yarn
|
|
23
|
+
yarn add jasone
|
|
24
|
+
|
|
25
|
+
# bun
|
|
26
|
+
bun add jasone
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Basic Usage
|
|
30
|
+
|
|
31
|
+
### Encoding
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { Jasone } from "jasone";
|
|
35
|
+
|
|
36
|
+
const data = { myDate: new Date("2025-04-05T14:30:00.000+02:00") };
|
|
37
|
+
const encoded = JSON.stringify(Jasone.encode(data));
|
|
38
|
+
|
|
39
|
+
console.log(encoded); // {"myDate":{"$":1,"timestamp":1743856200000}}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Decoding
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { decode } from "cborkit/decoder";
|
|
46
|
+
|
|
47
|
+
const encoded = '{"myDate":{"$":1,"timestamp":1743856200000}}';
|
|
48
|
+
const decoded = Jasone.decode(JSON.parse(encoded));
|
|
49
|
+
|
|
50
|
+
console.log(decoded); // { myDate: new Date("2025-04-05T14:30:00.000+02:00") }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Advanced Usage
|
|
54
|
+
|
|
55
|
+
Adding custom types is really easy. You just need to create an transformer object and register it with Jasone.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { Jasone } from "jasone";
|
|
59
|
+
|
|
60
|
+
class Car {
|
|
61
|
+
constructor(public brand: string, public model: string) {}
|
|
62
|
+
|
|
63
|
+
// ...
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// use the `createType` helper
|
|
67
|
+
const myType = createType({
|
|
68
|
+
// the match function is used to determine if the transformer can encode the given value
|
|
69
|
+
matches: (value) => value instanceof Car,
|
|
70
|
+
|
|
71
|
+
// the type id is used to identify the type on the encoded object
|
|
72
|
+
// (this needs to be unique and a string, numbers are currently reserved for built-in types)
|
|
73
|
+
typeId: "Car"
|
|
74
|
+
|
|
75
|
+
// the encode function which receives the matched value and need to return an valid JSON object
|
|
76
|
+
encode: (car) => ({ brand: car.brand, model: car.model }),
|
|
77
|
+
|
|
78
|
+
// the decode function which receives the encoded JSON object and needs to return a original value
|
|
79
|
+
decode: ({brand, model}) => new Car(brand, model),
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
For classes, you can also use a bit more performant way of creating the transformer object:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const myType = createType({
|
|
87
|
+
// by using the `target` property, under the hood, Jasone will use the constructor to match
|
|
88
|
+
// the type. Which is more performant than using the `matches` property.
|
|
89
|
+
target: Car,
|
|
90
|
+
typeId: "Car",
|
|
91
|
+
encode: (car) => ({brand: car.brand, model: car.model}),
|
|
92
|
+
decode: ({brand, model}) => new Car(brand, model),
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Now the only thing left is to register the type with Jasone:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
Jasone.register(myType);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
And that's it! If you want multiple instances of Jasone with different types, simply create a new instance like so:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const jasone = new Jasone({
|
|
106
|
+
types: [myType],
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Or
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const jasone = new Jasone();
|
|
114
|
+
|
|
115
|
+
jasone.register(myType);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Contributing
|
|
119
|
+
|
|
120
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
This project is licensed under the [MIT License](./LICENSE).
|
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var i=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var y=(t,e)=>{for(var r in e)i(t,r,{get:e[r],enumerable:!0})},h=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of f(e))!m.call(t,n)&&n!==r&&i(t,n,{get:()=>e[n],enumerable:!(o=T(e,n))||o.enumerable});return t};var u=t=>h(i({},"__esModule",{value:!0}),t);var E={};y(E,{Jasone:()=>c,createType:()=>s});module.exports=u(E);var a={};y(a,{bigIntType:()=>I,dateType:()=>g,mapType:()=>w,regExpType:()=>x,setType:()=>b,undefinedType:()=>l,urlType:()=>J});var s=t=>t;var l=s({matches:t=>t===void 0,typeId:0,encode:()=>({}),decode:()=>{}});var g=s({target:Date,typeId:1,encode:t=>({timestamp:t.getTime()}),decode:({timestamp:t})=>new Date(t)});var I=s({matches:t=>typeof t=="bigint",typeId:2,encode:t=>({bigint:t.toString()}),decode:({bigint:t})=>BigInt(t)});var x=s({matches:t=>t instanceof RegExp,typeId:3,encode:t=>({source:t.source,flags:t.flags}),decode:({source:t,flags:e})=>new RegExp(t,e)});var b=s({matches:t=>t instanceof Set,typeId:4,encode:(t,e)=>({set:Array.from(t.values().map(r=>e(r)))}),decode:({set:t},e)=>new Set(t.map(r=>e(r)))});var w=s({matches:t=>t instanceof Map,typeId:5,encode:(t,e)=>({map:Array.from(t.entries().map(([r,o])=>[e(r),e(o)]))}),decode:({map:t},e)=>new Map(t.map(([r,o])=>{if(r===void 0||o===void 0)throw new Error("Illegal value received.",{cause:t});return[e(r),e(o)]}))});var J=s({matches:t=>t instanceof URL,typeId:6,encode:t=>({url:t.toString()}),decode:({url:t})=>new URL(t)});var c=class t{#e;#r=new Map;#o=new Map;#n=[];constructor(e={}){this.#e=e.typeIdentifier??"$";for(let r of e.types??[])this.register(r)}register(e){this.#r.set(e.typeId,e.decode),"matches"in e?this.#n.push({matches:e.matches,typeId:e.typeId,encode:e.encode}):this.#o.set(e.target,{typeId:e.typeId,encode:e.encode})}encode(e){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(n=>this.encode(n));if(Object.getPrototypeOf(e)===Object.prototype){let n=Object.fromEntries(Object.entries(e).map(([d,p])=>[d,this.encode(p)]));return this.#e in n&&(n[this.#e]=[this.encode(n[this.#e])]),n}}let r;if(typeof e=="object"&&e!==null&&(r=this.#o.get(e.constructor)),r||(r=this.#n.find(({matches:n})=>n(e))),!r)throw new Error("No encoder found.",{cause:e});let o=r.encode(e,this.encode.bind(this));return{[this.#e]:r.typeId,...o}}decode(e){return this.#t(e)}#t(e,r=!1){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(o=>this.#t(o));if(this.#e in e&&!Array.isArray(e)&&!r){let o=e[this.#e];if(Array.isArray(o)){if(o.length!==1)throw new Error("Illegal value received. Escaped type identifiers must have exactly one element.",{cause:e});let[d]=o,p={...e};return p[this.#e]=d,this.#t(p,!0)}let n=this.#r.get(o);if(!n)throw new Error(`No decoder found for type id: ${o}`,{cause:e});return n(e,this.decode.bind(this))}if(Object.getPrototypeOf(e)===Object.prototype)return Object.fromEntries(Object.entries(e).map(([o,n])=>[o,this.#t(n)]));break}throw new Error("Illegal value received.",{cause:e})}static default=new t({types:Object.values(a)});static register=t.default.register.bind(t.default);static encode=t.default.encode.bind(t.default);static decode=t.default.decode.bind(t.default)};0&&(module.exports={Jasone,createType});
|
package/index.d.cts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
2
|
+
[key: string]: JsonValue;
|
|
3
|
+
};
|
|
4
|
+
type TypeId = string | number;
|
|
5
|
+
type TypeEncoder<TType = unknown, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = (value: TType, encode: <T = unknown>(value: T) => JsonValue) => TJson;
|
|
6
|
+
type TypeDecoder<TType = unknown, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = (value: TJson, decode: <T = unknown>(value: JsonValue) => T) => TType;
|
|
7
|
+
type MatchesFn<TType = unknown> = (value: unknown) => value is TType;
|
|
8
|
+
type ClassLike<TInstance> = new (...args: unknown[]) => TInstance;
|
|
9
|
+
declare const typeOf: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
|
|
10
|
+
type TypeOf = typeof typeOf;
|
|
11
|
+
|
|
12
|
+
type TypeMatcher<TType> = {
|
|
13
|
+
matches: MatchesFn<TType>;
|
|
14
|
+
} | {
|
|
15
|
+
target: ClassLike<TType>;
|
|
16
|
+
};
|
|
17
|
+
type TypeTransformer<TType, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = TypeMatcher<TType> & {
|
|
18
|
+
typeId: string | number;
|
|
19
|
+
encode: TypeEncoder<TType, TJson>;
|
|
20
|
+
decode: TypeDecoder<TType, TJson>;
|
|
21
|
+
};
|
|
22
|
+
declare const createType: <TType, TJson extends Record<string, JsonValue>>(transformer: TypeTransformer<TType, TJson>) => TypeTransformer<TType, TJson>;
|
|
23
|
+
|
|
24
|
+
type JasoneOptions = {
|
|
25
|
+
typeIdentifier?: string;
|
|
26
|
+
types?: TypeTransformer<any, any>[];
|
|
27
|
+
};
|
|
28
|
+
declare class Jasone {
|
|
29
|
+
#private;
|
|
30
|
+
constructor(options?: JasoneOptions);
|
|
31
|
+
register<TType, TJson extends Record<string, JsonValue>>(type: TypeTransformer<TType, TJson>): void;
|
|
32
|
+
encode(value: unknown): JsonValue;
|
|
33
|
+
decode<T = unknown>(value: JsonValue): T;
|
|
34
|
+
static default: Jasone;
|
|
35
|
+
static register: <TType, TJson extends Record<string, JsonValue>>(type: TypeTransformer<TType, TJson>) => void;
|
|
36
|
+
static encode: (value: unknown) => JsonValue;
|
|
37
|
+
static decode: <T = unknown>(value: JsonValue) => T;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { type ClassLike, Jasone, type JasoneOptions, type JsonValue, type MatchesFn, type TypeDecoder, type TypeEncoder, type TypeId, type TypeMatcher, type TypeOf, type TypeTransformer, createType };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
2
|
+
[key: string]: JsonValue;
|
|
3
|
+
};
|
|
4
|
+
type TypeId = string | number;
|
|
5
|
+
type TypeEncoder<TType = unknown, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = (value: TType, encode: <T = unknown>(value: T) => JsonValue) => TJson;
|
|
6
|
+
type TypeDecoder<TType = unknown, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = (value: TJson, decode: <T = unknown>(value: JsonValue) => T) => TType;
|
|
7
|
+
type MatchesFn<TType = unknown> = (value: unknown) => value is TType;
|
|
8
|
+
type ClassLike<TInstance> = new (...args: unknown[]) => TInstance;
|
|
9
|
+
declare const typeOf: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
|
|
10
|
+
type TypeOf = typeof typeOf;
|
|
11
|
+
|
|
12
|
+
type TypeMatcher<TType> = {
|
|
13
|
+
matches: MatchesFn<TType>;
|
|
14
|
+
} | {
|
|
15
|
+
target: ClassLike<TType>;
|
|
16
|
+
};
|
|
17
|
+
type TypeTransformer<TType, TJson extends Record<string, JsonValue> = Record<string, JsonValue>> = TypeMatcher<TType> & {
|
|
18
|
+
typeId: string | number;
|
|
19
|
+
encode: TypeEncoder<TType, TJson>;
|
|
20
|
+
decode: TypeDecoder<TType, TJson>;
|
|
21
|
+
};
|
|
22
|
+
declare const createType: <TType, TJson extends Record<string, JsonValue>>(transformer: TypeTransformer<TType, TJson>) => TypeTransformer<TType, TJson>;
|
|
23
|
+
|
|
24
|
+
type JasoneOptions = {
|
|
25
|
+
typeIdentifier?: string;
|
|
26
|
+
types?: TypeTransformer<any, any>[];
|
|
27
|
+
};
|
|
28
|
+
declare class Jasone {
|
|
29
|
+
#private;
|
|
30
|
+
constructor(options?: JasoneOptions);
|
|
31
|
+
register<TType, TJson extends Record<string, JsonValue>>(type: TypeTransformer<TType, TJson>): void;
|
|
32
|
+
encode(value: unknown): JsonValue;
|
|
33
|
+
decode<T = unknown>(value: JsonValue): T;
|
|
34
|
+
static default: Jasone;
|
|
35
|
+
static register: <TType, TJson extends Record<string, JsonValue>>(type: TypeTransformer<TType, TJson>) => void;
|
|
36
|
+
static encode: (value: unknown) => JsonValue;
|
|
37
|
+
static decode: <T = unknown>(value: JsonValue) => T;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { type ClassLike, Jasone, type JasoneOptions, type JsonValue, type MatchesFn, type TypeDecoder, type TypeEncoder, type TypeId, type TypeMatcher, type TypeOf, type TypeTransformer, createType };
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var a=Object.defineProperty;var y=(t,e)=>{for(var r in e)a(t,r,{get:e[r],enumerable:!0})};var d={};y(d,{bigIntType:()=>m,dateType:()=>f,mapType:()=>l,regExpType:()=>h,setType:()=>u,undefinedType:()=>T,urlType:()=>g});var n=t=>t;var T=n({matches:t=>t===void 0,typeId:0,encode:()=>({}),decode:()=>{}});var f=n({target:Date,typeId:1,encode:t=>({timestamp:t.getTime()}),decode:({timestamp:t})=>new Date(t)});var m=n({matches:t=>typeof t=="bigint",typeId:2,encode:t=>({bigint:t.toString()}),decode:({bigint:t})=>BigInt(t)});var h=n({matches:t=>t instanceof RegExp,typeId:3,encode:t=>({source:t.source,flags:t.flags}),decode:({source:t,flags:e})=>new RegExp(t,e)});var u=n({matches:t=>t instanceof Set,typeId:4,encode:(t,e)=>({set:Array.from(t.values().map(r=>e(r)))}),decode:({set:t},e)=>new Set(t.map(r=>e(r)))});var l=n({matches:t=>t instanceof Map,typeId:5,encode:(t,e)=>({map:Array.from(t.entries().map(([r,o])=>[e(r),e(o)]))}),decode:({map:t},e)=>new Map(t.map(([r,o])=>{if(r===void 0||o===void 0)throw new Error("Illegal value received.",{cause:t});return[e(r),e(o)]}))});var g=n({matches:t=>t instanceof URL,typeId:6,encode:t=>({url:t.toString()}),decode:({url:t})=>new URL(t)});var i=class t{#e;#r=new Map;#o=new Map;#n=[];constructor(e={}){this.#e=e.typeIdentifier??"$";for(let r of e.types??[])this.register(r)}register(e){this.#r.set(e.typeId,e.decode),"matches"in e?this.#n.push({matches:e.matches,typeId:e.typeId,encode:e.encode}):this.#o.set(e.target,{typeId:e.typeId,encode:e.encode})}encode(e){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(s=>this.encode(s));if(Object.getPrototypeOf(e)===Object.prototype){let s=Object.fromEntries(Object.entries(e).map(([c,p])=>[c,this.encode(p)]));return this.#e in s&&(s[this.#e]=[this.encode(s[this.#e])]),s}}let r;if(typeof e=="object"&&e!==null&&(r=this.#o.get(e.constructor)),r||(r=this.#n.find(({matches:s})=>s(e))),!r)throw new Error("No encoder found.",{cause:e});let o=r.encode(e,this.encode.bind(this));return{[this.#e]:r.typeId,...o}}decode(e){return this.#t(e)}#t(e,r=!1){switch(typeof e){case"string":case"number":case"boolean":return e;case"object":if(e===null)return null;if(Array.isArray(e))return e.map(o=>this.#t(o));if(this.#e in e&&!Array.isArray(e)&&!r){let o=e[this.#e];if(Array.isArray(o)){if(o.length!==1)throw new Error("Illegal value received. Escaped type identifiers must have exactly one element.",{cause:e});let[c]=o,p={...e};return p[this.#e]=c,this.#t(p,!0)}let s=this.#r.get(o);if(!s)throw new Error(`No decoder found for type id: ${o}`,{cause:e});return s(e,this.decode.bind(this))}if(Object.getPrototypeOf(e)===Object.prototype)return Object.fromEntries(Object.entries(e).map(([o,s])=>[o,this.#t(s)]));break}throw new Error("Illegal value received.",{cause:e})}static default=new t({types:Object.values(d)});static register=t.default.register.bind(t.default);static encode=t.default.encode.bind(t.default);static decode=t.default.decode.bind(t.default)};export{i as Jasone,n as createType};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jasone",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"author": "Lukas Heizmann <lukas@heizmann.dev>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./index.cjs",
|
|
8
|
+
"module": "./index.js",
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./index.js",
|
|
13
|
+
"require": "./index.cjs",
|
|
14
|
+
"types": "./index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/lkwr/jasone.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/lkwr/jasone",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/lkwr/jasone/issues"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"json",
|
|
27
|
+
"extended",
|
|
28
|
+
"typed",
|
|
29
|
+
"custom-types"
|
|
30
|
+
]
|
|
31
|
+
}
|