JSPM

@punchcard/shape-json

0.5.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q38127F
  • License Apache-2.0

Adds support for Json Serialization and Deserialization for Punchcard Shapes

Package Exports

  • @punchcard/shape-json

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@punchcard/shape-json) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@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'
  }
};