You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-9Lines changed: 29 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# json2typescript
2
2
3
-
In Angular 2 applications, everyone consumes JSON API's from an external source.
3
+
In Angular applications, everyone consumes JSON API's from an external source.
4
4
Type checking and object mapping is only possible in TypeScript, but not in the JavaScript runtime.
5
5
As the API may change at any point, it is important for larger projects to verify the consumed data.
6
6
@@ -21,15 +21,17 @@ let user: User = jsonConvert.deserializeObject(jsonObj, User);
21
21
console.log(user); // prints User{ ... } in JavaScript runtime, not Object{ ... }
22
22
```
23
23
24
-
> Tip: All `serialize()` and `deserialize()` methods may throw an exception in case of failure. Make sure you catch the errors in production!
24
+
> Tip: All `serialize()` and `deserialize()` methods may throw an exception in case of failure.
25
+
Make sure you catch the errors in production!
25
26
26
27
---
27
28
28
29
# Changelog
29
30
30
31
See the changelog in the seperate file for bug fixes, new features and breaking changes: [Changelog](CHANGELOG.md)
31
32
32
-
> Tip: Starting from version 1.0.6, we recommend to use unique class identifiers in the `@JsonObject` decorator. Read below how to use the decorators properly.
33
+
> Tip: Starting from version 1.0.6, we recommend to use unique class identifiers in the `@JsonObject` decorator.
34
+
Read below how to use the decorators properly.
33
35
34
36
> Tip: Version 1.0.0 has several breaking changes. When upgrading from `json2typescript` < 1.0.0, please make sure you fix these issues.
35
37
@@ -39,11 +41,13 @@ See the changelog in the seperate file for bug fixes, new features and breaking
39
41
40
42
## Requirements
41
43
42
-
We developed **json2typescript** for Angular 2+. In this document, we only cover this use case. However, you may use our package for pure TypeScript or even JavaScript applications.
44
+
We developed **json2typescript** for Angular and Ionic 2+. In this document, we only cover this use case.
45
+
However, you may use our package for pure TypeScript or even JavaScript applications.
43
46
44
47
## Setup a Test Application
45
48
46
-
We recommend to use the official **angular-cli** tool in order to set up a new Angular project. Then, all you need to do is type the following into your operating system's terminal:
49
+
We recommend to use the official **angular cli** tool in order to set up a new Angular project.
50
+
Then, all you need to do is type the following into your operating system's terminal:
47
51
48
52
```sh
49
53
ng new testApplication
@@ -52,7 +56,8 @@ cd testApplication
52
56
npm install json2typescript
53
57
```
54
58
55
-
Our package makes use of TypeScript decorators. Please activate them in your **tsconfig.json** under `compilerOptions` as follows:
59
+
Our package makes use of TypeScript decorators.
60
+
If not done already, please activate them in your **tsconfig.json** under `compilerOptions` as follows:
56
61
57
62
```json
58
63
{
@@ -64,11 +69,15 @@ Our package makes use of TypeScript decorators. Please activate them in your **t
64
69
}
65
70
```
66
71
72
+
> Tip: We have tried to make the compiler options of `json2typescript` to be as strict as possible.
73
+
This enables you to use compiler options such as `"strictNullChecks": true` or `"noImplicitAny": true` in your own project.
74
+
67
75
Now you are ready to use the package.
68
76
69
77
## Mapping example
70
78
71
-
In order to use the **json2typescript** package, all you need to do is write decorators and import the package. The following things need to be done if you would like to map JSON to existing classes:
79
+
In order to use the **json2typescript** package, all you need to do is write decorators and import the package.
80
+
The following things need to be done if you would like to map JSON to existing classes:
72
81
73
82
* Classes need to be preceeded by `@JsonObject(classIdentifier)`
74
83
* Properties need to be preceeded by `@JsonProperty(jsonProperty, conversionOption, isOptional)`
@@ -209,7 +218,8 @@ Play around with the JSON to provocate exceptions when deserializing the object.
209
218
210
219
## Important notes
211
220
212
-
Avoid circular depencencies on the classes that use `json2typescript`. Even if you don't have any errors in your IDE, `json2typescript` will not properly work in this case.
221
+
Avoid circular depencencies on the classes that use `json2typescript`.
222
+
Even if you don't have any errors in your IDE, `json2typescript` will not properly work in this case.
213
223
214
224
---
215
225
@@ -235,7 +245,6 @@ objects as above.
235
245
236
246
> Tip: Make sure you import `JsonObject` from `json2typescript`.
237
247
238
-
239
248
```typescript
240
249
@JsonProperty("jsonPropName", String, true)
241
250
@JsonProperty("jsonPropertyName", String, true)
@@ -448,6 +457,17 @@ If true, it will be allowed to assign primitive to other primitive types.
0 commit comments