{"id":19781959,"url":"https://github.com/ooesili/type-safe-json-decoder","last_synced_at":"2025-04-30T22:30:27.717Z","repository":{"id":12043722,"uuid":"71011087","full_name":"ooesili/type-safe-json-decoder","owner":"ooesili","description":"A strongly typed JSON decoder and validator inspired by Elm","archived":false,"fork":false,"pushed_at":"2018-10-16T10:11:28.000Z","size":153,"stargazers_count":34,"open_issues_count":3,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T05:32:24.089Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://ooesili.github.io/type-safe-json-decoder","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/ooesili.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}},"created_at":"2016-10-15T20:17:26.000Z","updated_at":"2023-05-13T06:19:26.000Z","dependencies_parsed_at":"2022-08-07T06:16:44.169Z","dependency_job_id":null,"html_url":"https://github.com/ooesili/type-safe-json-decoder","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ooesili%2Ftype-safe-json-decoder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ooesili%2Ftype-safe-json-decoder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ooesili%2Ftype-safe-json-decoder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ooesili%2Ftype-safe-json-decoder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ooesili","download_url":"https://codeload.github.com/ooesili/type-safe-json-decoder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251791414,"owners_count":21644390,"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-12T06:03:14.319Z","updated_at":"2025-04-30T22:30:27.332Z","avatar_url":"https://github.com/ooesili.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Type-safe JSON Decoder\n======================\n[![wercker status](https://app.wercker.com/status/981a74cb4e88dcfa211647cc71752035/s/master \"wercker status\")](https://app.wercker.com/project/byKey/981a74cb4e88dcfa211647cc71752035)\n[![typedoc](https://img.shields.io/badge/typedoc-reference-blue.svg?style=flat-square)][docs]\n[![npm](https://img.shields.io/npm/v/type-safe-json-decoder.svg?style=flat-square)](https://www.npmjs.com/package/type-safe-json-decoder)\n[![npm](https://img.shields.io/npm/l/type-safe-json-decoder.svg?style=flat-square \"license\")](https://github.com/ooesili/type-safe-json-decoder/blob/master/LICENSE)\n\nA strongly typed JSON decoder and validator inspired by Elm, namely the\n[Json.Decode][elm-decode] package.\n\n\nInstallation\n------------\n\n```\nnpm install --save type-safe-json-decoder\n```\n\n\nIntroduction\n------------\n\nParsing JSON introduces an unfortunate `any` in to TypeScript programs. The\nobjects returned from `JSON.parse` often become the data sources for entire\napplications, never once validated against the actual interfaces and classes\nwhich they go into. This module allows for the creation of decoders which\nperform runtime type checks on the input and return a fully typed result.\n\nGiven this JSON input:\n```typescript\nconst usersJSON = `{\n  \"users\": [\n    {\"id\": 1, \"name\": \"Alice\"},\n    {\"id\": 2, \"name\": \"Bob\"}\n  ]\n}`\n```\n\nWe can create a decoder that matches this expected structure:\n```typescript\nimport { Decoder, at, array, object, number, string } from 'type-safe-json-decoder'\n\ninterface User {\n  id: number\n  name: string\n}\n\nconst usersDecoder: Decoder\u003cUser[]\u003e = at(['users'], array(\n  object(\n    ['id', number()],\n    ['name', string()],\n    (id, name) =\u003e ({id, name})\n  )\n))\n\nconst users: User[] = usersDecoder.decodeJSON(usersJSON)\n```\n\nThe important thing to note here is that `decodeJSON` does not return `any`.\nIt returns a type assignable to `User[]`.\n\nA decoder will also a throw nice error message if it comes across an\nunexpected value at runtime:\n```typescript\nconst badJSON = `{\n  \"users\": [{\"id\": \"0\", \"name\": \"Mallory\"}]\n}`\n\nusersDecoder.decodeJSON(badJSON)\n// throws =\u003e error at .users[0].id: expected number, got string\n```\n\n\nDocumentation\n-------------\n\nDetalied API documentation can be found [here][docs]\n\n\n[elm-decode]: http://package.elm-lang.org/packages/elm-lang/core/latest/Json-Decode\n[docs]: https://ooesili.github.io/type-safe-json-decoder\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fooesili%2Ftype-safe-json-decoder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fooesili%2Ftype-safe-json-decoder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fooesili%2Ftype-safe-json-decoder/lists"}