Detalhes do pacote

json2jsii

cdklabs340.2kApache-2.00.5.11

Generates jsii structs from JSON schemas

readme (leia-me)

json2jsii

Generates jsii-compatible structs from JSON schemas

Usage

const g = TypeGenerator.forStruct('Person', {
  definitions: {
    Name: {
      description: 'Represents a name of a person',
      required: ['FirstName', 'last_name'],
      properties: {
        FirstName: {
          type: 'string',
          description: 'The first name of the person',
        },
        last_name: {
          type: 'string',
          description: 'The last name of the person',
        },
      },
    },
  },
  required: ['name'],
  properties: {
    name: {
      description: 'The person\'s name',
      $ref: '#/definitions/Name',
    },
    favorite_color: {
      description: 'Favorite color. Default is green',
      enum: ['red', 'green', 'blue', 'yellow'],
    },
  },
});

fs.writeFileSync('person.ts', g.render());
<summary>person.ts</summary> ts /** * @schema Person */ export interface Person { /** * The person's name * * @schema Person#name */ readonly name: Name; /** * Favorite color. Default is green * * @default green * @schema Person#favorite_color */ readonly favoriteColor?: PersonFavoriteColor; } /** * Converts an object of type 'Person' to JSON representation. */ /* eslint-disable max-len, quote-props */ export function toJson_Person(obj: Person | undefined): Record<string, any> | undefined { if (obj === undefined) { return undefined; } const result = { 'name': toJson_Name(obj.name), 'favorite_color': obj.favoriteColor, }; // filter undefined values return Object.entries(result).reduce((r, i) => (i[1] === undefined) ? r : ({ ...r, [i[0]]: i[1] }), {}); } /* eslint-enable max-len, quote-props */ /** * Represents a name of a person * * @schema Name */ export interface Name { /** * The first name of the person * * @schema Name#FirstName */ readonly firstName: string; /** * The last name of the person * * @schema Name#last_name */ readonly lastName: string; } /** * Converts an object of type 'Name' to JSON representation. */ /* eslint-disable max-len, quote-props */ export function toJson_Name(obj: Name | undefined): Record<string, any> | undefined { if (obj === undefined) { return undefined; } const result = { 'FirstName': obj.firstName, 'last_name': obj.lastName, }; // filter undefined values return Object.entries(result).reduce((r, i) => (i[1] === undefined) ? r : ({ ...r, [i[0]]: i[1] }), {}); } /* eslint-enable max-len, quote-props */ /** * Favorite color. Default is green * * @default green * @schema PersonFavoriteColor */ export enum PersonFavoriteColor { /** red */ RED = 'red', /** green */ GREEN = 'green', /** blue */ BLUE = 'blue', /** yellow */ YELLOW = 'yellow', }

The generated code includes JSII structs (TypeScript interfaces) and enums based on the schema (Person, Name and PersonFavoriteColor) as well as a function toJson_Xyz() for each struct.

The toJson() functions are required in order to serialize objects back to their original schema format.

For example, the following expression:

toJson_Person({
  name: {
    firstName: 'Jordan',
    lastName: 'McJordan'
  },
  favoriteColor: PersonFavoriteColor.GREEN
})

Will return:

{
  "name": {
    "FirstName": "Jordan",
    "last_name": "McJordan"
  },
  "favorite_color": "green"
}

Use cases

Type aliases

It is possible to offer an alias to a type definition using addAlias(from, to). The type generator will resolve any references to the original type with the alias:

const gen = new TypeGenerator();
gen.addDefinition('TypeA', { type: 'object', properties: { ref: { $ref: '#/definitions/TypeB' } } } );
gen.addDefinition('TypeC', { type: 'object', properties: { field: { type: 'string' } } });
gen.addAlias('TypeB', 'TypeC');

gen.emitType('TypeA');

This will output:

interface TypeA {
  readonly ref: TypeC;
}

interface TypeC {
  readonly field: string;
}

Language bindings

Once you generate jsii-compatible TypeScript source (such as person.ts above), you can use jsii-srcmak in order to produce source code in any of the jsii supported languages.

The following command will produce Python sources for the Person types:

$ jsii-srcmak gen/ts \
  --python-outdir gen/py --python-module-name person \
  --java-outdir gen/java --java-package person

See the jsii-srcmak for library usage.

Contributions

All contributions are celebrated.

License

Distributed under the Apache 2.0 license.

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.1.217 (2021-03-08)

0.1.216 (2021-03-08)

0.1.215 (2021-03-06)

0.1.214 (2021-03-05)

0.1.213 (2021-03-05)

0.1.212 (2021-03-05)

0.1.211 (2021-03-04)

0.1.210 (2021-03-04)

0.1.209 (2021-03-03)

0.1.208 (2021-03-03)

0.1.207 (2021-03-02)

0.1.206 (2021-03-02)

0.1.205 (2021-03-02)

0.1.204 (2021-03-01)

0.1.203 (2021-02-27)

0.1.202 (2021-02-26)

0.1.201 (2021-02-26)

0.1.200 (2021-02-25)

0.1.199 (2021-02-25)

0.1.198 (2021-02-24)

0.1.197 (2021-02-24)

0.1.196 (2021-02-24)

0.1.195 (2021-02-23)

0.1.194 (2021-02-23)

0.1.193 (2021-02-23)

0.1.192 (2021-02-22)

0.1.191 (2021-02-22)

0.1.190 (2021-02-22)

0.1.189 (2021-02-22)

0.1.188 (2021-01-15)

0.1.187 (2021-01-15)

0.1.186 (2021-01-14)

0.1.185 (2021-01-14)

0.1.184 (2021-01-13)

0.1.183 (2021-01-12)

0.1.182 (2021-01-12)

0.1.181 (2021-01-11)

0.1.180 (2021-01-11)

0.1.179 (2021-01-09)

0.1.178 (2021-01-08)

0.1.177 (2021-01-08)

0.1.176 (2021-01-07)

0.1.175 (2021-01-07)

0.1.174 (2021-01-06)

0.1.173 (2021-01-06)

0.1.172 (2021-01-05)

0.1.171 (2021-01-05)

0.1.170 (2021-01-04)

0.1.169 (2021-01-04)

0.1.168 (2021-01-02)

0.1.167 (2021-01-01)

0.1.166 (2021-01-01)

0.1.165 (2020-12-31)

0.1.164 (2020-12-31)

0.1.163 (2020-12-30)

0.1.162 (2020-12-30)

0.1.161 (2020-12-29)

0.1.160 (2020-12-29)

0.1.159 (2020-12-28)

0.1.158 (2020-12-28)

0.1.157 (2020-12-26)

0.1.156 (2020-12-25)

Features

Bug Fixes

0.1.155 (2020-12-24)

Features

Bug Fixes

0.1.154 (2020-12-24)

Features

Bug Fixes

0.1.153 (2020-12-23)

Features

Bug Fixes

0.1.152 (2020-12-22)

Features

Bug Fixes

0.1.151 (2020-12-22)

0.1.150 (2020-12-22)

0.1.149 (2020-12-21)

0.1.148 (2020-12-20)

0.1.147 (2020-12-18)

0.1.146 (2020-12-18)

0.1.145 (2020-12-17)

0.1.144 (2020-12-17)

0.1.143 (2020-12-16)

0.1.142 (2020-12-15)

0.1.141 (2020-12-15)

0.1.140 (2020-12-14)

0.1.139 (2020-12-12)

0.1.138 (2020-12-11)

0.1.137 (2020-12-11)

0.1.136 (2020-12-10)

0.1.135 (2020-12-09)

0.1.134 (2020-12-08)

0.1.133 (2020-12-08)

0.1.132 (2020-12-08)

0.1.131 (2020-12-07)

0.1.130 (2020-12-07)

0.1.129 (2020-12-06)

0.1.128 (2020-12-04)

0.1.127 (2020-12-04)

0.1.126 (2020-12-03)

0.1.125 (2020-12-03)

0.1.124 (2020-12-02)

0.1.123 (2020-12-01)

0.1.122 (2020-11-30)

0.1.121 (2020-11-30)

0.1.120 (2020-11-28)

0.1.119 (2020-11-27)

0.1.118 (2020-11-27)

0.1.117 (2020-11-26)

0.1.116 (2020-11-26)

0.1.115 (2020-11-25)

0.1.114 (2020-11-25)

0.1.113 (2020-11-24)

0.1.112 (2020-11-24)

0.1.111 (2020-11-23)

0.1.110 (2020-11-20)

0.1.109 (2020-11-19)

0.1.108 (2020-11-19)

0.1.107 (2020-11-18)

0.1.106 (2020-11-18)

0.1.105 (2020-11-17)

0.1.104 (2020-11-16)

Features

0.1.103 (2020-11-16)

0.1.102 (2020-11-16)

0.1.101 (2020-11-13)

0.1.100 (2020-11-13)

0.1.99 (2020-11-12)

0.1.98 (2020-11-12)

0.1.97 (2020-11-11)

0.1.96 (2020-11-11)

0.1.95 (2020-11-10)

0.1.94 (2020-11-10)

0.1.93 (2020-11-09)

0.1.92 (2020-11-09)

0.1.91 (2020-11-08)

0.1.90 (2020-11-07)

0.1.89 (2020-11-06)

0.1.88 (2020-11-06)

0.1.87 (2020-11-05)

0.1.86 (2020-11-04)

0.1.85 (2020-11-04)

0.1.84 (2020-11-03)

0.1.83 (2020-11-03)

0.1.82 (2020-11-02)

0.1.81 (2020-11-02)

0.1.80 (2020-10-30)

0.1.79 (2020-10-30)

0.1.78 (2020-10-29)

0.1.77 (2020-10-29)

0.1.76 (2020-10-28)

0.1.75 (2020-10-28)

0.1.74 (2020-10-27)

0.1.73 (2020-10-27)

0.1.72 (2020-10-26)

0.1.71 (2020-10-26)

0.1.70 (2020-10-26)

0.1.69 (2020-10-25)

Bug Fixes

0.1.68 (2020-10-24)

0.1.67 (2020-10-23)

0.1.66 (2020-10-23)

0.1.65 (2020-10-22)

0.1.64 (2020-10-22)

0.1.63 (2020-10-21)

0.1.62 (2020-10-21)

0.1.61 (2020-10-20)

0.1.60 (2020-10-20)

0.1.59 (2020-10-19)

0.1.58 (2020-10-18)

0.1.57 (2020-10-16)

0.1.56 (2020-10-16)

0.1.55 (2020-10-15)

0.1.54 (2020-10-15)

0.1.53 (2020-10-14)

0.1.52 (2020-10-14)

0.1.51 (2020-10-13)

0.1.50 (2020-10-13)

0.1.49 (2020-10-12)

0.1.48 (2020-10-12)

0.1.47 (2020-10-11)

0.1.46 (2020-10-09)

0.1.45 (2020-10-09)

0.1.44 (2020-10-08)

0.1.43 (2020-10-07)

0.1.42 (2020-10-06)

0.1.41 (2020-10-06)

0.1.40 (2020-10-05)

0.1.39 (2020-10-04)

0.1.38 (2020-10-01)

0.1.37 (2020-09-30)

0.1.36 (2020-09-28)

0.1.35 (2020-09-22)

0.1.34 (2020-09-21)

0.1.33 (2020-09-17)

0.1.32 (2020-09-14)

0.1.31 (2020-09-14)

0.1.30 (2020-09-11)

0.1.29 (2020-09-09)

0.1.28 (2020-09-07)

0.1.27 (2020-09-03)

0.1.26 (2020-09-03)

0.1.25 (2020-09-02)

0.1.24 (2020-09-02)

0.1.23 (2020-09-01)

0.1.22 (2020-08-28)

0.1.21 (2020-08-27)

0.1.20 (2020-08-26)

0.1.19 (2020-08-26)

0.1.18 (2020-08-25)

0.1.17 (2020-08-24)

0.1.16 (2020-08-21)

0.1.15 (2020-08-19)

0.1.14 (2020-08-17)

0.1.13 (2020-08-17)

0.1.12 (2020-08-13)

0.1.11 (2020-08-12)

0.1.10 (2020-08-12)

0.1.9 (2020-08-11)

0.1.8 (2020-08-06)

0.1.7 (2020-08-05)

0.1.6 (2020-08-03)

0.1.5 (2020-07-29)

0.1.4 (2020-07-16)

Bug Fixes

  • Pascal case for fully-lowercase types (#2) (c1e9170)

0.1.3 (2020-07-15)

0.1.2 (2020-06-17)

Bug Fixes

  • empty lines are indented (5c4928e)

0.1.1 (2020-06-17)