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 all commits
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#libraries
node_modules

build

npm-debug.log

#ide settings
.idea
.log
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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.
Expand Down
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 @@ -130,7 +130,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 @@ -155,7 +155,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
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-typescript-mapper",
"version": "1.1.3",
"name": "@qunhe/json-typescript-mapper",
"version": "1.1.4",
"typescript": {
"definition": "index.d.ts"
},
Expand All @@ -9,11 +9,13 @@
},
"devDependencies": {
"chai": "~1.8.0",
"mocha": "2.0.1"
"mocha": "2.0.1",
"typescript": "^2.1.5"
},
"scripts": {
"test": "mocha ./spec/*.js",
"typings:generate": "tsc --declaration"
"typings:generate": "rm -rf build && tsc --outDir build --declaration",
"do-publish": "npm run typings:generate && cp package.json ./build && cd build && npm publish && cd -"
},
"description": "For single page application, data sources are obtained from API server. Instead of directly using api data, we \r definitely require an adapter layer to transform data as needed. Furthermore, \r the adapter inverse the the data dependency from API server(API Server is considered uncontrollable and \r highly unreliable as data structure may be edit by backend coder for some specific purposes)to our adapter \r which becomes reliable. Thus, this library is created as the adapter make use of es7 reflect decorator.",
"main": "index.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