{"id":28640781,"url":"https://github.com/pd/typed-json","last_synced_at":"2026-03-02T17:04:51.713Z","repository":{"id":8419769,"uuid":"10006289","full_name":"pd/typed-json","owner":"pd","description":"Load JSON into custom JavaScript types","archived":false,"fork":false,"pushed_at":"2013-05-12T15:20:48.000Z","size":184,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-05T21:35:48.529Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pd.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":"2013-05-11T22:43:54.000Z","updated_at":"2017-01-12T00:08:32.000Z","dependencies_parsed_at":"2022-07-30T00:09:36.377Z","dependency_job_id":null,"html_url":"https://github.com/pd/typed-json","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pd/typed-json","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pd%2Ftyped-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pd%2Ftyped-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pd%2Ftyped-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pd%2Ftyped-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pd","download_url":"https://codeload.github.com/pd/typed-json/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pd%2Ftyped-json/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259522118,"owners_count":22870449,"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":"2025-06-12T20:08:23.199Z","updated_at":"2026-03-02T17:04:51.651Z","avatar_url":"https://github.com/pd.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# typed-json\n[![Build Status](https://travis-ci.org/pd/typed-json.png?branch=master)](https://travis-ci.org/pd/typed-json)\n[![Coverage Status](https://coveralls.io/repos/pd/typed-json/badge.png?branch=master)](https://coveralls.io/r/pd/typed-json?branch=master)\n\nEasily generate a `reviver` for [JSON.parse][json-parse] that can convert matching JSON objects to the type of your choosing.\n\n`typed-json` is not an extension of JSON. It does not alter or augment the JSON syntax. It is not a modified JSON parser. It uses the native `reviver` feature of `JSON.parse` to call out to *your* code when it encounters an object with the property you specify. It does not know how to instantiate your class or rebuild an object graph. It just makes it simpler for you to do so.\n\nYour serialized objects can have a property specifying where to find a `fromTypedJSON` function. If present, its return value will be the result of `JSON.parse`.\n\n~~~~js\nvar tj = require('typed-json');\n\nBook.fromTypedJSON = function(object) {\n  return new Book(object.title);\n};\n\nvar book = tj.revive('{ \"_type\": \"Book\", \"title\": \"Debt\" }');\n\nbook instanceof Book //=\u003e true\nbook.title //=\u003e \"Debt\"\nbook._type //=\u003e undefined\n~~~~\n\n## API\n\n### .revive(json, { key: '_type', loader: 'fromTypedJSON', resolver: global })\nOptions:\n\n- **key**: The name of the property used to identify the type.\n- **loader**:  If a string, it is the name of the function to call after identifying a type using the `resolver`. If a function, it will be called directly to deserialize the current JSON object.\n- **resolver**: If an object, type names are expected to correspond to properties of the object (eg, if `resolver: { Foo: ..., Bar: ... }`, types `Foo` and `Bar` are available). If a function, it will be called with the type name encountered, and is expected to return an object that responds to the `loader` method, which will be used to deserialize the object.\n\n#### Custom key name and type lookup\n~~~~js\nvar Deals = {\n  Coupon: { fromTypedJSON: ... },\n  Sale:   { fromTypedJSON: ... }\n};\n\nvar json  = '[{ \"kind\": \"coupon\", \"discount\": \"10%\" }, { \"kind\": \"sale\", \"discount\": \"25%\" }]';\nvar deals = tj.revive(json, {\n  key: \"kind\",\n  resolver: function(kind) { return Deals[kind.titleCase()]; }\n});\n\n// Calls Deals['Coupon'].fromTypedJSON({ discount: '10%' })\n//  then Deals['Sale'].fromTypedJSON({ discount: '25%' })\n\n//=\u003e [\u003cCoupon 10%\u003e, \u003cSale 25%\u003e]\n~~~~\n\n#### Custom loader function\nIf your types can not easily be retrieved from a single namespace, or you can't implement `fromTypedJSON` on all of them, you can instead pass a function to perform the object construction. In this case, the `resolver` will not be used at all:\n\n~~~~js\nvar customDeserializer = function(object, type, key) {\n  //=\u003e object: { color: 'red' }\n  //=\u003e type:   'Bike'\n  //=\u003e key:    '_type'\n\n  if      (type === 'Car')  return new Automobile(object);\n  else if (type === 'Bike') return new Cycle(wheels: 2, color: object.color);\n  else andSoOn();\n};\n\nvar transport = tj.revive('{ \"_type\": \"Bike\", \"color\": \"red\" }', {\n  loader: customDeserializer\n});\ntransport instanceof Cycle //=\u003e true\n~~~~\n\n### .reviver({ key: '_type', ns: global })\nReturns a `reviver` function suitable for use with [JSON.parse][json-parse].\n\nIf you are going to be calling `revive` a lot, you should probably keep one of these around:\n\n~~~~js\nvar reviver = tj.reviver({ key: 'ClassName', resolver: app.models });\n//=\u003e [Function]\n\nvar user = JSON.parse('{ \"ClassName\": \"User\", \"email\": \"user@example.com\" }', reviver);\nuser instanceof app.models.User\n//=\u003e true\n~~~~\n\n## Performance\nAsking `JSON.parse` to use a `reviver` is *not* cheap; a reviver which does absolutely nothing but return the value it receives will cut performance by at least half. Add in the logic of locating types, and the overhead of constructing new objects, and you're easily down to 20-30% the performance of raw JSON loading. If you have very high performance demands, reconstructing object graphs is probably not what you want to be doing.\n\nYou just want to load that data structure you wrote to disk back into your application next time you start it? Yep, this will work just fine for you.\n\nI've written a small set of [matcha][matcha] benchmarks in [test/benchmarks.js][benches] so you can get a feel of just what performance penalty you'll be facing. After you've run `npm install .`, just run `make bench`.\n\n[json-parse]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse\n[matcha]: https://github.com/logicalparadox/matcha\n[benches]: https://github.com/pd/typed-json/blob/master/test/benchmarks.js\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpd%2Ftyped-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpd%2Ftyped-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpd%2Ftyped-json/lists"}