Skip to content

Commit 96172ce

Browse files
Updated documentation, updated texts
1 parent c112dae commit 96172ce

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# json2typescript
22

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.
44
Type checking and object mapping is only possible in TypeScript, but not in the JavaScript runtime.
55
As the API may change at any point, it is important for larger projects to verify the consumed data.
66

@@ -21,15 +21,17 @@ let user: User = jsonConvert.deserializeObject(jsonObj, User);
2121
console.log(user); // prints User{ ... } in JavaScript runtime, not Object{ ... }
2222
```
2323

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!
2526

2627
---
2728

2829
# Changelog
2930

3031
See the changelog in the seperate file for bug fixes, new features and breaking changes: [Changelog](CHANGELOG.md)
3132

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.
3335

3436
> Tip: Version 1.0.0 has several breaking changes. When upgrading from `json2typescript` < 1.0.0, please make sure you fix these issues.
3537
@@ -39,11 +41,13 @@ See the changelog in the seperate file for bug fixes, new features and breaking
3941

4042
## Requirements
4143

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.
4346

4447
## Setup a Test Application
4548

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:
4751

4852
```sh
4953
ng new testApplication
@@ -52,7 +56,8 @@ cd testApplication
5256
npm install json2typescript
5357
```
5458

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:
5661

5762
```json
5863
{
@@ -64,11 +69,15 @@ Our package makes use of TypeScript decorators. Please activate them in your **t
6469
}
6570
```
6671

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+
6775
Now you are ready to use the package.
6876

6977
## Mapping example
7078

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:
7281

7382
* Classes need to be preceeded by `@JsonObject(classIdentifier)`
7483
* 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.
209218
210219
## Important notes
211220
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.
213223
214224
---
215225
@@ -235,7 +245,6 @@ objects as above.
235245
236246
> Tip: Make sure you import `JsonObject` from `json2typescript`.
237247
238-
239248
```typescript
240249
@JsonProperty("jsonPropName", String, true)
241250
@JsonProperty("jsonPropertyName", String, true)
@@ -448,6 +457,17 @@ If true, it will be allowed to assign primitive to other primitive types.
448457

449458
The default is `false`.
450459

460+
#### Property matching rule
461+
462+
`(number) JsonConvert.propertyMatchingRule`
463+
464+
Determines the rule of how JSON properties shall be matched with class properties during deserialization.
465+
You may assign the following two values:
466+
* `PropertyMatchingRule.CASE_STRICT`: JSON properties need to match exactly the names in the decorators
467+
* `PropertyMatchingRule.CASE_INSENSITIVE`: JSON properties need to match names in the decorators, but names they are not case sensitive
468+
469+
The default is `PropertyMatchingRule.CASE_STRICT`.
470+
451471
### Public methods
452472

453473
`json2typescript` allows you to map JSON objects (or arrays) to TypeScript objects (or arrays) and vice versa.

