Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@radixdlt/data-formats

Package Overview
Dependencies
Maintainers
3
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@radixdlt/data-formats

Data formats used for serialization.

  • 2.1.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42
increased by2.44%
Maintainers
3
Weekly downloads
 
Created
Source

@radixdlt/data-formats

Usage

JSON Decoding

Examples

Without dependencies, using provided taggedStringDecoder:

import { JSONDecoding, taggedStringDecoder } from '@radixdlt/data-formats'

const strTagDecoder = taggedStringDecoder(':str:')((value) => ok(value))

const { fromJSON } = JSONDecoding.withDecoders(strTagDecoder).create()

fromJSON(':str:xyz') // Ok('xyz')

An object with dependencies:

import { JSONDecoding, taggedStringDecoder } from '@radixdlt/data-formats'
import { ok } from 'neverthrow'

const strTagDecoder = taggedStringDecoder(':str:')((value) => ok(value))

const Object1 = {
    ...JSONDecoding.withDecoders(strTagDecoder).create()
}

const tstTagDecoder = taggedStringDecoder(':tst:')((value) => ok(value))

const { fromJSON } = JSONDecoding
	.withDependencies(Object1)
	.withDecoders(testTagDecoder)
	.create()

fromJSON({
    a: ':str:foo',
    b: ':tst:bar'
}) // ok({ a: 'foo', b: 'bar' })

JSON decoding takes an object and applies decoders to each key-value pair. taggedObjectDecoder and taggedStringDecoder are provided, but you can easily define a new decoder. Here is how taggedStringDecoder is defined:

import { decoder } from '@radixdlt/data-formats'

export const taggedStringDecoder = (tag: string) => <T>(
	algorithm: (value: string) => Result<T, Error>,
): Decoder =>
	decoder<T>((value) =>
		isString(value) && `:${value.split(':')[1]}:` === tag
			? algorithm(value.slice(tag.length))
			: undefined,
	)

A decoder should supply a function that defines how the decoding should be applied. First it should do some validation logic (does this decoder apply to this value?), in this case checking if the value is a string and if has a matching tag. Then, apply some algorithm function, which is the actual decoding (create an instance of some object). If the validation fails, the decoder has to return undefined.

Keywords

FAQs

Package last updated on 22 Aug 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc