sait / vfpjson

Json encoder and decoder for VFP Visual Fox Pro
31 stars 60 forks source link

Can't decode when number is not natural #8

Open levanquan1969 opened 3 years ago

levanquan1969 commented 3 years ago

if number is not natural, example : .001 23. -.01 1e+2 -1E-2 ....

then decode function is error, example : k = json_decode( [{"number":1E+1}] )

My solution is :

define df_begin_number '+-.'

function parseNumber()

local m.tk m.tk = this.getToken()

&& if it not begin digit if not ( isdigit( m.tk) or m.tk $ df_begin_number ) this.setError( 'number is missing') return 0 endif

local nStartPos, c, isInt, cNumero m.nStartPos = this.nPos

&& scan digits m.c = this.nextChar() do while isdigit( m.c) or m.c $ 'Ee' + df_begin_number m.c = this.nextChar() enddo

&& get digits m.cNumero = substr( this.cJson, m.nStartPos, this.nPos - m.nStartPos)

&& is valid number if type( m.cNumero) = 'N' return Evalu( m.cNumero) else && error this.setError( 'Invalid number: ' + m.cNumero) return 0 endif