Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
58322d23f4 | |||
2f012fc0ad | |||
9bb6e0b497 | |||
5275e13360 | |||
03fe2174d1 | |||
7df82ce64d | |||
09b74e4053 | |||
096cd74773 | |||
e9a45ac993 |
207
changelog.md
Normal file
207
changelog.md
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-09-12 - 5.0.21 - fix(Smartjson)
|
||||||
|
Cross-platform buffer/base64 handling, safer folding with cycle detection, parsing fixes, docs and dependency updates
|
||||||
|
|
||||||
|
- bufferhandling: prefer Node Buffer for base64 encode/decode when available and fall back to browser APIs for cross-platform support
|
||||||
|
- parseJsonL: more robust parsing of JSON Lines (trim lines and use smartjson.parse to restore buffers/typed arrays)
|
||||||
|
- parseBase64: use smartstring.base64.decodeUri or decode fallback before parsing, then parse result via smartjson.parse
|
||||||
|
- Smartjson class: add TypeScript generics to enfold methods, introduce foldToObjectInternal and robust cycle detection using Set, properly handle arrays and deep-clone values
|
||||||
|
- Tests: updated tap import path to @git.zone/tstest/tapbundle
|
||||||
|
- package.json: bumped several devDependencies and dependency versions and added packageManager pin
|
||||||
|
- Documentation: expanded README with examples, API reference and usage guidance
|
||||||
|
- Repo config: added pnpm-workspace.yaml and .claude/settings.local.json
|
||||||
|
|
||||||
|
## 2024-05-29 - 5.0.20 - docs
|
||||||
|
Update package description.
|
||||||
|
|
||||||
|
- Update package description text.
|
||||||
|
|
||||||
|
## 2024-05-27 - 5.0.19 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-04-17 - 5.0.18 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-04-17 - 5.0.17 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-04-17 - 5.0.16 - build/core
|
||||||
|
Build/config updates and core fixes.
|
||||||
|
|
||||||
|
- Updated tsconfig configuration (2024-04-14).
|
||||||
|
- Updated npmextra.json githost entries (multiple updates in early April).
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-03-19 - 5.0.15 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-03-19 - 5.0.14 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-03-03 - 5.0.13 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-03-03 - 5.0.12 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-03-03 - 5.0.11 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2024-02-25 - 5.0.10 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2023-08-24 - 5.0.9 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2023-08-19 - 5.0.8 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2023-08-19 - 5.0.7 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2023-07-10 - 5.0.6 - infra/core
|
||||||
|
Organization and core updates.
|
||||||
|
|
||||||
|
- Switched to new organization scheme.
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2023-06-03 - 5.0.5 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2022-10-26 - 5.0.4 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2022-10-26 - 5.0.3 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2022-10-26 - 5.0.2 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2022-09-13 - 5.0.1 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2022-06-26 - 5.0.0 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2022-06-09 - 4.0.7 - core (BREAKING CHANGE)
|
||||||
|
Breaking change in core.
|
||||||
|
|
||||||
|
- BREAKING CHANGE: core-related update (see code/compat notes).
|
||||||
|
- Miscellaneous core updates.
|
||||||
|
|
||||||
|
## 2022-06-09 - 4.0.6 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2020-10-05 - 4.0.4 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2020-10-05 - 4.0.3 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2020-10-05 - 4.0.2 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2020-10-05 - 4.0.1 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2020-10-05 - 4.0.0 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2020-10-05 - 3.0.12 - core (BREAKING CHANGE)
|
||||||
|
Breaking change in core.
|
||||||
|
|
||||||
|
- BREAKING CHANGE: core-related update (see migration notes).
|
||||||
|
- Miscellaneous core updates.
|
||||||
|
|
||||||
|
## 2020-10-03 - 3.0.11 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2020-10-03 - 3.0.10 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2019-02-14 - 3.0.0 - security
|
||||||
|
Security-related update.
|
||||||
|
|
||||||
|
- Added snyk policy for security scanning.
|
||||||
|
|
||||||
|
## 2019-02-14 - 2.0.2 - core (BREAKING CHANGE)
|
||||||
|
Breaking change in core API.
|
||||||
|
|
||||||
|
- BREAKING CHANGE: renamed Folable to Smartjson and added deterministic stringify.
|
||||||
|
- Update consumers accordingly.
|
||||||
|
|
||||||
|
## 2018-09-05 - 2.0.1 - core
|
||||||
|
Core fixes.
|
||||||
|
|
||||||
|
- Miscellaneous core updates and fixes.
|
||||||
|
|
||||||
|
## 2018-07-23 - 1.0.1 - scope (BREAKING CHANGE)
|
||||||
|
Scope change.
|
||||||
|
|
||||||
|
- BREAKING CHANGE: changed package scope.
|
||||||
|
- See migration notes for scope changes.
|
||||||
|
|
||||||
|
## 2017-02-27 - 1.0.0 - docs/init
|
||||||
|
Initial release.
|
||||||
|
|
||||||
|
- Added README.
|
||||||
|
- Initial project files and setup.
|
||||||
|
|
||||||
|
## 2018-07-23 — 2019-12-15 - 2.0.0, 3.0.1, 3.0.9 - version-only releases
|
||||||
|
Version-only tags / non-descriptive releases.
|
||||||
|
|
||||||
|
- These releases are recorded as version bumps without additional changelog details.
|
||||||
|
- Affected tags: 2.0.0, 3.0.1, 3.0.9.
|
22
package.json
22
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartjson",
|
"name": "@push.rocks/smartjson",
|
||||||
"version": "5.0.17",
|
"version": "5.0.21",
|
||||||
"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",
|
||||||
@@ -12,24 +12,23 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+ssh://git@gitlab.com/pushrocks/smartjson.git"
|
"url": "https://code.foss.global/push.rocks/smartjson.git"
|
||||||
},
|
},
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://gitlab.com/pushrocks/smartjson/issues"
|
"url": "https://gitlab.com/pushrocks/smartjson/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartjson#README",
|
"homepage": "https://code.foss.global/push.rocks/smartjson",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.1.66",
|
"@git.zone/tsbuild": "^2.6.8",
|
||||||
"@git.zone/tsrun": "^1.2.44",
|
"@git.zone/tsrun": "^1.3.3",
|
||||||
"@git.zone/tstest": "^1.0.88",
|
"@git.zone/tstest": "^2.3.8",
|
||||||
"@push.rocks/tapbundle": "^5.0.23",
|
"@types/node": "^22"
|
||||||
"@types/node": "^20.12.7"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/smartenv": "^5.0.12",
|
"@push.rocks/smartenv": "^5.0.13",
|
||||||
"@push.rocks/smartstring": "^4.0.15",
|
"@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"
|
||||||
},
|
},
|
||||||
@@ -61,5 +60,6 @@
|
|||||||
"TypeScript support",
|
"TypeScript support",
|
||||||
"data encoding",
|
"data encoding",
|
||||||
"data decoding"
|
"data decoding"
|
||||||
]
|
],
|
||||||
|
"packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748"
|
||||||
}
|
}
|
||||||
|
11017
pnpm-lock.yaml
generated
11017
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
3
pnpm-workspace.yaml
Normal file
3
pnpm-workspace.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
onlyBuiltDependencies:
|
||||||
|
- esbuild
|
||||||
|
- puppeteer
|
395
readme.md
395
readme.md
@@ -1,125 +1,352 @@
|
|||||||
# @push.rocks/smartjson
|
# @push.rocks/smartjson
|
||||||
typed json handlers
|
**🚀 Typed JSON handling for modern Node.js and TypeScript applications**
|
||||||
|
|
||||||
## Install
|
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.
|
||||||
|
|
||||||
To install `@push.rocks/smartjson`, you can use npm or yarn as follows:
|
## Installation
|
||||||
|
|
||||||
```shell
|
```bash
|
||||||
|
# Using npm
|
||||||
npm install @push.rocks/smartjson --save
|
npm install @push.rocks/smartjson --save
|
||||||
# or using yarn
|
|
||||||
|
# Using yarn
|
||||||
yarn add @push.rocks/smartjson
|
yarn add @push.rocks/smartjson
|
||||||
|
|
||||||
|
# Using pnpm (recommended)
|
||||||
|
pnpm add @push.rocks/smartjson
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Features
|
||||||
|
|
||||||
`@push.rocks/smartjson` offers typed JSON handling, including features like folding and enfolding classes from JSON, JSON parsing with support for buffers, and comparison of JSON objects for equality. This guide will walk through various use cases and scenarios to effectively utilize `@push.rocks/smartjson` in your projects.
|
✨ **Type-Safe JSON Operations** - Full TypeScript support with proper typing
|
||||||
|
🎯 **Class Instance Serialization** - Fold and unfold class instances to/from JSON
|
||||||
|
🔐 **Buffer & Binary Support** - Seamless handling of Buffers and Typed Arrays
|
||||||
|
📊 **JSON Lines Support** - Parse and compare JSONL data streams
|
||||||
|
🎨 **Pretty Printing** - Beautiful formatted JSON output
|
||||||
|
⚡ **Stable Stringification** - Consistent key ordering for reliable comparisons
|
||||||
|
🔍 **Deep Equality Checks** - Compare complex objects and JSON structures
|
||||||
|
🌐 **Base64 Encoding** - Built-in base64 JSON encoding/decoding
|
||||||
|
|
||||||
### Basic Import
|
## Quick Start
|
||||||
|
|
||||||
First, make sure to import the module:
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import * as smartjson from '@push.rocks/smartjson';
|
import * as smartjson from '@push.rocks/smartjson';
|
||||||
|
|
||||||
|
// Parse JSON with automatic buffer handling
|
||||||
|
const parsed = smartjson.parse('{"name":"example","data":{"type":"Buffer","data":[1,2,3]}}');
|
||||||
|
|
||||||
|
// Stringify with stable key ordering
|
||||||
|
const json = smartjson.stringify({ z: 1, a: 2, m: 3 });
|
||||||
|
// Result: '{"a":2,"m":3,"z":1}'
|
||||||
|
|
||||||
|
// Pretty print for human readability
|
||||||
|
const pretty = smartjson.stringifyPretty({ hello: 'world', count: 42 });
|
||||||
|
// Result:
|
||||||
|
// {
|
||||||
|
// "hello": "world",
|
||||||
|
// "count": 42
|
||||||
|
// }
|
||||||
```
|
```
|
||||||
|
|
||||||
### Parsing and Stringifying JSON
|
## Core Functions
|
||||||
|
|
||||||
- **Parsing JSON Strings:**
|
### JSON Parsing and Stringification
|
||||||
|
|
||||||
`smartjson` enhances JSON parsing by supporting JavaScript's typed arrays, particularly with `Buffer` handling.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const jsonString = '{"type":"Buffer","data":[116,101,115,116]}';
|
|
||||||
const parsedObject = smartjson.parse(jsonString);
|
|
||||||
console.log(parsedObject); // Output will be based on the content of jsonString
|
|
||||||
```
|
|
||||||
|
|
||||||
- **Stringifying Objects:**
|
|
||||||
|
|
||||||
`@push.rocks/smartjson` provides a `stringify` function that can convert JavaScript objects into JSON strings, with special handling for `Buffer` objects.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const myObject = {
|
|
||||||
exampleBuffer: new Uint8Array([116, 101, 115, 116])
|
|
||||||
};
|
|
||||||
const jsonString = smartjson.stringify(myObject);
|
|
||||||
console.log(jsonString); // Will include `exampleBuffer` encoded in a special format
|
|
||||||
```
|
|
||||||
|
|
||||||
### Working with Base64 Encoded JSON
|
|
||||||
|
|
||||||
For cases where JSON strings are encoded in base64 format, `smartjson` offers methods to encode and decode these strings transparently.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const objectToEncode = { hello: 'world' };
|
// Standard parsing with automatic Buffer detection
|
||||||
const base64EncodedJson = smartjson.stringifyBase64(objectToEncode);
|
const obj = smartjson.parse('{"hello":"world"}');
|
||||||
console.log(base64EncodedJson); // Encoded JSON string
|
|
||||||
|
|
||||||
const decodedObject = smartjson.parseBase64(base64EncodedJson);
|
// Stable stringification (consistent key ordering)
|
||||||
console.log(decodedObject); // Original object
|
const jsonStr = smartjson.stringify({ name: 'test', id: 1 });
|
||||||
|
|
||||||
|
// Pretty printing for debugging
|
||||||
|
const prettyJson = smartjson.stringifyPretty({
|
||||||
|
nested: {
|
||||||
|
data: 'value'
|
||||||
|
}
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Folding and Enfolding Classes
|
### Base64 JSON Encoding
|
||||||
|
|
||||||
`@push.rocks/smartjson` allows you to fold (serialize) and enfold (deserialize) class instances to and from JSON. This is particularly useful when working with typed objects and you need to maintain type integrity across serialization.
|
Encode JSON data as base64 for safe transmission:
|
||||||
|
|
||||||
- **Defining a Foldable Class:**
|
```typescript
|
||||||
|
const myData = {
|
||||||
|
message: 'Hello World',
|
||||||
|
timestamp: Date.now()
|
||||||
|
};
|
||||||
|
|
||||||
Decorate properties that should be included in JSON with `@smartjson.foldDec()`.
|
// Encode to base64
|
||||||
|
const encoded = smartjson.stringifyBase64(myData);
|
||||||
|
console.log(encoded); // Base64 string
|
||||||
|
|
||||||
```typescript
|
// Decode back to object
|
||||||
import { Smartjson, foldDec } from '@push.rocks/smartjson';
|
const decoded = smartjson.parseBase64(encoded);
|
||||||
|
console.log(decoded); // Original object
|
||||||
|
```
|
||||||
|
|
||||||
class MyDataModel extends Smartjson {
|
### JSON Lines (JSONL) Support
|
||||||
@foldDec() public someProperty: string = 'default';
|
|
||||||
|
Perfect for streaming data and log processing:
|
||||||
constructor(public id: number, someProperty?: string) {
|
|
||||||
super();
|
```typescript
|
||||||
if (someProperty) this.someProperty = someProperty;
|
// Parse JSON Lines format
|
||||||
}
|
const jsonLines = `{"event":"start","time":1234}
|
||||||
|
{"event":"data","value":42}
|
||||||
|
{"event":"end","time":5678}`;
|
||||||
|
|
||||||
|
const events = smartjson.parseJsonL(jsonLines);
|
||||||
|
// Result: Array of parsed objects
|
||||||
|
|
||||||
|
// Compare JSON Lines data
|
||||||
|
const jsonL1 = `{"id":1}\n{"id":2}`;
|
||||||
|
const jsonL2 = `{"id":1}\n{"id":2}`;
|
||||||
|
const isEqual = smartjson.deepEqualJsonLStrings(jsonL1, jsonL2); // true
|
||||||
|
```
|
||||||
|
|
||||||
|
## Advanced Class Serialization
|
||||||
|
|
||||||
|
### Creating Serializable Classes
|
||||||
|
|
||||||
|
Transform class instances to JSON and back while preserving type safety:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartjson, foldDec } from '@push.rocks/smartjson';
|
||||||
|
|
||||||
|
class User extends Smartjson {
|
||||||
|
@foldDec() public username: string;
|
||||||
|
@foldDec() public email: string;
|
||||||
|
@foldDec() public settings: UserSettings;
|
||||||
|
|
||||||
|
// Properties without @foldDec won't be serialized
|
||||||
|
private internalId: string;
|
||||||
|
|
||||||
|
constructor(username: string, email: string) {
|
||||||
|
super();
|
||||||
|
this.username = username;
|
||||||
|
this.email = email;
|
||||||
|
this.settings = new UserSettings();
|
||||||
|
this.internalId = Math.random().toString();
|
||||||
}
|
}
|
||||||
```
|
}
|
||||||
|
|
||||||
- **Folding and Enfolding Instances:**
|
class UserSettings extends Smartjson {
|
||||||
|
@foldDec() public theme: 'light' | 'dark' = 'light';
|
||||||
|
@foldDec() public notifications: boolean = true;
|
||||||
|
}
|
||||||
|
|
||||||
```typescript
|
// Create and serialize
|
||||||
const instance = new MyDataModel(1, 'value');
|
const user = new User('john_doe', 'john@example.com');
|
||||||
const folded = instance.foldToJson(); // Serialize to JSON
|
user.settings.theme = 'dark';
|
||||||
console.log(folded);
|
|
||||||
|
|
||||||
const enfoldedInstance = MyDataModel.enfoldFromJson(folded); // Deserialize back to instance
|
// Convert to JSON
|
||||||
console.log(enfoldedInstance);
|
const jsonString = user.foldToJson();
|
||||||
```
|
console.log(jsonString);
|
||||||
|
// {"username":"john_doe","email":"john@example.com","settings":{"theme":"dark","notifications":true}}
|
||||||
|
|
||||||
### Deep Comparison
|
// Restore from JSON with correct typing
|
||||||
|
const restoredUser = User.enfoldFromJson(jsonString);
|
||||||
`@push.rocks/smartjson` enables deep comparison of objects to determine if they are equivalent.
|
console.log(restoredUser instanceof User); // true
|
||||||
|
console.log(restoredUser.settings instanceof UserSettings); // true
|
||||||
```typescript
|
|
||||||
const obj1 = { a: 1, b: { c: 2 }};
|
|
||||||
const obj2 = { a: 1, b: { c: 2 }};
|
|
||||||
|
|
||||||
const isEqual = smartjson.deepEqualObjects(obj1, obj2);
|
|
||||||
console.log(isEqual); // true
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Handling Buffers and Typed Arrays
|
### Working with Nested Objects
|
||||||
|
|
||||||
`smartjson` transparently handles JavaScript's `Buffer` and Typed Arrays during JSON serialization, making it effortless to work with binary data in JSON format.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const buffer = new Uint8Array([1, 2, 3]);
|
class Company extends Smartjson {
|
||||||
const objWithBuffer = { key: buffer };
|
@foldDec() public name: string;
|
||||||
const serialized = smartjson.stringify(objWithBuffer);
|
@foldDec() public employees: Employee[] = [];
|
||||||
|
|
||||||
|
addEmployee(employee: Employee) {
|
||||||
|
this.employees.push(employee);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const deserialized = smartjson.parse(serialized);
|
class Employee extends Smartjson {
|
||||||
console.log(deserialized.key); // Instance of Uint8Array or Buffer
|
@foldDec() public name: string;
|
||||||
|
@foldDec() public role: string;
|
||||||
|
@foldDec() public salary: number;
|
||||||
|
|
||||||
|
constructor(name: string, role: string, salary: number) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.role = role;
|
||||||
|
this.salary = salary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const company = new Company();
|
||||||
|
company.name = 'TechCorp';
|
||||||
|
company.addEmployee(new Employee('Alice', 'Developer', 100000));
|
||||||
|
company.addEmployee(new Employee('Bob', 'Designer', 90000));
|
||||||
|
|
||||||
|
// Serialize entire object graph
|
||||||
|
const json = company.foldToJson();
|
||||||
|
|
||||||
|
// Deserialize with all nested objects properly instantiated
|
||||||
|
const restored = Company.enfoldFromJson(json);
|
||||||
```
|
```
|
||||||
|
|
||||||
In addition to these features, `@push.rocks/smartjson` supports efficient base64 encoding/decoding, deep object comparison, and JSON Lines parsing, making it a versatile library for dealing with JSON in TypeScript projects.
|
## Buffer and Binary Data Handling
|
||||||
|
|
||||||
For further information and more detailed examples, referring to the API documentation and the source code on [GitLab](https://gitlab.com/push.rocks/smartjson) can provide deeper insights into `@push.rocks/smartjson`'s capabilities.
|
SmartJson seamlessly handles binary data in JSON:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Automatic Buffer handling
|
||||||
|
const dataWithBuffer = {
|
||||||
|
name: 'BinaryData',
|
||||||
|
buffer: Buffer.from('Hello World'),
|
||||||
|
typedArray: new Uint8Array([1, 2, 3, 4, 5])
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stringify (buffers are automatically encoded)
|
||||||
|
const jsonStr = smartjson.stringify(dataWithBuffer);
|
||||||
|
|
||||||
|
// Parse (buffers are automatically restored)
|
||||||
|
const restored = smartjson.parse(jsonStr);
|
||||||
|
console.log(restored.buffer); // Buffer
|
||||||
|
console.log(restored.typedArray); // Uint8Array
|
||||||
|
```
|
||||||
|
|
||||||
|
## Deep Comparison
|
||||||
|
|
||||||
|
Compare complex objects with automatic normalization:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Deep object comparison
|
||||||
|
const obj1 = {
|
||||||
|
nested: {
|
||||||
|
array: [1, 2, { deep: 'value' }],
|
||||||
|
flag: true
|
||||||
|
},
|
||||||
|
name: 'test'
|
||||||
|
};
|
||||||
|
|
||||||
|
const obj2 = {
|
||||||
|
name: 'test', // Different order
|
||||||
|
nested: {
|
||||||
|
flag: true, // Different order
|
||||||
|
array: [1, 2, { deep: 'value' }]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const isEqual = smartjson.deepEqualObjects(obj1, obj2); // true
|
||||||
|
```
|
||||||
|
|
||||||
|
## Real-World Examples
|
||||||
|
|
||||||
|
### API Response Caching
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class CachedAPIResponse extends Smartjson {
|
||||||
|
@foldDec() public data: any;
|
||||||
|
@foldDec() public timestamp: number;
|
||||||
|
@foldDec() public endpoint: string;
|
||||||
|
|
||||||
|
isExpired(maxAge: number = 3600000): boolean {
|
||||||
|
return Date.now() - this.timestamp > maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromAPICall(endpoint: string, data: any): CachedAPIResponse {
|
||||||
|
const response = new CachedAPIResponse();
|
||||||
|
response.endpoint = endpoint;
|
||||||
|
response.data = data;
|
||||||
|
response.timestamp = Date.now();
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store API response
|
||||||
|
const apiData = await fetch('/api/users');
|
||||||
|
const cached = CachedAPIResponse.fromAPICall('/api/users', await apiData.json());
|
||||||
|
localStorage.setItem('cached_users', cached.foldToJson());
|
||||||
|
|
||||||
|
// Retrieve and check
|
||||||
|
const stored = localStorage.getItem('cached_users');
|
||||||
|
if (stored) {
|
||||||
|
const cached = CachedAPIResponse.enfoldFromJson(stored);
|
||||||
|
if (!cached.isExpired()) {
|
||||||
|
return cached.data; // Use cached data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration Management
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class AppConfig extends Smartjson {
|
||||||
|
@foldDec() public apiUrl: string;
|
||||||
|
@foldDec() public features: Map<string, boolean> = new Map();
|
||||||
|
@foldDec() public limits: {
|
||||||
|
maxUploadSize: number;
|
||||||
|
maxConcurrentRequests: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
enableFeature(name: string) {
|
||||||
|
this.features.set(name, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
save() {
|
||||||
|
fs.writeFileSync('config.json', this.foldToJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
static load(): AppConfig {
|
||||||
|
const json = fs.readFileSync('config.json', 'utf-8');
|
||||||
|
return AppConfig.enfoldFromJson(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### Core Functions
|
||||||
|
|
||||||
|
- `parse(jsonString: string): any` - Parse JSON with automatic buffer handling
|
||||||
|
- `stringify(obj: any, simpleOrderArray?: string[], options?: Options): string` - Convert to JSON with stable ordering
|
||||||
|
- `stringifyPretty(obj: any): string` - Pretty print JSON with 2-space indentation
|
||||||
|
- `stringifyBase64(obj: any): string` - Encode JSON as base64
|
||||||
|
- `parseBase64(base64String: string): any` - Decode base64 JSON
|
||||||
|
- `parseJsonL(jsonLinesString: string): any[]` - Parse JSON Lines format
|
||||||
|
- `deepEqualObjects(obj1: any, obj2: any): boolean` - Deep comparison of objects
|
||||||
|
- `deepEqualJsonLStrings(jsonL1: string, jsonL2: string): boolean` - Compare JSON Lines strings
|
||||||
|
|
||||||
|
### Smartjson Class
|
||||||
|
|
||||||
|
- `Smartjson.enfoldFromObject<T>(obj: any): T` - Create instance from plain object
|
||||||
|
- `Smartjson.enfoldFromJson<T>(json: string): T` - Create instance from JSON string
|
||||||
|
- `instance.foldToObject(): any` - Convert instance to plain object
|
||||||
|
- `instance.foldToJson(): string` - Convert instance to JSON string
|
||||||
|
|
||||||
|
### Decorators
|
||||||
|
|
||||||
|
- `@foldDec()` - Mark class property for serialization
|
||||||
|
|
||||||
|
## Performance Tips
|
||||||
|
|
||||||
|
1. **Use stable stringification** for consistent hashing and comparison
|
||||||
|
2. **Enable pretty printing** only for debugging (it's slower)
|
||||||
|
3. **Cache base64 encodings** when repeatedly sending the same data
|
||||||
|
4. **Use JSON Lines** for streaming large datasets
|
||||||
|
5. **Avoid circular references** in objects being serialized
|
||||||
|
|
||||||
|
## Migration Guide
|
||||||
|
|
||||||
|
If you're migrating from native JSON:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Before
|
||||||
|
JSON.parse(jsonString);
|
||||||
|
JSON.stringify(object);
|
||||||
|
|
||||||
|
// After
|
||||||
|
smartjson.parse(jsonString); // Adds buffer support
|
||||||
|
smartjson.stringify(object); // Adds stable ordering & buffer support
|
||||||
|
```
|
||||||
|
|
||||||
|
## Browser Support
|
||||||
|
|
||||||
|
This library supports modern browsers and Node.js environments. For older browsers, ensure you have appropriate polyfills for `TextEncoder` and `TextDecoder`.
|
||||||
|
|
||||||
## License and Legal Information
|
## License and Legal Information
|
||||||
|
|
||||||
@@ -138,4 +365,4 @@ 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 if you require 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.
|
@@ -1,4 +1,4 @@
|
|||||||
import { tap, expect } from '@push.rocks/tapbundle';
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||||
|
|
||||||
import * as smartjson from '../ts/index.js';
|
import * as smartjson from '../ts/index.js';
|
||||||
|
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* autocreated commitinfo by @pushrocks/commitinfo
|
* autocreated commitinfo by @push.rocks/commitinfo
|
||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartjson',
|
name: '@push.rocks/smartjson',
|
||||||
version: '5.0.17',
|
version: '5.0.21',
|
||||||
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.'
|
||||||
}
|
}
|
||||||
|
@@ -16,10 +16,23 @@ type TParseReplacer = (this: any, key: string, value: any) => any;
|
|||||||
|
|
||||||
// Utility functions to handle base64 encoding/decoding in a cross-platform way
|
// Utility functions to handle base64 encoding/decoding in a cross-platform way
|
||||||
function base64Encode(data: Uint8Array): string {
|
function base64Encode(data: Uint8Array): string {
|
||||||
|
// Prefer Node's Buffer when available
|
||||||
|
if (typeof Buffer !== 'undefined') {
|
||||||
|
// @ts-ignore Buffer might not exist in browser builds
|
||||||
|
return Buffer.from(data).toString('base64');
|
||||||
|
}
|
||||||
|
// Fallback for browsers
|
||||||
return btoa(String.fromCharCode(...data));
|
return btoa(String.fromCharCode(...data));
|
||||||
}
|
}
|
||||||
|
|
||||||
function base64Decode(str: string): Uint8Array {
|
function base64Decode(str: string): Uint8Array {
|
||||||
|
// Prefer Node's Buffer when available
|
||||||
|
if (typeof Buffer !== 'undefined') {
|
||||||
|
// @ts-ignore Buffer might not exist in browser builds
|
||||||
|
const buf = Buffer.from(str, 'base64');
|
||||||
|
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
||||||
|
}
|
||||||
|
// Fallback for browsers
|
||||||
return new Uint8Array(Array.from(atob(str)).map((char) => char.charCodeAt(0)));
|
return new Uint8Array(Array.from(atob(str)).map((char) => char.charCodeAt(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
78
ts/index.ts
78
ts/index.ts
@@ -1,6 +1,5 @@
|
|||||||
import * as plugins from './smartjson.plugins.js';
|
import * as plugins from './smartjson.plugins.js';
|
||||||
import * as bufferhandling from './bufferhandling.js';
|
import * as bufferhandling from './bufferhandling.js';
|
||||||
import { json } from 'stream/consumers';
|
|
||||||
|
|
||||||
interface JsonObject {
|
interface JsonObject {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
@@ -12,10 +11,11 @@ interface JsonObject {
|
|||||||
export const parse = bufferhandling.parse;
|
export const parse = bufferhandling.parse;
|
||||||
|
|
||||||
export const parseJsonL = (jsonlData: string): JsonObject[] => {
|
export const parseJsonL = (jsonlData: string): JsonObject[] => {
|
||||||
const lines = jsonlData.trim().split('\n');
|
const lines = jsonlData.split('\n');
|
||||||
const parsedData: JsonObject[] = lines.reduce((acc, line) => {
|
const parsedData: JsonObject[] = lines.reduce((acc, line) => {
|
||||||
if (line.trim().length > 0) {
|
const trimmed = line.trim();
|
||||||
acc.push(JSON.parse(line));
|
if (trimmed.length > 0) {
|
||||||
|
acc.push(parse(trimmed));
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}, [] as JsonObject[]);
|
}, [] as JsonObject[]);
|
||||||
@@ -50,18 +50,18 @@ export const stringifyBase64 = (...args: Parameters<typeof stringify>): string =
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const parseBase64 = (base64JsonStringArg: string) => {
|
export const parseBase64 = (base64JsonStringArg: string) => {
|
||||||
const simpleStringified = plugins.smartstring.base64.decode(base64JsonStringArg);
|
const base64 = plugins.smartstring.base64 as any;
|
||||||
|
const decodeFn: (input: string) => string = base64.decodeUri || base64.decode;
|
||||||
|
const simpleStringified = decodeFn(base64JsonStringArg);
|
||||||
return parse(simpleStringified);
|
return parse(simpleStringified);
|
||||||
};
|
};
|
||||||
|
|
||||||
parse;
|
|
||||||
|
|
||||||
export class Smartjson {
|
export class Smartjson {
|
||||||
/**
|
/**
|
||||||
* enfolds data from an object
|
* enfolds data from an object
|
||||||
*/
|
*/
|
||||||
public static enfoldFromObject(objectArg) {
|
public static enfoldFromObject<T extends typeof Smartjson>(this: T, objectArg: any): InstanceType<T> {
|
||||||
const newInstance = new this();
|
const newInstance = new this() as InstanceType<T>;
|
||||||
for (const keyName in objectArg) {
|
for (const keyName in objectArg) {
|
||||||
if (newInstance.saveableProperties.indexOf(keyName) !== -1) {
|
if (newInstance.saveableProperties.indexOf(keyName) !== -1) {
|
||||||
newInstance[keyName] = objectArg[keyName];
|
newInstance[keyName] = objectArg[keyName];
|
||||||
@@ -73,7 +73,7 @@ export class Smartjson {
|
|||||||
/**
|
/**
|
||||||
* enfold from json
|
* enfold from json
|
||||||
*/
|
*/
|
||||||
public static enfoldFromJson(jsonArg: string) {
|
public static enfoldFromJson<T extends typeof Smartjson>(this: T, jsonArg: string): InstanceType<T> {
|
||||||
const objectFromJson = parse(jsonArg);
|
const objectFromJson = parse(jsonArg);
|
||||||
return this.enfoldFromObject(objectFromJson);
|
return this.enfoldFromObject(objectFromJson);
|
||||||
}
|
}
|
||||||
@@ -89,21 +89,49 @@ export class Smartjson {
|
|||||||
*/
|
*/
|
||||||
public foldToObject() {
|
public foldToObject() {
|
||||||
const newFoldedObject: { [key: string]: any } = {};
|
const newFoldedObject: { [key: string]: any } = {};
|
||||||
const trackMap = [];
|
const trackSet = new Set<Smartjson>();
|
||||||
for (const keyName of this.saveableProperties) {
|
const foldValue = (val: any): any => {
|
||||||
let value = this[keyName];
|
if (val instanceof Smartjson) {
|
||||||
if (value instanceof Smartjson) {
|
if (trackSet.has(val)) {
|
||||||
if (trackMap.includes(value)) {
|
|
||||||
throw new Error('cycle detected');
|
throw new Error('cycle detected');
|
||||||
}
|
}
|
||||||
trackMap.push(value);
|
trackSet.add(val);
|
||||||
value = value.foldToObject();
|
return val.foldToObjectInternal(trackSet);
|
||||||
}
|
}
|
||||||
newFoldedObject[keyName] = plugins.lodashCloneDeep(value);
|
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;
|
return newFoldedObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private foldToObjectInternal(trackSet: Set<Smartjson>) {
|
||||||
|
const result: { [key: string]: any } = {};
|
||||||
|
const foldValue = (val: any): any => {
|
||||||
|
if (val instanceof Smartjson) {
|
||||||
|
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];
|
||||||
|
result[keyName] = foldValue(value);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* folds a class into an object
|
* folds a class into an object
|
||||||
*/
|
*/
|
||||||
@@ -132,17 +160,7 @@ export const deepEqualObjects = (object1: any, object2: any): boolean => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const deepEqualJsonLStrings = (jsonLString1: string, jsonLString2: string): boolean => {
|
export const deepEqualJsonLStrings = (jsonLString1: string, jsonLString2: string): boolean => {
|
||||||
const firstArray = [];
|
const firstArray = parseJsonL(jsonLString1);
|
||||||
jsonLString1.split('\n').forEach((line) => {
|
const secondArray = parseJsonL(jsonLString2);
|
||||||
if (line) {
|
|
||||||
firstArray.push(parse(line));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const secondArray = [];
|
|
||||||
jsonLString2.split('\n').forEach((line) => {
|
|
||||||
if (line) {
|
|
||||||
secondArray.push(parse(line));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return deepEqualObjects(firstArray, secondArray);
|
return deepEqualObjects(firstArray, secondArray);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user