{"id":16077955,"url":"https://github.com/cdklabs/json2jsii","last_synced_at":"2025-12-30T00:31:22.426Z","repository":{"id":37051588,"uuid":"272495770","full_name":"cdklabs/json2jsii","owner":"cdklabs","description":"Generates jsii-compatible structs from JSON schemas","archived":false,"fork":false,"pushed_at":"2024-10-24T00:15:03.000Z","size":6682,"stargazers_count":22,"open_issues_count":8,"forks_count":12,"subscribers_count":16,"default_branch":"main","last_synced_at":"2024-10-24T14:16:32.092Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cdklabs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-15T16:56:44.000Z","updated_at":"2024-10-24T00:15:05.000Z","dependencies_parsed_at":"2024-01-16T01:47:08.056Z","dependency_job_id":"133b49be-f469-42e5-82fd-6c12bc6efce1","html_url":"https://github.com/cdklabs/json2jsii","commit_stats":{"total_commits":1203,"total_committers":16,"mean_commits":75.1875,"dds":0.5793848711554448,"last_synced_commit":"a677186d147a983fdfe7b0a2f2e93aa90e120cd8"},"previous_names":["aws/json2jsii"],"tags_count":905,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdklabs%2Fjson2jsii","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdklabs%2Fjson2jsii/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdklabs%2Fjson2jsii/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdklabs%2Fjson2jsii/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdklabs","download_url":"https://codeload.github.com/cdklabs/json2jsii/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237739846,"owners_count":19358624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T10:05:35.679Z","updated_at":"2025-10-22T20:30:49.064Z","avatar_url":"https://github.com/cdklabs.png","language":"TypeScript","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"# json2jsii\n\n\u003e Generates jsii-compatible structs from JSON schemas\n\n## Usage\n\n```ts\nconst g = TypeGenerator.forStruct('Person', {\n  definitions: {\n    Name: {\n      description: 'Represents a name of a person',\n      required: ['FirstName', 'last_name'],\n      properties: {\n        FirstName: {\n          type: 'string',\n          description: 'The first name of the person',\n        },\n        last_name: {\n          type: 'string',\n          description: 'The last name of the person',\n        },\n      },\n    },\n  },\n  required: ['name'],\n  properties: {\n    name: {\n      description: 'The person\\'s name',\n      $ref: '#/definitions/Name',\n    },\n    favorite_color: {\n      description: 'Favorite color. Default is green',\n      enum: ['red', 'green', 'blue', 'yellow'],\n    },\n  },\n});\n\nfs.writeFileSync('person.ts', g.render());\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eperson.ts\u003c/summary\u003e\n\n```ts\n/**\n * @schema Person\n */\nexport interface Person {\n  /**\n   * The person's name\n   *\n   * @schema Person#name\n   */\n  readonly name: Name;\n\n  /**\n   * Favorite color. Default is green\n   *\n   * @default green\n   * @schema Person#favorite_color\n   */\n  readonly favoriteColor?: PersonFavoriteColor;\n\n}\n\n/**\n * Converts an object of type 'Person' to JSON representation.\n */\n/* eslint-disable max-len, quote-props */\nexport function toJson_Person(obj: Person | undefined): Record\u003cstring, any\u003e | undefined {\n  if (obj === undefined) { return undefined; }\n  const result = {\n    'name': toJson_Name(obj.name),\n    'favorite_color': obj.favoriteColor,\n  };\n  // filter undefined values\n  return Object.entries(result).reduce((r, i) =\u003e (i[1] === undefined) ? r : ({ ...r, [i[0]]: i[1] }), {});\n}\n/* eslint-enable max-len, quote-props */\n\n/**\n * Represents a name of a person\n *\n * @schema Name\n */\nexport interface Name {\n  /**\n   * The first name of the person\n   *\n   * @schema Name#FirstName\n   */\n  readonly firstName: string;\n\n  /**\n   * The last name of the person\n   *\n   * @schema Name#last_name\n   */\n  readonly lastName: string;\n\n}\n\n/**\n * Converts an object of type 'Name' to JSON representation.\n */\n/* eslint-disable max-len, quote-props */\nexport function toJson_Name(obj: Name | undefined): Record\u003cstring, any\u003e | undefined {\n  if (obj === undefined) { return undefined; }\n  const result = {\n    'FirstName': obj.firstName,\n    'last_name': obj.lastName,\n  };\n  // filter undefined values\n  return Object.entries(result).reduce((r, i) =\u003e (i[1] === undefined) ? r : ({ ...r, [i[0]]: i[1] }), {});\n}\n/* eslint-enable max-len, quote-props */\n\n/**\n * Favorite color. Default is green\n *\n * @default green\n * @schema PersonFavoriteColor\n */\nexport enum PersonFavoriteColor {\n  /** red */\n  RED = 'red',\n  /** green */\n  GREEN = 'green',\n  /** blue */\n  BLUE = 'blue',\n  /** yellow */\n  YELLOW = 'yellow',\n}\n```\n\n\u003c/details\u003e\n\nThe generated code includes JSII structs (TypeScript interfaces) and enums based\non the schema (`Person`, `Name` and `PersonFavoriteColor`) as well as a function\n`toJson_Xyz()` for each struct.\n\nThe `toJson()` functions are required in order to serialize objects back to their\noriginal schema format.\n\nFor example, the following expression:\n\n```ts\ntoJson_Person({\n  name: {\n    firstName: 'Jordan',\n    lastName: 'McJordan'\n  },\n  favoriteColor: PersonFavoriteColor.GREEN\n})\n```\n\nWill return:\n\n```json\n{\n  \"name\": {\n    \"FirstName\": \"Jordan\",\n    \"last_name\": \"McJordan\"\n  },\n  \"favorite_color\": \"green\"\n}\n```\n\n## Use cases\n\n### Type aliases\n\nIt is possible to offer an alias to a type definition using `addAlias(from,\nto)`. The type generator will resolve any references to the original type with\nthe alias:\n\n```ts\nconst gen = new TypeGenerator();\ngen.addDefinition('TypeA', { type: 'object', properties: { ref: { $ref: '#/definitions/TypeB' } } } );\ngen.addDefinition('TypeC', { type: 'object', properties: { field: { type: 'string' } } });\ngen.addAlias('TypeB', 'TypeC');\n\ngen.emitType('TypeA');\n```\n\nThis will output:\n\n```ts\ninterface TypeA {\n  readonly ref: TypeC;\n}\n\ninterface TypeC {\n  readonly field: string;\n}\n```\n\n## Language bindings\n\nOnce you generate jsii-compatible TypeScript source (such as `person.ts` above),\nyou can use [jsii-srcmak](https://github.com/eladb/jsii-srcmak) in order to\nproduce source code in any of the jsii supported languages.\n\nThe following command will produce Python sources for the `Person` types:\n\n```shell\n$ jsii-srcmak gen/ts \\\n  --python-outdir gen/py --python-module-name person \\\n  --java-outdir gen/java --java-package person\n```\n\nSee the [jsii-srcmak](https://github.com/eladb/jsii-srcmak) for library usage.\n\n## Contributions\n\nAll contributions are celebrated.\n\n## License\n\nDistributed under the [Apache 2.0](./LICENSE) license.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdklabs%2Fjson2jsii","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdklabs%2Fjson2jsii","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdklabs%2Fjson2jsii/lists"}