{"id":19499431,"url":"https://github.com/aircloud/fst-json","last_synced_at":"2025-08-03T12:09:08.067Z","repository":{"id":57242522,"uuid":"455807851","full_name":"aircloud/fst-json","owner":"aircloud","description":"fast-safe-typescript json stringify","archived":false,"fork":false,"pushed_at":"2022-02-12T07:23:00.000Z","size":71,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-07T17:45:33.839Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aircloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-05T07:44:32.000Z","updated_at":"2022-06-16T02:02:36.000Z","dependencies_parsed_at":"2022-09-09T04:01:46.706Z","dependency_job_id":null,"html_url":"https://github.com/aircloud/fst-json","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aircloud%2Ffst-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aircloud%2Ffst-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aircloud%2Ffst-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aircloud%2Ffst-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aircloud","download_url":"https://codeload.github.com/aircloud/fst-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223863951,"owners_count":17216234,"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-11-10T22:04:08.987Z","updated_at":"2024-11-10T22:04:10.143Z","avatar_url":"https://github.com/aircloud.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fst-json: fast-safe-typescript json stringify toolkit\n\n[![Coverage Status](https://coveralls.io/repos/github/aircloud/fst-json/badge.svg)](https://coveralls.io/github/aircloud/fst-json)\u0026nbsp;![Github Actions Build\u0026Test](https://github.com/aircloud/fst-json/actions/workflows/main.yml/badge.svg)\u0026nbsp;![npm version](https://img.shields.io/npm/v/fst-json)\n\n[中文文档](./README.zh-cn.md)\u0026nbsp;|\u0026nbsp;[Basic Example](./examples/helloworld)\n\nBy reusing the Typescript schema file, automatically generate [fast-json-stringify](https://github.com/fastify/fast-json-stringify) schema.\n\n## Background\n\nThis project relies on [fast-json-stringify](https://github.com/fastify/fast-json-stringify) supported by the [fastify](https://www.fastify.io/) framework. One of the main features of fastify is **schema based**(by using JSON Schema), which is roughly written as follows:\n\n```\nconst schema = {\n  schema: {\n    response: {\n      200: {\n        type: 'object',\n        properties: {\n          hello: {\n            type: 'string'\n          }\n        }\n      }\n    }\n  }\n}\nfastify\n  .get('/', schema, function (req, reply) {\n    reply\n      .send({ hello: 'world' })\n  })\n```\n\nWe can see that this set of writing methods will not only bring additional learning costs, but also somewhat duplicate with Typescript definitions.\n\nIn fact, using fst-json we can automatically generate the schema required by fastify by reusing the schema we defined in Typescript, so that we do not need to maintain additional schema definitions. We can see the effect of reusing at [here](./fst-json/examples/fastify-ts).\n\nOf course, fst-json does not rely entirely on fastify, the serialization method it generates can replace `JSON.stringify` almost everywhere, and its other advantages:\n\n* **Field validation according to schema:** When a required attribute is missing (for example, an attribute that is not modified by the `?` modifier when defining an interface), an error will be reported directly.\n* **Filter unnecessary schema fields:** For example, when Node.JS is used as the BFF layer, fields can be returned strictly according to the definition of Typescript to avoid returning unnecessary fields, thereby avoiding passing sensitive fields from upstream services.\n* **Faster serialization speed:** According to the test of [fast-json-stringify](https://github.com/fastify/fast-json-stringify), it can achieve nearly 2 times serialization speed.\n\n## Usage\n\n1. Install fst-json globally:\n\n```\nnpm i fst-json\n```\n\n2. Create a new `.fstconfig.js` in the project directory to declare the configuration. Its full configuration is as follows:\n\n```\nexport interface ClassOptions {\n   ignore: boolean; // whether to ignore exported class files\n}\n\nexport interface Options {\n   sourceFiles: string[], // The schema file list that needs to be parsed and generated, and **all** export definitions will be generated to a corresponding stringify method\n   distFile: string, // path to the js or ts file generated by serialization\n   tsConfig?: string; // address of ts configuration file\n   target?: 'commonjs' | 'es6', // Generation specification, can be omitted, default is es6\n   suffix?: 'ts' | 'js', // Generate ts or js, can be omitted, default ts\n   format?: 'stringify' | 'fastify', // Environment, if it is used in fastify, you need to fill in fastify, otherwise fill in stringify\n   classOptions?: ClassOptions, // Configuration for the defined class file\n}\n```\n\n1. Run `fst-json gen` under the project, the stringify file will be automatically generated.\n\nfst-json will parse and define the stringify method for all exported types by default. For example, if you define the following types:\n\n```\nexport interface SchemaInterface {\n  attr0: string;\n  attr1: number;\n  attr2: boolean;\n}\n```\n\nwill be generated in the distFile you define:\n\n```\nexports.SchemaInterfaceStringify = ...\n```\n\n## Precautions\n\n* Since fst-json will generate corresponding stringily functions for all export types in the files you configure, be sure to include only the type definitions that need to be generated to prevent the generation of unrelated functions (schemas in `import` files do not matter and will not be included).\n* Due to the limitation of our upstream dependency [fast-json-stringify](https://github.com/fastify/fast-json-stringify), we cannot support all typescript specifications. Currently known not supported are:\n  * Can't support generics\n  * May conflict if use object's toJSON\n  * It doesn't support non-specific properties. In this case, the properties will not be recognized and will default to not exist. For example, the following two writing methods are not supported:\n```\nexport intreface OverView {\n  [key: string]: number\n}\nexport type TypeT = Record\u003cstring, number\u003e\n```\n\n\nThis project supports most interfaces, type aliases, and class definitions. You can learn about the Typescript cases supported by this project through the examples folder.\n\nSince this project is still under improvement, there may be some more types that should be supported. If you have any questions about the unsupported content, please file an issue.\n\n## Security\n\nWhen using this tool, it is recommended to ensure that the schema is written by the developer and not inputed by the user.\n\nIn principle, security issues can be avoided to a large extent after doing this. This part can also refer to the [advice](https://github.com/fastify/fast-json-stringify#security) of fast-json-stringify.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faircloud%2Ffst-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faircloud%2Ffst-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faircloud%2Ffst-json/lists"}