Skip to content

Commit b124cc2

Browse files
committed
Convert empty to null for json
- Empty -> null for json - Add specs for Empty -> null - Add specs for Array body handling
1 parent 238f2f5 commit b124cc2

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

specs/Excel-REST - Specs.xlsm

43.1 KB
Binary file not shown.

specs/RestClientSpecs.bas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ Public Function Specs() As SpecSuite
133133

134134
.Expect(Response.StatusCode).ToEqual 200
135135
.Expect(Response.Data("body")("a")).ToEqual 3.14
136+
137+
Set Response = Client.PostJSON("/post", Array(1, 2, 3))
138+
139+
.Expect(Response.StatusCode).ToEqual 200
140+
.Expect(Response.Data("body")(1)).ToEqual 1
136141
End With
137142

138143
With Specs.It("should include options with GET and POST json")

specs/RestHelpersSpecs.bas

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ Public Function Specs() As SpecSuite
9292
Obj.Add "c", "Howdy!"
9393
Obj.Add "d", True
9494
Obj.Add "e", Array(1, 2)
95+
Obj.Add "f", Empty
96+
Obj.Add "g", Null
9597

9698
json = RestHelpers.ConvertToJSON(Obj)
97-
.Expect(json).ToEqual "{""a"":1,""b"":3.14,""c"":""Howdy!"",""d"":true,""e"":[1,2]}"
99+
.Expect(json).ToEqual "{""a"":1,""b"":3.14,""c"":""Howdy!"",""d"":true,""e"":[1,2],""f"":null,""g"":null}"
98100

99101
Set Obj = New Dictionary
100102
Obj.Add "a", "Howdy!"
@@ -106,9 +108,11 @@ Public Function Specs() As SpecSuite
106108
Coll.Add True
107109
Coll.Add Array(1, 2)
108110
Coll.Add Obj
111+
Coll.Add Empty
112+
Coll.Add Null
109113

110114
json = RestHelpers.ConvertToJSON(Coll)
111-
.Expect(json).ToEqual "[1,3.14,""Howdy!"",true,[1,2],{""a"":""Howdy!""}]"
115+
.Expect(json).ToEqual "[1,3.14,""Howdy!"",true,[1,2],{""a"":""Howdy!""},null,null]"
112116
End With
113117

114118
With Specs.It("should url encode values")

specs/RestRequestSpecs.bas

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ Public Function Specs() As SpecSuite
260260
.Expect(Request.Body).ToEqual "A=123&B=Howdy%21"
261261
End With
262262

263+
With Specs.It("should allow array/collection for body")
264+
Set Request = New RestRequest
265+
266+
Set Body = New Collection
267+
Body.Add "a"
268+
Body.Add "b"
269+
Body.Add "c"
270+
271+
Request.AddBody Body
272+
.Expect(Request.Body).ToEqual "[""a"",""b"",""c""]"
273+
274+
Request.AddBody Array("a", "b", "c")
275+
.Expect(Request.Body).ToEqual "[""a"",""b"",""c""]"
276+
End With
277+
263278
InlineRunner.RunSuite Specs
264279
End Function
265280

src/RestHelpers.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ Private Function json_toString(ByRef Obj As Variant) As String
13031303
Case vbNull
13041304
json_toString = "null"
13051305
Case vbEmpty
1306-
json_toString = """"""
1306+
json_toString = "null"
13071307
Case vbDate
13081308
json_toString = """" & CStr(Obj) & """"
13091309
Case vbString

0 commit comments

Comments
 (0)