
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@awsless/json
Advanced tools
The `@awsless/json` package adds support for more JavaScript native types to JSON.
The @awsless/json package adds support for more JavaScript native types to JSON.
Features:
JSON doesn't have support for types like:
undefinedNaNInfinityUint8ArrayRegExpSetMapURLDateBigIntBigFloat - npm package @awsless/bit-floatDuration - npm package @awsless/durationHaving to encode & decode these type of values can get quite annoying. We try to solve this problem by encoding these types using valid JSON syntax.
Additionally, the native JSON.parse/stringify functions do not address the potential loss of precision problem.
import { parse, stringify } from '@awsless/json'
// The output will be {"$bigint":"1"}
const json = stringify(1n)
// Parse the json with a bigint inside.
const value = parse(json)
In some situations, you may not have control over the JSON parser being used. In these instances, your JSON can still be parsed, but the output may be incorrect. We can correct the inaccurate 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)
We let you extend JSON to support your own custom types.
import { parse, stringify, Serializable, setGlobalTypes } 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'), {
types: { $custom },
})
// Parse the json with your custom type.
const value = parse(json, {
types: { $custom },
})
// Additionally, you can globally add your own extensions.
setGlobalTypes({ $custom })
The native JSON.stringify will strip undefined object properties to generate smaller JSON output. We do the same thing but we have a special option to opt out of this behavior if correctness is important to you.
const input = { key: undefined }
const smallerJson = stringify(input, {
preserveUndefinedValues: true,
}) // {}
const moreCorrectJson = stringify(input, {
preserveUndefinedValues: true,
}) // {"key":{"$undefined":0}}
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.
[!NOTE] These utility functions will only work in environments that support
JSON.rawJSON> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/rawJSON#browser_compatibility
import { safeNumberParse, safeNumberStringify } from '@awsless/json'
import { BigFloat, eq } from '@awsless/big-float'
const one = 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(one, result)) // true
$ 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.
FAQs
The `@awsless/json` package adds support for more JavaScript native types to JSON.
The npm package @awsless/json receives a total of 109 weekly downloads. As such, @awsless/json popularity was classified as not popular.
We found that @awsless/json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.