@@ -86,10 +86,15 @@ class Serializer {
86
86
if ( typeof i === 'bigint' ) {
87
87
return i . toString ( )
88
88
} else if ( Number . isInteger ( i ) ) {
89
- return this . asNumber ( i )
89
+ return '' + i
90
90
} else {
91
91
/* eslint no-undef: "off" */
92
- return this . asNumber ( this . parseInteger ( i ) )
92
+ const integer = this . parseInteger ( i )
93
+ if ( Number . isNaN ( integer ) ) {
94
+ throw new Error ( `The value "${ i } " cannot be converted to an integer.` )
95
+ } else {
96
+ return '' + integer
97
+ }
93
98
}
94
99
}
95
100
@@ -99,8 +104,8 @@ class Serializer {
99
104
100
105
asNumber ( i ) {
101
106
const num = Number ( i )
102
- if ( isNaN ( num ) ) {
103
- return 'null'
107
+ if ( Number . isNaN ( num ) ) {
108
+ throw new Error ( `The value " ${ i } " cannot be converted to a number.` )
104
109
} else {
105
110
return '' + num
106
111
}
@@ -725,7 +730,7 @@ function buildCode (location, code, laterCode, name) {
725
730
const schema = location . schema
726
731
const required = schema . required || [ ]
727
732
728
- Object . keys ( schema . properties || { } ) . forEach ( ( key , i , a ) => {
733
+ Object . keys ( schema . properties || { } ) . forEach ( ( key ) => {
729
734
let propertyLocation = mergeLocation ( location , { schema : schema . properties [ key ] } )
730
735
if ( schema . properties [ key ] . $ref ) {
731
736
propertyLocation = refFinder ( schema . properties [ key ] . $ref , location )
@@ -735,45 +740,18 @@ function buildCode (location, code, laterCode, name) {
735
740
// Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
736
741
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
737
742
738
- const type = schema . properties [ key ] . type
739
- const nullable = schema . properties [ key ] . nullable
740
743
const sanitized = JSON . stringify ( key )
741
744
const asString = JSON . stringify ( sanitized )
742
745
743
- if ( nullable ) {
744
- code += `
745
- if (obj[${ sanitized } ] === null) {
746
- ${ addComma }
747
- json += ${ asString } + ':null'
748
- } else {
749
- `
750
- }
751
-
752
- if ( type === 'number' ) {
753
- code += `
754
- var t = Number(obj[${ sanitized } ])
755
- if (!isNaN(t)) {
756
- ${ addComma }
757
- json += ${ asString } + ':' + t
758
- `
759
- } else if ( type === 'integer' ) {
760
- code += `
761
- var t = serializer.asInteger(obj[${ sanitized } ])
762
- if (!isNaN(t)) {
763
- ${ addComma }
764
- json += ${ asString } + ':' + t
746
+ code += `
747
+ if (obj[${ sanitized } ] !== undefined) {
748
+ ${ addComma }
749
+ json += ${ asString } + ':'
765
750
`
766
- } else {
767
- code += `
768
- if (obj[${ sanitized } ] !== undefined) {
769
- ${ addComma }
770
- json += ${ asString } + ':'
771
- `
772
751
773
- const result = nested ( laterCode , name , key , mergeLocation ( propertyLocation , { schema : schema . properties [ key ] } ) , undefined , false )
774
- code += result . code
775
- laterCode = result . laterCode
776
- }
752
+ const result = nested ( laterCode , name , key , mergeLocation ( propertyLocation , { schema : schema . properties [ key ] } ) , undefined , false )
753
+ code += result . code
754
+ laterCode = result . laterCode
777
755
778
756
const defaultValue = schema . properties [ key ] . default
779
757
if ( defaultValue !== undefined ) {
@@ -792,12 +770,6 @@ function buildCode (location, code, laterCode, name) {
792
770
code += `
793
771
}
794
772
`
795
-
796
- if ( nullable ) {
797
- code += `
798
- }
799
- `
800
- }
801
773
} )
802
774
803
775
for ( const requiredProperty of required ) {
0 commit comments