@@ -70,22 +70,40 @@ function build (schema, options) {
70
70
71
71
var main
72
72
73
+ // wrapper function for processing nullable values
74
+ var processValue = ( schema , code , name , nonNullHandler ) => {
75
+ code += `
76
+ function ${ name } (input) {
77
+ if(input === null && ${ schema . nullable } === true) {
78
+ return null
79
+ } else {
80
+ return ${ nonNullHandler } (input)
81
+ }
82
+ }
83
+ `
84
+ return code
85
+ }
86
+
73
87
switch ( schema . type ) {
74
88
case 'object' :
75
89
main = '$main'
76
90
code = buildObject ( schema , code , main , options . schema , schema )
77
91
break
78
92
case 'string' :
79
- main = $asString . name
93
+ main = '$main'
94
+ code = processValue ( schema , code , main , $asString . name )
80
95
break
81
96
case 'integer' :
82
- main = $asInteger . name
97
+ main = '$main'
98
+ code = processValue ( schema , code , main , $asInteger . name )
83
99
break
84
100
case 'number' :
85
- main = $asNumber . name
101
+ main = '$main'
102
+ code = processValue ( schema , code , main , $asNumber . name )
86
103
break
87
104
case 'boolean' :
88
- main = $asBoolean . name
105
+ main = '$main'
106
+ code = processValue ( schema , code , main , $asBoolean . name )
89
107
break
90
108
case 'null' :
91
109
main = $asNull . name
@@ -100,7 +118,7 @@ function build (schema, options) {
100
118
101
119
code += `
102
120
;
103
- return ${ main }
121
+ return ${ main }
104
122
`
105
123
106
124
if ( options . uglify ) {
@@ -481,6 +499,16 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
481
499
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
482
500
483
501
var type = schema . properties [ key ] . type
502
+ var nullable = schema . properties [ key ] . nullable
503
+
504
+ code += `
505
+ if (obj['${ key } '] === null && ${ nullable } ) {
506
+ ${ addComma }
507
+ json += '${ $asString ( key ) } :null'
508
+ var rendered = true
509
+ } else {
510
+ `
511
+
484
512
if ( type === 'number' ) {
485
513
code += `
486
514
var t = Number(obj['${ key } '])
@@ -547,10 +575,9 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
547
575
}
548
576
549
577
code += `
550
- }
578
+ }}
551
579
`
552
580
} )
553
-
554
581
return { code : code , laterCode : laterCode }
555
582
}
556
583
@@ -692,9 +719,19 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
692
719
function buildArray ( schema , code , name , externalSchema , fullSchema ) {
693
720
code += `
694
721
function ${ name } (obj) {
722
+ `
723
+ if ( schema . nullable ) {
724
+ code += `
725
+ if(obj === null) {
726
+ return '${ $asNull ( ) } ';
727
+ }
728
+ // }
729
+ `
730
+ // return code
731
+ }
732
+ code += `
695
733
var json = '['
696
734
`
697
-
698
735
var laterCode = ''
699
736
700
737
if ( schema . items [ '$ref' ] ) {
@@ -798,6 +835,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
798
835
}
799
836
800
837
var type = schema . type
838
+ var nullable = schema . nullable === true
801
839
802
840
var accessor = key . indexOf ( '[' ) === 0 ? key : `['${ key } ']`
803
841
switch ( type ) {
@@ -808,22 +846,22 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
808
846
break
809
847
case 'string' :
810
848
code += `
811
- json += $asString(obj${ accessor } )
849
+ json += ${ nullable } && obj ${ accessor } === null ? null : $ asString(obj${ accessor } )
812
850
`
813
851
break
814
852
case 'integer' :
815
853
code += `
816
- json += $asInteger(obj${ accessor } )
854
+ json += ${ nullable } && obj ${ accessor } === null ? null : $ asInteger(obj${ accessor } )
817
855
`
818
856
break
819
857
case 'number' :
820
858
code += `
821
- json += $asNumber(obj${ accessor } )
859
+ json += ${ nullable } && obj ${ accessor } === null ? null : $ asNumber(obj${ accessor } )
822
860
`
823
861
break
824
862
case 'boolean' :
825
863
code += `
826
- json += $asBoolean(obj${ accessor } )
864
+ json += ${ nullable } && obj ${ accessor } === null ? null : $ asBoolean(obj${ accessor } )
827
865
`
828
866
break
829
867
case 'object' :
0 commit comments