@punchcard/shape-json
TypeScript icon, indicating that this package has built-in type declarations

0.5.0 • Public • Published

@punchcard/shape-json

Provides JSON serialization and deserialization for Punchcard Shapes.

Create a Record

class MyType extends Record({
  key: string
    .apply(MinLength(1)), // apply constraints with Traits
  count: integer
    .apply(Minimum(0)),
  rating: number,
  array: array(string),
  map: map(string)
}) {}

Derive a Mapper from MyType

import { Json } from '@punchcard/shape-json';

const mapper = Json.mapper(MyType);

Serialize a type as JSON

const json = mapper.write(new MyType({ ... }));

Deserialize from JSON

const json: Json.Of<MyType> = ...;
const myType: MyType = mapper.read(json);

Derive a type for the JSON form of a Shape

Given a Shape, you can retrieve the serialzied JSON type from it:

const json: Json.Of<MyType> = {
  // statically checked JSON

  key: 'must be a string',
  count: 1,
  rating: 1.1,
  array: ['array', 'of', 'strings'],
  map: {
    key: 'map of string value'
  }
};

Package Sidebar

Install

npm i @punchcard/shape-json

Weekly Downloads

1

Version

0.5.0

License

Apache-2.0

Unpacked Size

51.3 kB

Total Files

13

Last publish

Collaborators

  • sam-goodwin