Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac04e2069 | |||
| 6fe5112bfb | |||
| acf8cea099 | |||
| bd67581b75 | |||
| 374a8e411a | |||
| fe8b5ce7c0 |
28
changelog.md
28
changelog.md
@@ -1,5 +1,33 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-12-10 - 6.0.0 - BREAKING CHANGE(Smartjson)
|
||||||
|
Require TC39 Stage 3 decorators for @foldDec (use accessor), switch foldDec to initializer-based implementation, improve buffer encode/decode handling, bump dependencies and update docs/tests.
|
||||||
|
|
||||||
|
- foldDec now implements TC39 Stage 3 accessor decorators and must be used with the 'accessor' keyword (e.g. '@foldDec() accessor prop: T'). The decorator registers saveable properties via context.addInitializer so properties are tracked per-instance.
|
||||||
|
- Buffer handling rewritten: replacer/reviver handle Uint8Array and Buffer cross-platform; EncodedBuffer.data now uses a 'base64:' prefix and reviver returns a Uint8Array. This changes the serialized representation of buffers.
|
||||||
|
- tsconfig updated to remove experimentalDecorators and useDefineForClassFields overrides — project now targets modern TypeScript decorator support (ensure your toolchain supports Stage 3 decorators).
|
||||||
|
- Documentation (readme and hints) and tests updated to reflect accessor-based decorators and new buffer handling.
|
||||||
|
- Dependencies bumped: @push.rocks/smartenv -> ^6.0.0, dev deps @git.zone/tsbuild, @git.zone/tsrun, @git.zone/tstest upgraded, and @types/node -> ^24.0.0.
|
||||||
|
- Removed pnpm-workspace.yaml onlyBuiltDependencies entries.
|
||||||
|
|
||||||
|
## 2025-09-12 - 5.2.0 - feat(smartjson)
|
||||||
|
Implement stableOneWayStringify: deterministic, cycle-safe JSON for hashing/comparisons; update docs and tests
|
||||||
|
|
||||||
|
- Implement stableOneWayStringify in ts/index.ts: produces deterministic JSON, encodes buffers, replaces circular references with "__cycle__" and marks unserializable values as "__unserializable__".
|
||||||
|
- Update README: add documentation and examples for stableOneWayStringify, clarify JSONL wording, and update performance/migration notes.
|
||||||
|
- Add unit test to verify stableOneWayStringify handles circular references without throwing.
|
||||||
|
- Add .claude/settings.local.json (local settings/config) as part of the change set.
|
||||||
|
|
||||||
|
## 2025-09-12 - 5.1.0 - feat(smartjson)
|
||||||
|
Add JSONL stringify and ordering comparator; fix empty buffer handling; refactor Smartjson folding/enfolding
|
||||||
|
|
||||||
|
- Export stringifyJsonL(items: any[]) and add README documentation for JSONL stringification
|
||||||
|
- stringify(obj, simpleOrderArray) now derives a stable-json comparator from simpleOrderArray when no custom cmp is provided, ensuring predictable key ordering
|
||||||
|
- Fix buffer handling: empty Uint8Array values are preserved (no longer serialized to an empty string) and encoding/decoding logic improved
|
||||||
|
- Refactor Smartjson.enfoldFromObject to safely use saveableProperties and avoid repeated property access
|
||||||
|
- Simplify Smartjson.foldToObject to delegate to an internal foldToObjectInternal with cycle detection and correct nested instance handling
|
||||||
|
- Add unit tests for empty buffers, JSONL parse/stringify, deepEqualJsonLStrings, and simpleOrderArray comparator behavior
|
||||||
|
|
||||||
## 2025-09-12 - 5.0.21 - fix(Smartjson)
|
## 2025-09-12 - 5.0.21 - fix(Smartjson)
|
||||||
Cross-platform buffer/base64 handling, safer folding with cycle detection, parsing fixes, docs and dependency updates
|
Cross-platform buffer/base64 handling, safer folding with cycle detection, parsing fixes, docs and dependency updates
|
||||||
|
|
||||||
|
|||||||
12
package.json
12
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartjson",
|
"name": "@push.rocks/smartjson",
|
||||||
"version": "5.0.21",
|
"version": "6.0.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A library for handling typed JSON data, providing functionalities for parsing, stringifying, and working with JSON objects, including support for encoding and decoding buffers.",
|
"description": "A library for handling typed JSON data, providing functionalities for parsing, stringifying, and working with JSON objects, including support for encoding and decoding buffers.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@@ -21,13 +21,13 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://code.foss.global/push.rocks/smartjson",
|
"homepage": "https://code.foss.global/push.rocks/smartjson",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.6.8",
|
"@git.zone/tsbuild": "^3.1.2",
|
||||||
"@git.zone/tsrun": "^1.3.3",
|
"@git.zone/tsrun": "^2.0.0",
|
||||||
"@git.zone/tstest": "^2.3.8",
|
"@git.zone/tstest": "^3.1.3",
|
||||||
"@types/node": "^22"
|
"@types/node": "^24.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/smartenv": "^5.0.13",
|
"@push.rocks/smartenv": "^6.0.0",
|
||||||
"@push.rocks/smartstring": "^4.1.0",
|
"@push.rocks/smartstring": "^4.1.0",
|
||||||
"fast-json-stable-stringify": "^2.1.0",
|
"fast-json-stable-stringify": "^2.1.0",
|
||||||
"lodash.clonedeep": "^4.5.0"
|
"lodash.clonedeep": "^4.5.0"
|
||||||
|
|||||||
2797
pnpm-lock.yaml
generated
2797
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,3 +0,0 @@
|
|||||||
onlyBuiltDependencies:
|
|
||||||
- esbuild
|
|
||||||
- puppeteer
|
|
||||||
@@ -1 +1,11 @@
|
|||||||
|
# Project Hints
|
||||||
|
|
||||||
|
## Decorators
|
||||||
|
- Uses TC39 Stage 3 decorators (not legacy `experimentalDecorators`)
|
||||||
|
- The `@foldDec()` decorator requires the `accessor` keyword on properties
|
||||||
|
- Example: `@foldDec() accessor myProp: string = 'value';`
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
- Last upgraded: December 2024
|
||||||
|
- Uses @push.rocks/smartenv v6.x
|
||||||
|
- Uses @git.zone/tstest v3.x for testing
|
||||||
|
|||||||
97
readme.md
97
readme.md
@@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
A powerful library for working with JSON in TypeScript, providing type-safe serialization, advanced buffer handling, deep object comparison, and support for complex class instances. Perfect for applications that need reliable JSON manipulation with full TypeScript support.
|
A powerful library for working with JSON in TypeScript, providing type-safe serialization, advanced buffer handling, deep object comparison, and support for complex class instances. Perfect for applications that need reliable JSON manipulation with full TypeScript support.
|
||||||
|
|
||||||
|
## Issue Reporting and Security
|
||||||
|
|
||||||
|
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -21,9 +25,10 @@ pnpm add @push.rocks/smartjson
|
|||||||
✨ **Type-Safe JSON Operations** - Full TypeScript support with proper typing
|
✨ **Type-Safe JSON Operations** - Full TypeScript support with proper typing
|
||||||
🎯 **Class Instance Serialization** - Fold and unfold class instances to/from JSON
|
🎯 **Class Instance Serialization** - Fold and unfold class instances to/from JSON
|
||||||
🔐 **Buffer & Binary Support** - Seamless handling of Buffers and Typed Arrays
|
🔐 **Buffer & Binary Support** - Seamless handling of Buffers and Typed Arrays
|
||||||
📊 **JSON Lines Support** - Parse and compare JSONL data streams
|
📊 **JSON Lines Support** - Parse, stringify and compare JSONL data streams
|
||||||
🎨 **Pretty Printing** - Beautiful formatted JSON output
|
🎨 **Pretty Printing** - Beautiful formatted JSON output
|
||||||
⚡ **Stable Stringification** - Consistent key ordering for reliable comparisons
|
⚡ **Stable Stringification** - Consistent key ordering for reliable comparisons
|
||||||
|
♻️ **Circular Reference Handling** - Safe one-way stringify for objects with cycles
|
||||||
🔍 **Deep Equality Checks** - Compare complex objects and JSON structures
|
🔍 **Deep Equality Checks** - Compare complex objects and JSON structures
|
||||||
🌐 **Base64 Encoding** - Built-in base64 JSON encoding/decoding
|
🌐 **Base64 Encoding** - Built-in base64 JSON encoding/decoding
|
||||||
|
|
||||||
@@ -67,6 +72,40 @@ const prettyJson = smartjson.stringifyPretty({
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Safe One-Way Stringification (New in v5.1.0)
|
||||||
|
|
||||||
|
Handle circular references and unserializable values safely for hashing and comparisons:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Create objects with circular references
|
||||||
|
const objA = { name: 'A' };
|
||||||
|
const objB = { name: 'B', ref: objA };
|
||||||
|
objA['ref'] = objB; // Circular reference!
|
||||||
|
|
||||||
|
// Normal stringify would throw, but stableOneWayStringify handles it
|
||||||
|
const safeJson = smartjson.stableOneWayStringify(objA);
|
||||||
|
// Circular references are replaced with "__cycle__"
|
||||||
|
|
||||||
|
// Perfect for object hashing
|
||||||
|
const hash1 = crypto.createHash('sha256')
|
||||||
|
.update(smartjson.stableOneWayStringify(complexObject))
|
||||||
|
.digest('hex');
|
||||||
|
|
||||||
|
// Handles unserializable values gracefully
|
||||||
|
const objWithGetter = {
|
||||||
|
normal: 'value',
|
||||||
|
get throwing() { throw new Error('Cannot serialize!'); }
|
||||||
|
};
|
||||||
|
const safe = smartjson.stableOneWayStringify(objWithGetter);
|
||||||
|
// Throwing getters are replaced with "__unserializable__"
|
||||||
|
|
||||||
|
// Custom key ordering for consistent hashes
|
||||||
|
const ordered = smartjson.stableOneWayStringify(
|
||||||
|
{ z: 1, a: 2 },
|
||||||
|
['a', 'z'] // Specify key order
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
### Base64 JSON Encoding
|
### Base64 JSON Encoding
|
||||||
|
|
||||||
Encode JSON data as base64 for safe transmission:
|
Encode JSON data as base64 for safe transmission:
|
||||||
@@ -99,6 +138,13 @@ const jsonLines = `{"event":"start","time":1234}
|
|||||||
const events = smartjson.parseJsonL(jsonLines);
|
const events = smartjson.parseJsonL(jsonLines);
|
||||||
// Result: Array of parsed objects
|
// Result: Array of parsed objects
|
||||||
|
|
||||||
|
// Produce JSONL from objects
|
||||||
|
const jsonlOut = smartjson.stringifyJsonL([
|
||||||
|
{ event: 'start', time: 1234 },
|
||||||
|
{ event: 'data', value: 42 },
|
||||||
|
{ event: 'end', time: 5678 }
|
||||||
|
]);
|
||||||
|
|
||||||
// Compare JSON Lines data
|
// Compare JSON Lines data
|
||||||
const jsonL1 = `{"id":1}\n{"id":2}`;
|
const jsonL1 = `{"id":1}\n{"id":2}`;
|
||||||
const jsonL2 = `{"id":1}\n{"id":2}`;
|
const jsonL2 = `{"id":1}\n{"id":2}`;
|
||||||
@@ -115,9 +161,9 @@ Transform class instances to JSON and back while preserving type safety:
|
|||||||
import { Smartjson, foldDec } from '@push.rocks/smartjson';
|
import { Smartjson, foldDec } from '@push.rocks/smartjson';
|
||||||
|
|
||||||
class User extends Smartjson {
|
class User extends Smartjson {
|
||||||
@foldDec() public username: string;
|
@foldDec() accessor username: string;
|
||||||
@foldDec() public email: string;
|
@foldDec() accessor email: string;
|
||||||
@foldDec() public settings: UserSettings;
|
@foldDec() accessor settings: UserSettings;
|
||||||
|
|
||||||
// Properties without @foldDec won't be serialized
|
// Properties without @foldDec won't be serialized
|
||||||
private internalId: string;
|
private internalId: string;
|
||||||
@@ -132,8 +178,8 @@ class User extends Smartjson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class UserSettings extends Smartjson {
|
class UserSettings extends Smartjson {
|
||||||
@foldDec() public theme: 'light' | 'dark' = 'light';
|
@foldDec() accessor theme: 'light' | 'dark' = 'light';
|
||||||
@foldDec() public notifications: boolean = true;
|
@foldDec() accessor notifications: boolean = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and serialize
|
// Create and serialize
|
||||||
@@ -155,8 +201,8 @@ console.log(restoredUser.settings instanceof UserSettings); // true
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class Company extends Smartjson {
|
class Company extends Smartjson {
|
||||||
@foldDec() public name: string;
|
@foldDec() accessor name: string;
|
||||||
@foldDec() public employees: Employee[] = [];
|
@foldDec() accessor employees: Employee[] = [];
|
||||||
|
|
||||||
addEmployee(employee: Employee) {
|
addEmployee(employee: Employee) {
|
||||||
this.employees.push(employee);
|
this.employees.push(employee);
|
||||||
@@ -164,9 +210,9 @@ class Company extends Smartjson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Employee extends Smartjson {
|
class Employee extends Smartjson {
|
||||||
@foldDec() public name: string;
|
@foldDec() accessor name: string;
|
||||||
@foldDec() public role: string;
|
@foldDec() accessor role: string;
|
||||||
@foldDec() public salary: number;
|
@foldDec() accessor salary: number;
|
||||||
|
|
||||||
constructor(name: string, role: string, salary: number) {
|
constructor(name: string, role: string, salary: number) {
|
||||||
super();
|
super();
|
||||||
@@ -240,9 +286,9 @@ const isEqual = smartjson.deepEqualObjects(obj1, obj2); // true
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class CachedAPIResponse extends Smartjson {
|
class CachedAPIResponse extends Smartjson {
|
||||||
@foldDec() public data: any;
|
@foldDec() accessor data: any;
|
||||||
@foldDec() public timestamp: number;
|
@foldDec() accessor timestamp: number;
|
||||||
@foldDec() public endpoint: string;
|
@foldDec() accessor endpoint: string;
|
||||||
|
|
||||||
isExpired(maxAge: number = 3600000): boolean {
|
isExpired(maxAge: number = 3600000): boolean {
|
||||||
return Date.now() - this.timestamp > maxAge;
|
return Date.now() - this.timestamp > maxAge;
|
||||||
@@ -276,9 +322,9 @@ if (stored) {
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class AppConfig extends Smartjson {
|
class AppConfig extends Smartjson {
|
||||||
@foldDec() public apiUrl: string;
|
@foldDec() accessor apiUrl: string;
|
||||||
@foldDec() public features: Map<string, boolean> = new Map();
|
@foldDec() accessor features: Map<string, boolean> = new Map();
|
||||||
@foldDec() public limits: {
|
@foldDec() accessor limits: {
|
||||||
maxUploadSize: number;
|
maxUploadSize: number;
|
||||||
maxConcurrentRequests: number;
|
maxConcurrentRequests: number;
|
||||||
};
|
};
|
||||||
@@ -304,10 +350,12 @@ class AppConfig extends Smartjson {
|
|||||||
|
|
||||||
- `parse(jsonString: string): any` - Parse JSON with automatic buffer handling
|
- `parse(jsonString: string): any` - Parse JSON with automatic buffer handling
|
||||||
- `stringify(obj: any, simpleOrderArray?: string[], options?: Options): string` - Convert to JSON with stable ordering
|
- `stringify(obj: any, simpleOrderArray?: string[], options?: Options): string` - Convert to JSON with stable ordering
|
||||||
|
- `stableOneWayStringify(obj: any, simpleOrderArray?: string[], options?: Options): string` - Safe stringify with circular reference handling (one-way, for hashing/comparison)
|
||||||
- `stringifyPretty(obj: any): string` - Pretty print JSON with 2-space indentation
|
- `stringifyPretty(obj: any): string` - Pretty print JSON with 2-space indentation
|
||||||
- `stringifyBase64(obj: any): string` - Encode JSON as base64
|
- `stringifyBase64(obj: any): string` - Encode JSON as base64
|
||||||
- `parseBase64(base64String: string): any` - Decode base64 JSON
|
- `parseBase64(base64String: string): any` - Decode base64 JSON
|
||||||
- `parseJsonL(jsonLinesString: string): any[]` - Parse JSON Lines format
|
- `parseJsonL(jsonLinesString: string): any[]` - Parse JSON Lines format
|
||||||
|
- `stringifyJsonL(items: any[]): string` - Stringify array to JSON Lines
|
||||||
- `deepEqualObjects(obj1: any, obj2: any): boolean` - Deep comparison of objects
|
- `deepEqualObjects(obj1: any, obj2: any): boolean` - Deep comparison of objects
|
||||||
- `deepEqualJsonLStrings(jsonL1: string, jsonL2: string): boolean` - Compare JSON Lines strings
|
- `deepEqualJsonLStrings(jsonL1: string, jsonL2: string): boolean` - Compare JSON Lines strings
|
||||||
|
|
||||||
@@ -320,7 +368,7 @@ class AppConfig extends Smartjson {
|
|||||||
|
|
||||||
### Decorators
|
### Decorators
|
||||||
|
|
||||||
- `@foldDec()` - Mark class property for serialization
|
- `@foldDec()` - Mark class property for serialization (use with `accessor` keyword)
|
||||||
|
|
||||||
## Performance Tips
|
## Performance Tips
|
||||||
|
|
||||||
@@ -328,7 +376,8 @@ class AppConfig extends Smartjson {
|
|||||||
2. **Enable pretty printing** only for debugging (it's slower)
|
2. **Enable pretty printing** only for debugging (it's slower)
|
||||||
3. **Cache base64 encodings** when repeatedly sending the same data
|
3. **Cache base64 encodings** when repeatedly sending the same data
|
||||||
4. **Use JSON Lines** for streaming large datasets
|
4. **Use JSON Lines** for streaming large datasets
|
||||||
5. **Avoid circular references** in objects being serialized
|
5. **Use stableOneWayStringify** for objects with circular references (for hashing/caching)
|
||||||
|
6. **Prefer regular stringify** for round-trip serialization without cycles
|
||||||
|
|
||||||
## Migration Guide
|
## Migration Guide
|
||||||
|
|
||||||
@@ -350,19 +399,21 @@ This library supports modern browsers and Node.js environments. For older browse
|
|||||||
|
|
||||||
## License and Legal Information
|
## License and Legal Information
|
||||||
|
|
||||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
||||||
|
|
||||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
### Trademarks
|
### Trademarks
|
||||||
|
|
||||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
||||||
|
|
||||||
|
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
||||||
|
|
||||||
### Company Information
|
### Company Information
|
||||||
|
|
||||||
Task Venture Capital GmbH
|
Task Venture Capital GmbH
|
||||||
Registered at District court Bremen HRB 35230 HB, Germany
|
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||||
@@ -3,7 +3,7 @@ import { tap, expect } from '@git.zone/tstest/tapbundle';
|
|||||||
import * as smartjson from '../ts/index.js';
|
import * as smartjson from '../ts/index.js';
|
||||||
|
|
||||||
class SomeClass extends smartjson.Smartjson {
|
class SomeClass extends smartjson.Smartjson {
|
||||||
@smartjson.foldDec() thisis: string = 'test';
|
@smartjson.foldDec() accessor thisis: string = 'test';
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
console.log(this.saveableProperties);
|
console.log(this.saveableProperties);
|
||||||
@@ -72,4 +72,47 @@ tap.test('should work with buffers', async () => {
|
|||||||
expect(text).toEqual('hello');
|
expect(text).toEqual('hello');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should handle empty buffers', async () => {
|
||||||
|
const someObject = { empty: new Uint8Array([]) };
|
||||||
|
const json = smartjson.stringify(someObject);
|
||||||
|
const parsed = smartjson.parse(json);
|
||||||
|
expect(parsed.empty).toBeInstanceOf(Uint8Array);
|
||||||
|
expect(parsed.empty.length).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should parse and stringify JSONL', async () => {
|
||||||
|
const items = [
|
||||||
|
{ id: 1, name: 'a' },
|
||||||
|
{ id: 2, name: 'b' }
|
||||||
|
];
|
||||||
|
const jsonl = smartjson.stringifyJsonL(items);
|
||||||
|
const parsed = smartjson.parseJsonL(jsonl);
|
||||||
|
expect(parsed).toEqual(items);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should deep-compare JSONL strings', async () => {
|
||||||
|
const a = '{"id":2,"name":"b"}\n{"id":1,"name":"a"}';
|
||||||
|
const b = '{"id":2,"name":"b"}\n{"id":1,"name":"a"}';
|
||||||
|
expect(smartjson.deepEqualJsonLStrings(a, b)).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should respect simpleOrderArray comparator', async () => {
|
||||||
|
const obj = { c: 3, a: 1, b: 2 };
|
||||||
|
const ordered = smartjson.stringify(obj, ['b', 'a']);
|
||||||
|
// ensure keys b, a come before c
|
||||||
|
expect(ordered.indexOf('"b"')).toBeLessThan(ordered.indexOf('"a"'));
|
||||||
|
expect(ordered.indexOf('"a"')).toBeLessThan(ordered.indexOf('"c"'));
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('stableOneWayStringify should handle circular references without throwing', async () => {
|
||||||
|
const a: any = { name: 'x' };
|
||||||
|
a.self = a;
|
||||||
|
const b: any = { name: 'x' };
|
||||||
|
b.self = b;
|
||||||
|
const s1 = smartjson.stableOneWayStringify(a);
|
||||||
|
const s2 = smartjson.stableOneWayStringify(b);
|
||||||
|
expect(typeof s1).toEqual('string');
|
||||||
|
expect(s1).toEqual(s2);
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartjson',
|
name: '@push.rocks/smartjson',
|
||||||
version: '5.0.21',
|
version: '6.0.0',
|
||||||
description: 'A library for handling typed JSON data, providing functionalities for parsing, stringifying, and working with JSON objects, including support for encoding and decoding buffers.'
|
description: 'A library for handling typed JSON data, providing functionalities for parsing, stringifying, and working with JSON objects, including support for encoding and decoding buffers.'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,11 +52,7 @@ const replacer: TParseReplacer = (key, value) => {
|
|||||||
|
|
||||||
// Handle IBufferLike objects with a .data property
|
// Handle IBufferLike objects with a .data property
|
||||||
if ('data' in value && isArray(value.data)) {
|
if ('data' in value && isArray(value.data)) {
|
||||||
if (value.data.length > 0) {
|
|
||||||
bufferData = new Uint8Array(value.data);
|
bufferData = new Uint8Array(value.data);
|
||||||
} else {
|
|
||||||
return ''; // Return empty string for empty data arrays
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Handle Uint8Array directly
|
// Handle Uint8Array directly
|
||||||
else if (value instanceof Uint8Array) {
|
else if (value instanceof Uint8Array) {
|
||||||
|
|||||||
130
ts/index.ts
130
ts/index.ts
@@ -22,6 +22,73 @@ export const parseJsonL = (jsonlData: string): JsonObject[] => {
|
|||||||
return parsedData;
|
return parsedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const stringifyJsonL = (items: any[]): string => {
|
||||||
|
return items.map((item) => stringify(item)).join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stableOneWayStringify
|
||||||
|
* - Produces a stable, deterministic JSON string
|
||||||
|
* - Handles circular references without throwing (replaces cycles)
|
||||||
|
* - Safe for hashing/comparisons ("one-way"; not intended for round-trips)
|
||||||
|
*/
|
||||||
|
export const stableOneWayStringify = (
|
||||||
|
objArg: any,
|
||||||
|
simpleOrderArray?: string[],
|
||||||
|
optionsArg: plugins.IStableJsonTypes['Options'] = {}
|
||||||
|
): string => {
|
||||||
|
// Prepare object without throwing on circular references, and encode buffers
|
||||||
|
const visited = new WeakSet<object>();
|
||||||
|
const sanitize = (val: any): any => {
|
||||||
|
// primitives
|
||||||
|
if (val === null || typeof val !== 'object') {
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
// Encode buffers/typed arrays via existing replacer
|
||||||
|
const replaced = (bufferhandling.replacer as any)('', val);
|
||||||
|
if (replaced && replaced.type === 'EncodedBuffer' && typeof replaced.data === 'string') {
|
||||||
|
return replaced;
|
||||||
|
}
|
||||||
|
// Handle circular references
|
||||||
|
if (visited.has(val)) {
|
||||||
|
return '__cycle__';
|
||||||
|
}
|
||||||
|
visited.add(val);
|
||||||
|
// Arrays
|
||||||
|
if (Array.isArray(val)) {
|
||||||
|
return val.map((item) => sanitize(item));
|
||||||
|
}
|
||||||
|
// Plain objects and class instances: copy enumerable own props
|
||||||
|
const out: Record<string, any> = {};
|
||||||
|
for (const key of Object.keys(val)) {
|
||||||
|
try {
|
||||||
|
out[key] = sanitize((val as any)[key]);
|
||||||
|
} catch (e) {
|
||||||
|
// In case of getters throwing or non-serializable, mark
|
||||||
|
out[key] = '__unserializable__';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
};
|
||||||
|
|
||||||
|
const obj = sanitize(objArg);
|
||||||
|
const options: plugins.IStableJsonTypes['Options'] = {
|
||||||
|
...optionsArg,
|
||||||
|
cycles: true,
|
||||||
|
};
|
||||||
|
if (simpleOrderArray && !options.cmp) {
|
||||||
|
const order = new Map<string, number>();
|
||||||
|
simpleOrderArray.forEach((key, idx) => order.set(key, idx));
|
||||||
|
options.cmp = (a, b) => {
|
||||||
|
const aIdx = order.has(a.key) ? (order.get(a.key) as number) : Number.POSITIVE_INFINITY;
|
||||||
|
const bIdx = order.has(b.key) ? (order.get(b.key) as number) : Number.POSITIVE_INFINITY;
|
||||||
|
if (aIdx !== bIdx) return aIdx - bIdx;
|
||||||
|
return a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return plugins.stableJson(obj, options);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param objArg
|
* @param objArg
|
||||||
@@ -34,7 +101,20 @@ export const stringify = (
|
|||||||
): string => {
|
): string => {
|
||||||
const bufferedJson = bufferhandling.stringify(objArg);
|
const bufferedJson = bufferhandling.stringify(objArg);
|
||||||
objArg = JSON.parse(bufferedJson);
|
objArg = JSON.parse(bufferedJson);
|
||||||
let returnJson = plugins.stableJson(objArg, optionsArg);
|
// derive a simple comparator from simpleOrderArray if provided and no custom cmp supplied
|
||||||
|
let options = { ...optionsArg };
|
||||||
|
if (simpleOrderArray && !options.cmp) {
|
||||||
|
const order = new Map<string, number>();
|
||||||
|
simpleOrderArray.forEach((key, idx) => order.set(key, idx));
|
||||||
|
options.cmp = (a, b) => {
|
||||||
|
const aIdx = order.has(a.key) ? (order.get(a.key) as number) : Number.POSITIVE_INFINITY;
|
||||||
|
const bIdx = order.has(b.key) ? (order.get(b.key) as number) : Number.POSITIVE_INFINITY;
|
||||||
|
if (aIdx !== bIdx) return aIdx - bIdx;
|
||||||
|
// fallback to lexicographic order for stable behavior
|
||||||
|
return a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let returnJson = plugins.stableJson(objArg, options);
|
||||||
return returnJson;
|
return returnJson;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -62,9 +142,10 @@ export class Smartjson {
|
|||||||
*/
|
*/
|
||||||
public static enfoldFromObject<T extends typeof Smartjson>(this: T, objectArg: any): InstanceType<T> {
|
public static enfoldFromObject<T extends typeof Smartjson>(this: T, objectArg: any): InstanceType<T> {
|
||||||
const newInstance = new this() as InstanceType<T>;
|
const newInstance = new this() as InstanceType<T>;
|
||||||
|
const saveables: string[] = (newInstance as any).saveableProperties || [];
|
||||||
for (const keyName in objectArg) {
|
for (const keyName in objectArg) {
|
||||||
if (newInstance.saveableProperties.indexOf(keyName) !== -1) {
|
if (saveables.indexOf(keyName) !== -1) {
|
||||||
newInstance[keyName] = objectArg[keyName];
|
(newInstance as any)[keyName] = objectArg[keyName];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newInstance;
|
return newInstance;
|
||||||
@@ -88,26 +169,9 @@ export class Smartjson {
|
|||||||
* folds a class into an object
|
* folds a class into an object
|
||||||
*/
|
*/
|
||||||
public foldToObject() {
|
public foldToObject() {
|
||||||
const newFoldedObject: { [key: string]: any } = {};
|
|
||||||
const trackSet = new Set<Smartjson>();
|
const trackSet = new Set<Smartjson>();
|
||||||
const foldValue = (val: any): any => {
|
trackSet.add(this);
|
||||||
if (val instanceof Smartjson) {
|
return this.foldToObjectInternal(trackSet);
|
||||||
if (trackSet.has(val)) {
|
|
||||||
throw new Error('cycle detected');
|
|
||||||
}
|
|
||||||
trackSet.add(val);
|
|
||||||
return val.foldToObjectInternal(trackSet);
|
|
||||||
}
|
|
||||||
if (Array.isArray(val)) {
|
|
||||||
return val.map((item) => foldValue(item));
|
|
||||||
}
|
|
||||||
return plugins.lodashCloneDeep(val);
|
|
||||||
};
|
|
||||||
for (const keyName of this.saveableProperties) {
|
|
||||||
const value = this[keyName];
|
|
||||||
newFoldedObject[keyName] = foldValue(value);
|
|
||||||
}
|
|
||||||
return newFoldedObject;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private foldToObjectInternal(trackSet: Set<Smartjson>) {
|
private foldToObjectInternal(trackSet: Set<Smartjson>) {
|
||||||
@@ -125,7 +189,8 @@ export class Smartjson {
|
|||||||
}
|
}
|
||||||
return plugins.lodashCloneDeep(val);
|
return plugins.lodashCloneDeep(val);
|
||||||
};
|
};
|
||||||
for (const keyName of this.saveableProperties) {
|
const props: string[] = (this as any).saveableProperties || [];
|
||||||
|
for (const keyName of props) {
|
||||||
const value = this[keyName];
|
const value = this[keyName];
|
||||||
result[keyName] = foldValue(value);
|
result[keyName] = foldValue(value);
|
||||||
}
|
}
|
||||||
@@ -142,14 +207,23 @@ export class Smartjson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decorator that marks a property as foldable
|
* Decorator that marks a property as foldable (TC39 Stage 3 decorator)
|
||||||
|
* Use with the `accessor` keyword: @foldDec() accessor myProp: string;
|
||||||
*/
|
*/
|
||||||
export const foldDec = () => {
|
export const foldDec = () => {
|
||||||
return (target: any, key: string) => {
|
return <This extends Smartjson, Value>(
|
||||||
if (!target.saveableProperties) {
|
_value: ClassAccessorDecoratorTarget<This, Value>,
|
||||||
target.saveableProperties = [];
|
context: ClassAccessorDecoratorContext<This, Value>
|
||||||
|
): ClassAccessorDecoratorResult<This, Value> | void => {
|
||||||
|
const propertyName = String(context.name);
|
||||||
|
context.addInitializer(function (this: This) {
|
||||||
|
if (!this.saveableProperties) {
|
||||||
|
this.saveableProperties = [];
|
||||||
}
|
}
|
||||||
target.saveableProperties.push(key);
|
if (!this.saveableProperties.includes(propertyName)) {
|
||||||
|
this.saveableProperties.push(propertyName);
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"experimentalDecorators": true,
|
|
||||||
"useDefineForClassFields": false,
|
|
||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "NodeNext",
|
"module": "NodeNext",
|
||||||
"moduleResolution": "NodeNext",
|
"moduleResolution": "NodeNext",
|
||||||
|
|||||||
Reference in New Issue
Block a user