https://github.com/hyperloris/tyson
A TypeScript serialization/deserialization library to convert objects to/from JSON
https://github.com/hyperloris/tyson
angular converter deserialization javascript json nodejs serialization typescript
Last synced: 2 months ago
JSON representation
A TypeScript serialization/deserialization library to convert objects to/from JSON
- Host: GitHub
- URL: https://github.com/hyperloris/tyson
- Owner: hyperloris
- License: mit
- Archived: true
- Created: 2018-03-18T20:33:22.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-02-09T12:06:59.000Z (over 2 years ago)
- Last Synced: 2025-03-16T12:07:25.100Z (2 months ago)
- Topics: angular, converter, deserialization, javascript, json, nodejs, serialization, typescript
- Language: TypeScript
- Homepage:
- Size: 1.15 MB
- Stars: 26
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: code-of-conduct.md
Awesome Lists containing this project
README
> Tyson is a TypeScript serialization/deserialization library to convert objects to/from JSON.
## Features
* Simple `toJson()` and `fromJson()` methods for conversions
* Many options on the property (e.g. custom naming, required and more)
* Type-safe JSON deserialization
* Support for multi-type arrays
* Custom conversions through adapters and factories## Installation
You can install `tyson` using [npm](http://npmjs.org):
```console
npm install --save @hyperloris/tyson
```## Usage
The primary class to use is [`Tyson`](https://hyperloris.github.io/tyson/classes/tyson.html) which you can just create by calling `new Tyson()`. There is also a class [`TysonBuilder`](https://hyperloris.github.io/tyson/classes/tysonbuilder.html) available that can be used to create a Tyson instance with various settings (e.g. register a custom type adapter).
### Requirements
There are three requirements to be met in order to make the library work properly:
* Set experimentalDecorators and emitDecoratorMetadata to true on your `tsconfig.json` file
* Properties need to be preceded by the [`@JsonProperty`](https://hyperloris.github.io/tyson/globals.html#jsonproperty) annotation
* Properties need to have a default value (e.g. `undefined`)### A nice example
Let's start with a JSON representing a city:
```json
{
"name": "Bologna",
"population": 388884,
"monuments": ["Piazza Maggiore", "Palazzo Re Enzo"],
"mayor": {
"full_name": "Virginio Merola",
"birthdate": "1955-02-14T00:00:00"
}
}
```Now we need a couple of TypeScript classes:
```typescript
export class User {
@JsonProperty("full_name")
name: string = undefined;
@JsonProperty({ type: Date })
birthdate: Date = undefined;
}export class City {
@JsonProperty()
name: string = undefined;
@JsonProperty()
population: number = undefined;
@JsonProperty({ name: "monuments", type: [String] })
private _monuments: string[] = undefined;
@JsonProperty("mayor")
private _mayor: User = undefined;
}
```At this point we are ready to use the library:
```typescript
const tyson = new Tyson();
const city = tyson.fromJson(json, City);
const json = tyson.toJson(city);
```## Documentation
Tyson [API](http://hyperloris.github.io/tyson): generated with [TypeDoc](http://typedoc.org) at every release.
## Inspiration
The library is inspired by the [Gson](https://github.com/google/gson) library.
## License
MIT