项目作者: olaferlandsen

项目描述 :
Json Object mapper writing in TypeScript
高级语言: TypeScript
项目地址: git://github.com/olaferlandsen/Typescript-Json-Object-Mapper.git
创建时间: 2018-08-23T13:20:16Z
项目社区:https://github.com/olaferlandsen/Typescript-Json-Object-Mapper

开源协议:MIT License

下载


(TypeScript) Json-Object-Mapper

Donate

This a simple package to mapping a json object.

Getting Started

Install

  1. npm install typescript-json-object-mapper
  1. yarn add typescript-json-object-mapper

Configure

To work with decorators, you need first enable emitDecoratorMetadata y experimentalDecorators on you tsconfig.json.
Example:

  1. {
  2. "compilerOptions": {
  3. "emitDecoratorMetadata": true,
  4. "experimentalDecorators": true
  5. }
  6. }

Create you own Views

This example tries to show all possible cases in which you might need to use this utility.

  1. class UserView extends JsonView {
  2. @JsonProperty
  3. username: string;
  4. @JsonProperty({
  5. ignore: true
  6. })
  7. password: string;
  8. @JsonProperty({
  9. topic: 'custom'
  10. })
  11. birthday: string;
  12. @JsonProperty({
  13. topic: 'custom2'
  14. })
  15. phone: string;
  16. }

Define you data object

  1. const json = {
  2. username: "annon",
  3. password: "12345678",
  4. birthday: "1992-03-20",
  5. phone: "+0123456789"
  6. };

Serilize(without topic’s)

  1. const serialized = JsonObjectMapper.serialize(json, UserView).toString();

results:

  1. {
  2. username: "annon",
  3. birthday: "1992-03-20",
  4. phone: "+0123456789"
  5. }

Serilize(with topic)

  1. const serialized = JsonObjectMapper.serialize(json, UserView, ['custom']).toString();

results:

  1. {
  2. username: "annon",
  3. birthday: "1992-03-20"
  4. }

Serilize(with topic)

  1. const serialized = JsonObjectMapper.serialize(json, UserView, ['custom', 'custom2']).toString();

results:

  1. {
  2. username: "annon",
  3. birthday: "1992-03-20",
  4. phone: "+0123456789"
  5. }

Features

  • No-Initiation(Using only reference to class)
  • Renaming properties
  • Change data types
    • to Date
      • from String using Date.parse
      • from Integer using Date
    • to Integer
      • from String using Number
    • to Float
      • from String using Number
    • to Boolean
    • to String
    • to Object
  • Sub-Views(Recursivity)
    • Array sub-views
    • Single sub-view
  • Date values(String, Number, Date)
  • Serialize from Object Array
  • Serialize from Object
  • Serialize from String
  • Property Topic’s

API:

JsonObjectMapper.serialize

This function always return Serialization object.
And can using it with data as Array or Object.

Example
  1. // from Array
  2. JsonObjectMapper.serialize([
  3. {
  4. email: "john.smith@example.com",
  5. password: "123456"
  6. },
  7. {
  8. email: "john.smith@example.com",
  9. password: "123456"
  10. },
  11. {
  12. email: "john.smith@example.com",
  13. password: "123456"
  14. }
  15. ], UserView);
  16. // from Object
  17. JsonObjectMapper.serialize({
  18. email: "john.smith@example.com",
  19. password: "123456"
  20. }, UserView);

Serialization

Serialization.toString(spaces:number = 4): string

This method return a string representation of object.

Serialization.toJson()

This method return a json object representation.