Compare commits

..

No commits in common. "master" and "v5.0.13" have entirely different histories.

7 changed files with 3682 additions and 4842 deletions

View File

@ -5,28 +5,12 @@
"gitzone": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
"githost": "gitlab.com",
"gitscope": "push.rocks",
"gitrepo": "smartjson",
"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": "typed json handlers",
"npmPackagename": "@push.rocks/smartjson",
"license": "MIT",
"keywords": [
"JSON handling",
"typed JSON",
"JSON parsing",
"JSON stringifying",
"base64 encoding",
"buffer handling",
"data serialization",
"data deserialization",
"TypeScript support",
"data encoding",
"data decoding"
]
"license": "MIT"
}
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
}
}

View File

@ -1,8 +1,8 @@
{
"name": "@push.rocks/smartjson",
"version": "5.0.20",
"version": "5.0.13",
"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": "typed json handlers",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"scripts": {
@ -12,24 +12,25 @@
},
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartjson.git"
"url": "git+ssh://git@gitlab.com/pushrocks/smartjson.git"
},
"author": "Lossless GmbH",
"license": "MIT",
"bugs": {
"url": "https://gitlab.com/pushrocks/smartjson/issues"
},
"homepage": "https://code.foss.global/push.rocks/smartjson",
"homepage": "https://gitlab.com/pushrocks/smartjson#README",
"devDependencies": {
"@git.zone/tsbuild": "^2.1.66",
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.88",
"@push.rocks/tapbundle": "^5.0.23",
"@types/node": "^20.12.7"
"@git.zone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^20.11.24"
},
"dependencies": {
"@push.rocks/smartbuffer": "^1.0.7",
"@push.rocks/smartenv": "^5.0.12",
"@push.rocks/smartstring": "^4.0.15",
"@push.rocks/smartstring": "^4.0.14",
"fast-json-stable-stringify": "^2.1.0",
"lodash.clonedeep": "^4.5.0"
},
@ -48,18 +49,5 @@
"browserslist": [
"last 1 chrome versions"
],
"type": "module",
"keywords": [
"JSON handling",
"typed JSON",
"JSON parsing",
"JSON stringifying",
"base64 encoding",
"buffer handling",
"data serialization",
"data deserialization",
"TypeScript support",
"data encoding",
"data decoding"
]
"type": "module"
}

8269
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1 +0,0 @@

163
readme.md
View File

@ -1,141 +1,64 @@
# @push.rocks/smartjson
typed json handlers
## Install
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartjson)
* [gitlab.com (source)](https://gitlab.com/push.rocks/smartjson)
* [github.com (source mirror)](https://github.com/push.rocks/smartjson)
* [docs (typedoc)](https://push.rocks.gitlab.io/smartjson/)
To install `@push.rocks/smartjson`, you can use npm or yarn as follows:
## Status for master
```shell
npm install @push.rocks/smartjson --save
# or using yarn
yarn add @push.rocks/smartjson
```
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/push.rocks/smartjson/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/push.rocks/smartjson/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartjson)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/push.rocks/smartjson)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartjson)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartjson)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartjson)](https://lossless.cloud)
## Usage
`@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.
Use TypeScript for best in class instellisense.
### Basic Import
### Classes
First, make sure to import the module:
SmartJson makes it easy to fold and enfold classes into and from JSON
```typescript
import * as smartjson from '@push.rocks/smartjson';
```
```javascript
import { Smartjson, foldDec } from 'smartjson';
### Parsing and Stringifying JSON
class AwesomeClass extends Smartjson {
static stringify;
- **Parsing JSON Strings:**
computedValue: string;
`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
const objectToEncode = { hello: 'world' };
const base64EncodedJson = smartjson.stringifyBase64(objectToEncode);
console.log(base64EncodedJson); // Encoded JSON string
const decodedObject = smartjson.parseBase64(base64EncodedJson);
console.log(decodedObject); // Original object
```
### Folding and Enfolding Classes
`@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.
- **Defining a Foldable Class:**
Decorate properties that should be included in JSON with `@smartjson.foldDec()`.
```typescript
import { Smartjson, foldDec } from '@push.rocks/smartjson';
class MyDataModel extends Smartjson {
@foldDec() public someProperty: string = 'default';
constructor(public id: number, someProperty?: string) {
super();
if (someProperty) this.someProperty = someProperty;
}
@foldDec() // mark anotherValueToStore as foldable
anotherValueToStore: string = null;
constructor() {
super(); // this is important
}
```
}
- **Folding and Enfolding Instances:**
```typescript
const instance = new MyDataModel(1, 'value');
const folded = instance.foldToJson(); // Serialize to JSON
console.log(folded);
const enfoldedInstance = MyDataModel.enfoldFromJson(folded); // Deserialize back to instance
console.log(enfoldedInstance);
```
### Deep Comparison
`@push.rocks/smartjson` enables deep comparison of objects to determine if they are equivalent.
```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
let myAwesomeInstance = new AwesomeClass();
let foldedObject = myAwesomeInstance.foldToObject(); // will return {anotherValueToStore: null}
// You can also fold to Json, with support for buffers
// const jsonString = myAwesomeInstance.foldToJson();
myAwesomeInstance = AwesomeClass.enfoldFromObject({ anotherValueToStore: 'hi' });
foldedObject = myAwesomeInstance.foldToObject(); // will return {anotherValueToStore: 'hi'}
```
### Handling Buffers and Typed Arrays
## Contribution
`smartjson` transparently handles JavaScript's `Buffer` and Typed Arrays during JSON serialization, making it effortless to work with binary data in JSON format.
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
```typescript
const buffer = new Uint8Array([1, 2, 3]);
const objWithBuffer = { key: buffer };
const serialized = smartjson.stringify(objWithBuffer);
For further information read the linked docs at the top of this readme.
const deserialized = smartjson.parse(serialized);
console.log(deserialized.key); // Instance of Uint8Array or Buffer
```
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.
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.
## 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.
**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
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.
### Company Information
Task Venture Capital GmbH
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.
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.
## Legal
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartjson',
version: '5.0.20',
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.'
version: '5.0.13',
description: 'typed json handlers'
}

View File

@ -1,26 +1,11 @@
import * as plugins from './smartjson.plugins.js';
import * as bufferhandling from './bufferhandling.js';
interface JsonObject {
[key: string]: any;
}
/**
* allows you to parse a json
*/
export const parse = bufferhandling.parse;
export const parseJsonL = (jsonlData: string): JsonObject[] => {
const lines = jsonlData.trim().split('\n');
const parsedData: JsonObject[] = lines.reduce((acc, line) => {
if (line.trim().length > 0) {
acc.push(JSON.parse(line));
}
return acc;
}, [] as JsonObject[]);
return parsedData;
}
/**
*
* @param objArg
@ -129,19 +114,3 @@ export const deepEqualObjects = (object1: any, object2: any): boolean => {
const object2String = stringify(object2);
return object1String === object2String;
};
export const deepEqualJsonLStrings = (jsonLString1: string, jsonLString2: string): boolean => {
const firstArray = [];
jsonLString1.split('\n').forEach((line) => {
if (line) {
firstArray.push(parse(line));
}
});
const secondArray = [];
jsonLString2.split('\n').forEach((line) => {
if (line) {
secondArray.push(parse(line));
}
});
return deepEqualObjects(firstArray, secondArray);
}