A library for handling typed JSON data, providing functionalities for parsing, stringifying, and working with JSON objects, including support for encoding and decoding buffers.
Go to file
2024-05-29 14:13:49 +02:00
.gitea/workflows fix(core): update 2024-02-25 01:43:07 +01:00
.vscode fix(core): update 2022-06-09 19:54:43 +02:00
test fix(core): update 2024-03-03 10:29:18 +01:00
ts fix(core): update 2024-05-28 00:50:30 +02:00
.gitignore fix(core): update 2020-10-05 11:26:06 +00:00
npmextra.json update tsconfig 2024-04-14 17:45:36 +02:00
package.json update description 2024-05-29 14:13:49 +02:00
pnpm-lock.yaml fix(core): update 2024-05-28 00:50:30 +02:00
readme.hints.md update tsconfig 2024-04-14 17:45:36 +02:00
readme.md update tsconfig 2024-04-14 17:45:36 +02:00
tsconfig.json fix(core): update 2024-02-25 01:43:07 +01:00

@push.rocks/smartjson

typed json handlers

Install

To install @push.rocks/smartjson, you can use npm or yarn as follows:

npm install @push.rocks/smartjson --save
# or using yarn
yarn add @push.rocks/smartjson

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.

Basic Import

First, make sure to import the module:

import * as smartjson from '@push.rocks/smartjson';

Parsing and Stringifying JSON

  • Parsing JSON Strings:

    smartjson enhances JSON parsing by supporting JavaScript's typed arrays, particularly with Buffer handling.

    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.

    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.

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().

    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;
      }
    }
    
  • Folding and Enfolding Instances:

    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.

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

smartjson transparently handles JavaScript's Buffer and Typed Arrays during JSON serialization, making it effortless to work with binary data in JSON format.

const buffer = new Uint8Array([1, 2, 3]);
const objWithBuffer = { key: buffer };
const serialized = smartjson.stringify(objWithBuffer);

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 can provide deeper insights into @push.rocks/smartjson's capabilities.

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 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.