# JsonProperty Decorator

Module
import { JsonProperty } from "@tsed/common"
Source/packages/common/src/jsonschema/decorators/property.ts

# Overview

function JsonProperty(options?: IPropertyOptions | string): Function;

# Description

@Property() let you decorate an attribute that can be serialized or deserialized. By default, no parameters are required to use it. But in some cases, we need to configure explicitly the JSON attribute name mapped to the provide attribute.

Here an example of different use cases with @Property():

class EventModel {
   @Property()
   name: string;

   @Format('date-time')
   startDate: Date;

   @Name('end-date')
   @Format('date-time')
   endDate: Date;

   @CollectionOf(Task)
   tasks: TaskModel[];
}

class TaskModel {
    @Property()
    subject: string;

    @Minimum(0)  // Property or Property is not required when a JsonSchema decorator is used
    @Maximum(100)
    rate: number;
}

> Theses ES6 collections can be used: Map and Set. Map will be serialized as an object and Set as an array.
By default Date, Array, Map and Set have a default custom Converter already embed. But you can override theses (see next part).

For the Array, you must use the @@CollectionOf@@ decorator.
`TypeClass` will be used to deserialize each item in the collection stored on the attribute source.

According to the previous example, the JsonSchema generated will be as follow:

```typescript
{
   "type": "object",
   "properties": {
      "name": {
         "type": "string"
      },
      "startDate": {
         "type": "string",
         "format": "date-time"
      },
      "endDate": {
         "type": "string",
         "format": "date-time"
      },
      "tasks": {
         "type": "array",
         "items": {
            "$ref": "#/definitions/Task"
         }
      }
   },
   "definitions": {
     "Task": {
       "type": "object",
       "properties": {
         "subject": {
            "type": "string",
         },
         "rate": {
            "type": "number"
            "minimum": 0,
            "maximum: 100
         }
       }
     }
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

@returns {Function} @param options @decorator @validation @swagger @schema @deprecated Use Property decorator instead