Skip to content

Commit af11243

Browse files
committed
Merge pull request #18 from timhall/parse-numbers
Update number parsing
2 parents 2d995fa + 0881b33 commit af11243

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

specs/Excel-REST - Specs.xlsm

42.6 KB
Binary file not shown.

specs/RestHelpersSpecs.bas

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ Public Function Specs() As SpecSuite
6161
End If
6262
End With
6363

64+
With Specs.It("should parse json numbers")
65+
json = "{""a"":1,""b"":1.23,""c"":14.6000000000,""d"":14.6e6,""e"":14.6E6,""f"":10000000000000000000000}"
66+
Set Parsed = RestHelpers.ParseJSON(json)
67+
68+
.Expect(Parsed).ToBeDefined
69+
If Not Parsed Is Nothing Then
70+
.Expect(Parsed("a")).ToEqual 1
71+
.Expect(Parsed("b")).ToEqual 1.23
72+
.Expect(Parsed("c")).ToEqual 14.6
73+
.Expect(Parsed("d")).ToEqual 14600000
74+
.Expect(Parsed("e")).ToEqual 14600000
75+
.Expect(Parsed("f")).ToEqual 1E+22
76+
End If
77+
End With
78+
6479
With Specs.It("should convert to json")
6580
Set Obj = New Dictionary
6681
Obj.Add "a", 1

src/RestHelpers.bas

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ Private Function json_parseString(ByRef str As String, ByRef index As Long) As S
626626
Case "u"
627627
index = index + 1
628628
Code = Mid$(str, index, 4)
629-
json_parseString = json_parseString & ChrW(val("&h" + Code))
629+
json_parseString = json_parseString & ChrW(Val("&h" + Code))
630630
index = index + 4
631631
End Select
632632
Case quote
@@ -656,17 +656,7 @@ Private Function json_parseNumber(ByRef str As String, ByRef index As Long)
656656
Value = Value & char
657657
index = index + 1
658658
Else
659-
If InStr(Value, ".") Or InStr(Value, "e") Or InStr(Value, "E") Then
660-
json_parseNumber = CDbl(Value)
661-
Else
662-
If Len(Value) < 5 Then
663-
json_parseNumber = CInt(Value)
664-
ElseIf Len(Value) < 10 Then
665-
json_parseNumber = CLng(Value)
666-
Else
667-
json_parseNumber = CDec(Value)
668-
End If
669-
End If
659+
json_parseNumber = Val(Value)
670660
Exit Function
671661
End If
672662
Loop

0 commit comments

Comments
 (0)