@bepalo/rjson 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The Bepalo Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,494 @@
1
+ # 🏆 @bepalo/rjson
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@bepalo/rjson.svg)](https://www.npmjs.com/package/@bepalo/rjson)
4
+ [![CI](https://img.shields.io/github/actions/workflow/status/bepalo/rjson/ci.yaml?label=ci)](https://github.com/bepalo/rjson/actions/workflows/ci.yaml)
5
+ [![tests](https://img.shields.io/github/actions/workflow/status/bepalo/rjson/testing.yaml?label=tests)](https://github.com/bepalo/rjson/actions/workflows/testing.yaml)
6
+ [![license](https://img.shields.io/npm/l/@bepalo/rjson.svg)](LICENSE)
7
+
8
+ <!-- ![Benchmarked](https://img.shields.io/badge/benchmarked-yes-green) -->
9
+
10
+ [![Vitest](https://img.shields.io/badge/vitest-6E9F18?style=for-the-badge&logo=vitest&logoColor=white)](test-result.md)
11
+
12
+ **A compact, URL-friendly serialization format that is smaller than JSON while remaining human-readable and fast to parse.**
13
+
14
+ RJSON (Remote JavaScript Object Notation) is a specification and implementation inspired by RISON and it is designed for:
15
+
16
+ - Better frontend-to-backend communication via search parameters.
17
+ - Compact payloads
18
+ - Human-readable configuration
19
+ - Fast parsing without AST generation
20
+ - JavaScript-native data structures
21
+
22
+ ## ✨ Features
23
+
24
+ - ⚡ Fast parser with direct character scanning (recursive descent parsing).
25
+ - 📦 Smaller than JSON for many real-world payloads
26
+ - 🌐 URL-friendly syntax
27
+ - 🧩 Supports objects, arrays, strings, numbers, booleans
28
+ - 🔐 Supports `null` and `undefined`
29
+ - 🗜️ Built-in mapped-array compression
30
+ - 📝 Tagged template support
31
+ - 🔧 Zero dependencies
32
+ - 🟦 TypeScript ready
33
+ - 🚀 Runtime agnostic (Node.js, Bun, Deno)
34
+
35
+ ## 📑 Table of Contents
36
+
37
+ 1. Features
38
+ 2. Installation
39
+ 3. Quick Start
40
+ 4. Syntax Overview
41
+ 5. Core Types
42
+ 6. Mapped Arrays
43
+ 7. API Reference
44
+ 8. Design Goals
45
+ 9. License
46
+
47
+ ## 🚀 Get Started
48
+
49
+ ### 📥 Installation
50
+
51
+ #### bun
52
+
53
+ ```bash
54
+ bun add @bepalo/rjson
55
+ ```
56
+
57
+ #### npm
58
+
59
+ ```bash
60
+ npm install @bepalo/rjson
61
+ ```
62
+
63
+ #### pnpm
64
+
65
+ ```bash
66
+ pnpm add @bepalo/rjson
67
+ ```
68
+
69
+ #### deno
70
+
71
+ ```ts
72
+ import { RJSON } from "jsr:@bepalo/rjson";
73
+ ```
74
+
75
+ ## 📦 Quick Start
76
+
77
+ ### Parse RJSON
78
+
79
+ ```ts
80
+ import { RJSON } from "@bepalo/rjson";
81
+
82
+ const user = RJSON.parse("(name:'Natnael',age:25,active:T)");
83
+
84
+ console.log(user);
85
+ ```
86
+
87
+ Output:
88
+
89
+ ```js
90
+ {
91
+ name: "Natnael",
92
+ age: 25,
93
+ active: true
94
+ }
95
+ ```
96
+
97
+ ### Stringify Objects
98
+
99
+ ```ts
100
+ import { RJSON } from "@bepalo/rjson";
101
+
102
+ const text = RJSON.stringify({
103
+ name: "Natnael",
104
+ age: 25,
105
+ active: true,
106
+ });
107
+
108
+ console.log(text);
109
+ ```
110
+
111
+ Output:
112
+
113
+ ```txt
114
+ (name:'Natnael',age:25,active:T)
115
+ ```
116
+
117
+ ### Stringify arrays
118
+
119
+ ```ts
120
+ import { RJSON } from "@bepalo/rjson";
121
+
122
+ const text = RJSON.stringify([1, 3.1415, null, undefined, true, "hello"]);
123
+
124
+ console.log(text);
125
+ ```
126
+
127
+ Output:
128
+
129
+ ```txt
130
+ _(1,3.1415,,U,T,'hello')_
131
+ ```
132
+
133
+ ### Stringify mapped-arrays
134
+
135
+ ```ts
136
+ import { RJSON } from "@bepalo/rjson";
137
+
138
+ const text = RJSON.stringifyMappedArray(true, ["id", "title", "body"]);
139
+
140
+ console.log(text);
141
+ ```
142
+
143
+ Output:
144
+
145
+ ```txt
146
+ ~T(id,title,body)~
147
+ ```
148
+
149
+ ### Tagged Template Helper
150
+
151
+ ```ts
152
+ import { rjson } from "@bepalo/rjson";
153
+
154
+ const user = rjson`(name:'Natnael',age:25,active:T)`;
155
+
156
+ console.log(user);
157
+ ```
158
+
159
+ Output:
160
+
161
+ ```js
162
+ {
163
+ name: "Natnael",
164
+ age: 25,
165
+ active: true,
166
+ }
167
+ ```
168
+
169
+ ## 📚 Syntax Overview
170
+
171
+ | Type | RJSON | JSON |
172
+ | ------------ | ----------------------------- | ---------------------------- |
173
+ | Object | `(name:'John')` | `{"name":"John"}` |
174
+ | Array | `_(1,2,3)_` | `[1,2,3]` |
175
+ | String | `'hello'` `"hello"` \`hello\` | `"hello"` |
176
+ | Number | `123` | `123` |
177
+ | Boolean | `T` / `F` | `true` / `false` |
178
+ | Null | `N` | `null` |
179
+ | Undefined | `U` | Not supported |
180
+ | Mapped Array | `~T(admin,user)~` | `{"admin":true,"user":true}` |
181
+
182
+ ### 📚 Core Types
183
+
184
+ #### Objects
185
+
186
+ ```txt
187
+ (name:'John',age:30)
188
+ ```
189
+
190
+ Produces:
191
+
192
+ ```js
193
+ {
194
+ name: "John",
195
+ age: 30
196
+ }
197
+ ```
198
+
199
+ #### Arrays
200
+
201
+ ```txt
202
+ _(1,2,3,4)_
203
+ ```
204
+
205
+ Produces:
206
+
207
+ ```js
208
+ [1, 2, 3, 4];
209
+ ```
210
+
211
+ #### Nested Structures
212
+
213
+ ```txt
214
+ (user:(name:'John',age:30),active:T)
215
+ ```
216
+
217
+ Produces:
218
+
219
+ ```js
220
+ {
221
+ user: {
222
+ name: "John",
223
+ age: 30
224
+ },
225
+ active: true
226
+ }
227
+ ```
228
+
229
+ #### Strings
230
+
231
+ Supported delimiters:
232
+
233
+ ```txt
234
+ 'hello'
235
+ "hello"
236
+ `hello`
237
+ ```
238
+
239
+ Escaping:
240
+
241
+ ```txt
242
+ 'can\'t'
243
+ ```
244
+
245
+ ```js
246
+ const text = `'can\\'t'`;
247
+ ```
248
+
249
+ Produces:
250
+
251
+ ```js
252
+ "can't";
253
+ ```
254
+
255
+ #### Numbers
256
+
257
+ Supported formats:
258
+
259
+ ```txt
260
+ 123
261
+ -123
262
+ +123
263
+ 12.5
264
+ 1e10
265
+ 1e-10
266
+ ```
267
+
268
+ Examples:
269
+
270
+ ```txt
271
+ age:25
272
+ price:19.99
273
+ distance:1.5e6
274
+ ```
275
+
276
+ #### Booleans
277
+
278
+ ```txt
279
+ T
280
+ ```
281
+
282
+ Produces:
283
+
284
+ ```js
285
+ true;
286
+ ```
287
+
288
+ ---
289
+
290
+ ```txt
291
+ F
292
+ ```
293
+
294
+ Produces:
295
+
296
+ ```js
297
+ false;
298
+ ```
299
+
300
+ #### Null
301
+
302
+ ```txt
303
+ N
304
+ ```
305
+
306
+ Produces:
307
+
308
+ ```js
309
+ null;
310
+ ```
311
+
312
+ Empty values are also treated as null:
313
+
314
+ ```txt
315
+ (name:)
316
+ ```
317
+
318
+ Produces:
319
+
320
+ ```js
321
+ {
322
+ name: null;
323
+ }
324
+ ```
325
+
326
+ #### Undefined
327
+
328
+ ```txt
329
+ (name:U)
330
+ ```
331
+
332
+ Produces:
333
+
334
+ ```js
335
+ {
336
+ name: undefined;
337
+ }
338
+ ```
339
+
340
+ ### 🗜️ Mapped Arrays
341
+
342
+ Mapped arrays compress repeated values.
343
+
344
+ Instead of:
345
+
346
+ ```js
347
+ {
348
+ admin: true,
349
+ editor: true,
350
+ user: true
351
+ }
352
+ ```
353
+
354
+ Use:
355
+
356
+ ```txt
357
+ ~T(admin,editor,user)~
358
+ ```
359
+
360
+ Result:
361
+
362
+ ```js
363
+ {
364
+ admin: true,
365
+ editor: true,
366
+ user: true
367
+ }
368
+ ```
369
+
370
+ ---
371
+
372
+ The value can be any valid RJSON type
373
+
374
+ ```txt
375
+ ~F(create,update,delete)~
376
+ ~(roles:_('admin','user')_)(create,update,delete)~
377
+ ~~F(admin,user)~(create,update,delete)~
378
+ ```
379
+
380
+ ## 🔧 API Reference
381
+
382
+ ### parseRJSON
383
+
384
+ Parses RJSON text.
385
+
386
+ ```ts
387
+ const value = parseRJSON("(name:'John',active:T)");
388
+ ```
389
+
390
+ ### stringifyRJSON
391
+
392
+ Serializes JavaScript values.
393
+
394
+ ```ts
395
+ const text = stringifyRJSON({
396
+ name: "John",
397
+ active: true,
398
+ });
399
+ ```
400
+
401
+ Result:
402
+
403
+ ```txt
404
+ (name:'John',active:T)
405
+ ```
406
+
407
+ ### stringifyRJSONMappedArray
408
+
409
+ Creates mapped-array expressions.
410
+
411
+ ```ts
412
+ stringifyRJSONMappedArray(true, ["read", "write", "delete"]);
413
+ ```
414
+
415
+ Result:
416
+
417
+ ```txt
418
+ ~T(read,write,delete)~
419
+ ```
420
+
421
+ ### rjson
422
+
423
+ Tagged-template parser.
424
+
425
+ ```ts
426
+ const data = rjson`
427
+ (name:'John')
428
+ `;
429
+ ```
430
+
431
+ ## 🎯 Why RJSON?
432
+
433
+ It is designed for better frontend-to-backend communication via search parameters.
434
+
435
+ Benefits:
436
+
437
+ - Smaller payloads
438
+ - Easier URL embedding
439
+ - Human-readable
440
+ - JavaScript-oriented
441
+ - Fast parsing
442
+
443
+ Example:
444
+
445
+ JSON
446
+
447
+ ```json
448
+ {
449
+ "name": "John",
450
+ "active": true,
451
+ "roles": ["admin", "user"]
452
+ }
453
+ ```
454
+
455
+ RJSON
456
+
457
+ ```txt
458
+ (name:'John',active:T,roles:_('admin','user')_)
459
+ ```
460
+
461
+ ## ⚠️ Current Limitations
462
+
463
+ - Whitespace is forbidden.
464
+ - Designed primarily for compact transport and URLs.
465
+
466
+ Invalid:
467
+
468
+ ```txt
469
+ (
470
+ name : 'John'
471
+ )
472
+ ```
473
+
474
+ Valid:
475
+
476
+ ```txt
477
+ (name:'John')
478
+ ```
479
+
480
+ ## 📄 License
481
+
482
+ MIT
483
+
484
+ ## 🕊️ Thanks and Enjoy
485
+
486
+ If you find RJSON useful, please consider starring the repository and sharing it with others.
487
+
488
+ ## 💖 Be a Sponsor
489
+
490
+ Support development and future improvements.
491
+
492
+ <a href="https://ko-fi.com/natieshzed">
493
+ <img height="32" src="https://img.shields.io/badge/Ko--fi-donate-orange?style=for-the-badge&logo=ko-fi&logoColor=white">
494
+ </a>
@@ -0,0 +1,3 @@
1
+ export * from "./rjson.ts";
2
+ export { default } from "./rjson.ts";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.default = void 0;
21
+ __exportStar(require("./rjson.js"), exports);
22
+ var rjson_ts_1 = require("./rjson.js");
23
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(rjson_ts_1).default; } });
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,uCAAqC;AAA5B,oHAAA,OAAO,OAAA"}
@@ -0,0 +1,30 @@
1
+ interface ParseRJSONState {
2
+ start: number;
3
+ end: number;
4
+ i: number;
5
+ charCode: number;
6
+ forbidTextKeys?: boolean;
7
+ forbidEmptyKeys?: boolean;
8
+ }
9
+ export declare const parseRJSON: (source: string, initalState?: Partial<ParseRJSONState>) => unknown;
10
+ export declare function rjson(strings: TemplateStringsArray, ...values: unknown[]): unknown;
11
+ export declare const stringifyRJSONKey: (key: string) => string;
12
+ export declare const stringifyRJSONArray: (data: unknown[]) => string;
13
+ export declare const stringifyRJSONObject: (data: Record<string, unknown>) => string;
14
+ export declare const stringifyRJSONMappedArray: (value: unknown | undefined | null, data: unknown[]) => string;
15
+ export declare const stringifyRJSONString: (data: string) => string;
16
+ export declare const stringifyRJSONNumber: (data: number) => string;
17
+ export declare const stringifyRJSONBoolean: (data: boolean) => string;
18
+ export declare const stringifyRJSON: (data?: unknown) => string;
19
+ export declare const RJSON: {
20
+ parse: (source: string, initalState?: Partial<ParseRJSONState>) => unknown;
21
+ stringify: (data?: unknown) => string;
22
+ stringifyKey: (key: string) => string;
23
+ stringifyObject: (data: Record<string, unknown>) => string;
24
+ stringifyMappedArray: (value: unknown | undefined | null, data: unknown[]) => string;
25
+ stringifyString: (data: string) => string;
26
+ stringifyNumber: (data: number) => string;
27
+ stringifyBoolean: (data: boolean) => string;
28
+ };
29
+ export default RJSON;
30
+ //# sourceMappingURL=rjson.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rjson.d.ts","sourceRoot":"","sources":["../../src/rjson.ts"],"names":[],"mappings":"AAwCA,UAAU,eAAe;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,CAAC,EAAE,MAAM,CAAC;IACV,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AA8bD,eAAO,MAAM,UAAU,GACrB,QAAQ,MAAM,EACd,cAAc,OAAO,CAAC,eAAe,CAAC,KACrC,OAwBF,CAAC;AAEF,wBAAgB,KAAK,CACnB,OAAO,EAAE,oBAAoB,EAC7B,GAAG,MAAM,EAAE,OAAO,EAAE,GACnB,OAAO,CAKT;AAED,eAAO,MAAM,iBAAiB,GAAI,KAAK,MAAM,KAAG,MAiB/C,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAI,MAAM,OAAO,EAAE,KAAG,MAErD,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,MAAM,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAMpE,CAAC;AAEF,eAAO,MAAM,yBAAyB,GACpC,OAAO,OAAO,GAAG,SAAS,GAAG,IAAI,EACjC,MAAM,OAAO,EAAE,KACd,MAEF,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,MAAM,MAAM,KAAG,MAQnD,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,MAAM,MAAM,KAAG,MASnD,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAI,MAAM,OAAO,KAAG,MAErD,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,OAAO,OAAO,KAAG,MAoB/C,CAAC;AAEF,eAAO,MAAM,KAAK;oBA3HR,MAAM,gBACA,OAAO,CAAC,eAAe,CAAC,KACrC,OAAO;uBAmG4B,OAAO,KAAG,MAAM;wBA/Df,MAAM,KAAG,MAAM;4BAuBX,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM;kCASlE,OAAO,GAAG,SAAS,GAAG,IAAI,QAC3B,OAAO,EAAE,KACd,MAAM;4BAIkC,MAAM,KAAG,MAAM;4BAUf,MAAM,KAAG,MAAM;6BAWd,OAAO,KAAG,MAAM;CAmC3D,CAAC;AAEF,eAAe,KAAK,CAAC"}