Skip to content

Commit 75609cd

Browse files
committed
Add option for escaping solidus
1 parent eefef23 commit 75609cd

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

JsonConverter.bas

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Private Type json_Options
134134
' to override set `JsonConverter.JsonOptions.UseDoubleForLargeNumbers = True`
135135
UseDoubleForLargeNumbers As Boolean
136136
AllowUnquotedKeys As Boolean
137+
EscapeSolidus As Boolean
137138
End Type
138139
Public JsonOptions As json_Options
139140

@@ -539,33 +540,37 @@ Private Function json_Encode(ByVal json_Text As Variant) As String
539540
json_AscCode = json_AscCode + 65536
540541
End If
541542

543+
' From spec, ", \, and control characters must be escaped (solidus is optional)
544+
542545
Select Case json_AscCode
543-
' " -> 34 -> \"
544546
Case 34
547+
' " -> 34 -> \"
545548
json_Char = "\"""
546-
' \ -> 92 -> \\
547549
Case 92
550+
' \ -> 92 -> \\
548551
json_Char = "\\"
549-
' / -> 47 -> \/
550552
Case 47
551-
json_Char = "\/"
552-
' backspace -> 8 -> \b
553+
' / -> 47 -> \/ (optional)
554+
If JsonConverter.JsonOptions.EscapeSolidus Then
555+
json_Char = "\/"
556+
End If
553557
Case 8
558+
' backspace -> 8 -> \b
554559
json_Char = "\b"
555-
' form feed -> 12 -> \f
556560
Case 12
561+
' form feed -> 12 -> \f
557562
json_Char = "\f"
558-
' line feed -> 10 -> \n
559563
Case 10
564+
' line feed -> 10 -> \n
560565
json_Char = "\n"
561-
' carriage return -> 13 -> \r
562566
Case 13
567+
' carriage return -> 13 -> \r
563568
json_Char = "\r"
564-
' tab -> 9 -> \t
565569
Case 9
570+
' tab -> 9 -> \t
566571
json_Char = "\t"
567-
' Non-ascii characters -> convert to 4-digit hex
568572
Case 0 To 31, 127 To 65535
573+
' Non-ascii characters -> convert to 4-digit hex
569574
json_Char = "\u" & VBA.Right$("0000" & VBA.Hex$(json_AscCode), 4)
570575
End Select
571576

specs/Specs.bas

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,22 @@ Public Function Specs() As SpecSuite
252252

253253
With Specs.It("should json-encode strings")
254254
Dim Strings As Variant
255-
Strings = Array("""\/" & vbCrLf & vbTab & vbBack & vbFormFeed, ChrW(128) & ChrW(32767), "#$%&{|}~")
255+
Strings = Array("""\" & vbCrLf & vbTab & vbBack & vbFormFeed, ChrW(128) & ChrW(32767), "#$%&{|}~")
256256

257257
JsonString = JsonConverter.ConvertToJson(Strings)
258-
.Expect(JsonString).ToEqual "[""\""\\\/\r\n\t\b\f"",""\u0080\u7FFF"",""#$%&{|}~""]"
258+
.Expect(JsonString).ToEqual "[""\""\\\r\n\t\b\f"",""\u0080\u7FFF"",""#$%&{|}~""]"
259+
End With
260+
261+
With Specs.It("should escape solidus with option")
262+
Strings = Array("a/b")
263+
264+
JsonString = JsonConverter.ConvertToJson(Strings)
265+
.Expect(JsonString).ToEqual "[""a/b""]"
266+
267+
JsonConverter.JsonOptions.EscapeSolidus = True
268+
JsonString = JsonConverter.ConvertToJson(Strings)
269+
.Expect(JsonString).ToEqual "[""a\/b""]"
270+
JsonConverter.JsonOptions.EscapeSolidus = False
259271
End With
260272

261273
' ============================================= '

specs/VBA-JSON - Specs.xlsm

1.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)