{"id":18563459,"url":"https://github.com/henry781/tipify","last_synced_at":"2025-04-10T03:32:50.335Z","repository":{"id":32446098,"uuid":"133561382","full_name":"henry781/tipify","owner":"henry781","description":"Convert JSON to Typescript objects :smiley_cat:","archived":false,"fork":false,"pushed_at":"2023-01-06T01:33:33.000Z","size":450,"stargazers_count":7,"open_issues_count":8,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-05T03:48:43.281Z","etag":null,"topics":["json","mapper","npm","type","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/henry781.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.MD","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-15T19:07:29.000Z","updated_at":"2024-05-19T12:48:18.000Z","dependencies_parsed_at":"2023-01-14T21:15:05.236Z","dependency_job_id":null,"html_url":"https://github.com/henry781/tipify","commit_stats":null,"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henry781%2Ftipify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henry781%2Ftipify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henry781%2Ftipify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henry781%2Ftipify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/henry781","download_url":"https://codeload.github.com/henry781/tipify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223424443,"owners_count":17142744,"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":["json","mapper","npm","type","typescript"],"created_at":"2024-11-06T22:12:43.666Z","updated_at":"2024-11-06T22:12:44.345Z","avatar_url":"https://github.com/henry781.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tipify\n[![Build Status](https://travis-ci.com/henry781/tipify.svg?branch=master)](https://travis-ci.com/henry781/tipify)\n\n## Usage\n\nInstall _tipify_ dependency.\n```\nnpm install --save tipify\n```\n\nEnable experimental decorators in tsconfig.\n```\n{\n  \"compilerOptions\": {\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true,\n    ...\n```\n\nInstantiate a new converter.\n```\nconst converter = new JsonConverter();\n\nconst car = new Car();\n\nconst json = converter.serialize(car);\nconst car2 = converter.deserialize(json, Car);\n```\n\n## Mapping\n\n`@jsonObject()` should be set on each class;\n\n`@jsonProperty('name', String)` should be set on each class field;\n\nType defined with `@jsonProperty()` can be :\n* A class decorated with `@jsonObject()`\n* A custom converter\n* A type defined below\n\n| @jsonObject(#)                     | Type                     |\n|------------------------------------|--------------------------|\n| String                             | string                   |\n| Number                             | number                   |\n| Boolean                            | boolean                  |\n| arrayOf(Passenger)                 | Passenger[]              |\n| arrayOf(array(String))             | string[][]               |\n| enumOf(Color)                      | Color                    |\n| any()                              | any                      |\n| keyValueOf(String, Passenger)      | {[key:string]: Passenger}|\n\n### Example\n\n```\n@jsonObject()\nexport class Passenger {\n\n    @jsonProperty('pid', PidConverter)\n    private _pid: Pid;\n\n    @jsonProperty('gender', enumOf(Gender, EnumStrategy.NAME))\n    private _gender: Gender;\n\n    @jsonProperty('name', String)\n    private _name: string;\n\n    @jsonProperty('informations', any())\n    private _informations: object;\n\n    constructor(options?: PassengerOptions) {\n        if (options) {\n            this._pid = options.pid;\n            this._gender = options.gender;\n            this._name = options.name;\n            this._informations = options.informations;\n        }\n    }\n}\n```\n\n### Implicit type mapping\nWhen type is not specified in `@jsonProperty` decorator, mapper will try to get type information from emitted metadata.\n\n**Warning** : It does not works with array and generics.\n\n```\n@jsonObject()\nexport class Passenger {\n\n    @jsonProperty('id')\n    private _id: number;\n\n    @jsonProperty('name')\n    private _name: string;\n\n    @jsonProperty('active')\n    private _active: boolean;\n    \n    @jsonProperty('airline')\n    private _airline: Airline;\n}\n```\n\n## Polymorphism\n\nTipify can manage polymorphism when `discriminatorProperty` and `discriminatorValue` are defined.\n\n#### Parent class\n```\n@jsonObject({discriminatorProperty: 'type'})\nexport abstract class Vehicle {\n\n    @jsonProperty('type')\n    private _type: string;\n    \n    constructor(type?: string) {\n        this._type = type;\n    }\n}\n```\n#### Child class\n```\n@jsonObject({discriminatorValue: 'car'})\nexport class Car extends Vehicle {\n\n    constructor() {\n        super('car');\n    }\n}\n```\n#### Usage\n```\nconst result = converter.deserialize({ \"type\" : \"car\" }, Vehicle);\nchai.expect(result).instanceof(Car);\n```\n\n\n## Enum\n\n```\n@jsonProperty('color', enumOf(Color, EnumStrategy.NAME_COMPATIBLE))\nprivate _color: Color;\n```\n\n|                 |   |\n|-----------------|---|\n| NAME_COMPATIBLE |   |\n| NAME            |   |\n| INDEX_COMPATIBLE|   |\n| INDEX           |   |\n\n## Custom converter\n\n```\nexport const pidConverter: CustomConverter\u003cPid, CustomConverterArgs\u003e = {\n\n    deserialize(obj: any): Pid {\n\n        if (isNullOrUndefined(obj)) {\n            return obj;\n        }\n\n        return {\n            id: parseInt(obj, 10),\n        } as Pid;\n    },\n\n    serialize(obj: Pid): any {\n\n        if (isNullOrUndefined(obj)) {\n            return obj;\n        }\n\n        return obj.id;\n    },\n};\n```\n\n## Boolean and number parsing\n\nTipify can parse boolean and numbers when option `tryParse` is enabled.\n\n**Note**: Parsing is enabled by default;\n\n```\nconst converter = new JsonConverter({ deserialize: { tryParse: true }});\nconst result = converter.deserialize('true', Boolean);\n```\n\n## Unsafe serialize mode\n\nTo serialize objects wrapped into non typed objects, use options `unsafe: true`.\n\n```\nconst car = new Car({brand: 'dodge', name: 'charger'});\nconst obj = [{charger: [car]}];\n\nconst result = converter.serialize(obj, undefined, {unsafe: true});\nconsole.log(result);\n// [{\"charger\":[{\"brand\":\"dodge\",\"type\":\"car\",\"name\":\"charger\"}]}]\n```\n\n## Keep initialized field value\n\nTipify will keep value initialized by class by default:\n```\n@jsonProperty('name', String)\npublic _name = 'titi';\n```\n\nA given JSON:\n```\n{}\n```\n\nis deserialized into:\n```\n{ _name: 'titi' }\n```\n\nThis feature can be disabled with:\n```\nconst converter = new JsonConverter({ deserialize: { keepObjectFieldValues: false } })\n```\n\n*Note*: Tipify use the constructor to create an instance.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenry781%2Ftipify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhenry781%2Ftipify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenry781%2Ftipify/lists"}