New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@awsless/json

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@awsless/json

The `@awsless/json` package adds support for more JavaScript native types to JSON.

Source
npmnpm
Version
0.0.6
Version published
Weekly downloads
130
233.33%
Maintainers
2
Weekly downloads
 
Created
Source

@awsless/json

The @awsless/json package adds support for more JavaScript native types to JSON.

Features:

  • Lightweight / Using the JS native JSON parser.
  • JSON backwards compatible.
  • No precision loss.
  • Includes support basic JS types.
  • Extendable.

The Problem

JSON doesn't have support for types like:

  • undefined
  • Set
  • Map
  • Date
  • BigInt
  • BigFloat - npm package @awsless/bit-float

Having to decode & encode these type of values can get quite annoying. We try to solve this problem by encoding these types using valid JSON syntax.

Also JSON.parse/stringify don't solve the potential loss of precision problem.

Basic Usage

import { parse, stringify } from '@awsless/json';

// Stringify a bigint.
// The output will be {"$bigint":"1"}
const json = stringify(1n)

// Parse the json with a bigint inside.
const value = parse(json)

Patching incorrectly parsed JSON that was parsed with a different JSON parser

In some cases you might not have control over the JSON parser that is being used. In these cases your JSON will still be able to parse, but the output will be incorrect. We can patch the incorrect output by using the patch function.

import { stringify, patch } from '@awsless/json';

const json = stringify(1n)

// The native JSON.parse function will not parse our bigint correctly.
const broken = JSON.parse(json)

// Will fix the broken output.
const fixed = patch(broken)

Extending Supported Types

We let you extend JSON to support your own custom types.

import { parse, stringify, Serializable } from '@awsless/json';

class Custom {
	readonly value

	constructor(value: string) {
		this.value = value
	}
}

const $custom: Serializable<Custom, string> = {
	is: v => v instanceof Custom,
	parse: v => new Custom(v),
	stringify: v => v.value,
}

// Stringify your custom type.
const json = stringify(new Custom('example'), { $custom })

// Parse the json with your custom type.
const value = parse(json, { $custom })

Precision Loss

When using the native JSON.parse/stringify functions you could lose precision when parsing native numbers. And you don't always have the ability to extend JSON with your own custom types. For example when you’re communicating with a third-party API. For this reason, we have 2 utility functions that will parse the native JSON number type to your own precision-safe alternative.

import { safeNumberParse, safeNumberStringify } from '@awsless/json';
import { BigFloat } from '@awsless/big-float';

const value = new BigFloat(1)
const json = safeNumberStringify(ONE, {
	is: v => v instanceof BigFloat,
	stringify: v => v.toString(),
})

console.log(json) // '1'

const result = safeNumberParse('1', {
	parse: v => new BigFloat(v),
})

console.log(eq(value, result)) // true

Known Issue's

Don't use the $ character inside your JSON.

We use the $ character to encode our special types inside JSON. In order to prevent parsing errors we recommend to avoid using the $ character inside your object property names.

Object properties with undefined as value type will be stripped away.

// Will result in an empty object.
const result = parse(stringify({ key: undefined }))

// Will log false.
console.log('key' in result)

// Will log true.
console.log(result.key === undefined)

Keywords

JSON

FAQs

Package last updated on 05 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts