@@ -86,10 +86,15 @@ class Serializer {
8686 if ( typeof i === 'bigint' ) {
8787 return i . toString ( )
8888 } else if ( Number . isInteger ( i ) ) {
89- return this . asNumber ( i )
89+ return '' + i
9090 } else {
9191 /* 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+ }
9398 }
9499 }
95100
@@ -99,8 +104,8 @@ class Serializer {
99104
100105 asNumber ( i ) {
101106 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.` )
104109 } else {
105110 return '' + num
106111 }
@@ -725,7 +730,7 @@ function buildCode (location, code, laterCode, name) {
725730 const schema = location . schema
726731 const required = schema . required || [ ]
727732
728- Object . keys ( schema . properties || { } ) . forEach ( ( key , i , a ) => {
733+ Object . keys ( schema . properties || { } ) . forEach ( ( key ) => {
729734 let propertyLocation = mergeLocation ( location , { schema : schema . properties [ key ] } )
730735 if ( schema . properties [ key ] . $ref ) {
731736 propertyLocation = refFinder ( schema . properties [ key ] . $ref , location )
@@ -735,45 +740,18 @@ function buildCode (location, code, laterCode, name) {
735740 // Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
736741 // see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
737742
738- const type = schema . properties [ key ] . type
739- const nullable = schema . properties [ key ] . nullable
740743 const sanitized = JSON . stringify ( key )
741744 const asString = JSON . stringify ( sanitized )
742745
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 } + ':'
765750 `
766- } else {
767- code += `
768- if (obj[${ sanitized } ] !== undefined) {
769- ${ addComma }
770- json += ${ asString } + ':'
771- `
772751
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
777755
778756 const defaultValue = schema . properties [ key ] . default
779757 if ( defaultValue !== undefined ) {
@@ -792,12 +770,6 @@ function buildCode (location, code, laterCode, name) {
792770 code += `
793771 }
794772 `
795-
796- if ( nullable ) {
797- code += `
798- }
799- `
800- }
801773 } )
802774
803775 for ( const requiredProperty of required ) {
0 commit comments