{"id":13678800,"url":"https://github.com/hopinc/json-methods","last_synced_at":"2026-04-07T15:35:04.552Z","repository":{"id":56777815,"uuid":"525061958","full_name":"hopinc/json-methods","owner":"hopinc","description":"Add methods to plain JSON objects","archived":false,"fork":false,"pushed_at":"2022-09-02T09:32:20.000Z","size":58,"stargazers_count":54,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-09-18T04:29:12.496Z","etag":null,"topics":["json","library","typescript","utility"],"latest_commit_sha":null,"homepage":"https://hop.io","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/hopinc.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":"2022-08-15T16:37:39.000Z","updated_at":"2025-02-03T12:29:50.000Z","dependencies_parsed_at":"2022-08-16T02:40:27.584Z","dependency_job_id":null,"html_url":"https://github.com/hopinc/json-methods","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hopinc/json-methods","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopinc%2Fjson-methods","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopinc%2Fjson-methods/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopinc%2Fjson-methods/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopinc%2Fjson-methods/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hopinc","download_url":"https://codeload.github.com/hopinc/json-methods/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hopinc%2Fjson-methods/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31518624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-07T03:10:19.677Z","status":"ssl_error","status_checked_at":"2026-04-07T03:10:13.982Z","response_time":105,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["json","library","typescript","utility"],"created_at":"2024-08-02T13:00:58.494Z","updated_at":"2026-04-07T15:35:04.535Z","avatar_url":"https://github.com/hopinc.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# `json-methods`\n\nA utility for adding methods to any JSON object. For example, deserializing a user object from your API and adding a `.isAdult()`. Whilst this library works great for validation, use cases extend far beyond that. It was built for `@onehop/js` to enable regular objects to have utility methods that are context aware (like fetching a deployment's containers in Hop).\n\n## Installation\n\n```bash\n# With yarn\nyarn add @onehop/json-methods\n\n# With npm\nnpm install --save @onehop/json-methods\n```\n\n## Basic Example\n\nFor something a little bit more in depth, but still simple, check out [/examples/basic.ts](/examples/basic.ts)\n\n```ts\nimport {create} from '@onehop/json-methods';\n\ninterface User {\n\tid: string;\n\temail: string;\n\tage: number;\n}\n\nconst Users = create\u003cUser\u003e().methods({\n\t// Methods must be created with method function syntax,\n\t// rather than property arrow functions (so that `this` can be bound)\n\tisAdult() {\n\t\treturn this.age \u003e= 18;\n\t},\n});\n\n// json is the JSON object that we want to add methods to\nconst json = await getUserFromAPI();\nconst user = Users.from(json);\n\n// Or, if you have a JSON string, we can parse it for you\nconst user = Users.parse(json);\n\n// Safely access properties:\nconsole.log(user.email);\n\n// And call our methods\nconsole.log('Can watch the movie?:', user.isAdult());\n```\n\n## Validation / Schemas\n\n`json-methods` supports third-party schemas out of the box. You can write your own, or use things like Zod, Yup or Joi. The precondition is that the schema object itself has a method with the signature `parse(data: unknown): T` Here's a basic example using Zod:\n\n```ts\nimport {create} from '@onehop/json-methods';\nimport {z} from 'zod';\n\n// This schema has a .parse method internally, so it will work\n// with json-methods without any modification\nconst schema = z.object({\n\tage: z.number().min(0),\n});\n\nconst Users = create(schema).methods({\n\tisAdult() {\n\t\treturn this.age \u003e= 18;\n\t},\n});\n\n// During this phase, the schema will be used to validate the passed object.\n// If you do not pass a schema, then the raw data will be used, but you\n// could run into runtime errors if you try to access properties that don't exist!\n// For this reason, it's recommended to always pass a schema if you can\nconst user = Users.from(await getUserFromAPI());\n\nconsole.log('Can watch the movie?:', user.isAdult());\n```\n\n## Advanced TypeScript\n\nFor more advanced use cases, there's a type exported called `Infer` that will allow you to get the full type of your object with the methods added\n\n```ts\nimport {create, Infer} from '@onehop/json-methods';\n\nconst Users = create\u003c{age: number}\u003e().methods({\n\tisAdult() {\n\t\treturn this.age \u003e= 18;\n\t},\n});\n\n// UserWithMethods is {age: number} \u0026 {isAdult(): boolean}\ntype UserWithMethods = Infer\u003ctypeof Users\u003e;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhopinc%2Fjson-methods","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhopinc%2Fjson-methods","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhopinc%2Fjson-methods/lists"}