Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add strict null check #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@ import { IDecoratorMetaData } from './';
export interface IGenericObject {
[key: string]: any;
}
/**
* When custom mapping of a property is required.
*
* @interface
*/
export interface ICustomConverter {
fromJson(data: any): any;
toJson(data: any): any;
}
/**
* IDecoratorMetaData<T>
* DecoratorConstraint
*
* @interface
* @property {ICustomConverter} customConverter, will be used for mapping the property, if specified
* @property {boolean} excludeToJson, will exclude the property for serialization, if true
*/
export interface IDecoratorMetaData<T> {
name?: string;
clazz?: {
new (): T;
};
customConverter?: ICustomConverter;
excludeToJson?: boolean;
}
/**
* JsonProperty
Expand All @@ -39,4 +52,12 @@ export declare function JsonProperty<T>(metadata?: IDecoratorMetaData<T> | strin
*/
export declare function deserialize<T extends IGenericObject>(Clazz: {
new (): T;
}, json: IGenericObject): T;
}, json: IGenericObject): T | undefined;
/**
* Serialize: Creates a ready-for-json-serialization object from the provided model instance.
* Only @JsonProperty decorated properties in the model instance are processed.
*
* @param instance an instance of a model class
* @returns {any} an object ready to be serialized to JSON
*/
export declare function serialize(instance: any): any;
6 changes: 3 additions & 3 deletions index.js

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

4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T,
if (metadata && metadata.clazz || isPrimitiveOrPrimitiveClass(clazz)) {
if (innerJson && isArrayOrArrayClass(innerJson)) {
return innerJson.map(
(item: any) => deserialize(metadata.clazz, item)
(item: any) => deserialize(metadata.clazz!, item)
);
}
return;
Expand All @@ -156,7 +156,7 @@ function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T,
*
* @return {T} return mapped object
*/
export function deserialize<T extends IGenericObject>(Clazz: {new(): T}, json: IGenericObject): T {
export function deserialize<T extends IGenericObject>(Clazz: {new(): T}, json: IGenericObject): T | undefined {
/**
* As it is a recursive function, ignore any arguments that are unset
*/
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"devDependencies": {
"chai": "~1.8.0",
"mocha": "2.0.1"
"mocha": "2.0.1",
"typescript": "^2.1.5"
},
"scripts": {
"test": "mocha ./spec/*.js",
Expand Down
78 changes: 39 additions & 39 deletions spec/index.js

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

42 changes: 21 additions & 21 deletions spec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import dateConverter from './common/dateconverter'

class Student {
@JsonProperty('name')
fullName: string;
fullName?: string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do these properties need to be made optional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As shown below, it's assigned with undefined, which could not pass the strict null checking:

image

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 good to go. Thanks!


@JsonProperty({name: 'dob', customConverter: dateConverter})
dateOfBirth: Date = undefined;
dateOfBirth?: Date = undefined;

constructor() {
this.fullName = void 0;
Expand All @@ -16,12 +16,12 @@ class Student {

class Address {
@JsonProperty('first-line')
firstLine: string;
firstLine?: string;
@JsonProperty('second-line')
secondLine: string;
secondLine?: string;
@JsonProperty({clazz: Student})
student: Student;
city: string;
student?: Student;
city?: string;

constructor() {
this.firstLine = void 0;
Expand All @@ -34,14 +34,14 @@ class Address {

class Person {
@JsonProperty('Name')
name: string;
name?: string;
@JsonProperty('xing')
surname: string;
age: number;
surname?: string;
age?: number;
@JsonProperty({clazz: Address, name: 'AddressArr'})
addressArr: Address[];
addressArr?: Address[];
@JsonProperty({clazz: Address, name: 'Address'})
address: Address;
address?: Address;

constructor() {
this.name = void 0;
Expand All @@ -61,7 +61,7 @@ describe('index()', function () {
"AddressArr": [] as Array<any>,
"Address": null as any
};
const person = deserialize(Person, json);
const person = deserialize(Person, json)!;
expect(person.address).to.be.equals(void 0);
expect(person.name).to.be.equal("Mark");
expect(person.surname).to.be.equal("Galea");
Expand All @@ -77,12 +77,12 @@ describe('index()', function () {
name: "Ailun"
}
};
const address = deserialize(Address, addressjson);
const address = deserialize(Address, addressjson)!;
expect(address.firstLine).to.be.equal("Some where");
expect(address.secondLine).to.be.equal("Over Here");
expect(address.city).to.be.equal("In This City");
expect(address.student).to.be.an('object');
expect(address.student.fullName).to.be.equal('Ailun');
expect(address.student!.fullName).to.be.equal('Ailun');
});

it('complex json object #1', function () {
Expand Down Expand Up @@ -117,18 +117,18 @@ describe('index()', function () {
}
}
};
const person = deserialize(Person, json);
const person = deserialize(Person, json)!;
expect(person.address).to.be.an.instanceOf(Address);
expect(person.age).to.be.a('number');
expect(person.name).to.be.a('string');
expect(person.address).to.be.an('object');
expect(person.addressArr.length).to.be.equals(2);
expect(person.address.student.fullName).to.be.equals('Ailun');
expect(person.addressArr!.length).to.be.equals(2);
expect(person.address!.student!.fullName).to.be.equals('Ailun');
});

it('empty json object #1', function () {
let json = {};
const person = deserialize(Person, json);
const person = deserialize(Person, json)!;
expect(person.address).to.be.equal(void 0);
expect(person.name).to.be.equal(void 0);
expect(person.surname).to.be.equal(void 0);
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('index()', function () {
let json = {
"NameTest": "Mark",
};
const person = deserialize(Person, json);
const person = deserialize(Person, json)!;
expect(person.name).to.be.equals(void 0);
});

Expand All @@ -178,9 +178,9 @@ describe('index()', function () {
"name": "John Doe",
dob: "1995-11-10"
};
const student = deserialize(Student, json);
const student = deserialize(Student, json)!;
expect(student.fullName).to.be.equals('John Doe');
expect(student.dateOfBirth).to.be.instanceof(Date);
expect(student.dateOfBirth.toString()).to.equal(new Date("1995-11-10").toString());
expect(student.dateOfBirth!.toString()).to.equal(new Date("1995-11-10").toString());
});
});
Loading