{"id":14949153,"url":"https://github.com/lpil/json-typedef","last_synced_at":"2025-07-30T05:33:35.283Z","repository":{"id":256024447,"uuid":"852782192","full_name":"lpil/json-typedef","owner":"lpil","description":"Work with JSON using a schema! RFC8927","archived":false,"fork":false,"pushed_at":"2025-05-17T13:41:11.000Z","size":125,"stargazers_count":46,"open_issues_count":2,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-08T18:10:35.680Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://jsontypedef.com/","language":"Gleam","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/lpil.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2024-09-05T12:19:26.000Z","updated_at":"2025-06-14T07:31:31.000Z","dependencies_parsed_at":"2024-11-03T10:20:46.750Z","dependency_job_id":"ab053bfd-ff95-421d-a0ef-9761e079fea1","html_url":"https://github.com/lpil/json-typedef","commit_stats":null,"previous_names":["lpil/json-typedef"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/lpil/json-typedef","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpil%2Fjson-typedef","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpil%2Fjson-typedef/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpil%2Fjson-typedef/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpil%2Fjson-typedef/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lpil","download_url":"https://codeload.github.com/lpil/json-typedef/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpil%2Fjson-typedef/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267815187,"owners_count":24148356,"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-30T02:00:09.044Z","response_time":70,"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":[],"created_at":"2024-09-24T05:00:36.234Z","updated_at":"2025-07-30T05:33:35.262Z","avatar_url":"https://github.com/lpil.png","language":"Gleam","funding_links":[],"categories":["Packages"],"sub_categories":["JSON"],"readme":"# json_typedef\n\nWork with JSON using a schema! \n\n[![Package Version](https://img.shields.io/hexpm/v/json_typedef)](https://hex.pm/packages/json_typedef)\n[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/json_typedef/)\n\nJSON-Typedef is an easy-to-learn, portable, and standardized way to describe the\nshape of your JSON data. This library provides encoders and decoders for the\nJSON-Typedef schema format, and can generate Gleam encoders and decoders for a\ngiven schema.\n\n- [JSON-Typedef website](https://jsontypedef.com/)\n- [RFC8927](https://datatracker.ietf.org/doc/html/rfc8927)\n\nFor example, if you have this JSON-Typedef schema:\n\n```json\n{\n  \"properties\": {\n    \"id\": { \"type\": \"string\" },\n    \"createdAt\": { \"type\": \"timestamp\" },\n    \"karma\": { \"type\": \"int32\" },\n    \"isAdmin\": { \"type\": \"boolean\" }\n  }\n}\n```\n\nThen you can use this package to generate this Gleam source code:\n\n```gleam\nimport decode\nimport gleam/json\n\npub type Data {\n  Data(\n    created_at: String,\n    id: String,\n    is_admin: Bool,\n    karma: Int,\n  )\n}\n\npub fn data_decoder() -\u003e decode.Decoder(Data) {\n  decode.into({\n    use created_at \u003c- decode.parameter\n    use id \u003c- decode.parameter\n    use is_admin \u003c- decode.parameter\n    use karma \u003c- decode.parameter\n    Data(created_at:, id:, is_admin:, karma:)\n  })\n  |\u003e decode.field(\"createdAt\", decode.string)\n  |\u003e decode.field(\"id\", decode.string)\n  |\u003e decode.field(\"isAdmin\", decode.bool)\n  |\u003e decode.field(\"karma\", decode.int)\n}\n\npub fn data_to_json(data: Data) -\u003e json.Json {\n  json.object([\n    #(\"createdAt\", json.string(data.created_at)),\n    #(\"id\", json.string(data.id)),\n    #(\"isAdmin\", json.bool(data.is_admin)),\n    #(\"karma\", json.int(data.karma)),\n  ])\n}\n```\n\n## Usage\n\nEverything starts with a string representation of your schema written using the \n[JSON Type Definition Format](https://jsontypedef.com/docs/jtd-in-5-minutes/)\n\n```gleam\nlet json_schema =\n    \"{\n  \\\"properties\\\": {\n    \\\"id\\\": { \\\"type\\\": \\\"string\\\" },\n    \\\"createdAt\\\": { \\\"type\\\": \\\"timestamp\\\" },\n    \\\"karma\\\": { \\\"type\\\": \\\"int32\\\" },\n    \\\"isAdmin\\\": { \\\"type\\\": \\\"boolean\\\" }\n  }\n}\"\n```\n\nThen you'll need to use the\n[gleam/json module](https://hexdocs.pm/gleam_json/gleam/json.html)'s\n[decode function](https://hexdocs.pm/gleam_json/gleam/json.html#decode) to\nconvert your string representation into a usable\n[RootSchema](https://hexdocs.pm/json_typedef/json_typedef.html#RootSchema) type\nlike this:\n\n```gleam\nimport gleam/json\nimport json_typedef\n\nlet assert Ok(root_schema) = json.decode(json_schema, json_typedef.decoder)\n```\n\nOnce you have your `root_schema` you can then generate your encoders and or\ndecoders! That can be achieved like this:\n\n```gleam\njson_typedef.codegen()\n|\u003e json_typedef.generate_decoders(True)\n|\u003e json_typedef.generate_encoders(True)\n|\u003e json_typedef.generate(root_schema)\n```\n\nyou can then save the `Ok(String)` value to a file and use the generated code to\ndecode and encode any future pieces of json that satisfy your schema.\n\nFurther documentation can be found at \u003chttps://hexdocs.pm/json_typedef\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpil%2Fjson-typedef","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flpil%2Fjson-typedef","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpil%2Fjson-typedef/lists"}