npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@push.rocks/smartjson

v5.0.20

Published

A library for handling typed JSON data, providing functionalities for parsing, stringifying, and working with JSON objects, including support for encoding and decoding buffers.

Downloads

827

Readme

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

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 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 [email protected].

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.