src/json2typescript/json-convert-decorators.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function JsonObject(target?: string | any): any {
7070

7171
throw new Error(
7272
"Fatal error in JsonConvert. " +
73-
"It's mandatory to pass a string as parameter in the @JsonObject decorator.\n\n" +
73+
"It is mandatory to pass a string as parameter in the @JsonObject decorator.\n\n" +
7474
"Use either @JsonObject or @JsonObject(classId) where classId is a string.\n\n"
7575
);
7676

@@ -111,7 +111,7 @@ export function JsonProperty(...params: any[]): any {
111111
case 1:
112112
if (params[0] === undefined) throw new Error(
113113
"Fatal error in JsonConvert. " +
114-
"It's not allowed to explicitly pass \"undefined\" as first parameter in the @JsonProperty decorator.\n\n" +
114+
"It is not allowed to explicitly pass \"undefined\" as first parameter in the @JsonProperty decorator.\n\n" +
115115
"\tClass property: \n" +
116116
"\t\t" + classPropertyName + "\n\n" +
117117
"Leave the decorator parameters empty if you do not wish to pass the first parameter.\n\n"
@@ -121,14 +121,14 @@ export function JsonProperty(...params: any[]): any {
121121
case 2:
122122
if (params[0] === undefined) throw new Error(
123123
"Fatal error in JsonConvert. " +
124-
"It's not allowed to explicitly pass \"undefined\" as first parameter in the @JsonProperty decorator.\n\n" +
124+
"It is not allowed to explicitly pass \"undefined\" as first parameter in the @JsonProperty decorator.\n\n" +
125125
"\tClass property: \n" +
126126
"\t\t" + classPropertyName + "\n\n" +
127127
"Leave the decorator parameters empty if you do not wish to pass the first parameter.\n\n"
128128
);
129129
if (params[1] === undefined) throw new Error(
130130
"Fatal error in JsonConvert. " +
131-
"It's not allowed to explicitly pass \"undefined\" as second parameter in the @JsonProperty decorator.\n\n" +
131+
"It is not allowed to explicitly pass \"undefined\" as second parameter in the @JsonProperty decorator.\n\n" +
132132
"\tClass property: \n" +
133133
"\t\t" + classPropertyName + "\n\n" +
134134
"Use \"Any\" to allow any type. You can import this class from \"json2typescript\".\n\n"
@@ -139,14 +139,14 @@ export function JsonProperty(...params: any[]): any {
139139
case 3:
140140
if (params[0] === undefined) throw new Error(
141141
"Fatal error in JsonConvert. " +
142-
"It's not allowed to explicitly pass \"undefined\" as first parameter in the @JsonProperty decorator.\n\n" +
142+
"It is not allowed to explicitly pass \"undefined\" as first parameter in the @JsonProperty decorator.\n\n" +
143143
"\tClass property: \n" +
144144
"\t\t" + classPropertyName + "\n\n" +
145145
"Leave the decorator parameters empty if you do not wish to pass the first parameter.\n\n"
146146
);
147147
if (params[1] === undefined) throw new Error(
148148
"Fatal error in JsonConvert. " +
149-
"It's not allowed to explicitly pass \"undefined\" as second parameter in the @JsonProperty decorator.\n\n" +
149+
"It is not allowed to explicitly pass \"undefined\" as second parameter in the @JsonProperty decorator.\n\n" +
150150
"\tClass property: \n" +
151151
"\t\t" + classPropertyName + "\n\n" +
152152
"Use \"Any\" to allow any type. You can import this class from \"json2typescript\".\n\n"

src/json2typescript/json-convert.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,29 @@ export class JsonConvert {
119119
* Determines the rule of how JSON properties shall be matched with class properties during deserialization.
120120
*
121121
* You may assign the following values:
122-
* - CASE_STRICT: JSON properties need to match exactly the names in the decorators
123-
* - CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
122+
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators
123+
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
124124
*/
125125
private _propertyMatchingRule: number = PropertyMatchingRule.CASE_STRICT;
126126

127127
/**
128128
* Determines the rule of how JSON properties shall be matched with class properties during deserialization.
129129
*
130130
* You may assign the following values:
131-
* - CASE_STRICT: JSON properties need to match exactly the names in the decorators
132-
* - CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
131+
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators
132+
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
133133
* @returns {number}
134134
*/
135135
get propertyMatchingRule(): number {
136136
return this._propertyMatchingRule;
137137
}
138138

139139
/**
140-
* Determines the rule of how JSON properties shall be matched with class properties during deserialization.
140+
* Determines the rule of how JSON properties shall be matched with class properties during deserialization.
141141
*
142142
* You may assign the following values:
143-
* - CASE_STRICT: JSON properties need to match exactly the names in the decorators
144-
* - CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
143+
* - PropertyMatchingRule.CASE_STRICT: JSON properties need to match exactly the names in the decorators
144+
* - PropertyMatchingRule.CASE_INSENSITIVE: JSON properties need to match names in the decorators, but names they are not case sensitive
145145
* @param value
146146
*/
147147
set propertyMatchingRule(value: number) {

0 commit comments

Comments
 (0)