{"id":26059985,"url":"https://github.com/swingcosmic/tason","last_synced_at":"2025-07-28T20:08:57.522Z","repository":{"id":278295291,"uuid":"934904210","full_name":"SwingCosmic/tason","owner":"SwingCosmic","description":"Type-Augmented Serialization Object Notation","archived":false,"fork":false,"pushed_at":"2025-05-14T16:36:54.000Z","size":239,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-28T20:04:58.370Z","etag":null,"topics":["antlr","antlr4","json","json5","parser","serialization","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SwingCosmic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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,"zenodo":null}},"created_at":"2025-02-18T15:38:13.000Z","updated_at":"2025-06-08T04:30:56.000Z","dependencies_parsed_at":"2025-02-19T02:32:50.307Z","dependency_job_id":"9c4057a4-1230-4191-a86c-0a58538ab637","html_url":"https://github.com/SwingCosmic/tason","commit_stats":null,"previous_names":["swingcosmic/tason"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SwingCosmic/tason","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Ftason","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Ftason/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Ftason/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Ftason/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SwingCosmic","download_url":"https://codeload.github.com/SwingCosmic/tason/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwingCosmic%2Ftason/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267578003,"owners_count":24110351,"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","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["antlr","antlr4","json","json5","parser","serialization","typescript"],"created_at":"2025-03-08T13:48:51.926Z","updated_at":"2025-07-28T20:08:57.446Z","avatar_url":"https://github.com/SwingCosmic.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TASON\n\n![npm](https://img.shields.io/npm/v/tason?color=green)\n\nTASON (Type-Augmented Serialization Object Notation，发音`/ˈtæsən/`) 是一种类型化的对象表示语法，可以用于序列化和反序列化数据。\n\n本仓库为原始JavaScript/TypeScript实现，其他语言的实现可以参考以下项目：\n* .NET/C#： [tason-net](https://github.com/SwingCosmic/tason-net)\n\n## 特性\n\nTASON的三大特性：**人类可读**、**自描述强类型**和**动态结构**\n\n### 人类可读\n* TASON是一种文本序列化，语法是JSON的超集，使用起来非常方便。\n* 在保留绝大部分JSON5对JSON的语法简化的同时，移除了少数容易产生混淆的语法和空白字符，以减低手工编写时的错误率。\n\n### 自描述强类型\n* TASON的语法包含了类型信息，不需要额外的描述文件，因此可以非常方便的进行反序列化。\n* TASON类型是语言无关的，即使是TASON内置类型，具体的实现也取决于所使用的语言，只需要语义上的一致即可\n* 支持类型判别器(discriminator)、创建类型别名和指定序列化类型，实现多态序列化、多对一反序列化、鸭子类型序列化\n\n### 动态结构\n* TASON支持动态结构，对象可以包含任意数量的属性，或者将动态对象和固定类型对象混合使用。\n* 在`Protocol Buffers`(gRPC), `MessagePack`等二进制序列化中，\n为了表达动态结构，仍然需要通过内嵌JSON字符串等方式进行降级，反而降低了其性能和强类型优势\n\n\n## 使用场景\n\n### 推荐场景\n\n1. 低代码平台、企业数据中台、数据仓库、仪表盘、BI分析等所使用的高度动态化查询结果，可以同时实现强类型，自描述和人类可读的数据格式。\n2. 含有动态类型的非关系型数据库如MongoDB的无损数据查询和保存。MongoDB支持在查询中使用复杂类型，\n如果使用传统基于JSON的API接口传递参数，例如传递字符串形式的Date, Int64和RegExp，可能会在查询动态结构文档时，类型不匹配而查询不到数据。\n事实上，MongoDB管理工具展现数据的格式和TASON非常相似，也是其灵感来源之一\n3. 将接口数据反序列化为JavaScript类，从而简化JavaScript基于实体类的跨平台/同构(isomorphic)应用开发\n\n### 其它可用场景\n\n1. 编写强类型的配置文件，直接获得配置对象实例，解决JSON弱类型带来的反序列化复杂性\n2. 配合SOAP, GraphQL等已有的强类型数据和服务规范，推出简化的Web API\n\n## 语法\n\n详细语法见此ANTLR4语法文件 [TASON.g4](src/grammar/TASON.g4)\n\nTASON语法以JSON5为蓝本，去掉了少数易混淆的语法，并增强了类型支持。\n\n以下是一个TASON文件示例，来表达MongoDB的一个文档记录：\n\n```javascript\n{\n  _id: ObjectId(\"6670f391dcb0bd791cb3bd18\"),\n  id: UUID(\"3e5b933e-adc1-48a8-b0f8-30aa701cfd77\"),\n  \"$type\": \"com.alibaba.fastjson.JSONObject\",\n  name: \"Pilipili Corporation\",\n  createTime: Date(\"2025-01-01T00:00:00.000Z\"),\n  reportDate: '2024-12',\n  financial: {\n    totalAsset: 6.66e11,\n    totalDebt: +9876547210.33,\n    netProfit: Decimal128(\"114514.1919\"),\n  },\n  legalPerson: User({\n    id: Int64(\"6571037680684232705\"),\n    name: \"🤣👉🤡👈🤣\",\n    age: 24\n  }),\n  keywords: [\"线下PVP\", '本子', \"femboy\", ]\n}\n```\n\n## 类型系统\n\n参见 [类型系统说明](docs/type-system.md)\n\n## 使用\n\n主要的类为`TASONSerializer`，提供了`parse`和`stringify`方法。\n该类的构造函数支持传递一些参数来控制序列化和反序列化的行为，例如是否允许重复的对象键，缩进级别，最大嵌套层次等。\n\n包的默认导出对象`TASON`是具有合理默认参数的`TASONSerializer`实例，可以直接使用。\n\n### 安装\n\n```bash\nnpm install tason\n# or\nyarn add tason\n# or\npnpm add tason\n```\n\n注意 `tason` 仅支持 ESM，因此你的项目如果在前端使用，需要一个模块打包器如 `vite`、`webpack`等；如果在node.js使用，需要原生支持ESM导入\n\n### 反序列化\n\n```typescript\nimport TASON from 'tason';\n\nclass Person {\n  name: string;\n  age: number;\n\n  constructor(name = \"\", age = 0) {\n    this.name = name;\n    this.age = age;\n  }\n}\n\nTASON.registry.registerType(\"Person\", {\n  kind: \"object\",\n  ctor: Person,\n});\n\nconst people = TASON.parse\u003cPerson[]\u003e(\n`[\n  Person({\n    \"name\": \"John\",\n    \"age\": 30\n  }),\n  // 可以包含注释\n  Person({\n    name: 'Jane',\n    age: 25,\n  }),\n]`);\n\n```\n\n### 序列化\n\n```typescript\nimport TASON from 'tason';\nconst serializer = new TASON.Serializer({\n  indent: 2, //指定缩进级别为2个空格\n  registry: TASON.registry.clone(), // 复用全局实例的类型注册表\n});\n\nconst people = [\n  new Person(\"John\", 30),\n  new Person(\"Jane\", 25),\n];\n\nconsole.log(serializer.stringify(people));\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswingcosmic%2Ftason","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswingcosmic%2Ftason","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswingcosmic%2Ftason/lists"}