Are decimals without leading zeros valid JSON?

前端 未结 2 815
陌清茗
陌清茗 2020-12-18 23:36

Given the JSON document

{\"percentageAmount\": .01}

Running it by JSONLint.com results in the error:

Parse error on line 2:         


        
相关标签:
2条回答
  • 2020-12-19 00:10

    Nope.

    According to the railroad diagram for numbers at JSON.org, numbers with fractional values must have digits before the decimal point:

    Diagram showing that numbers with fraction parts must have a digit before the decimal point.

    0 讨论(0)
  • 2020-12-19 00:19

    Are decimals without leading zeros valid JSON?

    From the specification:

      number = [ minus ] int [ frac ] [ exp ]
    
      decimal-point = %x2E       ; .
    
      digit1-9 = %x31-39         ; 1-9
    
      e = %x65 / %x45            ; e E
    
      exp = e [ minus / plus ] 1*DIGIT
    
      frac = decimal-point 1*DIGIT
    
      int = zero / ( digit1-9 *DIGIT )
    
      minus = %x2D               ; -
    
      plus = %x2B                ; +
    
      zero = %x30                ; 0
    

    The only part of a number that is mandatory is int which is defined as zero or 1-9 followed by any number of digits.

    So JSON Lint is correct.

    Why is this against JSON spec?

    As far as I know, the author's reasons for defining it that way are not documented anywhere.

    0 讨论(0)
提交回复
热议问题