Skip to content

Commit

Permalink
Removed support for undefined type
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-aeschlimann committed Sep 14, 2017
1 parent f4eee91 commit 9645e79
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 64 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# v1.0.5 (2017-09-14)

## Features

* Added README note about circular dependency issues

## Breaking Changes

* `json2typescript` will now throw an exception if the `JsonProperty` decorator has `undefined` as second parameter. Pass `null` if you want to skip the type check instead or don't pass the second parameter at all.

# v1.0.4 (2017-09-12)

## Bug Fixes
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class City {
// This maps the JSON key "data" to the class property "data".
// We are not sure about the type, so we omit the second parameter.
// There will be an exception if the JSON value is missing.
@JsonProperty("data") // is the same as @JsonProperty("data", undefined)
@JsonProperty("data") // is the same as @JsonProperty("data", Any)
data: any = undefined;

// This maps the JSON key "keywords" to the class property "keywords".
Expand Down Expand Up @@ -207,6 +207,10 @@ export class AppComponent implements OnInit {
```
Play around with the JSON to provocate exceptions when deserializing the object.
## Important notes
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.
---
# Detailed reference
Expand Down Expand Up @@ -253,7 +257,7 @@ In our case, `json["jsonPropertyName"]` gets mapped to `user.name`.
#### Second parameter (optional): conversionOption
The second parameter of `@JsonProperty` describes what happens when doing the mapping between JSON and TypeScript objects.
This parameter is optional; the default value is `undefined` (which means no type check is done when the mapping happens).
This parameter is optional; the default value is `Any` (which means no type check is done when the mapping happens).
##### Use of expected type
Expand All @@ -262,7 +266,8 @@ check according to given TypeScript types, you can pass a type you
expect. Follow the following guide when doing that:
- Make sure you pass the class name and not an instance of the class.
- In case of primitive types, you have to use the upper case names.
- In case of primitive types, you have to use the upper case names.
- In case of `any` type, import from `json2typescript` the class `Any`.
See the following cheat sheet for reference:
Expand All @@ -272,13 +277,13 @@ See the following cheat sheet for reference:
| Number | number |
| Boolean | boolean |
| User | User |
| undefined | any |
| Any | any |
| | |
| [String] | string[] |
| [Number] | number[] |
| [Boolean] | boolean[] |
| [User] | User[] |
| [undefined] or [] | any[] |
| [Any] | any[] |
At first, our array notation on the left looks odd.
But this notation allows you to define even nested arrays.
Expand Down Expand Up @@ -323,7 +328,7 @@ The same applies for the case when you try to serialize a TypeScript object to a
* Make sure you define the expected type as accurate as possible, even if you expect primitive types.
* By default, casting primitives into other primitives is not allowed. Check the public properties below in this document to change this behaviour.
* By default, primitives are not allowed to be null. Check the public properties below in this document to change this.
* If you don't know the type, you may use `undefined` as expected type. You may also omit the second parameter of `@JsonProperty`.
* If you don't know the type, you may use `Any` as expected type. You may also omit the second parameter of `@JsonProperty`.
#### More about the array syntax
Expand Down
2 changes: 2 additions & 0 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { JsonConvert } from "./src/json2typescript/json-convert";
export { JsonCustomConvert } from "./src/json2typescript/json-custom-convert";
export { ValueCheckingMode, OperationMode } from "./src/json2typescript/json-convert-enums";
export { JsonObject, JsonProperty, JsonConverter } from "./src/json2typescript/json-convert-decorators";
export { JsonObject, JsonProperty, JsonConverter } from "./src/json2typescript/json-convert-decorators";
export { Any } from "./src/json2typescript/any";
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json2typescript",
"version": "1.0.4",
"version": "1.0.5",
"description": "Provides TypeScript methods to map a JSON object to a JavaScript object on runtime",
"keywords": [
"convert",
Expand Down
9 changes: 9 additions & 0 deletions src/json2typescript/any.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/json2typescript/any.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/json2typescript/any.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Virtual any type for the property decorator.
*/
export class Any {}
39 changes: 34 additions & 5 deletions src/json2typescript/json-convert-decorators.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/json2typescript/json-convert-decorators.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 35 additions & 6 deletions src/json2typescript/json-convert-decorators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MappingOptions, Settings } from "./json-convert-options";
import { Any } from "./any";

/**
* Decorator of a class that is a custom converter.
Expand Down Expand Up @@ -31,15 +32,46 @@ export function JsonObject(target: any) {
* If you decide to use a custom converter, make sure this class implements the interface JsonCustomConvert from this package.
*
* @param jsonPropertyName optional param (default: classPropertyName) the property name in the expected JSON object
* @param conversionOption optional param (default: undefined), should be either the expected type (String|Boolean|Number|etc) or a custom converter class implementing JsonCustomConvert
* @param conversionOption optional param (default: Any), should be either the expected type (String|Boolean|Number|etc) or a custom converter class implementing JsonCustomConvert
* @param isOptional optional param (default: false), if true, the json property does not have to be present in the object
*
* @returns {(target:any, key:string)=>void}
*/
export function JsonProperty(jsonPropertyName?: string, conversionOption?: any, isOptional?: boolean): any {
export function JsonProperty(...params: any[]): any {

return function (target: any, classPropertyName: string): void {

let jsonPropertyName: string = classPropertyName;
let conversionOption: any = Any;
let isOptional: boolean = false;

switch (params.length) {
case 0:
break;
case 1:
jsonPropertyName = params[0];
break;
case 2:
if (params[1] === undefined) throw new Error(
"Fatal error in JsonConvert. " +
"It's not allowed to explicitely pass \"undefined\" as second parameter in the @JsonProperty decorator.\n\n" +
"\tClass name: \n\t\t" + target.constructor.name + "\n\n" +
"\tClass property: \n\t\t" + classPropertyName + "\n\n" +
"Use \"Any\" to allow any type. You can import this class from \"json2typescript\".\n\n"
);
jsonPropertyName = params[0];
conversionOption = params[1];
break;
case 3:
jsonPropertyName = params[0];
conversionOption = params[1];
isOptional = params[2];
break;
default:
break;
}


if (typeof(target[Settings.MAPPING_PROPERTY]) === "undefined") {
target[Settings.MAPPING_PROPERTY] = [];
}
Expand All @@ -50,17 +82,14 @@ export function JsonProperty(jsonPropertyName?: string, conversionOption?: any,
jsonPropertyName = classPropertyName;
}

if (typeof(isOptional) === "undefined") {
isOptional = false;
}

let jsonPropertyMappingOptions = new MappingOptions();
jsonPropertyMappingOptions.classPropertyName = classPropertyName;
jsonPropertyMappingOptions.jsonPropertyName = jsonPropertyName;
jsonPropertyMappingOptions.isOptional = isOptional ? isOptional : false;

// Check if conversionOption is a type or a custom converter.
if (typeof(conversionOption) !== "undefined" && typeof(conversionOption[Settings.MAPPER_PROPERTY]) !== "undefined") {
if (typeof(conversionOption) !== "undefined" && conversionOption !== null && typeof(conversionOption[Settings.MAPPER_PROPERTY]) !== "undefined") {
jsonPropertyMappingOptions.customConverter = new conversionOption();
} else {
jsonPropertyMappingOptions.expectedJsonType = conversionOption;
Expand Down
Loading

0 comments on commit 9645e79

Please sign in to comment.