Skip to content

Commit b5c6ca2

Browse files
Allow optional null in data
1 parent 16874a3 commit b5c6ca2

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

src/json2typescript/json-convert.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ export class JsonConvert {
575575
let classInstancePropertyValue: any = instance[classPropertyName];
576576

577577

578-
// Check if the json value exists
578+
// Check if the class property value exists
579579
if (typeof (classInstancePropertyValue) === "undefined") {
580580

581581
if (isOptional) return;
@@ -589,9 +589,13 @@ export class JsonConvert {
589589
}
590590

591591

592+
// Check if the property is optional
593+
// If the json value is null, we don't assign it in that case
594+
if (isOptional && classInstancePropertyValue === null) return;
595+
596+
592597
// Map the property
593598
try {
594-
// Each class property might have multiple decorators - only use the JSON property name as defined in the first one
595599
json[jsonPropertyName] = customConverter !== null ? customConverter.serialize(classInstancePropertyValue) : this.verifyProperty(expectedJsonType, classInstancePropertyValue, true);
596600
} catch (e) {
597601
throw new Error(
@@ -649,6 +653,11 @@ export class JsonConvert {
649653
}
650654

651655

656+
// Check if the property is optional
657+
// If the json value is null, we don't assign it in that case
658+
if (isOptional && jsonValue === null) return;
659+
660+
652661
// Map the property
653662
try {
654663
instance[classPropertyName] = customConverter !== null ? customConverter.deserialize(jsonValue) : this.verifyProperty(expectedJsonType, jsonValue);

test/json2typescript.integration.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@ describe('Integration tests', () => {
3131
let dog1JsonObject: IDog = {
3232
name: "Barky",
3333
barking: true,
34-
owner: null,
3534
birthdate: "2016-01-02",
3635
friends: [],
3736
other: 0
3837
};
3938
let cat2JsonObject: ICat = {
4039
catName: "Links",
4140
district: 50,
42-
owner: human1JsonObject,
4341
birthdate: "2016-01-02",
44-
//friends: [cat1JsonObject, dog1JsonObject],
45-
friends: null,
4642
talky: true,
4743
other: ""
4844
};
@@ -64,16 +60,13 @@ describe('Integration tests', () => {
6460
let dog1 = new Dog();
6561
dog1.name = "Barky";
6662
dog1.isBarking = true;
67-
dog1.owner = null;
6863
dog1.birthdate = new Date("2016-01-02");
6964
dog1.friends = [];
7065

7166
let cat2 = new Cat();
7267
cat2.name = "Links";
7368
cat2.district = 50;
74-
cat2.owner = human1;
7569
cat2.birthdate = new Date("2016-01-02");
76-
//cat2.friends = [cat1, dog1];
7770
cat2.talky = true;
7871

7972
let animals = [cat1, dog1];
@@ -83,7 +76,7 @@ describe('Integration tests', () => {
8376
// SERIALIZE INTEGRATION
8477
describe('serialize', () => {
8578

86-
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
79+
jsonConvert.valueCheckingMode = ValueCheckingMode.DISALLOW_NULL;
8780

8881
it('should serialize a TypeScript object to a JSON object', () => {
8982
expect(jsonConvert.serialize<Cat>(cat1)).toEqual(cat1JsonObject);
@@ -110,7 +103,7 @@ describe('Integration tests', () => {
110103
// DESERIALIZE INTEGRATION
111104
describe('deserialize', () => {
112105

113-
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
106+
jsonConvert.valueCheckingMode = ValueCheckingMode.DISALLOW_NULL;
114107

115108
it('should deserialize a JSON object to a TypeScript object', () => {
116109
expect(jsonConvert.deserialize<Cat>(cat1JsonObject, Cat)).toEqual(cat1);

test/json2typescript.unit.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ describe('Unit tests', () => {
3030
owner: human1JsonObject,
3131
talky: true,
3232
other: "cute",
33-
birthdate: null,
3433
friends: []
3534
};
3635
let cat2JsonObject: ICat = {
3736
catName: "Links",
3837
district: 50,
39-
owner: human2JsonObject,
4038
talky: true,
4139
other: "sweet",
4240
birthdate: "2014-09-01",
@@ -45,7 +43,6 @@ describe('Unit tests', () => {
4543
let dog1JsonObject: IDog = {
4644
name: "Barky",
4745
barking: true,
48-
owner: null,
4946
other: 1.1,
5047
};
5148

@@ -69,7 +66,6 @@ describe('Unit tests', () => {
6966
let cat2 = new Cat();
7067
cat2.name = "Links";
7168
cat2.district = 50;
72-
cat2.owner = human2;
7369
cat2.other = "sweet";
7470
cat2.birthdate = new Date("2014-09-01");
7571
cat2.friends = null;
@@ -179,11 +175,6 @@ describe('Unit tests', () => {
179175
let t_cat = new Cat();
180176
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "name", {"catName": "Meowy"});
181177
expect(t_cat.name).toEqual("Meowy");
182-
183-
let t_dog = new Dog();
184-
(<any>jsonConvert).deserializeObject_loopProperty(t_dog, "name", {"name": "Barky"});
185-
expect(t_dog.name).toEqual("Barky");
186-
187178
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "district", {"district": 100});
188179
expect(t_cat.district).toEqual(100);
189180
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "owner", {
@@ -192,6 +183,11 @@ describe('Unit tests', () => {
192183
lastName: "Muster"
193184
}
194185
});
186+
expect(t_cat.owner!.lastname).toEqual("Muster");
187+
188+
let t_dog = new Dog();
189+
(<any>jsonConvert).deserializeObject_loopProperty(t_dog, "name", {"name": "Barky"});
190+
expect(t_dog.name).toEqual("Barky");
195191

196192
jsonConvert.propertyMatchingRule = PropertyMatchingRule.CASE_INSENSITIVE;
197193

@@ -202,7 +198,23 @@ describe('Unit tests', () => {
202198
expect(() => (<any>jsonConvert).deserializeObject_loopProperty(t_cat, "name", {"catNames": "Meowy"})).toThrow();
203199

204200
jsonConvert.propertyMatchingRule = PropertyMatchingRule.CASE_STRICT;
201+
205202
});
203+
204+
jsonConvert.valueCheckingMode = ValueCheckingMode.DISALLOW_NULL;
205+
206+
it('serializeObject_loopProperty()', () => {
207+
let t_cat = {};
208+
(<any>jsonConvert).serializeObject_loopProperty(cat2, "owner", t_cat);
209+
expect((<any>t_cat)["owner"]).toBe(undefined);
210+
});
211+
it('deserializeObject_loopProperty()', () => {
212+
let t_cat = new Cat();
213+
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "owner", { "owner": null });
214+
expect(t_cat.owner).toBe(null);
215+
216+
});
217+
206218
});
207219

208220
// HELPER METHODS

test/model/json/i-cat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface ICat {
22
catName: string;
3-
owner?: object;
3+
owner?: object | null;
44
birthdate?: string | null;
55
friends?: any[] | null;
66
district: number;

0 commit comments

Comments
 (0)